Index: go/src/infra/libs/clock/testclock.go |
diff --git a/go/src/infra/libs/clock/testclock.go b/go/src/infra/libs/clock/testclock.go |
new file mode 100644 |
index 0000000000000000000000000000000000000000..a9b034a7692d7aab5edb22fa0a9c69fdcc33b2af |
--- /dev/null |
+++ b/go/src/infra/libs/clock/testclock.go |
@@ -0,0 +1,48 @@ |
+// Copyright (c) 2014 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" |
+) |
+ |
+// TestClock is a test-oriented implementation of the 'Clock' interface. |
+// |
+// This implementation's Clock responses are configurable by modifying its member |
+// variables. Time-based events are explicitly triggered by sending on a Timer |
+// instance's channel. |
+type TestClock struct { |
+ T time.Time // The current clock time. |
+ C chan time.Time // Channel to unblock sleeping and After. |
+ Timer func() Timer // Timer generator function. Must be set to use NewTimer() and After(). |
+} |
+ |
iannucci
2015/06/02 06:15:34
Add a line here
var _ Clock = (*TestClock)(nil)
dnj
2015/06/02 17:03:12
Done.
|
+// Now implements Clock. |
+func (c *TestClock) Now() time.Time { |
+ return c.T |
+} |
+ |
+// Sleep implements Clock. |
+func (c *TestClock) Sleep(time.Duration) { |
+ if c.C != nil { |
+ // Wait for sleep signal; update current time. |
+ c.T = <-c.C |
+ } |
+} |
+ |
+// NewTimer implements Clock. |
+func (c *TestClock) NewTimer() Timer { |
+ if c.Timer == nil { |
+ panic("No test timer generator is configured.") |
+ } |
+ return c.Timer() |
+} |
+ |
+// After implements Clock. |
+func (c *TestClock) After(d time.Duration) <-chan time.Time { |
+ t := c.NewTimer() |
+ t.Reset(d) |
+ return t.GetC() |
+} |