diff --git a/pkg/batcher/batcher_test.go b/pkg/batcher/batcher_test.go index 7553f1bd8d..4960b76ede 100644 --- a/pkg/batcher/batcher_test.go +++ b/pkg/batcher/batcher_test.go @@ -7,6 +7,11 @@ import ( "time" ) +const ( + defaultMaxDelay = 50 * time.Millisecond + slowMaxDelay = 5 * defaultMaxDelay +) + func mockExecution(inputs []string) (map[string]string, error) { results := make(map[string]string) for _, input := range inputs { @@ -39,7 +44,7 @@ func TestBatcher(t *testing.T) { name: "TestBatcher: single task", mockFunc: mockExecution, maxEntries: 10, - maxDelay: 1 * time.Second, + maxDelay: defaultMaxDelay, tasks: []string{"task1"}, expectResult: true, expectErrors: false, @@ -48,7 +53,7 @@ func TestBatcher(t *testing.T) { name: "TestBatcher: multiple tasks", mockFunc: mockExecution, maxEntries: 10, - maxDelay: 1 * time.Second, + maxDelay: defaultMaxDelay, tasks: []string{"task1", "task2", "task3"}, expectResult: true, expectErrors: false, @@ -57,7 +62,7 @@ func TestBatcher(t *testing.T) { name: "TestBatcher: same task", mockFunc: mockExecution, maxEntries: 10, - maxDelay: 1 * time.Second, + maxDelay: defaultMaxDelay, tasks: []string{"task1", "task1", "task1"}, expectResult: true, expectErrors: false, @@ -66,7 +71,7 @@ func TestBatcher(t *testing.T) { name: "TestBatcher: max capacity", mockFunc: mockExecution, maxEntries: 5, - maxDelay: 100 * time.Second, + maxDelay: slowMaxDelay, tasks: []string{"task1", "task2", "task3", "task4", "task5"}, expectResult: true, expectErrors: false, @@ -75,7 +80,7 @@ func TestBatcher(t *testing.T) { name: "TestBatcher: max delay", mockFunc: mockExecution, maxEntries: 100, - maxDelay: 2 * time.Second, + maxDelay: defaultMaxDelay, tasks: []string{"task1", "task2", "task3", "task4"}, expectResult: true, expectErrors: false, @@ -84,7 +89,7 @@ func TestBatcher(t *testing.T) { name: "TestBatcher: no execution without max delay or max entries", mockFunc: mockExecution, maxEntries: 10, - maxDelay: 15 * time.Second, + maxDelay: slowMaxDelay, tasks: []string{"task1", "task2", "task3"}, expectResult: false, }, @@ -92,7 +97,7 @@ func TestBatcher(t *testing.T) { name: "TestBatcher: error handling", mockFunc: mockExecutionWithError, maxEntries: 10, - maxDelay: 1 * time.Second, + maxDelay: defaultMaxDelay, tasks: []string{"errorTask"}, expectErrors: true, }, @@ -139,7 +144,7 @@ func TestBatcher(t *testing.T) { if r.Result != task && tc.expectResult { t.Errorf("Expected result for task %v, but got %v", task, r.Result) } - case <-time.After(10 * time.Second): + case <-time.After(slowMaxDelay - defaultMaxDelay): if tc.expectResult { t.Errorf("Timed out waiting for result of task %d", i) } diff --git a/pkg/cloud/cloud_test.go b/pkg/cloud/cloud_test.go index 9c26bb3278..cfb5208dca 100644 --- a/pkg/cloud/cloud_test.go +++ b/pkg/cloud/cloud_test.go @@ -44,6 +44,8 @@ const ( defaultVolumeID = "vol-test-1234" defaultNodeID = "node-1234" defaultPath = "/dev/xvdaa" + + defaultCreateDiskDeadline = time.Second * 5 ) func generateVolumes(volIdCount, volTagCount int) []*ec2.Volume { @@ -807,7 +809,8 @@ func TestCreateDisk(t *testing.T) { VolumeId: aws.String("snap-test-volume"), State: aws.String("completed"), } - ctx := context.Background() + ctx, ctxCancel := context.WithDeadline(context.Background(), time.Now().Add(defaultCreateDiskDeadline)) + defer ctxCancel() if tc.expCreateVolumeInput != nil { matcher := eqCreateVolume(tc.expCreateVolumeInput)