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

Side by Side Diff: common/clock/testclock/testtimer_test.go

Issue 1679023005: Add Context cancellation to clock. (Closed) Base URL: https://github.com/luci/luci-go@master
Patch Set: Much more invasive, cancel by default, remove meter package. 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 testclock 5 package testclock
6 6
7 import ( 7 import (
8 "testing" 8 "testing"
9 "time" 9 "time"
10 10
11 "github.com/luci/luci-go/common/clock" 11 "github.com/luci/luci-go/common/clock"
12 "golang.org/x/net/context"
13
12 . "github.com/smartystreets/goconvey/convey" 14 . "github.com/smartystreets/goconvey/convey"
13 ) 15 )
14 16
15 func TestTestTimer(t *testing.T) { 17 func TestTestTimer(t *testing.T) {
16 t.Parallel() 18 t.Parallel()
17 19
18 Convey(`A testing clock instance`, t, func() { 20 Convey(`A testing clock instance`, t, func() {
19 » » now := time.Date(2015, 01, 01, 00, 00, 00, 00, time.UTC) 21 » » ctx := context.Background()
22 » » now := TestTimeLocal
20 c := New(now) 23 c := New(now)
21 24
22 Convey(`A timer instance`, func() { 25 Convey(`A timer instance`, func() {
23 » » » t := c.NewTimer() 26 » » » t := c.NewTimer(ctx)
24 27
25 Convey(`Should have a non-nil C.`, func() { 28 Convey(`Should have a non-nil C.`, func() {
26 » » » » So(t.GetC(), ShouldBeNil) 29 » » » » So(t.GetC(), ShouldNotBeNil)
27 }) 30 })
28 31
29 Convey(`When activated`, func() { 32 Convey(`When activated`, func() {
30 So(t.Reset(1*time.Second), ShouldBeFalse) 33 So(t.Reset(1*time.Second), ShouldBeFalse)
31 34
32 Convey(`When reset, should return active.`, func () { 35 Convey(`When reset, should return active.`, func () {
33 So(t.Reset(1*time.Hour), ShouldBeTrue) 36 So(t.Reset(1*time.Hour), ShouldBeTrue)
34 So(t.GetC(), ShouldNotBeNil) 37 So(t.GetC(), ShouldNotBeNil)
35 }) 38 })
36 39
37 Convey(`When stopped, should return active.`, fu nc() { 40 Convey(`When stopped, should return active.`, fu nc() {
38 So(t.Stop(), ShouldBeTrue) 41 So(t.Stop(), ShouldBeTrue)
39 » » » » » So(t.GetC(), ShouldBeNil) 42 » » » » » So(t.GetC(), ShouldNotBeNil)
40 43
41 Convey(`And when stopped again, should r eturn inactive.`, func() { 44 Convey(`And when stopped again, should r eturn inactive.`, func() {
42 So(t.Stop(), ShouldBeFalse) 45 So(t.Stop(), ShouldBeFalse)
43 » » » » » » So(t.GetC(), ShouldBeNil) 46 » » » » » » So(t.GetC(), ShouldNotBeNil)
44 }) 47 })
45 }) 48 })
46 49
47 Convey(`When stopped after expiring, should not have a signal.`, func() { 50 Convey(`When stopped after expiring, should not have a signal.`, func() {
48 c.Add(1 * time.Second) 51 c.Add(1 * time.Second)
49 So(t.Stop(), ShouldBeTrue) 52 So(t.Stop(), ShouldBeTrue)
50 53
51 var signalled bool 54 var signalled bool
52 select { 55 select {
53 case <-t.GetC(): 56 case <-t.GetC():
54 signalled = true 57 signalled = true
55 default: 58 default:
56 break 59 break
57 } 60 }
58 So(signalled, ShouldBeFalse) 61 So(signalled, ShouldBeFalse)
59 }) 62 })
60 }) 63 })
61 64
62 Convey(`Should successfully signal.`, func() { 65 Convey(`Should successfully signal.`, func() {
63 So(t.Reset(1*time.Second), ShouldBeFalse) 66 So(t.Reset(1*time.Second), ShouldBeFalse)
64 c.Add(1 * time.Second) 67 c.Add(1 * time.Second)
65 68
66 » » » » So(t.GetC(), ShouldNotBeNil) 69 » » » » So(<-t.GetC(), ShouldResemble, clock.AfterResult {Time: now.Add(1 * time.Second)})
67 » » » » So(<-t.GetC(), ShouldResemble, now.Add(1*time.Se cond))
68 }) 70 })
69 }) 71 })
70 72
71 » » Convey(`Multiple goroutines using the timer...`, func() { 73 » » Convey(`Multiple goroutines using timers...`, func() {
72 // Mark when timers are started, so we can ensure that o ur signalling 74 // Mark when timers are started, so we can ensure that o ur signalling
73 // happens after the timers have been instantiated. 75 // happens after the timers have been instantiated.
74 timerStartedC := make(chan bool) 76 timerStartedC := make(chan bool)
75 c.SetTimerCallback(func(_ time.Duration, _ clock.Timer) { 77 c.SetTimerCallback(func(_ time.Duration, _ clock.Timer) {
76 timerStartedC <- true 78 timerStartedC <- true
77 }) 79 })
78 80
79 » » » resultC := make(chan time.Time) 81 » » » resultC := make(chan clock.AfterResult)
80 for i := time.Duration(0); i < 5; i++ { 82 for i := time.Duration(0); i < 5; i++ {
81 go func(d time.Duration) { 83 go func(d time.Duration) {
82 » » » » » timer := c.NewTimer() 84 » » » » » timer := c.NewTimer(ctx)
83 timer.Reset(d) 85 timer.Reset(d)
84 resultC <- <-timer.GetC() 86 resultC <- <-timer.GetC()
85 }(i * time.Second) 87 }(i * time.Second)
86 <-timerStartedC 88 <-timerStartedC
87 } 89 }
88 90
89 moreResults := func() bool { 91 moreResults := func() bool {
90 select { 92 select {
91 case <-resultC: 93 case <-resultC:
92 return true 94 return true
(...skipping 14 matching lines...) Expand all
107 <-resultC 109 <-resultC
108 So(moreResults(), ShouldBeFalse) 110 So(moreResults(), ShouldBeFalse)
109 111
110 // Advance clock to +10s. One final timer should signal. 112 // Advance clock to +10s. One final timer should signal.
111 c.Set(now.Add(10 * time.Second)) 113 c.Set(now.Add(10 * time.Second))
112 <-resultC 114 <-resultC
113 So(moreResults(), ShouldBeFalse) 115 So(moreResults(), ShouldBeFalse)
114 }) 116 })
115 }) 117 })
116 } 118 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698