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

Unified Diff: go/src/infra/libs/clock/testclock.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/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()
+}

Powered by Google App Engine
This is Rietveld 408576698