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