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

Side by Side Diff: common/clock/external_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 clock 5 package clock
6 6
7 import ( 7 import (
8 "testing" 8 "testing"
9 "time" 9 "time"
10 10
11 . "github.com/smartystreets/goconvey/convey" 11 . "github.com/smartystreets/goconvey/convey"
12 "golang.org/x/net/context" 12 "golang.org/x/net/context"
13 ) 13 )
14 14
15 // testClock is a Clock implementation used for testing. 15 // testClock is a Clock implementation used for testing.
16 type testClock struct { 16 type testClock struct {
17 nowCallback func() time.Time 17 nowCallback func() time.Time
18 » sleepCallback func() 18 » sleepCallback func() error
19 newTimerCallback func() Timer 19 newTimerCallback func() Timer
20 » afterCallback func() <-chan time.Time 20 » afterCallback func() <-chan AfterResult
21 } 21 }
22 22
23 func (tc *testClock) Now() time.Time { 23 func (tc *testClock) Now() time.Time {
24 return tc.nowCallback() 24 return tc.nowCallback()
25 } 25 }
26 26
27 func (tc *testClock) Sleep(time.Duration) { 27 func (tc *testClock) Sleep(context.Context, time.Duration) error {
28 » tc.sleepCallback() 28 » return tc.sleepCallback()
29 } 29 }
30 30
31 func (tc *testClock) NewTimer() Timer { 31 func (tc *testClock) NewTimer(context.Context) Timer {
32 return tc.newTimerCallback() 32 return tc.newTimerCallback()
33 } 33 }
34 34
35 func (tc *testClock) After(time.Duration) <-chan time.Time { 35 func (tc *testClock) After(context.Context, time.Duration) <-chan AfterResult {
36 return tc.afterCallback() 36 return tc.afterCallback()
37 } 37 }
38 38
39 func TestExternal(t *testing.T) { 39 func TestExternal(t *testing.T) {
40 t.Parallel() 40 t.Parallel()
41 41
42 now := time.Date(2015, 01, 01, 0, 0, 0, 0, time.UTC) 42 now := time.Date(2015, 01, 01, 0, 0, 0, 0, time.UTC)
43 Convey(`A Context with a testClock installed`, t, func() { 43 Convey(`A Context with a testClock installed`, t, func() {
44 tc := &testClock{} 44 tc := &testClock{}
45 c := Set(context.Background(), tc) 45 c := Set(context.Background(), tc)
46 46
47 Convey(`Now() will use the testClock's Now().`, func() { 47 Convey(`Now() will use the testClock's Now().`, func() {
48 used := false 48 used := false
49 tc.nowCallback = func() time.Time { 49 tc.nowCallback = func() time.Time {
50 used = true 50 used = true
51 return now 51 return now
52 } 52 }
53 53
54 So(Now(c), ShouldResemble, now) 54 So(Now(c), ShouldResemble, now)
55 So(used, ShouldBeTrue) 55 So(used, ShouldBeTrue)
56 }) 56 })
57 57
58 Convey(`Sleep() will use testClock's Sleep().`, func() { 58 Convey(`Sleep() will use testClock's Sleep().`, func() {
59 used := false 59 used := false
60 » » » tc.sleepCallback = func() { 60 » » » tc.sleepCallback = func() error {
61 used = true 61 used = true
62 return nil
62 } 63 }
63 64
64 Sleep(c, time.Second) 65 Sleep(c, time.Second)
65 So(used, ShouldBeTrue) 66 So(used, ShouldBeTrue)
66 }) 67 })
67 68
68 Convey(`NewTimer() will use testClock's NewTimer().`, func() { 69 Convey(`NewTimer() will use testClock's NewTimer().`, func() {
69 used := false 70 used := false
70 tc.newTimerCallback = func() Timer { 71 tc.newTimerCallback = func() Timer {
71 used = true 72 used = true
72 return nil 73 return nil
73 } 74 }
74 75
75 NewTimer(c) 76 NewTimer(c)
76 So(used, ShouldBeTrue) 77 So(used, ShouldBeTrue)
77 }) 78 })
78 79
79 Convey(`After() will use testClock's After().`, func() { 80 Convey(`After() will use testClock's After().`, func() {
80 used := false 81 used := false
81 » » » tc.afterCallback = func() <-chan time.Time { 82 » » » tc.afterCallback = func() <-chan AfterResult {
82 used = true 83 used = true
83 return nil 84 return nil
84 } 85 }
85 86
86 After(c, time.Second) 87 After(c, time.Second)
87 So(used, ShouldBeTrue) 88 So(used, ShouldBeTrue)
88 }) 89 })
89 }) 90 })
90 91
91 Convey(`An Context with no clock installed`, t, func() { 92 Convey(`An Context with no clock installed`, t, func() {
92 c := context.Background() 93 c := context.Background()
93 94
94 Convey(`Will return a SystemClock instance.`, func() { 95 Convey(`Will return a SystemClock instance.`, func() {
95 So(Get(c), ShouldHaveSameTypeAs, systemClock{}) 96 So(Get(c), ShouldHaveSameTypeAs, systemClock{})
96 }) 97 })
97 }) 98 })
98 } 99 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698