Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use error instead of *Error in Group #58

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions group.go
Expand Up @@ -6,7 +6,7 @@ import "sync"
// coalesced.
type Group struct {
mutex sync.Mutex
err *Error
err error
Copy link

@dolmen dolmen Mar 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is not necessary.

Instead Wait should return g.err.ErrorOrNil().

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agree

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a matter of opinion. You could also say using *Error here is unnecessary. Using error is consistent with the API as written in this PR.

wg sync.WaitGroup
}

Expand All @@ -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 {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This breaks the existing API.

Copy link
Author

@ccampo133 ccampo133 May 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Unfortunately it is a poorly designed API. Release a v2.

g.wg.Wait()
g.mutex.Lock()
defer g.mutex.Unlock()
Expand Down
35 changes: 35 additions & 0 deletions group_test.go
Expand Up @@ -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())
}
}