Git Product home page Git Product logo

clock's Introduction

clock GoDoc Build Status

Time utility with lovely mocking support.

This is essentially a replacement for the time package which allows you to seamlessly swap in mock times, timers, and tickers. See the godocs (link above) for more detailed usage.

Example

hello.go

package main

import (
    "fmt"
    "github.com/mixer/clock"
)

func main() {
    fmt.Printf("the time is %s", displayer{clock.C}.formatted())
}

type displayer struct {
    c clock.Clock
}

func (d displayer) formatted() string {
    now := d.c.Now()
    return fmt.Sprintf("%d:%d:%d", now.Hour(), now.Minute(), now.Second())
}

hello_test.go

package main

import (
    "testing"
    "time"

    "github.com/mixer/clock"
    "github.com/stretchr/testify/assert"
)

func TestDisplaysCorrectly(t *testing.T) {
    date, _ := time.Parse(time.UnixDate, "Sat Mar  7 11:12:39 PST 2015")
    c := clock.NewMockClock(date)
    d := displayer{c}

    assert.Equal(t, "11:12:39", d.formatted())
    c.AddTime(42 * time.Second)
    assert.Equal(t, "11:13:21", d.formatted())
}

API & Compatibility

The API provided by this package and the mock version is nearly identical to that of the time package, with two notable differences:

  • The channel for Ticker and Timer instances is accessed via the .Chan() method, rather than reading the .C property. This allows the structures to be swapped out for their mock variants.
  • The mock Ticker never skips ticks when time advances. This allows you to call .AddTime/.SetTime on the mock clock without having to advance to each "ticked" time.

clock's People

Contributors

connor4312 avatar joseustra avatar kandros avatar microsoft-github-policy-service[bot] avatar nicktitle avatar pborzenkov avatar runeaune avatar symbiont-jacob-romer avatar urld avatar zwass avatar zy84338719 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

clock's Issues

Ticker chan sometimes doesn't fire

I have this test:

func TestMockClock(t *testing.T) {
	clk := clock.NewMockClock()
	ticker := clk.NewTicker(250 * time.Millisecond)
	clk.AddTime(251 * time.Millisecond)
	select {
	case <-ticker.Chan():
		return
	case <-time.After(time.Second):
		t.Fatal("Timed out waiting for test to complete")
	}
}

If I run this test a bunch of times, it fails:

$ go test . -run TestMockClock -count 100000 -failfast
[... a bunch of successful runs ...]
--- FAIL: TestMockClock (1.00s)
	pinger_test.go:40: Timed out waiting for test to complete
FAIL
FAIL	github.com/symbiont-io/assembly/smartlog/lib/network	1.169s

Is there something wrong with my test setup?

Deadlock when ticker stopped with no readers on channel

The following test consistently causes a deadlock:

func TestTickerDeadlock(t *testing.T) {
	c := NewMockClock()
	tk := c.NewTicker(5 * time.Millisecond)
	c.AddTime(6 * time.Millisecond)
	time.Sleep(1 * time.Millisecond)
	tk.Stop()
}

Per the time godoc, the ticker may "drops tick to make up for slow receivers". We probably need to do this to avoid the deadlock here.

incorrect time returned on Ticker channel

test:

package main

import (
	"fmt"
	"time"

	"github.com/WatchBeam/clock"
)

func main() {
	date, _ := time.Parse(time.UnixDate, "0")
	c := clock.NewMockClock(date)
	ticker := c.NewTicker(time.Second)
	ch := ticker.Chan()
	done := make(chan struct{})
	go func() {
		chTime := <-ch
		cTime := c.Now()
		fmt.Println(chTime)
		fmt.Println(cTime)
		close(done)
	}()
	c.AddTime(time.Second)
	<-done
}

expected:

0001-01-01 00:00:01 +0000 UTC
0001-01-01 00:00:01 +0000 UTC

actual:

2016-12-06 11:19:51.326261153 +0000 UTC
0001-01-01 00:00:01 +0000 UTC

ticker doesn't close the channel after calling Stop()

Repro:

package main

import (
    "sync"
    "time"

    "github.com/WatchBeam/clock"
)

func main() {
    c := clock.NewMockClock(time.Now())
    ticker := c.NewTicker(time.Second)
    ch := ticker.Chan()
    wg := sync.WaitGroup{}
    wg.Add(1)
    go func() {
        for range ch {
        }
        wg.Done()
    }()
    ticker.Stop()
    wg.Wait()
}

TestTimerResets flaps when run with race detector

Not always, but typically using -count 100 is sufficient to see a few failures:

$ go test -race -run TestTimerResets -count 100
--- FAIL: TestTimerResets (0.05s)
	mock_test.go:14: expected timer to get after reset when interval is up after restarted
--- FAIL: TestTimerResets (0.05s)
	mock_test.go:14: expected timer to get after reset when interval is up after restarted
--- FAIL: TestTimerResets (0.05s)
	mock_test.go:14: expected timer to get after reset when interval is up after restarted
FAIL
exit status 1
FAIL	github.com/mixer/clock	3.573s

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.