Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(265)

Side by Side Diff: common/clock/systemtimer_test.go

Issue 1679023005: Add Context cancellation to clock. (Closed) Base URL: https://github.com/luci/luci-go@master
Patch Set: Updated from comments, conformant to time interface. Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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(veryLongTime)
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(veryLongTime)
41 cancelFunc()
42
43 So((<-t.GetC()).Err, ShouldEqual, context.Canceled)
44 })
45
46 Convey(`A timer will use the same channel when Reset.`, func() {
47 timerC := t.GetC()
48
49 // Reset our timer to something more reasonable. It shou ld trigger the
50 // timer channel, which should trigger our
51 t.Reset(timeBase)
52 So((<-timerC).Err, ShouldBeNil)
53 })
54
55 Convey(`A timer will not signal if stopped.`, func() {
56 t.Reset(timeBase)
57 t.Stop()
58
59 // This isn't a perfect test, but it's a good boundary f or flake.
60 time.Sleep(3 * timeBase)
61
62 triggered := false
63 select {
64 case <-t.GetC():
65 triggered = true
66 default:
67 break
68 }
69 So(triggered, ShouldBeFalse)
70 })
71
28 Convey(`When reset`, func() { 72 Convey(`When reset`, func() {
29 » » » active := t.Reset(1 * time.Hour) 73 » » » So(t.Reset(veryLongTime), ShouldBeFalse)
30 » » » So(active, ShouldBeFalse)
31 74
32 » » » Convey(`When reset to a short duration`, func() { 75 » » » 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 76 // Upper bound of supported platform resolution. Windows is 15ms, so
34 // make sure we exceed that. 77 // make sure we exceed that.
35 » » » » active := t.Reset(100 * time.Millisecond) 78 » » » » So(t.Reset(timeBase), ShouldBeTrue)
79 » » » » So((<-t.GetC()).IsZero(), ShouldBeFalse)
36 80
37 » » » » Convey(`Should return active.`, func() { 81 » » » » // Again (reschedule).
38 » » » » » So(active, ShouldBeTrue) 82 » » » » So(t.Reset(timeBase), ShouldBeFalse)
39 » » » » }) 83 » » » » So((<-t.GetC()).IsZero(), ShouldBeFalse)
40
41 » » » » Convey(`Should trigger shortly.`, func() {
42 » » » » » tm := <-t.GetC()
43 » » » » » So(tm, ShouldNotResemble, time.Time{})
44 » » » » })
45 }) 84 })
46 85
47 Convey(`When stopped, should return active and have a no n-nil C.`, func() { 86 Convey(`When stopped, should return active and have a no n-nil C.`, func() {
48 active := t.Stop() 87 active := t.Stop()
49 So(active, ShouldBeTrue) 88 So(active, ShouldBeTrue)
50 So(t.GetC(), ShouldNotBeNil) 89 So(t.GetC(), ShouldNotBeNil)
51 }) 90 })
52 91
53 Convey(`When stopped, should return active.`, func() { 92 Convey(`When stopped, should return active.`, func() {
54 active := t.Stop() 93 active := t.Stop()
55 So(active, ShouldBeTrue) 94 So(active, ShouldBeTrue)
56 }) 95 })
57 96
58 Convey(`Should have a non-nil channel.`, func() { 97 Convey(`Should have a non-nil channel.`, func() {
59 So(t.GetC(), ShouldNotBeNil) 98 So(t.GetC(), ShouldNotBeNil)
60 }) 99 })
61
62 Reset(func() {
63 t.Stop()
64 })
65 }) 100 })
66 }) 101 })
67 } 102 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698