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

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: Cleanup memlock, restore old determinism, better name. 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..492189b8a70d451377a9fd1da5862f435f9ef551 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 TimerResult of the timer's expiration written to it
+ // when this timer triggers or is canceled.
+ afterC chan clock.TimerResult
// 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.TimerResult, 1),
}
}
-func (t *timer) GetC() (c <-chan time.Time) {
- if t.cancelFunc != nil {
- c = t.signalC
- }
- return
+func (t *timer) GetC() <-chan clock.TimerResult {
+ return t.afterC
}
func (t *timer) Reset(d time.Duration) (active bool) {
@@ -45,14 +53,21 @@ func (t *timer) Reset(d time.Duration) (active bool) {
triggerTime := now.Add(d)
// Signal our timerSet callback.
+ afterC := make(chan clock.TimerResult, 1)
t.clock.signalTimerSet(d, t)
// Stop our current polling goroutine, if it's running.
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, func(now time.Time, err error) {
+ t.cancelFunc = nil
+ afterC <- clock.TimerResult{Time: now, Err: err}
+ })
+
+ t.afterC = afterC
return
}
@@ -67,10 +82,3 @@ func (t *timer) Stop() bool {
t.cancelFunc = nil
return true
}
-
-// Sends a single time signal.
-func (t *timer) signal(now time.Time) {
- if t.signalC != nil {
- t.signalC <- now
- }
-}

Powered by Google App Engine
This is Rietveld 408576698