Index: go/src/infra/libs/clock/testclock/testtimer_test.go |
diff --git a/go/src/infra/libs/clock/testclock/testtimer_test.go b/go/src/infra/libs/clock/testclock/testtimer_test.go |
new file mode 100644 |
index 0000000000000000000000000000000000000000..f331cb0d8961c0391e232674c10fbe70ba944750 |
--- /dev/null |
+++ b/go/src/infra/libs/clock/testclock/testtimer_test.go |
@@ -0,0 +1,91 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+package testclock |
+ |
+import ( |
+ "fmt" |
+ "testing" |
+ "time" |
+ |
+ . "github.com/smartystreets/goconvey/convey" |
+ "infra/libs/clock" |
+) |
+ |
+func TestTestTimer(t *testing.T) { |
+ Convey(`A testing clock instance`, t, func() { |
+ now := time.Date(2015, 01, 01, 00, 00, 00, 00, time.UTC) |
+ c := New(now) |
+ |
+ Convey(`A timer instance`, func() { |
+ t := c.NewTimer() |
+ |
+ Convey(`Should have a non-nil C.`, func() { |
+ So(t.GetC(), ShouldBeNil) |
+ }) |
+ |
+ Convey(`When activated`, func() { |
+ So(t.Reset(1*time.Second), ShouldBeFalse) |
+ |
+ Convey(`When reset, should return active.`, func() { |
+ So(t.Reset(1*time.Hour), ShouldBeTrue) |
+ So(t.GetC(), ShouldNotBeNil) |
+ }) |
+ |
+ Convey(`When stopped, should return active.`, func() { |
+ So(t.Stop(), ShouldBeTrue) |
+ So(t.GetC(), ShouldBeNil) |
+ |
+ Convey(`And when stopped again, should return inactive.`, func() { |
+ So(t.Stop(), ShouldBeFalse) |
+ So(t.GetC(), ShouldBeNil) |
+ }) |
+ }) |
+ }) |
+ |
+ Convey(`Should successfully signal.`, func() { |
+ So(t.Reset(1*time.Second), ShouldBeFalse) |
+ c.SetNow(now.Add(1 * time.Second)) |
+ |
+ So(t.GetC(), ShouldNotBeNil) |
+ So(<-t.GetC(), ShouldResemble, now.Add(1*time.Second)) |
+ }) |
+ }) |
+ |
+ Convey(`Multiple goroutines using the timer...`, func() { |
+ // Mark when timers are started, so we can ensure that our signalling |
+ // happens after the timers have been instantiated. |
+ timerStartedC := make(chan bool) |
+ c.SetTimerCallback(func(_ clock.Timer) { |
+ timerStartedC <- true |
+ }) |
+ |
+ resultC := make(chan time.Time) |
+ for i := time.Duration(0); i < 5; i++ { |
+ go func(d time.Duration) { |
+ timer := c.NewTimer() |
+ timer.Reset(d) |
+ t := <-timer.GetC() |
+ fmt.Println("GOT TIME:", d, t) |
+ resultC <- t |
+ }(i * time.Second) |
+ <-timerStartedC |
+ } |
+ |
+ // Advance the clock to +2s. Three timers should signal. |
+ c.SetNow(now.Add(2 * time.Second)) |
+ <-resultC |
+ <-resultC |
+ <-resultC |
+ |
+ // Advance clock to +3s. One timer should signal. |
+ c.SetNow(now.Add(3 * time.Second)) |
+ <-resultC |
+ |
+ // Advance clock to +10s. One final timer should signal. |
+ c.SetNow(now.Add(10 * time.Second)) |
+ <-resultC |
+ }) |
+ }) |
+} |