diff --git a/waiterr_test.go b/waiterr_test.go index 6f570a0..f2ba7b2 100644 --- a/waiterr_test.go +++ b/waiterr_test.go @@ -43,27 +43,59 @@ func TestWait(t *testing.T) { } } -func TestWaitForError(t *testing.T) { - we := new(waiterr.WaitErr) - er1 := errors.New("uh-oh") - er2 := errors.New("oops") - we.Go(func() error { return nil }) - we.Go(func() error { return er1 }) - we.Go(func() error { return er2 }) +func TestWaitForError(tt *testing.T) { + tt.Run("first error", func(t *testing.T) { + we := new(waiterr.WaitErr) + er1 := errors.New("uh-oh") + er2 := errors.New("oops") + we.Go(func() error { return nil }) + we.Go(func() error { return er1 }) + we.Go(func() error { return er2 }) - err := we.WaitForError() - // Due to how goroutines run, it is possible that either of those return first. This is an acceptable limitation - be.True(t, err == er1 || err == er2) -} + err := we.WaitForError() + // Due to how goroutines run, it is possible that either of those return first. This is an acceptable limitation + be.True(t, err == er1 || err == er2) + }) -func TestWaitForErrorNoErr(t *testing.T) { - we := new(waiterr.WaitErr) - we.Go(func() error { return nil }) - we.Go(func() error { return nil }) - we.Go(func() error { return nil }) + tt.Run("no error", func(t *testing.T) { + we := new(waiterr.WaitErr) + we.Go(func() error { return nil }) + we.Go(func() error { return nil }) + we.Go(func() error { return nil }) - err := we.WaitForError() - be.Err(t, err, nil) + err := we.WaitForError() + be.Err(t, err, nil) + }) + + tt.Run("panic", func(t *testing.T) { + defer func() { + if r := recover(); r == nil { + t.Errorf("The code did not panic") + } + }() + + we := new(waiterr.WaitErr) + _ = we.WaitForError() + }) + + tt.Run("first error set", func(tt2 *testing.T) { + we := new(waiterr.WaitErr) + expectedErr := errors.New("pre-set error") + + synctest.Test(tt2, func(t *testing.T) { + we.Go(func() error { return expectedErr }) + // synctest.Wait ensures that the gorouting has finished before anything else. + synctest.Wait() + + we.Go(func() error { return errors.New("another error") }) + synctest.Wait() + + we.Go(func() error { return nil }) + + actualErr := we.WaitForError() + be.Err(t, actualErr, expectedErr) + }) + }) } func TestUnwrap(tt *testing.T) { @@ -93,33 +125,3 @@ func TestUnwrap(tt *testing.T) { be.Equal(t, weNoErr.Unwrap(), nil) }) } - -func TestWaitForErrorPanic(t *testing.T) { - defer func() { - if r := recover(); r == nil { - t.Errorf("The code did not panic") - } - }() - - we := new(waiterr.WaitErr) - _ = we.WaitForError() -} - -func TestWaitForErrorFirstErrSet(tt *testing.T) { - we := new(waiterr.WaitErr) - expectedErr := errors.New("pre-set error") - - synctest.Test(tt, func(t *testing.T) { - we.Go(func() error { return expectedErr }) - // synctest.Wait ensures that the gorouting has finished before anything else. - synctest.Wait() - - we.Go(func() error { return errors.New("another error") }) - synctest.Wait() - - we.Go(func() error { return nil }) - - actualErr := we.WaitForError() - be.Err(t, actualErr, expectedErr) - }) -}