OLD | NEW |
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 testclock | 5 package testclock |
6 | 6 |
7 import ( | 7 import ( |
8 "time" | 8 "time" |
9 | 9 |
10 "github.com/luci/luci-go/common/clock" | 10 "github.com/luci/luci-go/common/clock" |
| 11 "golang.org/x/net/context" |
11 ) | 12 ) |
12 | 13 |
13 // timer is an implementation of clock.TestTimer that uses a channel | 14 // timer is an implementation of clock.TestTimer that uses a channel |
14 // to signal the timer to fire. | 15 // to signal the timer to fire. |
15 // | 16 // |
16 // The channel is buffered so it can be used without requiring a separate | 17 // The channel is buffered so it can be used without requiring a separate |
17 // signalling goroutine. | 18 // signalling goroutine. |
18 type timer struct { | 19 type timer struct { |
19 » clock *testClock | 20 » ctx context.Context |
20 » signalC chan time.Time | 21 » clock *testClock |
| 22 |
| 23 » // tags is the set of tags in the Context when this timer was created. |
| 24 » tags []string |
| 25 |
| 26 » // afterC will have the AfterResult of the timer's expiration written to
it |
| 27 » // when this timer triggers or is canceled. |
| 28 » afterC chan clock.AfterResult |
21 | 29 |
22 // Cancels callback from clock.invokeAt. Being not nil implies that the
timer | 30 // Cancels callback from clock.invokeAt. Being not nil implies that the
timer |
23 // is active. | 31 // is active. |
24 » cancelFunc cancelFunc | 32 » cancelFunc context.CancelFunc |
25 } | 33 } |
26 | 34 |
27 var _ clock.Timer = (*timer)(nil) | 35 var _ clock.Timer = (*timer)(nil) |
28 | 36 |
29 // NewTimer returns a new, instantiated timer. | 37 // NewTimer returns a new, instantiated timer. |
30 func newTimer(clock *testClock) clock.Timer { | 38 func newTimer(ctx context.Context, clk *testClock) *timer { |
31 return &timer{ | 39 return &timer{ |
32 » » clock: clock, | 40 » » ctx: ctx, |
| 41 » » clock: clk, |
| 42 » » tags: clock.Tags(ctx), |
| 43 » » afterC: make(chan clock.AfterResult, 1), |
33 } | 44 } |
34 } | 45 } |
35 | 46 |
36 func (t *timer) GetC() (c <-chan time.Time) { | 47 func (t *timer) GetC() <-chan clock.AfterResult { |
37 » if t.cancelFunc != nil { | 48 » return t.afterC |
38 » » c = t.signalC | |
39 » } | |
40 » return | |
41 } | 49 } |
42 | 50 |
43 func (t *timer) Reset(d time.Duration) (active bool) { | 51 func (t *timer) Reset(d time.Duration) (active bool) { |
44 now := t.clock.Now() | 52 now := t.clock.Now() |
45 triggerTime := now.Add(d) | 53 triggerTime := now.Add(d) |
46 | 54 |
47 // Signal our timerSet callback. | 55 // Signal our timerSet callback. |
48 t.clock.signalTimerSet(d, t) | 56 t.clock.signalTimerSet(d, t) |
49 | 57 |
50 // Stop our current polling goroutine, if it's running. | 58 // Stop our current polling goroutine, if it's running. |
51 active = t.Stop() | 59 active = t.Stop() |
52 | 60 |
53 // Set timer properties. | 61 // Set timer properties. |
54 » t.signalC = make(chan time.Time, 1) | 62 » var ctx context.Context |
55 » t.cancelFunc = t.clock.invokeAt(triggerTime, t.signal) | 63 » ctx, t.cancelFunc = context.WithCancel(t.ctx) |
| 64 » t.clock.invokeAt(ctx, triggerTime, t.invokeCallback) |
56 return | 65 return |
57 } | 66 } |
58 | 67 |
59 func (t *timer) Stop() bool { | 68 func (t *timer) Stop() bool { |
60 // If the timer is not running, we're done. | 69 // If the timer is not running, we're done. |
61 if t.cancelFunc == nil { | 70 if t.cancelFunc == nil { |
62 return false | 71 return false |
63 } | 72 } |
64 | 73 |
65 // Clear our state. | 74 // Clear our state. |
66 t.cancelFunc() | 75 t.cancelFunc() |
67 t.cancelFunc = nil | 76 t.cancelFunc = nil |
68 return true | 77 return true |
69 } | 78 } |
70 | 79 |
71 // Sends a single time signal. | 80 // invokeCallback sends a single time signal. |
72 func (t *timer) signal(now time.Time) { | 81 func (t *timer) invokeCallback(now time.Time, err error) { |
73 » if t.signalC != nil { | 82 » if t.afterC != nil { |
74 » » t.signalC <- now | 83 » » t.afterC <- clock.AfterResult{Time: now, Err: err} |
75 } | 84 } |
76 } | 85 } |
OLD | NEW |