diff --git a/group.go b/group.go index 9c29efb..ef27142 100644 --- a/group.go +++ b/group.go @@ -6,7 +6,7 @@ import "sync" // coalesced. type Group struct { mutex sync.Mutex - err *Error + err error wg sync.WaitGroup } @@ -30,7 +30,7 @@ func (g *Group) Go(f func() error) { // Wait blocks until all function calls from the Go method have returned, then // returns the multierror. -func (g *Group) Wait() *Error { +func (g *Group) Wait() error { g.wg.Wait() g.mutex.Lock() defer g.mutex.Unlock() diff --git a/group_test.go b/group_test.go index 9d472fd..873bba7 100644 --- a/group_test.go +++ b/group_test.go @@ -42,3 +42,38 @@ func TestGroup(t *testing.T) { } } } + +func TestGroupWait_ErrorNil(t *testing.T) { + g := new(Group) + g.Go(func() error { return nil }) + err := g.Wait() + if err != nil { + t.Fatalf("expected error to be nil, but was %v", err) + } +} + +func TestGroupWait_ErrorNotNil(t *testing.T) { + g := new(Group) + msg := "test error" + g.Go(func() error { return errors.New(msg) }) + err := g.Wait() + if err == nil { + t.Fatalf("expected error to be nil, but was %v", err) + } + + // err is a *Error, and e is set to the error's value + var e *Error + if !errors.As(err, &e) { + t.Fatalf("expected err to be type *Error, but was type %T, value %v", err, err) + } + + errs := e.WrappedErrors() + if len(errs) != 1 { + t.Fatalf("expected one wrapped error, but found %d", len(errs)) + } + + wrapped := errs[0] + if wrapped.Error() != "test error" { + t.Fatalf("expected wrap error message to be '%s', but was '%s'", msg, wrapped.Error()) + } +}