test_: fix failed test TestScheduler_Enqueue_ValidateOrder

This commit is contained in:
frank 2024-08-16 20:18:09 +08:00 committed by Andrea Maria Piana
parent d3b1999963
commit 09f8051238
2 changed files with 8 additions and 4 deletions

View File

@ -116,8 +116,9 @@ func (s *Scheduler) Enqueue(taskType TaskType, taskFn taskFunction, resFn result
// if other task type is running
// notify the queued one that it is overwritten or ignored
if existingTask.policy == ReplacementPolicyCancelOld {
oldResFn := existingTask.resFn
go func() {
existingTask.resFn(nil, existingTask.taskType, ErrTaskOverwritten)
oldResFn(nil, existingTask.taskType, ErrTaskOverwritten)
}()
// Overwrite the queued one of the same type
existingTask.taskFn = taskFn

View File

@ -287,6 +287,7 @@ func TestScheduler_Enqueue_ValidateOrder(t *testing.T) {
if p.taskType.Policy == ReplacementPolicyCancelOld && ctx.Err() != nil && errors.Is(ctx.Err(), context.Canceled) {
taskCanceledChan <- p
t.Logf("task canceled, task seq: %d, task type: %+v", currentIndex, p.taskType)
return nil, ctx.Err()
}
@ -295,13 +296,16 @@ func TestScheduler_Enqueue_ValidateOrder(t *testing.T) {
return nil, errors.New("test error")
}
taskSuccessChan <- p
t.Logf("task executed successfully, task seq: %d, task type: %+v", currentIndex, p.taskType)
return 10 * (currentIndex + 1), nil
}, func(res interface{}, taskType TaskType, err error) {
require.Equal(t, p.taskType, taskType)
resChan <- p
t.Logf("response invoked, task seq: %d, task type: %+v, result: %+v", currentIndex, taskType, res)
})
if ignored {
t.Logf("task ignored, task seq: %d, task type: %+v", currentIndex, p.taskType)
ignoredCount++
}
@ -355,7 +359,7 @@ func TestScheduler_Enqueue_ValidateOrder(t *testing.T) {
require.Equal(t, 1, taskFailedCount[testTask3], "expected one task call for type: %d had %d", 3, taskSuccessCount[testTask3])
require.Equal(t, 2, resChanCount[testTask1], "expected two task call for type: %d had %d", 1, taskSuccessCount[testTask1])
require.Equal(t, 2, resChanCount[testTask2], "expected tow task call for type: %d had %d", 2, taskSuccessCount[testTask2])
require.Equal(t, 2, resChanCount[testTask2], "expected two task call for type: %d had %d", 2, taskSuccessCount[testTask2])
require.Equal(t, 1, resChanCount[testTask3], "expected one task call for type: %d had %d", 3, taskSuccessCount[testTask3])
}
@ -400,13 +404,12 @@ func TestScheduler_Enqueue_Quick_Stop(t *testing.T) {
wg.Add(2)
longRunningTask := func(ctx context.Context) (interface{}, error) {
defer wg.Done()
select {
case <-ctx.Done():
// we should reach here rather than other condition branch as Stop() canceled the context quickly
wg.Done()
return nil, ctx.Err()
case <-time.After(10 * time.Second):
wg.Done()
return "task completed", nil
}
}