Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(983)

Side by Side Diff: filter/count/count_test.go

Issue 1253263002: Make rawdatastore API safer for writing filters. (Closed) Base URL: https://github.com/luci/gae.git@master
Patch Set: fix comments Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | filter/count/gi.go » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/info" 13 "github.com/luci/gae/service/info"
14 "github.com/luci/gae/service/memcache" 14 "github.com/luci/gae/service/memcache"
15 "github.com/luci/gae/service/rawdatastore" 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
24 Convey("Test Count filter", t, func() { 32 Convey("Test Count filter", t, func() {
25 » » c, ctr := FilterRDS(memory.Use(context.Background())) 33 » » c, fb := featureBreaker.FilterRDS(memory.Use(context.Background( )), nil)
34 » » c, ctr := FilterRDS(c)
26 35
27 So(c, ShouldNotBeNil) 36 So(c, ShouldNotBeNil)
28 So(ctr, ShouldNotBeNil) 37 So(ctr, ShouldNotBeNil)
29 38
30 rds := rawdatastore.Get(c) 39 rds := rawdatastore.Get(c)
31 40
32 Convey("Calling a rds function should reflect in counter", func( ) { 41 Convey("Calling a rds function should reflect in counter", func( ) {
33 p := rawdatastore.Property{} 42 p := rawdatastore.Property{}
34 p.SetValue(100, false) 43 p.SetValue(100, false)
35 » » » _, err := rds.Put(rds.NewKey("Kind", "", 0, nil), &rawda tastore.PropertyMap{ 44 » » » keys := []rawdatastore.Key{rds.NewKey("Kind", "", 0, nil )}
36 » » » » "Val": {p}, 45 » » » vals := []rawdatastore.PropertyLoadSaver{&rawdatastore.P ropertyMap{"Val": {p}}}
37 » » » }) 46
38 » » » So(err, ShouldBeNil) 47 » » » So(rds.PutMulti(keys, vals, pnil), ShouldBeNil)
39 So(ctr.NewKey.Successes, ShouldEqual, 1) 48 So(ctr.NewKey.Successes, ShouldEqual, 1)
40 » » » So(ctr.Put.Successes, ShouldEqual, 1) 49 » » » So(ctr.PutMulti.Successes, ShouldEqual, 1)
41 50
42 Convey("effects are cumulative", func() { 51 Convey("effects are cumulative", func() {
43 » » » » _, err := rds.Put(rds.NewKey("Kind", "", 0, nil) , &rawdatastore.PropertyMap{ 52 » » » » So(rds.PutMulti(keys, vals, pnil), ShouldBeNil)
44 » » » » » "Val": {p}, 53 » » » » So(ctr.PutMulti.Successes, ShouldEqual, 2)
45 » » » » })
46 » » » » So(err, ShouldBeNil)
47 » » » » So(ctr.NewKey.Successes, ShouldEqual, 2)
48 » » » » So(ctr.Put.Successes, ShouldEqual, 2)
49 54
50 Convey("even within transactions", func() { 55 Convey("even within transactions", func() {
56 root := rds.NewKey("Root", "", 1, nil)
51 rds.RunInTransaction(func(c context.Cont ext) error { 57 rds.RunInTransaction(func(c context.Cont ext) error {
52 rds := rawdatastore.Get(c) 58 rds := rawdatastore.Get(c)
53 » » » » » » k := rds.NewKey("Wat", "sup", 0, nil) 59 » » » » » » keys := []rawdatastore.Key{
54 » » » » » » rds.Put(k, &rawdatastore.Propert yMap{"Wat": {p}}) 60 » » » » » » » rds.NewKey("Kind", "hi", 0, root),
55 » » » » » » rds.Put(k, &rawdatastore.Propert yMap{"Wat": {p}}) 61 » » » » » » » rds.NewKey("Kind", "ther e", 0, root),
62 » » » » » » }
63 » » » » » » vals = append(vals, vals[0])
64 » » » » » » So(rds.PutMulti(keys, vals, pnil ), ShouldBeNil)
56 return nil 65 return nil
57 }, nil) 66 }, nil)
58 }) 67 })
59 }) 68 })
60 }) 69 })
70
61 Convey("errors count against errors", func() { 71 Convey("errors count against errors", func() {
62 » » » rds.Get(nil, nil) 72 » » » keys := []rawdatastore.Key{rds.NewKey("Kind", "", 1, nil )}
63 » » » So(ctr.Get.Errors, ShouldEqual, 1) 73 » » » vals := []rawdatastore.PropertyLoadSaver{&rawdatastore.P ropertyMap{"Val": {{}}}}
64 » » » k, err := rds.Put(rds.NewKey("Kind", "", 0, nil), &rawda tastore.PropertyMap{ 74
65 » » » » "Val": {rawdatastore.Property{}}, 75 » » » fb.BreakFeatures(nil, "GetMulti")
76
77 » » » rds.GetMulti(keys, gnil)
78 » » » So(ctr.GetMulti.Errors, ShouldEqual, 1)
79
80 » » » fb.UnbreakFeatures("GetMulti")
81
82 » » » err := rds.PutMulti(keys, vals, func(k rawdatastore.Key, err error) {
83 » » » » keys[0] = k
84 » » » » So(err, ShouldBeNil)
66 }) 85 })
67 So(err, ShouldBeNil) 86 So(err, ShouldBeNil)
68 » » » So(ctr.NewKey.Successes, ShouldEqual, 1) 87
69 » » » rds.Get(k, &rawdatastore.PropertyMap{}) 88 » » » rds.GetMulti(keys, gnil)
70 » » » So(ctr.Get.Errors, ShouldEqual, 1) 89 » » » So(ctr.GetMulti.Errors, ShouldEqual, 1)
71 » » » So(ctr.Get.Successes, ShouldEqual, 1) 90 » » » So(ctr.GetMulti.Successes, ShouldEqual, 1)
72 » » » So(ctr.Get.Total(), ShouldEqual, 2) 91 » » » So(ctr.GetMulti.Total(), ShouldEqual, 2)
73 }) 92 })
74 }) 93 })
75 94
76 Convey("works for memcache", t, func() { 95 Convey("works for memcache", t, func() {
77 c, ctr := FilterMC(memory.Use(context.Background())) 96 c, ctr := FilterMC(memory.Use(context.Background()))
78 So(c, ShouldNotBeNil) 97 So(c, ShouldNotBeNil)
79 So(ctr, ShouldNotBeNil) 98 So(ctr, ShouldNotBeNil)
80 mc := memcache.Get(c) 99 mc := memcache.Get(c)
81 100
82 mc.Set(mc.NewItem("hello").SetValue([]byte("sup"))) 101 mc.Set(mc.NewItem("hello").SetValue([]byte("sup")))
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
126 // functions use RDS from the context like normal... they don't need to know 145 // functions use RDS from the context like normal... they don't need to know
127 // that there are any filters at all. 146 // that there are any filters at all.
128 someCalledFunc := func(c context.Context) { 147 someCalledFunc := func(c context.Context) {
129 rds := rawdatastore.Get(c) 148 rds := rawdatastore.Get(c)
130 key := rds.NewKey("Kind", "", 1, nil) 149 key := rds.NewKey("Kind", "", 1, nil)
131 prop := rawdatastore.Property{} 150 prop := rawdatastore.Property{}
132 prop.SetValue(100, false) 151 prop.SetValue(100, false)
133 val := rawdatastore.PropertyMap{ 152 val := rawdatastore.PropertyMap{
134 "FieldName": {prop}, 153 "FieldName": {prop},
135 } 154 }
136 » » rds.Put(key, &val) 155 » » rds.PutMulti([]rawdatastore.Key{key}, []rawdatastore.PropertyLoa dSaver{&val}, nil)
137 } 156 }
138 157
139 // Using the other function. 158 // Using the other function.
140 someCalledFunc(c) 159 someCalledFunc(c)
141 someCalledFunc(c) 160 someCalledFunc(c)
142 161
143 // Then we can see what happened! 162 // Then we can see what happened!
144 fmt.Printf("%#v\n", counter.NewKey) 163 fmt.Printf("%#v\n", counter.NewKey)
145 » fmt.Printf("%d\n", counter.Put.Successes) 164 » fmt.Printf("%d\n", counter.PutMulti.Successes)
146 // Output: 165 // Output:
147 // count.Entry{Successes:2, Errors:0} 166 // count.Entry{Successes:2, Errors:0}
148 // 2 167 // 2
149 } 168 }
OLDNEW
« no previous file with comments | « no previous file | filter/count/gi.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698