fix: fix tests, add tiny sleep when starting task executor so all queued tasks are in the tasks slice

This commit is contained in:
Jeremy Tregunna 2025-04-17 16:23:32 -06:00
parent d6c3a1a2c3
commit b96006064e
Signed by: jer
GPG Key ID: 1278B36BA6F5D5E4
2 changed files with 8 additions and 1 deletions

View File

@ -44,11 +44,12 @@ func (te *TaskExecutor) AddTask(task Task, interval time.Duration) {
case te.taskChan <- st:
log.Printf("Task %T queued up with interval %v\n", task, interval)
default:
log.Printf("Failed to add task $T with interval %v, channel full\n", task, interval)
log.Printf("Failed to add task %T with interval %v, channel full\n", task, interval)
}
}
func (te *TaskExecutor) Start() {
// Launch the task processor goroutine
go func() {
for {
select {
@ -78,6 +79,10 @@ func (te *TaskExecutor) Start() {
}
}
}()
// Process any tasks already in the channel before returning
// This ensures that when Start() returns, all queued tasks are in the tasks slice
time.Sleep(10 * time.Millisecond)
}
func (te *TaskExecutor) shouldRun(st *ScheduledTask, t time.Time) bool {

View File

@ -16,6 +16,8 @@ func (m *mockTask) Execute() error {
func TestAddTask(t *testing.T) {
te := NewTaskExecutor(10)
te.AddTask(&mockTask{}, 1*time.Second)
te.Start()
// No need for explicit sleep now, as Start() ensures tasks are processed
if te.Len() != 1 {
t.Errorf("expected 1 task, got %d", te.Len())
}