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

Unified Diff: common/clock/testclock/testtimer.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 side-by-side diff with in-line comments
Download patch
Index: common/clock/testclock/testtimer.go
diff --git a/common/clock/testclock/testtimer.go b/common/clock/testclock/testtimer.go
index 92c6bd83c9f3467d9ab6993d9d9c4cff7d3bac56..2d3e596537b2b42c62619e1548e0a71fdf25a955 100644
--- a/common/clock/testclock/testtimer.go
+++ b/common/clock/testclock/testtimer.go
@@ -8,6 +8,7 @@ import (
"time"
"github.com/luci/luci-go/common/clock"
+ "golang.org/x/net/context"
)
// timer is an implementation of clock.TestTimer that uses a channel
@@ -16,28 +17,35 @@ import (
// The channel is buffered so it can be used without requiring a separate
// signalling goroutine.
type timer struct {
- clock *testClock
- signalC chan time.Time
+ 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 AfterResult of the timer's expiration written to it
+ // when this timer triggers or is canceled.
+ afterC chan clock.AfterResult
// Cancels callback from clock.invokeAt. Being not nil implies that the timer
// is active.
- cancelFunc cancelFunc
+ cancelFunc context.CancelFunc
}
var _ clock.Timer = (*timer)(nil)
// NewTimer returns a new, instantiated timer.
-func newTimer(clock *testClock) clock.Timer {
+func newTimer(ctx context.Context, clk *testClock) *timer {
return &timer{
- clock: clock,
+ ctx: ctx,
+ clock: clk,
+ tags: clock.Tags(ctx),
+ afterC: make(chan clock.AfterResult, 1),
}
}
-func (t *timer) GetC() (c <-chan time.Time) {
- if t.cancelFunc != nil {
- c = t.signalC
- }
- return
+func (t *timer) GetC() <-chan clock.AfterResult {
+ return t.afterC
}
func (t *timer) Reset(d time.Duration) (active bool) {
@@ -51,8 +59,9 @@ func (t *timer) Reset(d time.Duration) (active bool) {
active = t.Stop()
// Set timer properties.
- t.signalC = make(chan time.Time, 1)
- t.cancelFunc = t.clock.invokeAt(triggerTime, t.signal)
+ var ctx context.Context
+ ctx, t.cancelFunc = context.WithCancel(t.ctx)
+ t.clock.invokeAt(ctx, triggerTime, t.invokeCallback)
return
}
@@ -68,9 +77,9 @@ func (t *timer) Stop() bool {
return true
}
-// Sends a single time signal.
-func (t *timer) signal(now time.Time) {
- if t.signalC != nil {
- t.signalC <- now
+// invokeCallback sends a single time signal.
+func (t *timer) invokeCallback(now time.Time, err error) {
+ if t.afterC != nil {
+ t.afterC <- clock.AfterResult{Time: now, Err: err}
}
}

Powered by Google App Engine
This is Rietveld 408576698