Skip to content

Commit

Permalink
Merge pull request #1942 from AndrewSirenko/fix-slow-batcher-unit-tests
Browse files Browse the repository at this point in the history
Fix slow createDisk and batcher unit tests
  • Loading branch information
k8s-ci-robot authored Feb 26, 2024
2 parents 6f67b3d + fbf742b commit 7f19755
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
21 changes: 13 additions & 8 deletions pkg/batcher/batcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -84,15 +89,15 @@ 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,
},
{
name: "TestBatcher: error handling",
mockFunc: mockExecutionWithError,
maxEntries: 10,
maxDelay: 1 * time.Second,
maxDelay: defaultMaxDelay,
tasks: []string{"errorTask"},
expectErrors: true,
},
Expand Down Expand Up @@ -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)
}
Expand Down
5 changes: 4 additions & 1 deletion pkg/cloud/cloud_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 7f19755

Please sign in to comment.