mirrored from https://chromium.googlesource.com/infra/luci/luci-go
-
Notifications
You must be signed in to change notification settings - Fork 43
/
testtimer.go
163 lines (137 loc) · 3.79 KB
/
testtimer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Copyright 2015 The LUCI Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package testclock
import (
"context"
"time"
"go.chromium.org/luci/common/clock"
)
// timer is an implementation of clock.TestTimer that uses a channel
// to signal the timer to fire.
//
// The channel is buffered so it can be used without requiring a separate
// signalling goroutine.
type timer struct {
ctx context.Context
clock *testClock
// tags is the set of tags in the Context when this timer was created.
tags []string
// afterC will have the TimerResult of the timer's expiration written to it
// when this timer triggers or is canceled.
afterC chan clock.TimerResult
// monitorFinishedC is used by our timer monitor to signal that the timer has
// stopped.
monitorFinishedC chan bool
}
var _ clock.Timer = (*timer)(nil)
// NewTimer returns a new, instantiated timer.
func newTimer(ctx context.Context, clk *testClock) *timer {
return &timer{
ctx: ctx,
clock: clk,
tags: clock.Tags(ctx),
afterC: make(chan clock.TimerResult, 1),
}
}
func (t *timer) GetC() <-chan clock.TimerResult {
return t.afterC
}
func (t *timer) Reset(d time.Duration) (active bool) {
if d < 0 {
d = 0
}
// Stop our current polling goroutine, if it's running.
active = t.Stop()
// Add ourself to our Clock's scheduler.
triggerC := make(chan time.Time, 1)
t.clock.addPendingTimer(t, d, triggerC)
// Start a timer monitor goroutine.
t.monitorFinishedC = make(chan bool, 1)
go t.monitor(triggerC, t.monitorFinishedC)
return
}
func (t *timer) Stop() (stopped bool) {
// If the timer is not running, we're done.
if t.monitorFinishedC == nil {
return false
}
// Shutdown and wait for our monitoring goroutine.
t.clock.clearPendingTimer(t)
stopped = <-t.monitorFinishedC
// Clear state.
t.monitorFinishedC = nil
return
}
// GetTags returns the tags associated with the specified timer. If the timer
// has no tags, an empty slice (nil) will be returned.
func GetTags(t clock.Timer) []string {
if tt, ok := t.(*timer); ok {
return clock.Tags(tt.ctx)
}
return nil
}
// HasTags tests if a given timer has the same tags.
func HasTags(t clock.Timer, first string, tags ...string) bool {
timerTags := GetTags(t)
if len(timerTags) == 0 {
return false
}
if first != timerTags[0] {
return false
}
if len(timerTags) > 1 {
// Compare the remainder.
timerTags = timerTags[1:]
if len(timerTags) != len(tags) {
return false
}
for i, tag := range timerTags {
if tags[i] != tag {
return false
}
}
}
return true
}
// monitor is run in a goroutine, monitoring the timer Context for
// cancellation and forcing a premature timer completion if the monitor
// was canceled.
func (t *timer) monitor(triggerC chan time.Time, finishedC chan bool) {
monitorRunning := false
defer func() {
finishedC <- monitorRunning
close(finishedC)
}()
select {
case now, ok := <-triggerC:
if !ok {
// Our monitor goroutine has been reaped, probably because we were
// stopped.
monitorRunning = true
break
}
// The Clock has signalled that this timer has expired.
t.afterC <- clock.TimerResult{
Time: now,
Err: nil,
}
case <-t.ctx.Done():
// Our Context has been canceled.
t.clock.clearPendingTimer(t)
t.afterC <- clock.TimerResult{
Time: t.clock.Now(),
Err: t.ctx.Err(),
}
}
}