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

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

Issue 1154213012: Add context-aware "time" library wrapper. (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git@master
Patch Set: 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/testtimer.go
diff --git a/go/src/infra/libs/clock/testtimer.go b/go/src/infra/libs/clock/testtimer.go
new file mode 100644
index 0000000000000000000000000000000000000000..d8e98b3df8e2438ff9a5f806f54f09397dcac535
--- /dev/null
+++ b/go/src/infra/libs/clock/testtimer.go
@@ -0,0 +1,52 @@
+// 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 clock
+
+import (
+ "time"
+)
+
+// TestTimer is a Timer implementation that can be used for testing. The timer can be
+// signalled via its Signal method, which writes an an underlying buffered channel.
+//
+// The channel is buffered so it can be used without requiring a separate signalling
+// goroutine.
+type TestTimer interface {
+ Timer
+ Signal(time.Time) // Signal the test timer.
+}
+
+type testTimer struct {
+ signalC chan time.Time // Underlying signal channel.
+ active bool // If true, the timer is active.
+}
+
+// NewTestTimer returns a new, instantiated TestTimer.
+func NewTestTimer() TestTimer {
+ return &testTimer{
+ signalC: make(chan time.Time, 1),
+ }
+}
+
+// Signals the timer.
+func (t *testTimer) Signal(tm time.Time) {
+ t.signalC <- tm
+}
+
+// Implements Timer.
+func (t *testTimer) GetC() (c <-chan time.Time) {
+ return t.signalC
+}
+
+// Implements Timer.
+func (t *testTimer) Reset(d time.Duration) bool {
+ return t.active
+}
+
+// Implements Timer.
+func (t *testTimer) Stop() (active bool) {
+ active, t.active = t.active, false
+ return
+}

Powered by Google App Engine
This is Rietveld 408576698