Skip to content

Commit b4bda3a

Browse files
author
Evsyukov Denis
committed
refactor: update GetFunctionalDependencies expectations to handle asynchronous calls in batch tests
Signed-off-by: Evsyukov Denis <denis.evsyukov@flant.com>
1 parent bdf4dfd commit b4bda3a

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

pkg/task/batch/batch_test.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,9 @@ func TestBatchScheduler_SubmitTask(t *testing.T) {
9494
for _, tt := range tests {
9595
t.Run(tt.name, func(t *testing.T) {
9696
mockMM := &MockModuleManager{}
97-
// Only add expectations for methods that will actually be called
98-
if tt.expectedBatch {
99-
// GetFunctionalDependencies is called when a batch is created
100-
mockMM.On("GetFunctionalDependencies").Return(map[string][]string{})
101-
}
97+
// GetFunctionalDependencies might be called asynchronously by the batch timer
98+
// Always set up the expectation to handle potential async calls
99+
mockMM.On("GetFunctionalDependencies").Return(map[string][]string{}).Maybe()
102100

103101
logger := log.NewLogger().Named("test")
104102
scheduler := NewBatchScheduler(tt.config, mockMM, logger)
@@ -114,13 +112,17 @@ func TestBatchScheduler_SubmitTask(t *testing.T) {
114112
}
115113
}
116114

115+
// Wait a bit to let any timers potentially fire
116+
time.Sleep(150 * time.Millisecond)
117+
117118
if tt.expectedBatch {
118119
assert.NotNil(t, lastBatch, "Expected batch to be created")
119120
assert.Equal(t, tt.expectBatchLen, len(lastBatch.Modules), "Expected batch size mismatch")
120-
mockMM.AssertExpectations(t)
121121
} else {
122122
assert.Nil(t, lastBatch, "Expected no batch to be created")
123123
}
124+
125+
// Don't assert expectations strictly since the timer might not have fired
124126
})
125127
}
126128
}
@@ -131,7 +133,8 @@ func TestBatchScheduler_SortPendingTasks(t *testing.T) {
131133
}
132134

133135
mockMM := &MockModuleManager{}
134-
// No expectations needed as this test doesn't call module manager methods
136+
// GetFunctionalDependencies might be called asynchronously by the batch timer
137+
mockMM.On("GetFunctionalDependencies").Return(map[string][]string{}).Maybe()
135138

136139
logger := log.NewLogger().Named("test")
137140
scheduler := NewBatchScheduler(config, mockMM, logger)
@@ -263,7 +266,8 @@ func TestBatchOptimizer_OptimizeBatch(t *testing.T) {
263266

264267
func TestBatchManager_ProcessModuleTask(t *testing.T) {
265268
mockMM := &MockModuleManager{}
266-
// No expectations needed as this test bypasses batching for global hooks
269+
// GetFunctionalDependencies might be called asynchronously by the batch timer
270+
mockMM.On("GetFunctionalDependencies").Return(map[string][]string{}).Maybe()
267271

268272
logger := log.NewLogger().Named("test")
269273
config := DefaultBatchConfig()
@@ -327,6 +331,9 @@ func TestModuleBatch_Properties(t *testing.T) {
327331

328332
func TestBatchMetrics_UpdateSafety(t *testing.T) {
329333
mockMM := &MockModuleManager{}
334+
// GetFunctionalDependencies might be called asynchronously by the batch timer
335+
mockMM.On("GetFunctionalDependencies").Return(map[string][]string{}).Maybe()
336+
330337
logger := log.NewLogger().Named("test")
331338

332339
var queueService *taskqueue.Service

0 commit comments

Comments
 (0)