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 count | 5 package count |
6 | 6 |
7 import ( | 7 import ( |
8 "fmt" | 8 "fmt" |
9 "testing" | 9 "testing" |
10 | 10 |
11 "github.com/luci/gae" | |
12 "github.com/luci/gae/filters/featureBreaker" | 11 "github.com/luci/gae/filters/featureBreaker" |
13 » "github.com/luci/gae/memory" | 12 » "github.com/luci/gae/impl/memory" |
| 13 » "github.com/luci/gae/service/info" |
| 14 » "github.com/luci/gae/service/memcache" |
| 15 » "github.com/luci/gae/service/rawdatastore" |
| 16 » "github.com/luci/gae/service/taskqueue" |
14 . "github.com/smartystreets/goconvey/convey" | 17 . "github.com/smartystreets/goconvey/convey" |
15 "golang.org/x/net/context" | 18 "golang.org/x/net/context" |
16 ) | 19 ) |
17 | 20 |
18 func TestCount(t *testing.T) { | 21 func TestCount(t *testing.T) { |
19 t.Parallel() | 22 t.Parallel() |
20 | 23 |
21 Convey("Test Count filter", t, func() { | 24 Convey("Test Count filter", t, func() { |
22 c, ctr := FilterRDS(memory.Use(context.Background())) | 25 c, ctr := FilterRDS(memory.Use(context.Background())) |
23 | 26 |
24 So(c, ShouldNotBeNil) | 27 So(c, ShouldNotBeNil) |
25 So(ctr, ShouldNotBeNil) | 28 So(ctr, ShouldNotBeNil) |
26 | 29 |
27 » » rds := gae.GetRDS(c) | 30 » » rds := rawdatastore.Get(c) |
28 | 31 |
29 Convey("Calling a rds function should reflect in counter", func(
) { | 32 Convey("Calling a rds function should reflect in counter", func(
) { |
30 » » » p := gae.DSProperty{} | 33 » » » p := rawdatastore.Property{} |
31 p.SetValue(100, false) | 34 p.SetValue(100, false) |
32 » » » _, err := rds.Put(rds.NewKey("Kind", "", 0, nil), &gae.D
SPropertyMap{ | 35 » » » _, err := rds.Put(rds.NewKey("Kind", "", 0, nil), &rawda
tastore.PropertyMap{ |
33 "Val": {p}, | 36 "Val": {p}, |
34 }) | 37 }) |
35 So(err, ShouldBeNil) | 38 So(err, ShouldBeNil) |
36 So(ctr.NewKey.Successes, ShouldEqual, 1) | 39 So(ctr.NewKey.Successes, ShouldEqual, 1) |
37 So(ctr.Put.Successes, ShouldEqual, 1) | 40 So(ctr.Put.Successes, ShouldEqual, 1) |
38 | 41 |
39 Convey("effects are cumulative", func() { | 42 Convey("effects are cumulative", func() { |
40 » » » » _, err := rds.Put(rds.NewKey("Kind", "", 0, nil)
, &gae.DSPropertyMap{ | 43 » » » » _, err := rds.Put(rds.NewKey("Kind", "", 0, nil)
, &rawdatastore.PropertyMap{ |
41 "Val": {p}, | 44 "Val": {p}, |
42 }) | 45 }) |
43 So(err, ShouldBeNil) | 46 So(err, ShouldBeNil) |
44 So(ctr.NewKey.Successes, ShouldEqual, 2) | 47 So(ctr.NewKey.Successes, ShouldEqual, 2) |
45 So(ctr.Put.Successes, ShouldEqual, 2) | 48 So(ctr.Put.Successes, ShouldEqual, 2) |
46 | 49 |
47 Convey("even within transactions", func() { | 50 Convey("even within transactions", func() { |
48 rds.RunInTransaction(func(c context.Cont
ext) error { | 51 rds.RunInTransaction(func(c context.Cont
ext) error { |
49 » » » » » » rds := gae.GetRDS(c) | 52 » » » » » » rds := rawdatastore.Get(c) |
50 k := rds.NewKey("Wat", "sup", 0,
nil) | 53 k := rds.NewKey("Wat", "sup", 0,
nil) |
51 » » » » » » rds.Put(k, &gae.DSPropertyMap{"W
at": {p}}) | 54 » » » » » » rds.Put(k, &rawdatastore.Propert
yMap{"Wat": {p}}) |
52 » » » » » » rds.Put(k, &gae.DSPropertyMap{"W
at": {p}}) | 55 » » » » » » rds.Put(k, &rawdatastore.Propert
yMap{"Wat": {p}}) |
53 return nil | 56 return nil |
54 }, nil) | 57 }, nil) |
55 }) | 58 }) |
56 }) | 59 }) |
57 }) | 60 }) |
58 Convey("errors count against errors", func() { | 61 Convey("errors count against errors", func() { |
59 rds.Get(nil, nil) | 62 rds.Get(nil, nil) |
60 So(ctr.Get.Errors, ShouldEqual, 1) | 63 So(ctr.Get.Errors, ShouldEqual, 1) |
61 » » » k, err := rds.Put(rds.NewKey("Kind", "", 0, nil), &gae.D
SPropertyMap{ | 64 » » » k, err := rds.Put(rds.NewKey("Kind", "", 0, nil), &rawda
tastore.PropertyMap{ |
62 » » » » "Val": {gae.DSProperty{}}, | 65 » » » » "Val": {rawdatastore.Property{}}, |
63 }) | 66 }) |
64 So(err, ShouldBeNil) | 67 So(err, ShouldBeNil) |
65 So(ctr.NewKey.Successes, ShouldEqual, 1) | 68 So(ctr.NewKey.Successes, ShouldEqual, 1) |
66 » » » rds.Get(k, &gae.DSPropertyMap{}) | 69 » » » rds.Get(k, &rawdatastore.PropertyMap{}) |
67 So(ctr.Get.Errors, ShouldEqual, 1) | 70 So(ctr.Get.Errors, ShouldEqual, 1) |
68 So(ctr.Get.Successes, ShouldEqual, 1) | 71 So(ctr.Get.Successes, ShouldEqual, 1) |
69 So(ctr.Get.Total(), ShouldEqual, 2) | 72 So(ctr.Get.Total(), ShouldEqual, 2) |
70 }) | 73 }) |
71 }) | 74 }) |
72 | 75 |
73 Convey("works for memcache", t, func() { | 76 Convey("works for memcache", t, func() { |
74 c, ctr := FilterMC(memory.Use(context.Background())) | 77 c, ctr := FilterMC(memory.Use(context.Background())) |
75 So(c, ShouldNotBeNil) | 78 So(c, ShouldNotBeNil) |
76 So(ctr, ShouldNotBeNil) | 79 So(ctr, ShouldNotBeNil) |
77 » » mc := gae.GetMC(c) | 80 » » mc := memcache.Get(c) |
78 | 81 |
79 mc.Set(mc.NewItem("hello").SetValue([]byte("sup"))) | 82 mc.Set(mc.NewItem("hello").SetValue([]byte("sup"))) |
80 mc.Get("Wat") | 83 mc.Get("Wat") |
81 mc.Get("hello") | 84 mc.Get("hello") |
82 | 85 |
83 So(ctr.Set, ShouldResemble, Entry{1, 0}) | 86 So(ctr.Set, ShouldResemble, Entry{1, 0}) |
84 So(ctr.Get, ShouldResemble, Entry{1, 1}) | 87 So(ctr.Get, ShouldResemble, Entry{1, 1}) |
85 So(ctr.NewItem, ShouldResemble, Entry{1, 0}) | 88 So(ctr.NewItem, ShouldResemble, Entry{1, 0}) |
86 }) | 89 }) |
87 | 90 |
88 Convey("works for taskqueue", t, func() { | 91 Convey("works for taskqueue", t, func() { |
89 c, ctr := FilterTQ(memory.Use(context.Background())) | 92 c, ctr := FilterTQ(memory.Use(context.Background())) |
90 So(c, ShouldNotBeNil) | 93 So(c, ShouldNotBeNil) |
91 So(ctr, ShouldNotBeNil) | 94 So(ctr, ShouldNotBeNil) |
92 » » tq := gae.GetTQ(c) | 95 » » tq := taskqueue.Get(c) |
93 | 96 |
94 » » tq.Add(&gae.TQTask{Name: "wat"}, "") | 97 » » tq.Add(&taskqueue.Task{Name: "wat"}, "") |
95 » » tq.Add(&gae.TQTask{Name: "wat"}, "DNE_QUEUE") | 98 » » tq.Add(&taskqueue.Task{Name: "wat"}, "DNE_QUEUE") |
96 | 99 |
97 So(ctr.Add, ShouldResemble, Entry{1, 1}) | 100 So(ctr.Add, ShouldResemble, Entry{1, 1}) |
98 }) | 101 }) |
99 | 102 |
100 Convey("works for global info", t, func() { | 103 Convey("works for global info", t, func() { |
101 c, fb := featureBreaker.FilterGI(memory.Use(context.Background()
), nil) | 104 c, fb := featureBreaker.FilterGI(memory.Use(context.Background()
), nil) |
102 c, ctr := FilterGI(c) | 105 c, ctr := FilterGI(c) |
103 So(c, ShouldNotBeNil) | 106 So(c, ShouldNotBeNil) |
104 So(ctr, ShouldNotBeNil) | 107 So(ctr, ShouldNotBeNil) |
105 | 108 |
106 » » gi := gae.GetGI(c) | 109 » » gi := info.Get(c) |
107 | 110 |
108 gi.Namespace("foo") | 111 gi.Namespace("foo") |
109 fb.BreakFeatures(nil, "Namespace") | 112 fb.BreakFeatures(nil, "Namespace") |
110 gi.Namespace("boom") | 113 gi.Namespace("boom") |
111 | 114 |
112 So(ctr.Namespace, ShouldResemble, Entry{1, 1}) | 115 So(ctr.Namespace, ShouldResemble, Entry{1, 1}) |
113 }) | 116 }) |
114 } | 117 } |
115 | 118 |
116 func ExampleFilterRDS() { | 119 func ExampleFilterRDS() { |
117 // Set up your context using a base service implementation (memory or pr
od) | 120 // Set up your context using a base service implementation (memory or pr
od) |
118 c := memory.Use(context.Background()) | 121 c := memory.Use(context.Background()) |
119 | 122 |
120 // Apply the counter.FilterRDS | 123 // Apply the counter.FilterRDS |
121 c, counter := FilterRDS(c) | 124 c, counter := FilterRDS(c) |
122 | 125 |
123 // functions use RDS from the context like normal... they don't need to
know | 126 // functions use RDS from the context like normal... they don't need to
know |
124 // that there are any filters at all. | 127 // that there are any filters at all. |
125 someCalledFunc := func(c context.Context) { | 128 someCalledFunc := func(c context.Context) { |
126 » » rds := gae.GetRDS(c) | 129 » » rds := rawdatastore.Get(c) |
127 key := rds.NewKey("Kind", "", 1, nil) | 130 key := rds.NewKey("Kind", "", 1, nil) |
128 » » prop := gae.DSProperty{} | 131 » » prop := rawdatastore.Property{} |
129 prop.SetValue(100, false) | 132 prop.SetValue(100, false) |
130 » » val := gae.DSPropertyMap{ | 133 » » val := rawdatastore.PropertyMap{ |
131 "FieldName": {prop}, | 134 "FieldName": {prop}, |
132 } | 135 } |
133 rds.Put(key, &val) | 136 rds.Put(key, &val) |
134 } | 137 } |
135 | 138 |
136 // Using the other function. | 139 // Using the other function. |
137 someCalledFunc(c) | 140 someCalledFunc(c) |
138 someCalledFunc(c) | 141 someCalledFunc(c) |
139 | 142 |
140 // Then we can see what happened! | 143 // Then we can see what happened! |
141 fmt.Printf("%#v\n", counter.NewKey) | 144 fmt.Printf("%#v\n", counter.NewKey) |
142 fmt.Printf("%d\n", counter.Put.Successes) | 145 fmt.Printf("%d\n", counter.Put.Successes) |
143 // Output: | 146 // Output: |
144 // count.Entry{Successes:2, Errors:0} | 147 // count.Entry{Successes:2, Errors:0} |
145 // 2 | 148 // 2 |
146 } | 149 } |
OLD | NEW |