From 4e44750e4093ec6a41aa0cca8fb3392a6c2fe3c6 Mon Sep 17 00:00:00 2001 From: "Xinyue.Wang" Date: Tue, 27 Jun 2023 16:08:58 -0700 Subject: [PATCH] add test coverage --- wait_any_test.go | 57 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) diff --git a/wait_any_test.go b/wait_any_test.go index 87c8a22..75fb650 100644 --- a/wait_any_test.go +++ b/wait_any_test.go @@ -24,10 +24,10 @@ func TestWaitAny(t *testing.T) { // should finish after right away assert.True(t, elapsed < 2*time.Millisecond, fmt.Sprintf("actually elapsed: %v", elapsed)) + start = time.Now() countingTsk1 := asynctask.Start(ctx, getCountingTask(10, "countingPer40ms", 40*time.Millisecond)) countingTsk2 := asynctask.Start(ctx, getCountingTask(10, "countingPer20ms", 20*time.Millisecond)) countingTsk3 = asynctask.Start(ctx, getCountingTask(10, "countingPer2ms", 2*time.Millisecond)) - start = time.Now() err = asynctask.WaitAny(ctx, &asynctask.WaitAnyOptions{FailFast: true}, countingTsk1, countingTsk2, countingTsk3) elapsed = time.Since(start) assert.NoError(t, err) @@ -42,6 +42,31 @@ func TestWaitAny(t *testing.T) { time.Sleep(1 * time.Millisecond) } +func TestWaitAnyContextCancel(t *testing.T) { + t.Parallel() + ctx, cancelTaskExecution := newTestContextWithTimeout(t, 2*time.Second) + + start := time.Now() + + countingTsk1 := asynctask.Start(ctx, getCountingTask(10, "countingPer40ms", 40*time.Millisecond)) + countingTsk2 := asynctask.Start(ctx, getCountingTask(10, "countingPer20ms", 20*time.Millisecond)) + go func() { + time.Sleep(5 * time.Millisecond) + cancelTaskExecution() + }() + err := asynctask.WaitAny(ctx, nil, countingTsk1, countingTsk2) + elapsed := time.Since(start) + assert.Error(t, err) + assert.Equal(t, "WaitAny context canceled", err.Error(), "expecting context canceled error") + // should finish right after countingTsk3 + assert.True(t, elapsed >= 5*time.Millisecond && elapsed < 200*time.Millisecond, fmt.Sprintf("actually elapsed: %v", elapsed)) + + // counting task do testing.Logf in another go routine + // while testing.Logf would cause DataRace error when test is already finished: https://github.com/golang/go/issues/40343 + // wait minor time for the go routine to finish. + time.Sleep(1 * time.Millisecond) +} + func TestWaitAnyErrorCase(t *testing.T) { t.Parallel() ctx, cancelTaskExecution := newTestContextWithTimeout(t, 3*time.Second) @@ -58,6 +83,7 @@ func TestWaitAnyErrorCase(t *testing.T) { completedTskState := completedTsk.State() assert.Equal(t, asynctask.StateCompleted, completedTskState, "completed task should finished") + start = time.Now() countingTsk := asynctask.Start(ctx, getCountingTask(10, "countingPer40ms", 40*time.Millisecond)) errorTsk = asynctask.Start(ctx, getErrorTask("expected error", 10*time.Millisecond)) panicTsk := asynctask.Start(ctx, getPanicTask(20*time.Millisecond)) @@ -84,6 +110,34 @@ func TestWaitAnyErrorCase(t *testing.T) { time.Sleep(1 * time.Millisecond) } +func TestWaitAnyAllFailCase(t *testing.T) { + t.Parallel() + ctx, cancelTaskExecution := newTestContextWithTimeout(t, 3*time.Second) + + start := time.Now() + errorTsk := asynctask.Start(ctx, getErrorTask("expected error", 10*time.Millisecond)) + panicTsk := asynctask.Start(ctx, getPanicTask(20*time.Millisecond)) + err := asynctask.WaitAny(ctx, nil, errorTsk, panicTsk) + assert.Error(t, err) + + panicTskState := panicTsk.State() + errTskState := errorTsk.State() + elapsed := time.Since(start) + cancelTaskExecution() // all assertion variable captured, cancel counting task + + assert.Equal(t, "expected error", err.Error(), "expecting first error") + // should finsh after both error + assert.True(t, elapsed >= 20*time.Millisecond, fmt.Sprintf("actually elapsed: %v", elapsed)) + + assert.Equal(t, asynctask.StateFailed, errTskState, "error task should failed") + assert.Equal(t, asynctask.StateFailed, panicTskState, "panic task should Not failed") + + // counting task do testing.Logf in another go routine + // while testing.Logf would cause DataRace error when test is already finished: https://github.com/golang/go/issues/40343 + // wait minor time for the go routine to finish. + time.Sleep(1 * time.Millisecond) +} + func TestWaitAnyErrorWithFailFastCase(t *testing.T) { t.Parallel() ctx, cancelTaskExecution := newTestContextWithTimeout(t, 3*time.Second) @@ -98,6 +152,7 @@ func TestWaitAnyErrorWithFailFastCase(t *testing.T) { // should finish after right away assert.True(t, elapsed < 20*time.Millisecond, fmt.Sprintf("actually elapsed: %v", elapsed)) + start = time.Now() countingTsk := asynctask.Start(ctx, getCountingTask(10, "countingPer40ms", 40*time.Millisecond)) errorTsk = asynctask.Start(ctx, getErrorTask("expected error", 10*time.Millisecond)) panicTsk := asynctask.Start(ctx, getPanicTask(20*time.Millisecond))