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

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: 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 "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 prematurely if canceled.`, func() {
33 t.Reset(time.Hour)
34 cancelFunc()
35 So((<-t.GetC()).Err, ShouldEqual, context.Canceled)
36 })
37
28 Convey(`When reset`, func() { 38 Convey(`When reset`, func() {
29 active := t.Reset(1 * time.Hour) 39 active := t.Reset(1 * time.Hour)
30 So(active, ShouldBeFalse) 40 So(active, ShouldBeFalse)
31 41
32 Convey(`When reset to a short duration`, func() { 42 Convey(`When reset to a short duration`, func() {
33 // Upper bound of supported platform resolution. Windows is 15ms, so 43 // Upper bound of supported platform resolution. Windows is 15ms, so
34 // make sure we exceed that. 44 // make sure we exceed that.
35 active := t.Reset(100 * time.Millisecond) 45 active := t.Reset(100 * time.Millisecond)
36 46
37 Convey(`Should return active.`, func() { 47 Convey(`Should return active.`, func() {
(...skipping 13 matching lines...) Expand all
51 }) 61 })
52 62
53 Convey(`When stopped, should return active.`, func() { 63 Convey(`When stopped, should return active.`, func() {
54 active := t.Stop() 64 active := t.Stop()
55 So(active, ShouldBeTrue) 65 So(active, ShouldBeTrue)
56 }) 66 })
57 67
58 Convey(`Should have a non-nil channel.`, func() { 68 Convey(`Should have a non-nil channel.`, func() {
59 So(t.GetC(), ShouldNotBeNil) 69 So(t.GetC(), ShouldNotBeNil)
60 }) 70 })
61
62 Reset(func() {
63 t.Stop()
64 })
65 }) 71 })
66 }) 72 })
67 } 73 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698