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

Side by Side Diff: common/clock/cancelSleep_test.go

Issue 1679023005: Add Context cancellation to clock. (Closed) Base URL: https://github.com/luci/luci-go@master
Patch Set: 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
(Empty)
1 // Copyright 2016 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 clock
6
7 import (
8 "testing"
9 "time"
10
11 "golang.org/x/net/context"
12
13 . "github.com/smartystreets/goconvey/convey"
14 )
15
16 // instrumentedAfterClock is a Clock implementation that implements After. It
17 // is instrumented to allow the test code to enforce operational order.
18 type instrumentedAfterClock struct {
19 Clock
20
21 sleepingC chan struct{}
22 wakeC chan struct{}
23 }
24
25 func (tc *instrumentedAfterClock) After(time.Duration) <-chan time.Time {
26 awakeC := make(chan time.Time, 1)
27 go func() {
28 if tc.sleepingC != nil {
29 tc.sleepingC <- struct{}{}
30 }
31
32 // Wait for signal to wake.
33 <-tc.wakeC
34 awakeC <- time.Time{}
35 }()
36
37 return awakeC
38 }
39
40 // TestCancelSleep tests the CancelSleep function.
41 func TestCancelSleep(t *testing.T) {
42 t.Parallel()
43
44 Convey(`A cancelable Context`, t, func() {
45 tc := instrumentedAfterClock{
46 wakeC: make(chan struct{}, 1),
47 }
48 defer close(tc.wakeC)
49
50 c := Set(context.Background(), &tc)
51 c, cancelFunc := context.WithCancel(c)
52
53 Convey(`Will perform a full sleep if the Context isn't canceled. `, func() {
54 tc.wakeC <- struct{}{}
55
56 So(CancelSleep(c, time.Minute), ShouldBeNil)
57 })
58
59 Convey(`Will return Context error if canceled.`, func() {
60 tc.wakeC <- struct{}{}
61 cancelFunc()
62
63 So(CancelSleep(c, time.Minute), ShouldEqual, context.Can celed)
64 })
65
66 Convey(`Will terminate the Sleep prematurely if the Context is c anceled.`, func() {
67 tc.sleepingC = make(chan struct{})
68 go func() {
69 <-tc.sleepingC
70 cancelFunc()
71 }()
72
73 So(CancelSleep(c, time.Minute), ShouldEqual, context.Can celed)
74 })
75 })
76 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698