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

Unified Diff: go/src/infra/libs/clock/testclock/testtimer.go

Issue 1154213012: Add context-aware "time" library wrapper. (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git@master
Patch Set: Refactored into sub-packages. Created 5 years, 7 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: go/src/infra/libs/clock/testclock/testtimer.go
diff --git a/go/src/infra/libs/clock/testclock/testtimer.go b/go/src/infra/libs/clock/testclock/testtimer.go
new file mode 100644
index 0000000000000000000000000000000000000000..e3cefdc0677a6093c776379a227a29dfc15be6e8
--- /dev/null
+++ b/go/src/infra/libs/clock/testclock/testtimer.go
@@ -0,0 +1,61 @@
+// Copyright (c) 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package testclock
+
+import (
+ "time"
+
+ "infra/libs/clock"
+)
+
+// TimerFactory is a function that generates a new Timer instance.
+type TimerFactory func() clock.Timer
+
+// TestTimer is a Timer implementation that can be used for testing. The timer can be
+// signalled via its Signal method.
+type TestTimer interface {
+ clock.Timer
+ Signal(time.Time) // Signal the test timer.
+}
+
+// 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 {
+ signalC chan time.Time // Underlying signal channel.
+ active bool // If true, the timer is active.
+}
+
+var _ TestTimer = (*timer)(nil)
+
+// NewTimer returns a new, instantiated timer.
+func NewTimer() TestTimer {
+ return &timer{
+ signalC: make(chan time.Time, 1),
+ }
+}
+
+// Signals the timer.
+func (t *timer) Signal(tm time.Time) {
+ t.signalC <- tm
+}
+
+// Implements Timer.
iannucci 2015/06/02 22:19:03 don't need these comments
dnj 2015/06/03 00:25:28 Done.
+func (t *timer) GetC() (c <-chan time.Time) {
+ return t.signalC
+}
+
+// Implements Timer.
+func (t *timer) Reset(d time.Duration) bool {
+ return t.active
+}
+
+// Implements Timer.
+func (t *timer) Stop() (active bool) {
+ active, t.active = t.active, false
+ return
+}

Powered by Google App Engine
This is Rietveld 408576698