-
Notifications
You must be signed in to change notification settings - Fork 11
/
error_test.go
69 lines (57 loc) · 2.09 KB
/
error_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package asynctask_test
import (
"context"
"errors"
"testing"
"time"
"github.com/Azure/go-asynctask"
"github.com/stretchr/testify/assert"
)
func getPanicTask(sleepDuration time.Duration) asynctask.AsyncFunc[string] {
return func(ctx context.Context) (string, error) {
time.Sleep(sleepDuration)
panic("yo")
}
}
func getErrorTask(errorString string, sleepDuration time.Duration) asynctask.AsyncFunc[int] {
return func(ctx context.Context) (int, error) {
time.Sleep(sleepDuration)
return 0, errors.New(errorString)
}
}
func TestTimeoutCase(t *testing.T) {
t.Parallel()
ctx, cancelFunc := newTestContextWithTimeout(t, 3*time.Second)
defer cancelFunc()
tsk := asynctask.Start(ctx, getCountingTask(10, "timeoutCase", countingTaskDefaultStepLatency))
_, err := tsk.WaitWithTimeout(ctx, 30*time.Millisecond)
assert.True(t, errors.Is(err, context.DeadlineExceeded), "expecting DeadlineExceeded")
// the last Wait error should affect running task
// I can continue wait with longer time
rawResult, err := tsk.WaitWithTimeout(ctx, 2*time.Second)
assert.NoError(t, err)
assert.Equal(t, 9, rawResult)
// any following Wait should complete immediately
rawResult, err = tsk.WaitWithTimeout(ctx, 2*time.Nanosecond)
assert.NoError(t, err)
assert.Equal(t, 9, rawResult)
}
func TestPanicCase(t *testing.T) {
t.Parallel()
ctx, cancelFunc := newTestContextWithTimeout(t, 3*time.Second)
defer cancelFunc()
tsk := asynctask.Start(ctx, getPanicTask(200*time.Millisecond))
_, err := tsk.WaitWithTimeout(ctx, 300*time.Millisecond)
assert.True(t, errors.Is(err, asynctask.ErrPanic), "expecting ErrPanic")
}
func TestErrorCase(t *testing.T) {
t.Parallel()
ctx, cancelFunc := newTestContextWithTimeout(t, 3*time.Second)
defer cancelFunc()
tsk := asynctask.Start(ctx, getErrorTask("dummy error", 200*time.Millisecond))
_, err := tsk.WaitWithTimeout(ctx, 300*time.Millisecond)
assert.Error(t, err)
assert.False(t, errors.Is(err, asynctask.ErrPanic), "not expecting ErrPanic")
assert.False(t, errors.Is(err, context.DeadlineExceeded), "not expecting DeadlineExceeded")
assert.Equal(t, "dummy error", err.Error())
}