Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(890)

Unified Diff: common/errors/multierror_test.go

Issue 1249933002: Move multierror logic into luci-go. (Closed) Base URL: https://github.com/luci/luci-go@add_mathrand
Patch Set: rename Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: common/errors/multierror_test.go
diff --git a/common/errors/multierror_test.go b/common/errors/multierror_test.go
new file mode 100644
index 0000000000000000000000000000000000000000..a7a15329e1f430accde5798033af90a1a3d0b129
--- /dev/null
+++ b/common/errors/multierror_test.go
@@ -0,0 +1,138 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package errors
+
+import (
+ "errors"
+ "sync"
+ "testing"
+
+ . "github.com/smartystreets/goconvey/convey"
+)
+
+type otherMEType []error
+
+func (o otherMEType) Error() string { return "FAIL" }
+
+func TestUpstreamErrors(t *testing.T) {
+ t.Parallel()
+
+ Convey("Test MultiError", t, func() {
+ Convey("nil", func() {
+ me := MultiError(nil)
+ So(me.Error(), ShouldEqual, "(0 errors)")
+ Convey("single", func() {
+ So(SingleError(error(me)), ShouldBeNil)
+ })
+ })
+ Convey("one", func() {
+ me := MultiError{errors.New("sup")}
+ So(me.Error(), ShouldEqual, "sup")
+ })
+ Convey("two", func() {
+ me := MultiError{errors.New("sup"), errors.New("what")}
+ So(me.Error(), ShouldEqual, "sup (and 1 other error)")
+ })
+ Convey("more", func() {
+ me := MultiError{errors.New("sup"), errors.New("what"), errors.New("nerds")}
+ So(me.Error(), ShouldEqual, "sup (and 2 other errors)")
+
+ Convey("single", func() {
+ So(SingleError(error(me)), ShouldResemble, errors.New("sup"))
+ })
+ })
+ })
+
+ Convey("SingleError passes through", t, func() {
+ e := errors.New("unique")
+ So(SingleError(e), ShouldEqual, e)
+ })
+
+ Convey("Test MultiError Conversion", t, func() {
+ ome := otherMEType{errors.New("sup")}
+ So(Fix(ome), ShouldHaveSameTypeAs, MultiError{})
+ })
+
+ Convey("Fix passes through", t, func() {
+ e := errors.New("unique")
+ So(Fix(e), ShouldEqual, e)
+ })
+}
+
+func TestLazyMultiError(t *testing.T) {
+ t.Parallel()
+
+ Convey("Test LazyMultiError", t, func() {
+ lme := LazyMultiError{Size: 10}
+ So(lme.Get(), ShouldEqual, nil)
+
+ e := errors.New("sup")
+ lme.Assign(6, e)
+ So(lme.Get(), ShouldResemble,
+ MultiError{nil, nil, nil, nil, nil, nil, e, nil, nil, nil})
+
+ lme.Assign(2, e)
+ So(lme.Get(), ShouldResemble,
+ MultiError{nil, nil, e, nil, nil, nil, e, nil, nil, nil})
+
+ So(func() { lme.Assign(20, e) }, ShouldPanic)
+
+ Convey("Try to freak out the race detector", func() {
+ lme := LazyMultiError{Size: 64}
+ Convey("all nils", func() {
+ wg := sync.WaitGroup{}
+ for i := 0; i < 64; i++ {
+ wg.Add(1)
+ go func(i int) {
+ lme.Assign(i, nil)
+ wg.Done()
+ }(i)
+ }
+ wg.Wait()
+ So(lme.Get(), ShouldBeNil)
+ })
+ Convey("every other", func() {
+ wow := errors.New("wow")
+ wg := sync.WaitGroup{}
+ for i := 0; i < 64; i++ {
+ wg.Add(1)
+ go func(i int) {
+ e := error(nil)
+ if i&1 == 1 {
+ e = wow
+ }
+ lme.Assign(i, e)
+ wg.Done()
+ }(i)
+ }
+ wg.Wait()
+ me := make(MultiError, 64)
+ for i := range me {
+ if i&1 == 1 {
+ me[i] = wow
+ }
+ }
+ So(lme.Get(), ShouldResemble, me)
+ })
+ Convey("all", func() {
+ wow := errors.New("wow")
+ wg := sync.WaitGroup{}
+ for i := 0; i < 64; i++ {
+ wg.Add(1)
+ go func(i int) {
+ lme.Assign(i, wow)
+ wg.Done()
+ }(i)
+ }
+ wg.Wait()
+ me := make(MultiError, 64)
+ for i := range me {
+ me[i] = wow
+ }
+ So(lme.Get(), ShouldResemble, me)
+ })
+ })
+ })
+}

Powered by Google App Engine
This is Rietveld 408576698