♻️ Refactor: Organize waiterr_test.go with subtests
This commit is contained in:
parent
67eb618288
commit
a68fc26481
1 changed files with 50 additions and 48 deletions
|
|
@ -43,7 +43,8 @@ func TestWait(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestWaitForError(t *testing.T) {
|
func TestWaitForError(tt *testing.T) {
|
||||||
|
tt.Run("first error", func(t *testing.T) {
|
||||||
we := new(waiterr.WaitErr)
|
we := new(waiterr.WaitErr)
|
||||||
er1 := errors.New("uh-oh")
|
er1 := errors.New("uh-oh")
|
||||||
er2 := errors.New("oops")
|
er2 := errors.New("oops")
|
||||||
|
|
@ -54,9 +55,9 @@ func TestWaitForError(t *testing.T) {
|
||||||
err := we.WaitForError()
|
err := we.WaitForError()
|
||||||
// Due to how goroutines run, it is possible that either of those return first. This is an acceptable limitation
|
// 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)
|
be.True(t, err == er1 || err == er2)
|
||||||
}
|
})
|
||||||
|
|
||||||
func TestWaitForErrorNoErr(t *testing.T) {
|
tt.Run("no error", func(t *testing.T) {
|
||||||
we := new(waiterr.WaitErr)
|
we := new(waiterr.WaitErr)
|
||||||
we.Go(func() error { return nil })
|
we.Go(func() error { return nil })
|
||||||
we.Go(func() error { return nil })
|
we.Go(func() error { return nil })
|
||||||
|
|
@ -64,6 +65,37 @@ func TestWaitForErrorNoErr(t *testing.T) {
|
||||||
|
|
||||||
err := we.WaitForError()
|
err := we.WaitForError()
|
||||||
be.Err(t, err, nil)
|
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) {
|
func TestUnwrap(tt *testing.T) {
|
||||||
|
|
@ -93,33 +125,3 @@ func TestUnwrap(tt *testing.T) {
|
||||||
be.Equal(t, weNoErr.Unwrap(), nil)
|
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)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue