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

Unified Diff: go/src/infra/libs/clock/clock.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
« no previous file with comments | « no previous file | go/src/infra/libs/clock/context.go » ('j') | go/src/infra/libs/clock/context.go » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: go/src/infra/libs/clock/clock.go
diff --git a/go/src/infra/libs/clock/clock.go b/go/src/infra/libs/clock/clock.go
new file mode 100644
index 0000000000000000000000000000000000000000..c666f23763428d86e381745abdc8262bb05cd73a
--- /dev/null
+++ b/go/src/infra/libs/clock/clock.go
@@ -0,0 +1,52 @@
+// 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"
+)
+
+// A Clock is an interface to system time.
+//
+// The standard clock is SystemClock, which falls through to the system time library.
+// Another clock, FakeClock, is available to simulate time facilities for testing.
+type Clock interface {
+ Now() time.Time // Returns the current time (see time.Now).
+ Sleep(time.Duration) // Sleeps the current goroutine (see time.Sleep)
+ NewTimer() Timer // Creates a new Timer instance.
+ After(time.Duration) <-chan time.Time // Waits a duration, then sends the current time.
+}
+
+// Implementation of Clock that uses Go's standard library.
iannucci 2015/06/02 06:15:33 implementations should go in subpackages I think.
+type systemClock struct{}
+
+// System clock instance.
+var systemClockInstance systemClock
+
+// SystemClock returns an instance of a Clock whose method calls directly use Go's "time"
+// library.
+func SystemClock() Clock {
+ return systemClockInstance
+}
+
+// Implements Clock.
+func (systemClock) Now() time.Time {
+ return time.Now()
+}
+
+// Implements Clock.
+func (systemClock) Sleep(d time.Duration) {
+ time.Sleep(d)
+}
+
+// Implements Clock.
+func (systemClock) NewTimer() Timer {
+ return new(systemTimer)
+}
+
+// Implements Clock.
+func (systemClock) After(d time.Duration) <-chan time.Time {
+ return time.After(d)
+}
« no previous file with comments | « no previous file | go/src/infra/libs/clock/context.go » ('j') | go/src/infra/libs/clock/context.go » ('J')

Powered by Google App Engine
This is Rietveld 408576698