fix: fix tests, add tiny sleep when starting task executor so all queued tasks are in the tasks slice
This commit is contained in:
parent
d6c3a1a2c3
commit
b96006064e
7
task.go
7
task.go
@ -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 {
|
||||
|
@ -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())
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user