package foo import ( "testing" "time" "github.com/dimonomid/clock" ) const testTimeLayout = "Jan 2, 2006 at 15:04:05.000" func TestFoo(t *testing.T) { // Create a mocked time, initialized at May 1, 2020 midnight. mockedClock := clock.NewMock() now, _ := time.Parse(testTimeLayout, "May 1, 2020 at 00:00:00.000") mockedClock.Set(now) // Create output channel, we'll check later that it receives the numbers we // expect. out := make(chan int, 1) // Create Foo, it will also start the internal goroutine to send numbers // to the channel. NewFoo(&FooParams{ Clock: mockedClock, Out: out, Interval: 1 * time.Second, }) // Assert that we receive the numbers we expect mockedClock.Add(1 * time.Second) assertRecvInt(t, out, 0) mockedClock.Add(1 * time.Second) assertRecvInt(t, out, 1) mockedClock.Add(1 * time.Second) assertRecvInt(t, out, 2) } func assertRecvInt(t *testing.T, ch <-chan int, want int) { select { case got := <-ch: if got != want { t.Errorf("wanted %d, got %d", want, got) } default: t.Errorf("wanted %d, got nothing", want) } }