Skip to content

Commit

Permalink
Fix slow batcher unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewSirenko committed Feb 21, 2024
1 parent 6f67b3d commit 0e826b2
Showing 1 changed file with 13 additions and 8 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

0 comments on commit 0e826b2

Please sign in to comment.