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

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

Issue 1154213012: Add context-aware "time" library wrapper. (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git@master
Patch Set: Added coverage files. 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 | « go/src/infra/libs/clock/clockflag/time_test.go ('k') | go/src/infra/libs/clock/systemclock.go » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: go/src/infra/libs/clock/context.go
diff --git a/go/src/infra/libs/clock/context.go b/go/src/infra/libs/clock/context.go
new file mode 100644
index 0000000000000000000000000000000000000000..66af5ac5bd589576f526196c027196550c926112
--- /dev/null
+++ b/go/src/infra/libs/clock/context.go
@@ -0,0 +1,71 @@
+// Copyright 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"
+
+ "golang.org/x/net/context"
+)
+
+// Context key for clock.
+type clockKeyType int
+
+// Unique value for clock key.
+var clockKey clockKeyType
+
+// Factory is a generator function that produces a Clock instnace.
+type Factory func(context.Context) Clock
+
+// SetFactory creates a new Context using the supplied Clock factory.
+func SetFactory(ctx context.Context, f Factory) context.Context {
+ return context.WithValue(ctx, clockKey, f)
+}
+
+// Set creates a new Context using the supplied Clock.
+func Set(ctx context.Context, c Clock) context.Context {
+ return SetFactory(ctx, func(context.Context) Clock { return c })
+}
+
+// Get returns the Clock set in the supplied Context, defaulting to
+// SystemClock() if none is set.
+func Get(ctx context.Context) (clock Clock) {
+ v := ctx.Value(clockKey)
+ if v != nil {
+ f := v.(Factory)
+ if f != nil {
+ clock = f(ctx)
+ }
+ }
+ if clock == nil {
+ clock = GetSystemClock()
+ }
+ return
+}
+
+//
+// "Implement" the Clock interface at the package level.
+//
+
+// Now calls Clock.Now on the Clock instance stored in the supplied Context.
+func Now(ctx context.Context) time.Time {
+ return Get(ctx).Now()
+}
+
+// Sleep calls Clock.Sleep on the Clock instance stored in the supplied Context.
+func Sleep(ctx context.Context, d time.Duration) {
+ Get(ctx).Sleep(d)
+}
+
+// NewTimer calls Clock.NewTimer on the Clock instance stored in the supplied
+// Context.
+func NewTimer(ctx context.Context) Timer {
+ return Get(ctx).NewTimer()
+}
+
+// After calls Clock.After on the Clock instance stored in the supplied Context.
+func After(ctx context.Context, d time.Duration) <-chan time.Time {
+ return Get(ctx).After(d)
+}
« no previous file with comments | « go/src/infra/libs/clock/clockflag/time_test.go ('k') | go/src/infra/libs/clock/systemclock.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698