OLD | NEW |
1 // Copyright 2015 The LUCI Authors. All rights reserved. | 1 // Copyright 2015 The LUCI Authors. All rights reserved. |
2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
3 // that can be found in the LICENSE file. | 3 // that can be found in the LICENSE file. |
4 | 4 |
5 package errors | 5 package parallel |
6 | 6 |
7 import ( | 7 import ( |
8 "errors" | |
9 "fmt" | 8 "fmt" |
10 "testing" | 9 "testing" |
11 | 10 |
| 11 "github.com/luci/luci-go/common/errors" |
12 . "github.com/smartystreets/goconvey/convey" | 12 . "github.com/smartystreets/goconvey/convey" |
13 ) | 13 ) |
14 | 14 |
15 type otherMEType []error | 15 type otherMEType []error |
16 | 16 |
17 func (o otherMEType) Error() string { return "FAIL" } | 17 func (o otherMEType) Error() string { return "FAIL" } |
18 | 18 |
19 func TestMultiError(t *testing.T) { | 19 func TestMultiError(t *testing.T) { |
20 t.Parallel() | 20 t.Parallel() |
21 | 21 |
22 Convey("MultiError works", t, func() { | 22 Convey("MultiError works", t, func() { |
23 var me error = MultiError{fmt.Errorf("hello"), fmt.Errorf("bob")
} | |
24 | |
25 So(me.Error(), ShouldEqual, `hello (and 1 other error)`) | |
26 | |
27 Convey("MultiErrorFromErrors with errors works", func() { | 23 Convey("MultiErrorFromErrors with errors works", func() { |
28 mec := make(chan error, 4) | 24 mec := make(chan error, 4) |
29 mec <- nil | 25 mec <- nil |
30 mec <- fmt.Errorf("first error") | 26 mec <- fmt.Errorf("first error") |
31 mec <- nil | 27 mec <- nil |
32 mec <- fmt.Errorf("what") | 28 mec <- fmt.Errorf("what") |
33 close(mec) | 29 close(mec) |
34 | 30 |
35 err := MultiErrorFromErrors(mec) | 31 err := MultiErrorFromErrors(mec) |
36 So(err.Error(), ShouldEqual, `first error (and 1 other e
rror)`) | 32 So(err.Error(), ShouldEqual, `first error (and 1 other e
rror)`) |
37 }) | 33 }) |
38 | 34 |
39 Convey("MultiErrorFromErrors with nil works", func() { | 35 Convey("MultiErrorFromErrors with nil works", func() { |
40 So(MultiErrorFromErrors(nil), ShouldBeNil) | 36 So(MultiErrorFromErrors(nil), ShouldBeNil) |
41 | 37 |
42 c := make(chan error) | 38 c := make(chan error) |
43 close(c) | 39 close(c) |
44 So(MultiErrorFromErrors(c), ShouldBeNil) | 40 So(MultiErrorFromErrors(c), ShouldBeNil) |
45 | 41 |
46 c = make(chan error, 2) | 42 c = make(chan error, 2) |
47 c <- nil | 43 c <- nil |
48 c <- nil | 44 c <- nil |
49 close(c) | 45 close(c) |
50 So(MultiErrorFromErrors(c), ShouldBeNil) | 46 So(MultiErrorFromErrors(c), ShouldBeNil) |
51 }) | 47 }) |
52 }) | 48 }) |
53 } | 49 } |
54 | 50 |
55 func TestUpstreamErrors(t *testing.T) { | 51 func ExampleMultiErrorFromErrors() { |
56 » t.Parallel() | |
57 | |
58 » Convey("Test MultiError", t, func() { | |
59 » » Convey("nil", func() { | |
60 » » » me := MultiError(nil) | |
61 » » » So(me.Error(), ShouldEqual, "(0 errors)") | |
62 » » » Convey("single", func() { | |
63 » » » » So(SingleError(error(me)), ShouldBeNil) | |
64 » » » }) | |
65 » » }) | |
66 » » Convey("one", func() { | |
67 » » » me := MultiError{errors.New("sup")} | |
68 » » » So(me.Error(), ShouldEqual, "sup") | |
69 » » }) | |
70 » » Convey("two", func() { | |
71 » » » me := MultiError{errors.New("sup"), errors.New("what")} | |
72 » » » So(me.Error(), ShouldEqual, "sup (and 1 other error)") | |
73 » » }) | |
74 » » Convey("more", func() { | |
75 » » » me := MultiError{errors.New("sup"), errors.New("what"),
errors.New("nerds")} | |
76 » » » So(me.Error(), ShouldEqual, "sup (and 2 other errors)") | |
77 | |
78 » » » Convey("single", func() { | |
79 » » » » So(SingleError(error(me)), ShouldResemble, error
s.New("sup")) | |
80 » » » }) | |
81 » » }) | |
82 » }) | |
83 | |
84 » Convey("SingleError passes through", t, func() { | |
85 » » e := errors.New("unique") | |
86 » » So(SingleError(e), ShouldEqual, e) | |
87 » }) | |
88 | |
89 » Convey("Test MultiError Conversion", t, func() { | |
90 » » ome := otherMEType{errors.New("sup")} | |
91 » » So(Fix(ome), ShouldHaveSameTypeAs, MultiError{}) | |
92 » }) | |
93 | |
94 » Convey("Fix passes through", t, func() { | |
95 » » e := errors.New("unique") | |
96 » » So(Fix(e), ShouldEqual, e) | |
97 » }) | |
98 } | |
99 | |
100 func ExampleMultiError() { | |
101 errCh := make(chan error, 10) | 52 errCh := make(chan error, 10) |
102 errCh <- nil // nils are ignored | 53 errCh <- nil // nils are ignored |
103 errCh <- fmt.Errorf("what") | 54 errCh <- fmt.Errorf("what") |
104 close(errCh) | 55 close(errCh) |
105 | 56 |
106 err := MultiErrorFromErrors(errCh) | 57 err := MultiErrorFromErrors(errCh) |
107 » fmt.Printf("got: %s len=%d\n", err, len(err.(MultiError))) | 58 » fmt.Printf("got: %s len=%d\n", err, len(err.(errors.MultiError))) |
108 | 59 |
109 errCh = make(chan error, 10) | 60 errCh = make(chan error, 10) |
110 errCh <- nil // and if the channel only has nils | 61 errCh <- nil // and if the channel only has nils |
111 close(errCh) | 62 close(errCh) |
112 | 63 |
113 err = MultiErrorFromErrors(errCh) // then it returns nil | 64 err = MultiErrorFromErrors(errCh) // then it returns nil |
114 fmt.Printf("got: %v\n", err) | 65 fmt.Printf("got: %v\n", err) |
115 | 66 |
116 // Output: | 67 // Output: |
117 // got: what len=1 | 68 // got: what len=1 |
118 // got: <nil> | 69 // got: <nil> |
119 } | 70 } |
OLD | NEW |