Skip to content

Commit 17a3cb8

Browse files
committed
feat: terminate Until when test function returns error.
1 parent 43bd28b commit 17a3cb8

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

until.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,16 @@ func until(parent context.Context, testFn, fn AsyncFn) ([]any, error) {
3737
ctx := getContext(parent)
3838

3939
for {
40-
out, err := invokeAsyncFn(fn, ctx, nil)
40+
out, _ := invokeAsyncFn(fn, ctx, nil)
41+
42+
testOut, testErr := invokeAsyncFn(testFn, ctx, out)
43+
if testErr != nil {
44+
return out, testErr
45+
}
4146

42-
testOut, _ := invokeAsyncFn(testFn, ctx, out)
4347
isDone := testOut[0].(bool)
4448
if isDone {
45-
return out, err
49+
return out, nil
4650
}
4751
}
4852
}

until_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package async
22

33
import (
44
"context"
5+
"errors"
56
"testing"
67
"time"
78

@@ -54,6 +55,35 @@ func TestUntilInvalidParameters(t *testing.T) {
5455
}, ErrInvalidTestFunc)
5556
}
5657

58+
func TestUntilWithFunctionError(t *testing.T) {
59+
a := assert.New(t)
60+
count := 0
61+
unexpectedErr := errors.New("unexpected error")
62+
63+
out, err := Until(func(c int, err error) bool {
64+
return c == 5
65+
}, func() (int, error) {
66+
count++
67+
return count, unexpectedErr
68+
})
69+
a.NilNow(err)
70+
a.EqualNow(out, []any{5, unexpectedErr})
71+
}
72+
73+
func TestUntilWithTestFunctionError(t *testing.T) {
74+
a := assert.New(t)
75+
expectedErr := errors.New("expected error")
76+
77+
out, err := Until(func(n int) bool {
78+
panic(expectedErr)
79+
}, func() int {
80+
return 0
81+
})
82+
a.NotNilNow(err)
83+
a.EqualNow(err, expectedErr)
84+
a.EqualNow(out, []any{0})
85+
}
86+
5787
func TestUntilWithContext(t *testing.T) {
5888
a := assert.New(t)
5989
ctx, canFunc := context.WithTimeout(context.Background(), 100*time.Millisecond)

0 commit comments

Comments
 (0)