| 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" | |
| 10 "testing" | 9 "testing" |
| 11 | 10 |
| 12 . "github.com/smartystreets/goconvey/convey" | 11 . "github.com/smartystreets/goconvey/convey" |
| 13 ) | 12 ) |
| 14 | 13 |
| 15 type otherMEType []error | 14 type otherMEType []error |
| 16 | 15 |
| 17 func (o otherMEType) Error() string { return "FAIL" } | 16 func (o otherMEType) Error() string { return "FAIL" } |
| 18 | 17 |
| 19 func TestUpstreamErrors(t *testing.T) { | 18 func TestUpstreamErrors(t *testing.T) { |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 53 Convey("Test MultiError Conversion", t, func() { | 52 Convey("Test MultiError Conversion", t, func() { |
| 54 ome := otherMEType{errors.New("sup")} | 53 ome := otherMEType{errors.New("sup")} |
| 55 So(Fix(ome), ShouldHaveSameTypeAs, MultiError{}) | 54 So(Fix(ome), ShouldHaveSameTypeAs, MultiError{}) |
| 56 }) | 55 }) |
| 57 | 56 |
| 58 Convey("Fix passes through", t, func() { | 57 Convey("Fix passes through", t, func() { |
| 59 e := errors.New("unique") | 58 e := errors.New("unique") |
| 60 So(Fix(e), ShouldEqual, e) | 59 So(Fix(e), ShouldEqual, e) |
| 61 }) | 60 }) |
| 62 } | 61 } |
| 63 | |
| 64 func TestLazyMultiError(t *testing.T) { | |
| 65 t.Parallel() | |
| 66 | |
| 67 Convey("Test LazyMultiError", t, func() { | |
| 68 lme := LazyMultiError{Size: 10} | |
| 69 So(lme.Get(), ShouldEqual, nil) | |
| 70 | |
| 71 e := errors.New("sup") | |
| 72 lme.Assign(6, e) | |
| 73 So(lme.Get(), ShouldResemble, | |
| 74 MultiError{nil, nil, nil, nil, nil, nil, e, nil, nil, ni
l}) | |
| 75 | |
| 76 lme.Assign(2, e) | |
| 77 So(lme.Get(), ShouldResemble, | |
| 78 MultiError{nil, nil, e, nil, nil, nil, e, nil, nil, nil}
) | |
| 79 | |
| 80 So(func() { lme.Assign(20, e) }, ShouldPanic) | |
| 81 | |
| 82 Convey("Try to freak out the race detector", func() { | |
| 83 lme := LazyMultiError{Size: 64} | |
| 84 Convey("all nils", func() { | |
| 85 wg := sync.WaitGroup{} | |
| 86 for i := 0; i < 64; i++ { | |
| 87 wg.Add(1) | |
| 88 go func(i int) { | |
| 89 lme.Assign(i, nil) | |
| 90 wg.Done() | |
| 91 }(i) | |
| 92 } | |
| 93 wg.Wait() | |
| 94 So(lme.Get(), ShouldBeNil) | |
| 95 }) | |
| 96 Convey("every other", func() { | |
| 97 wow := errors.New("wow") | |
| 98 wg := sync.WaitGroup{} | |
| 99 for i := 0; i < 64; i++ { | |
| 100 wg.Add(1) | |
| 101 go func(i int) { | |
| 102 e := error(nil) | |
| 103 if i&1 == 1 { | |
| 104 e = wow | |
| 105 } | |
| 106 lme.Assign(i, e) | |
| 107 wg.Done() | |
| 108 }(i) | |
| 109 } | |
| 110 wg.Wait() | |
| 111 me := make(MultiError, 64) | |
| 112 for i := range me { | |
| 113 if i&1 == 1 { | |
| 114 me[i] = wow | |
| 115 } | |
| 116 } | |
| 117 So(lme.Get(), ShouldResemble, me) | |
| 118 }) | |
| 119 Convey("all", func() { | |
| 120 wow := errors.New("wow") | |
| 121 wg := sync.WaitGroup{} | |
| 122 for i := 0; i < 64; i++ { | |
| 123 wg.Add(1) | |
| 124 go func(i int) { | |
| 125 lme.Assign(i, wow) | |
| 126 wg.Done() | |
| 127 }(i) | |
| 128 } | |
| 129 wg.Wait() | |
| 130 me := make(MultiError, 64) | |
| 131 for i := range me { | |
| 132 me[i] = wow | |
| 133 } | |
| 134 So(lme.Get(), ShouldResemble, me) | |
| 135 }) | |
| 136 }) | |
| 137 }) | |
| 138 } | |
| OLD | NEW |