| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package clock | 5 package clock |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "testing" | 8 "testing" |
| 9 "time" | 9 "time" |
| 10 | 10 |
| 11 "golang.org/x/net/context" |
| 12 |
| 11 . "github.com/smartystreets/goconvey/convey" | 13 . "github.com/smartystreets/goconvey/convey" |
| 12 ) | 14 ) |
| 13 | 15 |
| 14 func TestSystemTimer(t *testing.T) { | 16 func TestSystemTimer(t *testing.T) { |
| 15 t.Parallel() | 17 t.Parallel() |
| 16 | 18 |
| 17 Convey(`A systemTimer instance`, t, func() { | 19 Convey(`A systemTimer instance`, t, func() { |
| 18 » » t := new(systemTimer) | 20 » » ctx, cancelFunc := context.WithCancel(context.Background()) |
| 21 » » t := GetSystemClock().NewTimer(ctx) |
| 22 » » defer t.Stop() |
| 19 | 23 |
| 20 » » Convey(`Should start with a nil channel.`, func() { | 24 » » Convey(`Should start with a non-nil channel.`, func() { |
| 21 » » » So(t.GetC(), ShouldBeNil) | 25 » » » So(t.GetC(), ShouldNotBeNil) |
| 22 }) | 26 }) |
| 23 | 27 |
| 24 Convey(`When stopped, should return inactive.`, func() { | 28 Convey(`When stopped, should return inactive.`, func() { |
| 25 So(t.Stop(), ShouldBeFalse) | 29 So(t.Stop(), ShouldBeFalse) |
| 26 }) | 30 }) |
| 27 | 31 |
| 32 Convey(`Will return immediately if the Context is canceled befor
e Reset.`, func() { |
| 33 cancelFunc() |
| 34 |
| 35 t.Reset(time.Hour) |
| 36 So((<-t.GetC()).Err, ShouldEqual, context.Canceled) |
| 37 }) |
| 38 |
| 39 Convey(`Will return if the Context is canceled after Reset.`, fu
nc() { |
| 40 t.Reset(time.Hour) |
| 41 cancelFunc() |
| 42 |
| 43 So((<-t.GetC()).Err, ShouldEqual, context.Canceled) |
| 44 }) |
| 45 |
| 28 Convey(`When reset`, func() { | 46 Convey(`When reset`, func() { |
| 29 » » » active := t.Reset(1 * time.Hour) | 47 » » » So(t.Reset(1*time.Hour), ShouldBeFalse) |
| 30 » » » So(active, ShouldBeFalse) | |
| 31 | 48 |
| 32 » » » Convey(`When reset to a short duration`, func() { | 49 » » » Convey(`When reset again to a short duration, should ret
urn that it was active and trigger.`, func() { |
| 33 // Upper bound of supported platform resolution.
Windows is 15ms, so | 50 // Upper bound of supported platform resolution.
Windows is 15ms, so |
| 34 // make sure we exceed that. | 51 // make sure we exceed that. |
| 35 » » » » active := t.Reset(100 * time.Millisecond) | 52 » » » » So(t.Reset(100*time.Millisecond), ShouldBeTrue) |
| 53 » » » » So((<-t.GetC()).IsZero(), ShouldBeFalse) |
| 36 | 54 |
| 37 » » » » Convey(`Should return active.`, func() { | 55 » » » » // Again (reschedule). |
| 38 » » » » » So(active, ShouldBeTrue) | 56 » » » » So(t.Reset(100*time.Millisecond), ShouldBeFalse) |
| 39 » » » » }) | 57 » » » » So((<-t.GetC()).IsZero(), ShouldBeFalse) |
| 40 | |
| 41 » » » » Convey(`Should trigger shortly.`, func() { | |
| 42 » » » » » tm := <-t.GetC() | |
| 43 » » » » » So(tm, ShouldNotResemble, time.Time{}) | |
| 44 » » » » }) | |
| 45 }) | 58 }) |
| 46 | 59 |
| 47 Convey(`When stopped, should return active and have a no
n-nil C.`, func() { | 60 Convey(`When stopped, should return active and have a no
n-nil C.`, func() { |
| 48 active := t.Stop() | 61 active := t.Stop() |
| 49 So(active, ShouldBeTrue) | 62 So(active, ShouldBeTrue) |
| 50 So(t.GetC(), ShouldNotBeNil) | 63 So(t.GetC(), ShouldNotBeNil) |
| 51 }) | 64 }) |
| 52 | 65 |
| 53 Convey(`When stopped, should return active.`, func() { | 66 Convey(`When stopped, should return active.`, func() { |
| 54 active := t.Stop() | 67 active := t.Stop() |
| 55 So(active, ShouldBeTrue) | 68 So(active, ShouldBeTrue) |
| 56 }) | 69 }) |
| 57 | 70 |
| 58 Convey(`Should have a non-nil channel.`, func() { | 71 Convey(`Should have a non-nil channel.`, func() { |
| 59 So(t.GetC(), ShouldNotBeNil) | 72 So(t.GetC(), ShouldNotBeNil) |
| 60 }) | 73 }) |
| 61 | |
| 62 Reset(func() { | |
| 63 t.Stop() | |
| 64 }) | |
| 65 }) | 74 }) |
| 66 }) | 75 }) |
| 67 } | 76 } |
| OLD | NEW |