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

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: Removed goroutine safety from testtimer. 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
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 Convey(`A testing clock instance`, t, func() {
17 now := time.Date(2015, 01, 01, 00, 00, 00, 00, time.UTC)
18 c := New(now)
19
20 Convey(`A timer instance`, func() {
21 t := c.NewTimer()
22
23 Convey(`Should have a non-nil C.`, func() {
24 So(t.GetC(), ShouldBeNil)
25 })
26
27 Convey(`When activated`, func() {
28 So(t.Reset(1*time.Second), ShouldBeFalse)
29
30 Convey(`When reset, should return active.`, func () {
31 So(t.Reset(1*time.Hour), ShouldBeTrue)
32 So(t.GetC(), ShouldNotBeNil)
33 })
34
35 Convey(`When stopped, should return active.`, fu nc() {
36 So(t.Stop(), ShouldBeTrue)
37 So(t.GetC(), ShouldBeNil)
38
39 Convey(`And when stopped again, should r eturn inactive.`, func() {
40 So(t.Stop(), ShouldBeFalse)
41 So(t.GetC(), ShouldBeNil)
42 })
43 })
44
45 Convey(`When stopped after expiring, should not have a signal.`, func() {
46 c.Add(1 * time.Second)
47 So(t.Stop(), ShouldBeTrue)
48
49 var signalled bool
50 select {
51 case <-t.GetC():
52 signalled = true
53 default:
54 break
55 }
56 So(signalled, ShouldBeFalse)
57 })
58 })
59
60 Convey(`Should successfully signal.`, func() {
61 So(t.Reset(1*time.Second), ShouldBeFalse)
62 c.Add(1 * time.Second)
63
64 So(t.GetC(), ShouldNotBeNil)
65 So(<-t.GetC(), ShouldResemble, now.Add(1*time.Se cond))
66 })
67 })
68
69 Convey(`Multiple goroutines using the timer...`, func() {
70 // Mark when timers are started, so we can ensure that o ur signalling
71 // happens after the timers have been instantiated.
72 timerStartedC := make(chan bool)
73 c.SetTimerCallback(func(_ clock.Timer) {
74 timerStartedC <- true
75 })
76
77 resultC := make(chan time.Time)
78 for i := time.Duration(0); i < 5; i++ {
79 go func(d time.Duration) {
80 timer := c.NewTimer()
81 timer.Reset(d)
82 resultC <- <-timer.GetC()
83 }(i * time.Second)
84 <-timerStartedC
85 }
86
87 moreResults := func() bool {
88 select {
89 case <-resultC:
90 return true
91 default:
92 return false
93 }
94 }
95
96 // Advance the clock to +2s. Three timers should signal.
97 c.Set(now.Add(2 * time.Second))
98 <-resultC
99 <-resultC
100 <-resultC
101 So(moreResults(), ShouldBeFalse)
102
103 // Advance clock to +3s. One timer should signal.
104 c.Set(now.Add(3 * time.Second))
105 <-resultC
106 So(moreResults(), ShouldBeFalse)
107
108 // Advance clock to +10s. One final timer should signal.
109 c.Set(now.Add(10 * time.Second))
110 <-resultC
111 So(moreResults(), ShouldBeFalse)
112 })
113 })
114 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698