OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package testclock |
| 6 |
| 7 import ( |
| 8 "fmt" |
| 9 "testing" |
| 10 "time" |
| 11 |
| 12 . "github.com/smartystreets/goconvey/convey" |
| 13 "infra/libs/clock" |
| 14 ) |
| 15 |
| 16 func TestTestTimer(t *testing.T) { |
| 17 Convey(`A testing clock instance`, t, func() { |
| 18 now := time.Date(2015, 01, 01, 00, 00, 00, 00, time.UTC) |
| 19 c := New(now) |
| 20 |
| 21 Convey(`A timer instance`, func() { |
| 22 t := c.NewTimer() |
| 23 |
| 24 Convey(`Should have a non-nil C.`, func() { |
| 25 So(t.GetC(), ShouldBeNil) |
| 26 }) |
| 27 |
| 28 Convey(`When activated`, func() { |
| 29 So(t.Reset(1*time.Second), ShouldBeFalse) |
| 30 |
| 31 Convey(`When reset, should return active.`, func
() { |
| 32 So(t.Reset(1*time.Hour), ShouldBeTrue) |
| 33 So(t.GetC(), ShouldNotBeNil) |
| 34 }) |
| 35 |
| 36 Convey(`When stopped, should return active.`, fu
nc() { |
| 37 So(t.Stop(), ShouldBeTrue) |
| 38 So(t.GetC(), ShouldBeNil) |
| 39 |
| 40 Convey(`And when stopped again, should r
eturn inactive.`, func() { |
| 41 So(t.Stop(), ShouldBeFalse) |
| 42 So(t.GetC(), ShouldBeNil) |
| 43 }) |
| 44 }) |
| 45 }) |
| 46 |
| 47 Convey(`Should successfully signal.`, func() { |
| 48 So(t.Reset(1*time.Second), ShouldBeFalse) |
| 49 c.SetNow(now.Add(1 * time.Second)) |
| 50 |
| 51 So(t.GetC(), ShouldNotBeNil) |
| 52 So(<-t.GetC(), ShouldResemble, now.Add(1*time.Se
cond)) |
| 53 }) |
| 54 }) |
| 55 |
| 56 Convey(`Multiple goroutines using the timer...`, func() { |
| 57 // Mark when timers are started, so we can ensure that o
ur signalling |
| 58 // happens after the timers have been instantiated. |
| 59 timerStartedC := make(chan bool) |
| 60 c.SetTimerCallback(func(_ clock.Timer) { |
| 61 timerStartedC <- true |
| 62 }) |
| 63 |
| 64 resultC := make(chan time.Time) |
| 65 for i := time.Duration(0); i < 5; i++ { |
| 66 go func(d time.Duration) { |
| 67 timer := c.NewTimer() |
| 68 timer.Reset(d) |
| 69 t := <-timer.GetC() |
| 70 fmt.Println("GOT TIME:", d, t) |
| 71 resultC <- t |
| 72 }(i * time.Second) |
| 73 <-timerStartedC |
| 74 } |
| 75 |
| 76 // Advance the clock to +2s. Three timers should signal. |
| 77 c.SetNow(now.Add(2 * time.Second)) |
| 78 <-resultC |
| 79 <-resultC |
| 80 <-resultC |
| 81 |
| 82 // Advance clock to +3s. One timer should signal. |
| 83 c.SetNow(now.Add(3 * time.Second)) |
| 84 <-resultC |
| 85 |
| 86 // Advance clock to +10s. One final timer should signal. |
| 87 c.SetNow(now.Add(10 * time.Second)) |
| 88 <-resultC |
| 89 }) |
| 90 }) |
| 91 } |
OLD | NEW |