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