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/filter/featureBreaker" | 11 "github.com/luci/gae/filter/featureBreaker" |
12 "github.com/luci/gae/impl/memory" | 12 "github.com/luci/gae/impl/memory" |
13 "github.com/luci/gae/service/datastore" | 13 "github.com/luci/gae/service/datastore" |
14 "github.com/luci/gae/service/info" | 14 "github.com/luci/gae/service/info" |
15 "github.com/luci/gae/service/memcache" | 15 "github.com/luci/gae/service/memcache" |
16 "github.com/luci/gae/service/taskqueue" | 16 "github.com/luci/gae/service/taskqueue" |
17 . "github.com/smartystreets/goconvey/convey" | 17 . "github.com/smartystreets/goconvey/convey" |
18 "golang.org/x/net/context" | 18 "golang.org/x/net/context" |
19 ) | 19 ) |
20 | 20 |
| 21 func shouldHaveSuccessesAndErrors(actual interface{}, expected ...interface{}) s
tring { |
| 22 a := actual.(Entry) |
| 23 if len(expected) != 2 { |
| 24 panic("Invalid number of expected, should be 2 (successes, error
s).") |
| 25 } |
| 26 s, e := expected[0].(int), expected[1].(int) |
| 27 |
| 28 if val := a.Successes.Get(); val != s { |
| 29 return fmt.Sprintf("Actual successes (%d) don't match expected (
%d)", val, s) |
| 30 } |
| 31 if val := a.Errors.Get(); val != e { |
| 32 return fmt.Sprintf("Actual errors (%d) don't match expected (%d)
", val, e) |
| 33 } |
| 34 return "" |
| 35 } |
| 36 |
21 func TestCount(t *testing.T) { | 37 func TestCount(t *testing.T) { |
22 t.Parallel() | 38 t.Parallel() |
23 | 39 |
24 Convey("Test Count filter", t, func() { | 40 Convey("Test Count filter", t, func() { |
25 c, fb := featureBreaker.FilterRDS(memory.Use(context.Background(
)), nil) | 41 c, fb := featureBreaker.FilterRDS(memory.Use(context.Background(
)), nil) |
26 c, ctr := FilterRDS(c) | 42 c, ctr := FilterRDS(c) |
27 | 43 |
28 So(c, ShouldNotBeNil) | 44 So(c, ShouldNotBeNil) |
29 So(ctr, ShouldNotBeNil) | 45 So(ctr, ShouldNotBeNil) |
30 | 46 |
31 ds := datastore.Get(c) | 47 ds := datastore.Get(c) |
32 vals := []datastore.PropertyMap{{ | 48 vals := []datastore.PropertyMap{{ |
33 "Val": {datastore.MkProperty(100)}, | 49 "Val": {datastore.MkProperty(100)}, |
34 "$key": {datastore.MkPropertyNI(ds.NewKey("Kind", "", 1,
nil))}, | 50 "$key": {datastore.MkPropertyNI(ds.NewKey("Kind", "", 1,
nil))}, |
35 }} | 51 }} |
36 | 52 |
37 Convey("Calling a ds function should reflect in counter", func()
{ | 53 Convey("Calling a ds function should reflect in counter", func()
{ |
38 So(ds.PutMulti(vals), ShouldBeNil) | 54 So(ds.PutMulti(vals), ShouldBeNil) |
39 » » » So(ctr.NewKey.Successes, ShouldEqual, 1) | 55 » » » So(ctr.NewKey.Successes.Get(), ShouldEqual, 1) |
40 » » » So(ctr.PutMulti.Successes, ShouldEqual, 1) | 56 » » » So(ctr.PutMulti.Successes.Get(), ShouldEqual, 1) |
41 | 57 |
42 Convey("effects are cumulative", func() { | 58 Convey("effects are cumulative", func() { |
43 So(ds.PutMulti(vals), ShouldBeNil) | 59 So(ds.PutMulti(vals), ShouldBeNil) |
44 » » » » So(ctr.PutMulti.Successes, ShouldEqual, 2) | 60 » » » » So(ctr.PutMulti.Successes.Get(), ShouldEqual, 2) |
45 | 61 |
46 Convey("even within transactions", func() { | 62 Convey("even within transactions", func() { |
47 ds.RunInTransaction(func(c context.Conte
xt) error { | 63 ds.RunInTransaction(func(c context.Conte
xt) error { |
48 ds := datastore.Get(c) | 64 ds := datastore.Get(c) |
49 So(ds.PutMulti(append(vals, vals
[0])), ShouldBeNil) | 65 So(ds.PutMulti(append(vals, vals
[0])), ShouldBeNil) |
50 return nil | 66 return nil |
51 }, nil) | 67 }, nil) |
52 }) | 68 }) |
53 }) | 69 }) |
54 }) | 70 }) |
55 | 71 |
56 Convey("errors count against errors", func() { | 72 Convey("errors count against errors", func() { |
57 fb.BreakFeatures(nil, "GetMulti") | 73 fb.BreakFeatures(nil, "GetMulti") |
58 | 74 |
59 ds.GetMulti(vals) | 75 ds.GetMulti(vals) |
60 » » » So(ctr.GetMulti.Errors, ShouldEqual, 1) | 76 » » » So(ctr.GetMulti.Errors.Get(), ShouldEqual, 1) |
61 | 77 |
62 fb.UnbreakFeatures("GetMulti") | 78 fb.UnbreakFeatures("GetMulti") |
63 | 79 |
64 So(ds.PutMulti(vals), ShouldBeNil) | 80 So(ds.PutMulti(vals), ShouldBeNil) |
65 | 81 |
66 ds.GetMulti(vals) | 82 ds.GetMulti(vals) |
67 » » » So(ctr.GetMulti.Errors, ShouldEqual, 1) | 83 » » » So(ctr.GetMulti.Errors.Get(), ShouldEqual, 1) |
68 » » » So(ctr.GetMulti.Successes, ShouldEqual, 1) | 84 » » » So(ctr.GetMulti.Successes.Get(), ShouldEqual, 1) |
69 So(ctr.GetMulti.Total(), ShouldEqual, 2) | 85 So(ctr.GetMulti.Total(), ShouldEqual, 2) |
70 }) | 86 }) |
71 }) | 87 }) |
72 | 88 |
73 Convey("works for memcache", t, func() { | 89 Convey("works for memcache", t, func() { |
74 c, ctr := FilterMC(memory.Use(context.Background())) | 90 c, ctr := FilterMC(memory.Use(context.Background())) |
75 So(c, ShouldNotBeNil) | 91 So(c, ShouldNotBeNil) |
76 So(ctr, ShouldNotBeNil) | 92 So(ctr, ShouldNotBeNil) |
77 mc := memcache.Get(c) | 93 mc := memcache.Get(c) |
78 | 94 |
79 mc.Set(mc.NewItem("hello").SetValue([]byte("sup"))) | 95 mc.Set(mc.NewItem("hello").SetValue([]byte("sup"))) |
80 So(mc.Get(mc.NewItem("Wat")), ShouldNotBeNil) | 96 So(mc.Get(mc.NewItem("Wat")), ShouldNotBeNil) |
81 mc.Get(mc.NewItem("hello")) | 97 mc.Get(mc.NewItem("hello")) |
82 | 98 |
83 » » So(ctr.SetMulti, ShouldResemble, Entry{1, 0}) | 99 » » So(ctr.SetMulti, shouldHaveSuccessesAndErrors, 1, 0) |
84 » » So(ctr.GetMulti, ShouldResemble, Entry{2, 0}) | 100 » » So(ctr.GetMulti, shouldHaveSuccessesAndErrors, 2, 0) |
85 » » So(ctr.NewItem, ShouldResemble, Entry{3, 0}) | 101 » » So(ctr.NewItem, shouldHaveSuccessesAndErrors, 3, 0) |
86 }) | 102 }) |
87 | 103 |
88 Convey("works for taskqueue", t, func() { | 104 Convey("works for taskqueue", t, func() { |
89 c, ctr := FilterTQ(memory.Use(context.Background())) | 105 c, ctr := FilterTQ(memory.Use(context.Background())) |
90 So(c, ShouldNotBeNil) | 106 So(c, ShouldNotBeNil) |
91 So(ctr, ShouldNotBeNil) | 107 So(ctr, ShouldNotBeNil) |
92 tq := taskqueue.Get(c) | 108 tq := taskqueue.Get(c) |
93 | 109 |
94 tq.Add(&taskqueue.Task{Name: "wat"}, "") | 110 tq.Add(&taskqueue.Task{Name: "wat"}, "") |
95 tq.Add(&taskqueue.Task{Name: "wat"}, "DNE_QUEUE") | 111 tq.Add(&taskqueue.Task{Name: "wat"}, "DNE_QUEUE") |
96 | 112 |
97 » » So(ctr.AddMulti, ShouldResemble, Entry{1, 1}) | 113 » » So(ctr.AddMulti, shouldHaveSuccessesAndErrors, 1, 1) |
98 }) | 114 }) |
99 | 115 |
100 Convey("works for global info", t, func() { | 116 Convey("works for global info", t, func() { |
101 c, fb := featureBreaker.FilterGI(memory.Use(context.Background()
), nil) | 117 c, fb := featureBreaker.FilterGI(memory.Use(context.Background()
), nil) |
102 c, ctr := FilterGI(c) | 118 c, ctr := FilterGI(c) |
103 So(c, ShouldNotBeNil) | 119 So(c, ShouldNotBeNil) |
104 So(ctr, ShouldNotBeNil) | 120 So(ctr, ShouldNotBeNil) |
105 | 121 |
106 gi := info.Get(c) | 122 gi := info.Get(c) |
107 | 123 |
108 gi.Namespace("foo") | 124 gi.Namespace("foo") |
109 fb.BreakFeatures(nil, "Namespace") | 125 fb.BreakFeatures(nil, "Namespace") |
110 gi.Namespace("boom") | 126 gi.Namespace("boom") |
111 | 127 |
112 » » So(ctr.Namespace, ShouldResemble, Entry{1, 1}) | 128 » » So(ctr.Namespace, shouldHaveSuccessesAndErrors, 1, 1) |
113 }) | 129 }) |
114 } | 130 } |
115 | 131 |
116 func ExampleFilterRDS() { | 132 func ExampleFilterRDS() { |
117 // Set up your context using a base service implementation (memory or pr
od) | 133 // Set up your context using a base service implementation (memory or pr
od) |
118 c := memory.Use(context.Background()) | 134 c := memory.Use(context.Background()) |
119 | 135 |
120 // Apply the counter.FilterRDS | 136 // Apply the counter.FilterRDS |
121 c, counter := FilterRDS(c) | 137 c, counter := FilterRDS(c) |
122 | 138 |
123 // functions use ds from the context like normal... they don't need to k
now | 139 // functions use ds from the context like normal... they don't need to k
now |
124 // that there are any filters at all. | 140 // that there are any filters at all. |
125 someCalledFunc := func(c context.Context) { | 141 someCalledFunc := func(c context.Context) { |
126 ds := datastore.Get(c) | 142 ds := datastore.Get(c) |
127 vals := []datastore.PropertyMap{{ | 143 vals := []datastore.PropertyMap{{ |
128 "FieldName": {datastore.MkProperty(100)}, | 144 "FieldName": {datastore.MkProperty(100)}, |
129 "$key": {datastore.MkProperty(ds.NewKey("Kind", "",
1, nil))}}, | 145 "$key": {datastore.MkProperty(ds.NewKey("Kind", "",
1, nil))}}, |
130 } | 146 } |
131 if err := ds.PutMulti(vals); err != nil { | 147 if err := ds.PutMulti(vals); err != nil { |
132 panic(err) | 148 panic(err) |
133 } | 149 } |
134 } | 150 } |
135 | 151 |
136 // Using the other function. | 152 // Using the other function. |
137 someCalledFunc(c) | 153 someCalledFunc(c) |
138 someCalledFunc(c) | 154 someCalledFunc(c) |
139 | 155 |
140 // Then we can see what happened! | 156 // Then we can see what happened! |
141 » fmt.Printf("%#v\n", counter.NewKey) | 157 » fmt.Printf("%s\n", counter.NewKey.String()) |
142 » fmt.Printf("%d\n", counter.PutMulti.Successes) | 158 » fmt.Printf("%d\n", counter.PutMulti.Successes.Get()) |
143 // Output: | 159 // Output: |
144 » // count.Entry{Successes:2, Errors:0} | 160 » // {Successes:2, Errors:0} |
145 // 2 | 161 // 2 |
146 } | 162 } |
OLD | NEW |