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 gae | 5 package wrapper |
6 | 6 |
7 import ( | 7 import ( |
8 "errors" | 8 "errors" |
9 "fmt" | 9 "fmt" |
10 "sync" | 10 "sync" |
11 "testing" | 11 "testing" |
12 | 12 |
13 . "github.com/smartystreets/goconvey/convey" | 13 . "github.com/smartystreets/goconvey/convey" |
14 ) | 14 ) |
15 | 15 |
16 type foo struct { | 16 type foo struct { |
17 BrokenFeatures | 17 BrokenFeatures |
18 } | 18 } |
19 | 19 |
20 func (f *foo) RunIfNotBroken(fn func() error) error { | 20 func (f *foo) halp() error { // test the ability to call IsBroken from an intern
al helper |
21 » // can 'override' RunIfNotBroken | 21 » return f.IsBroken() |
22 » return f.BrokenFeatures.RunIfNotBroken(fn) | |
23 } | 22 } |
24 | 23 |
25 func (f *foo) Foo() (ret string, err error) { | 24 func (f *foo) Foo() (string, error) { |
26 » err = f.RunIfNotBroken(func() error { | 25 » err := f.halp() |
27 » » ret = "foo" | 26 » if err != nil { |
28 » » return nil | 27 » » return "", err |
29 » }) | 28 » } |
30 » return | 29 » return "foo", nil |
31 } | 30 } |
32 | 31 |
33 func (f *foo) Bar() (ret string, err error) { | 32 func (f *foo) Bar() (string, error) { |
34 » err = f.RunIfNotBroken(func() error { | 33 » err := f.halp() |
35 » » ret = "bar" | 34 » if err != nil { |
36 » » return nil | 35 » » return "", err |
37 » }) | 36 » } |
38 » return | 37 » return "bar", nil |
39 } | 38 } |
40 | 39 |
41 type override struct { | 40 type override struct { |
42 BrokenFeatures | 41 BrokenFeatures |
43 totallyRekt bool | 42 totallyRekt bool |
44 } | 43 } |
45 | 44 |
46 func (o *override) RunIfNotBroken(f func() error) error { | 45 func (o *override) IsBroken() error { |
47 if o.totallyRekt { | 46 if o.totallyRekt { |
48 return fmt.Errorf("totallyRekt") | 47 return fmt.Errorf("totallyRekt") |
49 } | 48 } |
50 » return o.BrokenFeatures.RunIfNotBroken(f) | 49 » return o.BrokenFeatures.IsBroken() |
51 } | 50 } |
52 | 51 |
53 func (o *override) Foo() error { | 52 func (o *override) Foo() error { |
54 » return o.RunIfNotBroken(func() error { return nil }) | 53 » return o.IsBroken() |
55 } | 54 } |
56 | 55 |
57 func TestBrokenFeatures(t *testing.T) { | 56 func TestBrokenFeatures(t *testing.T) { |
58 e := errors.New("sup") | 57 e := errors.New("sup") |
59 eCustom := fmt.Errorf("bad stuff happened") | 58 eCustom := fmt.Errorf("bad stuff happened") |
60 f := foo{BrokenFeatures{DefaultError: e}} | 59 f := foo{BrokenFeatures{DefaultError: e}} |
61 | 60 |
62 Convey("BrokenFeatures", t, func() { | 61 Convey("BrokenFeatures", t, func() { |
63 Convey("can break functions", func() { | 62 Convey("can break functions", func() { |
64 s, err := f.Foo() | 63 s, err := f.Foo() |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 | 111 |
113 Convey("Can be broken if not embedded", func(c C) { | 112 Convey("Can be broken if not embedded", func(c C) { |
114 var wg sync.WaitGroup | 113 var wg sync.WaitGroup |
115 wg.Add(1) | 114 wg.Add(1) |
116 go func() { | 115 go func() { |
117 defer wg.Done() | 116 defer wg.Done() |
118 bf := BrokenFeatures{DefaultError: e} | 117 bf := BrokenFeatures{DefaultError: e} |
119 // break some feature so we're forced to crawl t
he stack. | 118 // break some feature so we're forced to crawl t
he stack. |
120 bf.BreakFeatures(nil, "Nerds") | 119 bf.BreakFeatures(nil, "Nerds") |
121 // should break because there's no exported func
tions on the stack. | 120 // should break because there's no exported func
tions on the stack. |
122 » » » » err := bf.RunIfNotBroken(func() error { return n
il }) | 121 » » » » c.So(bf.IsBroken(), ShouldEqual, ErrBrokenFeatur
esBroken) |
123 » » » » c.So(err, ShouldEqual, ErrBrokenFeaturesBroken) | |
124 }() | 122 }() |
125 wg.Wait() | 123 wg.Wait() |
126 }) | 124 }) |
127 }) | 125 }) |
128 } | 126 } |
OLD | NEW |