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/info" | 14 "github.com/luci/gae/service/info" |
14 "github.com/luci/gae/service/memcache" | 15 "github.com/luci/gae/service/memcache" |
15 "github.com/luci/gae/service/rawdatastore" | |
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 TestCount(t *testing.T) { | 21 func TestCount(t *testing.T) { |
22 t.Parallel() | 22 t.Parallel() |
23 | 23 |
24 pnil := func(_ rawdatastore.Key, err error) { | |
25 So(err, ShouldBeNil) | |
26 } | |
27 | |
28 gnil := func(_ rawdatastore.PropertyMap, err error) { | |
29 So(err, ShouldBeNil) | |
30 } | |
31 | |
32 Convey("Test Count filter", t, func() { | 24 Convey("Test Count filter", t, func() { |
33 c, fb := featureBreaker.FilterRDS(memory.Use(context.Background(
)), nil) | 25 c, fb := featureBreaker.FilterRDS(memory.Use(context.Background(
)), nil) |
34 c, ctr := FilterRDS(c) | 26 c, ctr := FilterRDS(c) |
35 | 27 |
36 So(c, ShouldNotBeNil) | 28 So(c, ShouldNotBeNil) |
37 So(ctr, ShouldNotBeNil) | 29 So(ctr, ShouldNotBeNil) |
38 | 30 |
39 » » rds := rawdatastore.Get(c) | 31 » » ds := datastore.Get(c) |
| 32 » » vals := []datastore.PropertyMap{{ |
| 33 » » » "Val": {datastore.MkProperty(100)}, |
| 34 » » » "$key": {datastore.MkPropertyNI(ds.NewKey("Kind", "", 1,
nil))}, |
| 35 » » }} |
40 | 36 |
41 » » Convey("Calling a rds function should reflect in counter", func(
) { | 37 » » Convey("Calling a ds function should reflect in counter", func()
{ |
42 » » » p := rawdatastore.Property{} | 38 » » » So(ds.PutMulti(vals), ShouldBeNil) |
43 » » » p.SetValue(100, false) | |
44 » » » keys := []rawdatastore.Key{rds.NewKey("Kind", "", 0, nil
)} | |
45 » » » vals := []rawdatastore.PropertyLoadSaver{&rawdatastore.P
ropertyMap{"Val": {p}}} | |
46 | |
47 » » » So(rds.PutMulti(keys, vals, pnil), ShouldBeNil) | |
48 So(ctr.NewKey.Successes, ShouldEqual, 1) | 39 So(ctr.NewKey.Successes, ShouldEqual, 1) |
49 So(ctr.PutMulti.Successes, ShouldEqual, 1) | 40 So(ctr.PutMulti.Successes, ShouldEqual, 1) |
50 | 41 |
51 Convey("effects are cumulative", func() { | 42 Convey("effects are cumulative", func() { |
52 » » » » So(rds.PutMulti(keys, vals, pnil), ShouldBeNil) | 43 » » » » So(ds.PutMulti(vals), ShouldBeNil) |
53 So(ctr.PutMulti.Successes, ShouldEqual, 2) | 44 So(ctr.PutMulti.Successes, ShouldEqual, 2) |
54 | 45 |
55 Convey("even within transactions", func() { | 46 Convey("even within transactions", func() { |
56 » » » » » root := rds.NewKey("Root", "", 1, nil) | 47 » » » » » ds.RunInTransaction(func(c context.Conte
xt) error { |
57 » » » » » rds.RunInTransaction(func(c context.Cont
ext) error { | 48 » » » » » » ds := datastore.Get(c) |
58 » » » » » » rds := rawdatastore.Get(c) | 49 » » » » » » So(ds.PutMulti(append(vals, vals
[0])), ShouldBeNil) |
59 » » » » » » keys := []rawdatastore.Key{ | |
60 » » » » » » » rds.NewKey("Kind", "hi",
0, root), | |
61 » » » » » » » rds.NewKey("Kind", "ther
e", 0, root), | |
62 » » » » » » } | |
63 » » » » » » vals = append(vals, vals[0]) | |
64 » » » » » » So(rds.PutMulti(keys, vals, pnil
), ShouldBeNil) | |
65 return nil | 50 return nil |
66 }, nil) | 51 }, nil) |
67 }) | 52 }) |
68 }) | 53 }) |
69 }) | 54 }) |
70 | 55 |
71 Convey("errors count against errors", func() { | 56 Convey("errors count against errors", func() { |
72 keys := []rawdatastore.Key{rds.NewKey("Kind", "", 1, nil
)} | |
73 vals := []rawdatastore.PropertyLoadSaver{&rawdatastore.P
ropertyMap{"Val": {{}}}} | |
74 | |
75 fb.BreakFeatures(nil, "GetMulti") | 57 fb.BreakFeatures(nil, "GetMulti") |
76 | 58 |
77 » » » rds.GetMulti(keys, gnil) | 59 » » » ds.GetMulti(vals) |
78 So(ctr.GetMulti.Errors, ShouldEqual, 1) | 60 So(ctr.GetMulti.Errors, ShouldEqual, 1) |
79 | 61 |
80 fb.UnbreakFeatures("GetMulti") | 62 fb.UnbreakFeatures("GetMulti") |
81 | 63 |
82 » » » err := rds.PutMulti(keys, vals, func(k rawdatastore.Key,
err error) { | 64 » » » So(ds.PutMulti(vals), ShouldBeNil) |
83 » » » » keys[0] = k | |
84 » » » » So(err, ShouldBeNil) | |
85 » » » }) | |
86 » » » So(err, ShouldBeNil) | |
87 | 65 |
88 » » » rds.GetMulti(keys, gnil) | 66 » » » ds.GetMulti(vals) |
89 So(ctr.GetMulti.Errors, ShouldEqual, 1) | 67 So(ctr.GetMulti.Errors, ShouldEqual, 1) |
90 So(ctr.GetMulti.Successes, ShouldEqual, 1) | 68 So(ctr.GetMulti.Successes, ShouldEqual, 1) |
91 So(ctr.GetMulti.Total(), ShouldEqual, 2) | 69 So(ctr.GetMulti.Total(), ShouldEqual, 2) |
92 }) | 70 }) |
93 }) | 71 }) |
94 | 72 |
95 Convey("works for memcache", t, func() { | 73 Convey("works for memcache", t, func() { |
96 c, ctr := FilterMC(memory.Use(context.Background())) | 74 c, ctr := FilterMC(memory.Use(context.Background())) |
97 So(c, ShouldNotBeNil) | 75 So(c, ShouldNotBeNil) |
98 So(ctr, ShouldNotBeNil) | 76 So(ctr, ShouldNotBeNil) |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
135 }) | 113 }) |
136 } | 114 } |
137 | 115 |
138 func ExampleFilterRDS() { | 116 func ExampleFilterRDS() { |
139 // Set up your context using a base service implementation (memory or pr
od) | 117 // Set up your context using a base service implementation (memory or pr
od) |
140 c := memory.Use(context.Background()) | 118 c := memory.Use(context.Background()) |
141 | 119 |
142 // Apply the counter.FilterRDS | 120 // Apply the counter.FilterRDS |
143 c, counter := FilterRDS(c) | 121 c, counter := FilterRDS(c) |
144 | 122 |
145 » // functions use RDS from the context like normal... they don't need to
know | 123 » // functions use ds from the context like normal... they don't need to k
now |
146 // that there are any filters at all. | 124 // that there are any filters at all. |
147 someCalledFunc := func(c context.Context) { | 125 someCalledFunc := func(c context.Context) { |
148 » » rds := rawdatastore.Get(c) | 126 » » ds := datastore.Get(c) |
149 » » key := rds.NewKey("Kind", "", 1, nil) | 127 » » vals := []datastore.PropertyMap{{ |
150 » » prop := rawdatastore.Property{} | 128 » » » "FieldName": {datastore.MkProperty(100)}, |
151 » » prop.SetValue(100, false) | 129 » » » "$key": {datastore.MkProperty(ds.NewKey("Kind", "",
1, nil))}}, |
152 » » val := rawdatastore.PropertyMap{ | |
153 » » » "FieldName": {prop}, | |
154 } | 130 } |
155 » » rds.PutMulti([]rawdatastore.Key{key}, []rawdatastore.PropertyLoa
dSaver{&val}, nil) | 131 » » if err := ds.PutMulti(vals); err != nil { |
| 132 » » » panic(err) |
| 133 » » } |
156 } | 134 } |
157 | 135 |
158 // Using the other function. | 136 // Using the other function. |
159 someCalledFunc(c) | 137 someCalledFunc(c) |
160 someCalledFunc(c) | 138 someCalledFunc(c) |
161 | 139 |
162 // Then we can see what happened! | 140 // Then we can see what happened! |
163 fmt.Printf("%#v\n", counter.NewKey) | 141 fmt.Printf("%#v\n", counter.NewKey) |
164 fmt.Printf("%d\n", counter.PutMulti.Successes) | 142 fmt.Printf("%d\n", counter.PutMulti.Successes) |
165 // Output: | 143 // Output: |
166 // count.Entry{Successes:2, Errors:0} | 144 // count.Entry{Successes:2, Errors:0} |
167 // 2 | 145 // 2 |
168 } | 146 } |
OLD | NEW |