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