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

Unified Diff: go/src/infra/libs/clock/systemclock/systemclock.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/systemclock/systemclock.go
diff --git a/go/src/infra/libs/clock/systemclock/systemclock.go b/go/src/infra/libs/clock/systemclock/systemclock.go
new file mode 100644
index 0000000000000000000000000000000000000000..532a0f253051dde1e9b8d874888f2e9375852250
--- /dev/null
+++ b/go/src/infra/libs/clock/systemclock/systemclock.go
@@ -0,0 +1,48 @@
+// Copyright (c) 2014 The Chromium Authors. All rights reserved.
iannucci 2015/06/02 22:19:02 (copyrights) Also, as discussed, I think it makes
dnj 2015/06/03 00:25:28 Done.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package systemclock
+
+import (
+ "time"
+
+ "golang.org/x/net/context"
+ "infra/libs/clock"
+)
+
+// Implementation of Clock that uses Go's standard library.
+type systemClock struct{}
+
+// System clock instance.
+var systemClockInstance systemClock
+
+// systemClock implements clock.Clock.
+var _ clock.Clock = (*systemClock)(nil)
iannucci 2015/06/02 22:19:03 I think this would be even better as var _ cloc
dnj 2015/06/03 00:25:28 Done.
+
+// Get returns an instance of a Clock whose method calls directly use Go's "time"
+// library.
+func Get() clock.Clock {
+ return systemClockInstance
+}
+
+func (systemClock) Now() time.Time {
+ return time.Now()
+}
+
+func (systemClock) Sleep(d time.Duration) {
+ time.Sleep(d)
+}
+
+func (systemClock) NewTimer() clock.Timer {
+ return new(systemTimer)
+}
+
+func (systemClock) After(d time.Duration) <-chan time.Time {
+ return time.After(d)
+}
+
+// Set creates a new Context using the supplied TestClock instance.
+func Set(ctx context.Context) context.Context {
+ return clock.SetClock(ctx, Get())
+}

Powered by Google App Engine
This is Rietveld 408576698