OLD | NEW |
(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 "math/rand" |
| 9 "testing" |
| 10 "time" |
| 11 |
| 12 "golang.org/x/net/context" |
| 13 |
| 14 "appengine/memcache" |
| 15 |
| 16 . "github.com/smartystreets/goconvey/convey" |
| 17 ) |
| 18 |
| 19 func TestContextAccess(t *testing.T) { |
| 20 Convey("Context Access", t, func() { |
| 21 c := context.Background() |
| 22 |
| 23 Convey("blank", func() { |
| 24 So(GetDS(c), ShouldBeNil) |
| 25 So(GetMC(c), ShouldBeNil) |
| 26 So(GetTQ(c), ShouldBeNil) |
| 27 So(GetGI(c), ShouldBeNil) |
| 28 |
| 29 now := time.Now() |
| 30 So(GetTimeNow(c), ShouldHappenOnOrAfter, now) |
| 31 }) |
| 32 |
| 33 Convey("DS", func() { |
| 34 c = SetDS(c, DummyDS()) |
| 35 So(GetDS(c), ShouldNotBeNil) |
| 36 So(func() { GetDS(c).Kind(nil) }, ShouldPanic) |
| 37 }) |
| 38 |
| 39 Convey("MC", func() { |
| 40 c = SetMC(c, DummyMC()) |
| 41 So(GetMC(c), ShouldNotBeNil) |
| 42 So(func() { GetMC(c).InflateCodec(memcache.Codec{}) }, S
houldPanic) |
| 43 }) |
| 44 |
| 45 Convey("TQ", func() { |
| 46 c = SetTQ(c, DummyTQ()) |
| 47 So(GetTQ(c), ShouldNotBeNil) |
| 48 So(func() { GetTQ(c).Purge("") }, ShouldPanic) |
| 49 }) |
| 50 |
| 51 Convey("GI", func() { |
| 52 c = SetGI(c, DummyGI()) |
| 53 So(GetGI(c), ShouldNotBeNil) |
| 54 So(func() { GetGI(c).Datacenter() }, ShouldPanic) |
| 55 }) |
| 56 |
| 57 Convey("QY", func() { |
| 58 q := DummyQY() |
| 59 So(func() { q.Distinct() }, ShouldPanic) |
| 60 }) |
| 61 |
| 62 Convey("TimeNow", func() { |
| 63 thing := time.Date(2000, time.August, 20, 0, 0, 0, 0, ti
me.UTC) |
| 64 c = SetTimeNow(c, &thing) |
| 65 So(GetTimeNow(c), ShouldResemble, thing) |
| 66 |
| 67 Convey("MathRand", func() { |
| 68 r := rand.New(rand.NewSource(thing.UnixNano())) |
| 69 i := r.Int() |
| 70 |
| 71 // when it's unset it picks TimeNow every time |
| 72 So(GetMathRand(c).Int(), ShouldEqual, i) |
| 73 So(GetMathRand(c).Int(), ShouldEqual, i) |
| 74 |
| 75 // But we could set it to something concrete to
have it persist. |
| 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 }) |
| 86 |
| 87 }) |
| 88 } |
OLD | NEW |