| 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 gae | |
| 6 | |
| 7 import ( | |
| 8 "errors" | |
| 9 "fmt" | |
| 10 "sync" | |
| 11 "testing" | |
| 12 | |
| 13 . "github.com/smartystreets/goconvey/convey" | |
| 14 ) | |
| 15 | |
| 16 type foo struct { | |
| 17 BrokenFeatures | |
| 18 } | |
| 19 | |
| 20 func (f *foo) RunIfNotBroken(fn func() error) error { | |
| 21 // can 'override' RunIfNotBroken | |
| 22 return f.BrokenFeatures.RunIfNotBroken(fn) | |
| 23 } | |
| 24 | |
| 25 func (f *foo) Foo() (ret string, err error) { | |
| 26 err = f.RunIfNotBroken(func() error { | |
| 27 ret = "foo" | |
| 28 return nil | |
| 29 }) | |
| 30 return | |
| 31 } | |
| 32 | |
| 33 func (f *foo) Bar() (ret string, err error) { | |
| 34 err = f.RunIfNotBroken(func() error { | |
| 35 ret = "bar" | |
| 36 return nil | |
| 37 }) | |
| 38 return | |
| 39 } | |
| 40 | |
| 41 type override struct { | |
| 42 BrokenFeatures | |
| 43 totallyRekt bool | |
| 44 } | |
| 45 | |
| 46 func (o *override) RunIfNotBroken(f func() error) error { | |
| 47 if o.totallyRekt { | |
| 48 return fmt.Errorf("totallyRekt") | |
| 49 } | |
| 50 return o.BrokenFeatures.RunIfNotBroken(f) | |
| 51 } | |
| 52 | |
| 53 func (o *override) Foo() error { | |
| 54 return o.RunIfNotBroken(func() error { return nil }) | |
| 55 } | |
| 56 | |
| 57 func TestBrokenFeatures(t *testing.T) { | |
| 58 e := errors.New("sup") | |
| 59 eCustom := fmt.Errorf("bad stuff happened") | |
| 60 f := foo{BrokenFeatures{DefaultError: e}} | |
| 61 | |
| 62 Convey("BrokenFeatures", t, func() { | |
| 63 Convey("can break functions", func() { | |
| 64 s, err := f.Foo() | |
| 65 So(s, ShouldEqual, "foo") | |
| 66 So(err, ShouldBeNil) | |
| 67 | |
| 68 f.BreakFeatures(nil, "Foo") | |
| 69 _, err = f.Foo() | |
| 70 So(err, ShouldEqual, e) | |
| 71 | |
| 72 Convey("and unbreak them", func() { | |
| 73 f.UnbreakFeatures("Foo") | |
| 74 s, err = f.Foo() | |
| 75 So(s, ShouldEqual, "foo") | |
| 76 So(err, ShouldBeNil) | |
| 77 }) | |
| 78 | |
| 79 Convey("and breaking features doesn't break unrelated on
es", func() { | |
| 80 s, err := f.Bar() | |
| 81 So(s, ShouldEqual, "bar") | |
| 82 So(err, ShouldBeNil) | |
| 83 }) | |
| 84 }) | |
| 85 | |
| 86 Convey("Can override IsBroken too", func() { | |
| 87 o := &override{BrokenFeatures{DefaultError: e}, false} | |
| 88 Convey("Can break functions as normal", func() { | |
| 89 o.BreakFeatures(nil, "Foo") | |
| 90 So(o.Foo(), ShouldEqual, e) | |
| 91 | |
| 92 Convey("but can also break them in a user define
d way", func() { | |
| 93 o.totallyRekt = true | |
| 94 So(o.Foo().Error(), ShouldContainSubstri
ng, "totallyRekt") | |
| 95 }) | |
| 96 }) | |
| 97 }) | |
| 98 | |
| 99 Convey("Not specifying a default gets you a generic error", func
() { | |
| 100 f.BrokenFeatures.DefaultError = nil | |
| 101 f.BreakFeatures(nil, "Foo") | |
| 102 _, err := f.Foo() | |
| 103 So(err.Error(), ShouldContainSubstring, `"Foo"`) | |
| 104 }) | |
| 105 | |
| 106 Convey("Can override the error returned", func() { | |
| 107 f.BreakFeatures(eCustom, "Foo") | |
| 108 v, err := f.Foo() | |
| 109 So(v, ShouldEqual, "") | |
| 110 So(err, ShouldEqual, eCustom) | |
| 111 }) | |
| 112 | |
| 113 Convey("Can be broken if not embedded", func(c C) { | |
| 114 var wg sync.WaitGroup | |
| 115 wg.Add(1) | |
| 116 go func() { | |
| 117 defer wg.Done() | |
| 118 bf := BrokenFeatures{DefaultError: e} | |
| 119 // break some feature so we're forced to crawl t
he stack. | |
| 120 bf.BreakFeatures(nil, "Nerds") | |
| 121 // should break because there's no exported func
tions on the stack. | |
| 122 err := bf.RunIfNotBroken(func() error { return n
il }) | |
| 123 c.So(err, ShouldEqual, ErrBrokenFeaturesBroken) | |
| 124 }() | |
| 125 wg.Wait() | |
| 126 }) | |
| 127 }) | |
| 128 } | |
| OLD | NEW |