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 errors | 5 package errors |
6 | 6 |
7 import ( | 7 import ( |
8 "errors" | 8 "errors" |
9 "sync" | 9 "sync" |
10 "testing" | 10 "testing" |
11 | 11 |
12 . "github.com/smartystreets/goconvey/convey" | 12 . "github.com/smartystreets/goconvey/convey" |
13 ) | 13 ) |
14 | 14 |
15 type otherMEType []error | |
16 | |
17 func (o otherMEType) Error() string { return "FAIL" } | |
18 | |
19 func TestUpstreamErrors(t *testing.T) { | |
20 t.Parallel() | |
21 | |
22 Convey("Test MultiError", t, func() { | |
23 Convey("nil", func() { | |
24 me := MultiError(nil) | |
25 So(me.Error(), ShouldEqual, "(0 errors)") | |
26 Convey("single", func() { | |
27 So(SingleError(error(me)), ShouldBeNil) | |
28 }) | |
29 }) | |
30 Convey("one", func() { | |
31 me := MultiError{errors.New("sup")} | |
32 So(me.Error(), ShouldEqual, "sup") | |
33 }) | |
34 Convey("two", func() { | |
35 me := MultiError{errors.New("sup"), errors.New("what")} | |
36 So(me.Error(), ShouldEqual, "sup (and 1 other error)") | |
37 }) | |
38 Convey("more", func() { | |
39 me := MultiError{errors.New("sup"), errors.New("what"),
errors.New("nerds")} | |
40 So(me.Error(), ShouldEqual, "sup (and 2 other errors)") | |
41 | |
42 Convey("single", func() { | |
43 So(SingleError(error(me)), ShouldResemble, error
s.New("sup")) | |
44 }) | |
45 }) | |
46 }) | |
47 | |
48 Convey("SingleError passes through", t, func() { | |
49 e := errors.New("unique") | |
50 So(SingleError(e), ShouldEqual, e) | |
51 }) | |
52 | |
53 Convey("Test MultiError Conversion", t, func() { | |
54 ome := otherMEType{errors.New("sup")} | |
55 So(Fix(ome), ShouldHaveSameTypeAs, MultiError{}) | |
56 }) | |
57 | |
58 Convey("Fix passes through", t, func() { | |
59 e := errors.New("unique") | |
60 So(Fix(e), ShouldEqual, e) | |
61 }) | |
62 } | |
63 | |
64 func TestLazyMultiError(t *testing.T) { | 15 func TestLazyMultiError(t *testing.T) { |
65 t.Parallel() | 16 t.Parallel() |
66 | 17 |
67 Convey("Test LazyMultiError", t, func() { | 18 Convey("Test LazyMultiError", t, func() { |
68 » » lme := LazyMultiError{Size: 10} | 19 » » lme := NewLazyMultiError(10) |
69 So(lme.Get(), ShouldEqual, nil) | 20 So(lme.Get(), ShouldEqual, nil) |
70 | 21 |
71 e := errors.New("sup") | 22 e := errors.New("sup") |
72 lme.Assign(6, e) | 23 lme.Assign(6, e) |
73 So(lme.Get(), ShouldResemble, | 24 So(lme.Get(), ShouldResemble, |
74 MultiError{nil, nil, nil, nil, nil, nil, e, nil, nil, ni
l}) | 25 MultiError{nil, nil, nil, nil, nil, nil, e, nil, nil, ni
l}) |
75 | 26 |
76 lme.Assign(2, e) | 27 lme.Assign(2, e) |
77 So(lme.Get(), ShouldResemble, | 28 So(lme.Get(), ShouldResemble, |
78 MultiError{nil, nil, e, nil, nil, nil, e, nil, nil, nil}
) | 29 MultiError{nil, nil, e, nil, nil, nil, e, nil, nil, nil}
) |
79 | 30 |
80 So(func() { lme.Assign(20, e) }, ShouldPanic) | 31 So(func() { lme.Assign(20, e) }, ShouldPanic) |
81 | 32 |
82 Convey("Try to freak out the race detector", func() { | 33 Convey("Try to freak out the race detector", func() { |
83 » » » lme := LazyMultiError{Size: 64} | 34 » » » lme := NewLazyMultiError(64) |
84 Convey("all nils", func() { | 35 Convey("all nils", func() { |
85 wg := sync.WaitGroup{} | 36 wg := sync.WaitGroup{} |
86 for i := 0; i < 64; i++ { | 37 for i := 0; i < 64; i++ { |
87 wg.Add(1) | 38 wg.Add(1) |
88 go func(i int) { | 39 go func(i int) { |
89 lme.Assign(i, nil) | 40 lme.Assign(i, nil) |
90 wg.Done() | 41 wg.Done() |
91 }(i) | 42 }(i) |
92 } | 43 } |
93 wg.Wait() | 44 wg.Wait() |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
129 wg.Wait() | 80 wg.Wait() |
130 me := make(MultiError, 64) | 81 me := make(MultiError, 64) |
131 for i := range me { | 82 for i := range me { |
132 me[i] = wow | 83 me[i] = wow |
133 } | 84 } |
134 So(lme.Get(), ShouldResemble, me) | 85 So(lme.Get(), ShouldResemble, me) |
135 }) | 86 }) |
136 }) | 87 }) |
137 }) | 88 }) |
138 } | 89 } |
OLD | NEW |