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

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

Issue 1154213012: Add context-aware "time" library wrapper. (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git@master
Patch Set: Removed goroutine safety from testtimer. 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
1 // Copyright 2015 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 package wrapper 5 package wrapper
6 6
7 import ( 7 import (
8 "math/rand" 8 "math/rand"
9 "testing" 9 "testing"
10 "time" 10 "time"
11 11
12 "golang.org/x/net/context" 12 "golang.org/x/net/context"
13 13
14 "appengine/memcache" 14 "appengine/memcache"
15 15
16 . "github.com/smartystreets/goconvey/convey" 16 . "github.com/smartystreets/goconvey/convey"
17 "infra/libs/clock"
18 "infra/libs/clock/testclock"
17 ) 19 )
18 20
19 func TestContextAccess(t *testing.T) { 21 func TestContextAccess(t *testing.T) {
20 Convey("Context Access", t, func() { 22 Convey("Context Access", t, func() {
21 c := context.Background() 23 c := context.Background()
22 24
25 now := time.Date(2015, 1, 1, 0, 0, 0, 0, time.UTC)
26 tc := testclock.New(now)
27 c = clock.SetClock(c, tc)
iannucci 2015/06/03 17:37:19 wdyt about c = testclock.UseTime(c, time.Date(
dnj 2015/06/03 18:21:26 You need a reference to the test clock in order to
28
23 Convey("blank", func() { 29 Convey("blank", func() {
24 So(GetDS(c), ShouldBeNil) 30 So(GetDS(c), ShouldBeNil)
25 So(GetMC(c), ShouldBeNil) 31 So(GetMC(c), ShouldBeNil)
26 So(GetTQ(c), ShouldBeNil) 32 So(GetTQ(c), ShouldBeNil)
27 So(GetGI(c), ShouldBeNil) 33 So(GetGI(c), ShouldBeNil)
28
29 now := time.Now()
30 So(GetTimeNow(c), ShouldHappenOnOrAfter, now)
31 }) 34 })
32 35
33 Convey("DS", func() { 36 Convey("DS", func() {
34 c = SetDS(c, DummyDS()) 37 c = SetDS(c, DummyDS())
35 So(GetDS(c), ShouldNotBeNil) 38 So(GetDS(c), ShouldNotBeNil)
36 So(func() { GetDS(c).Kind(nil) }, ShouldPanic) 39 So(func() { GetDS(c).Kind(nil) }, ShouldPanic)
37 }) 40 })
38 41
39 Convey("MC", func() { 42 Convey("MC", func() {
40 c = SetMC(c, DummyMC()) 43 c = SetMC(c, DummyMC())
(...skipping 11 matching lines...) Expand all
52 c = SetGI(c, DummyGI()) 55 c = SetGI(c, DummyGI())
53 So(GetGI(c), ShouldNotBeNil) 56 So(GetGI(c), ShouldNotBeNil)
54 So(func() { GetGI(c).Datacenter() }, ShouldPanic) 57 So(func() { GetGI(c).Datacenter() }, ShouldPanic)
55 }) 58 })
56 59
57 Convey("QY", func() { 60 Convey("QY", func() {
58 q := DummyQY() 61 q := DummyQY()
59 So(func() { q.Distinct() }, ShouldPanic) 62 So(func() { q.Distinct() }, ShouldPanic)
60 }) 63 })
61 64
62 » » Convey("TimeNow", func() { 65 » » Convey("MathRand", func() {
63 » » » thing := time.Date(2000, time.August, 20, 0, 0, 0, 0, ti me.UTC) 66 » » » r := rand.New(rand.NewSource(now.UnixNano()))
64 » » » c = SetTimeNow(c, &thing) 67 » » » i := r.Int()
65 » » » So(GetTimeNow(c), ShouldResemble, thing)
66 68
67 » » » Convey("MathRand", func() { 69 » » » // when it's unset it picks the current time every time
68 » » » » r := rand.New(rand.NewSource(thing.UnixNano())) 70 » » » So(GetMathRand(c).Int(), ShouldEqual, i)
69 » » » » i := r.Int() 71 » » » So(GetMathRand(c).Int(), ShouldEqual, i)
70 72
71 » » » » // when it's unset it picks TimeNow every time 73 » » » // But we could set it to something concrete to have it persist.
72 » » » » So(GetMathRand(c).Int(), ShouldEqual, i) 74 » » » c = SetMathRand(c, rand.New(rand.NewSource(now.UnixNano( ))))
73 » » » » So(GetMathRand(c).Int(), ShouldEqual, i) 75 » » » r = rand.New(rand.NewSource(now.UnixNano()))
74 76 » » » So(GetMathRand(c).Int(), ShouldEqual, r.Int())
75 » » » » // But we could set it to something concrete to have it persist. 77 » » » So(GetMathRand(c).Int(), ShouldEqual, r.Int())
76 » » » » c = SetMathRand(c, rand.New(rand.NewSource(thing .UnixNano())))
77 » » » » r = rand.New(rand.NewSource(thing.UnixNano()))
78 » » » » So(GetMathRand(c).Int(), ShouldEqual, r.Int())
79 » » » » So(GetMathRand(c).Int(), ShouldEqual, r.Int())
80 » » » })
81
82 » » » c = SetTimeNow(c, nil)
83 » » » now := time.Now()
84 » » » So(GetTimeNow(c), ShouldHappenOnOrAfter, now)
85 }) 78 })
86
87 }) 79 })
88 } 80 }
OLDNEW
« no previous file with comments | « no previous file | go/src/infra/gae/libs/wrapper/mathrand.go » ('j') | go/src/infra/gae/libs/wrapper/memory/memcache.go » ('J')

Powered by Google App Engine
This is Rietveld 408576698