OLD | NEW |
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/testclock" |
17 ) | 18 ) |
18 | 19 |
19 func TestContextAccess(t *testing.T) { | 20 func TestContextAccess(t *testing.T) { |
20 Convey("Context Access", t, func() { | 21 Convey("Context Access", t, func() { |
21 » » c := context.Background() | 22 » » now := time.Date(2015, 1, 1, 0, 0, 0, 0, time.UTC) |
| 23 » » c, _ := testclock.UseTime(context.Background(), now) |
22 | 24 |
23 Convey("blank", func() { | 25 Convey("blank", func() { |
24 So(GetDS(c), ShouldBeNil) | 26 So(GetDS(c), ShouldBeNil) |
25 So(GetMC(c), ShouldBeNil) | 27 So(GetMC(c), ShouldBeNil) |
26 So(GetTQ(c), ShouldBeNil) | 28 So(GetTQ(c), ShouldBeNil) |
27 So(GetGI(c), ShouldBeNil) | 29 So(GetGI(c), ShouldBeNil) |
28 | |
29 now := time.Now() | |
30 So(GetTimeNow(c), ShouldHappenOnOrAfter, now) | |
31 }) | 30 }) |
32 | 31 |
33 Convey("DS", func() { | 32 Convey("DS", func() { |
34 c = SetDS(c, DummyDS()) | 33 c = SetDS(c, DummyDS()) |
35 So(GetDS(c), ShouldNotBeNil) | 34 So(GetDS(c), ShouldNotBeNil) |
36 So(func() { GetDS(c).Kind(nil) }, ShouldPanic) | 35 So(func() { GetDS(c).Kind(nil) }, ShouldPanic) |
37 }) | 36 }) |
38 | 37 |
39 Convey("MC", func() { | 38 Convey("MC", func() { |
40 c = SetMC(c, DummyMC()) | 39 c = SetMC(c, DummyMC()) |
(...skipping 11 matching lines...) Expand all Loading... |
52 c = SetGI(c, DummyGI()) | 51 c = SetGI(c, DummyGI()) |
53 So(GetGI(c), ShouldNotBeNil) | 52 So(GetGI(c), ShouldNotBeNil) |
54 So(func() { GetGI(c).Datacenter() }, ShouldPanic) | 53 So(func() { GetGI(c).Datacenter() }, ShouldPanic) |
55 }) | 54 }) |
56 | 55 |
57 Convey("QY", func() { | 56 Convey("QY", func() { |
58 q := DummyQY() | 57 q := DummyQY() |
59 So(func() { q.Distinct() }, ShouldPanic) | 58 So(func() { q.Distinct() }, ShouldPanic) |
60 }) | 59 }) |
61 | 60 |
62 » » Convey("TimeNow", func() { | 61 » » Convey("MathRand", func() { |
63 » » » thing := time.Date(2000, time.August, 20, 0, 0, 0, 0, ti
me.UTC) | 62 » » » r := rand.New(rand.NewSource(now.UnixNano())) |
64 » » » c = SetTimeNow(c, &thing) | 63 » » » i := r.Int() |
65 » » » So(GetTimeNow(c), ShouldResemble, thing) | |
66 | 64 |
67 » » » Convey("MathRand", func() { | 65 » » » // when it's unset it picks the current time every time |
68 » » » » r := rand.New(rand.NewSource(thing.UnixNano())) | 66 » » » So(GetMathRand(c).Int(), ShouldEqual, i) |
69 » » » » i := r.Int() | 67 » » » So(GetMathRand(c).Int(), ShouldEqual, i) |
70 | 68 |
71 » » » » // when it's unset it picks TimeNow every time | 69 » » » // But we could set it to something concrete to have it
persist. |
72 » » » » So(GetMathRand(c).Int(), ShouldEqual, i) | 70 » » » c = SetMathRand(c, rand.New(rand.NewSource(now.UnixNano(
)))) |
73 » » » » So(GetMathRand(c).Int(), ShouldEqual, i) | 71 » » » r = rand.New(rand.NewSource(now.UnixNano())) |
74 | 72 » » » So(GetMathRand(c).Int(), ShouldEqual, r.Int()) |
75 » » » » // But we could set it to something concrete to
have it persist. | 73 » » » 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 }) | 74 }) |
86 | |
87 }) | 75 }) |
88 } | 76 } |
OLD | NEW |