microsoft/clock

Ticker chan sometimes doesn't fire

Closed this issue · 1 comments

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?

This is due to a race condition where clk.AddTime is executed before the Ticker has set up a listener via m.clock.After() here.

I'll have a PR in a minute.