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

Side by Side Diff: go/src/infra/libs/clock/testclock/testtimer_test.go

Issue 1154213012: Add context-aware "time" library wrapper. (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git@master
Patch Set: Added coverage files. Created 5 years, 6 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
« no previous file with comments | « go/src/infra/libs/clock/testclock/testtimer.go ('k') | go/src/infra/libs/clock/timer.go » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 "testing"
9 "time"
10
11 . "github.com/smartystreets/goconvey/convey"
12 "infra/libs/clock"
13 )
14
15 func TestTestTimer(t *testing.T) {
16 t.Parallel()
17
18 Convey(`A testing clock instance`, t, func() {
19 now := time.Date(2015, 01, 01, 00, 00, 00, 00, time.UTC)
20 c := New(now)
21
22 Convey(`A timer instance`, func() {
23 t := c.NewTimer()
24
25 Convey(`Should have a non-nil C.`, func() {
26 So(t.GetC(), ShouldBeNil)
27 })
28
29 Convey(`When activated`, func() {
30 So(t.Reset(1*time.Second), ShouldBeFalse)
31
32 Convey(`When reset, should return active.`, func () {
33 So(t.Reset(1*time.Hour), ShouldBeTrue)
34 So(t.GetC(), ShouldNotBeNil)
35 })
36
37 Convey(`When stopped, should return active.`, fu nc() {
38 So(t.Stop(), ShouldBeTrue)
39 So(t.GetC(), ShouldBeNil)
40
41 Convey(`And when stopped again, should r eturn inactive.`, func() {
42 So(t.Stop(), ShouldBeFalse)
43 So(t.GetC(), ShouldBeNil)
44 })
45 })
46
47 Convey(`When stopped after expiring, should not have a signal.`, func() {
48 c.Add(1 * time.Second)
49 So(t.Stop(), ShouldBeTrue)
50
51 var signalled bool
52 select {
53 case <-t.GetC():
54 signalled = true
55 default:
56 break
57 }
58 So(signalled, ShouldBeFalse)
59 })
60 })
61
62 Convey(`Should successfully signal.`, func() {
63 So(t.Reset(1*time.Second), ShouldBeFalse)
64 c.Add(1 * time.Second)
65
66 So(t.GetC(), ShouldNotBeNil)
67 So(<-t.GetC(), ShouldResemble, now.Add(1*time.Se cond))
68 })
69 })
70
71 Convey(`Multiple goroutines using the timer...`, func() {
72 // Mark when timers are started, so we can ensure that o ur signalling
73 // happens after the timers have been instantiated.
74 timerStartedC := make(chan bool)
75 c.SetTimerCallback(func(_ clock.Timer) {
76 timerStartedC <- true
77 })
78
79 resultC := make(chan time.Time)
80 for i := time.Duration(0); i < 5; i++ {
81 go func(d time.Duration) {
82 timer := c.NewTimer()
83 timer.Reset(d)
84 resultC <- <-timer.GetC()
85 }(i * time.Second)
86 <-timerStartedC
87 }
88
89 moreResults := func() bool {
90 select {
91 case <-resultC:
92 return true
93 default:
94 return false
95 }
96 }
97
98 // Advance the clock to +2s. Three timers should signal.
99 c.Set(now.Add(2 * time.Second))
100 <-resultC
101 <-resultC
102 <-resultC
103 So(moreResults(), ShouldBeFalse)
104
105 // Advance clock to +3s. One timer should signal.
106 c.Set(now.Add(3 * time.Second))
107 <-resultC
108 So(moreResults(), ShouldBeFalse)
109
110 // Advance clock to +10s. One final timer should signal.
111 c.Set(now.Add(10 * time.Second))
112 <-resultC
113 So(moreResults(), ShouldBeFalse)
114 })
115 })
116 }
OLDNEW
« no previous file with comments | « go/src/infra/libs/clock/testclock/testtimer.go ('k') | go/src/infra/libs/clock/timer.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698