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

Side by Side Diff: go/src/infra/gae/libs/wrapper/time.go

Issue 1151473003: Better attempt at an appengine wrapper. (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git@master
Patch Set: fix coverage numbers Created 5 years, 6 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 package wrapper
6
7 import (
8 "time"
9
10 "golang.org/x/net/context"
11 )
12
13 // TimeNowFactory is the function signature for factory methods compatible with
14 // SetTimeNowFactory.
15 type TimeNowFactory func(context.Context) time.Time
16
17 // GetTimeNow gets the current time.Time from the context. If the context has no
18 // time factory set with SetTimeNowFactory, it returns time.Now().
19 func GetTimeNow(c context.Context) time.Time {
20 if f, ok := c.Value(timeNowKey).(TimeNowFactory); ok && f != nil {
21 return f(c)
22 }
23 return time.Now()
24 }
25
26 // SetTimeNowFactory sets the function to produce time.Time instances, as
27 // returned by the GetTimeNow method.
28 func SetTimeNowFactory(c context.Context, tnf TimeNowFactory) context.Context {
29 return context.WithValue(c, timeNowKey, tnf)
30 }
31
32 // SetTimeNow sets a pointer to the current time.Time object in the context.
33 // Useful for testing with a quick mock. This is just a shorthand
34 // SetTimeNowFactory invocation to set a factory which always returns the same
35 // object.
36 //
37 // Note the pointer asymmetry with GetTimeNow. This is done intentionally to
38 // allow you to modify the pointed-to-time to be able to manually monkey with
39 // the clock in a test. Otherwise this would essentially freeze time in place.
40 // If you pass 'nil', this will set the context back to returning time.Now().
41 func SetTimeNow(c context.Context, t *time.Time) context.Context {
42 if t == nil {
43 return SetTimeNowFactory(c, nil)
44 }
45 return SetTimeNowFactory(c, func(context.Context) time.Time { return *t })
46 }
OLDNEW
« no previous file with comments | « go/src/infra/gae/libs/wrapper/testable.go ('k') | go/src/infra/gae/libs/wrapper/wrapper.infra_testing » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698