package worker import ( "context" "sync" "testing" "time" ) func TestRunnerStartRunsJobsImmediatelyAndOnSchedule(t *testing.T) { t.Parallel() job := &stubJob{} runner := NewRunner([]Job{job}, 10*time.Millisecond, nil) ctx, cancel := context.WithCancel(context.Background()) defer cancel() runner.Start(ctx) deadline := time.Now().Add(250 * time.Millisecond) for { if job.Count() >= 2 { break } if time.Now().After(deadline) { t.Fatalf("job count = %d, want at least 2 scheduled runs", job.Count()) } time.Sleep(5 * time.Millisecond) } } type stubJob struct { mu sync.Mutex count int } func (j *stubJob) Name() string { return "stub" } func (j *stubJob) Run(context.Context) error { j.mu.Lock() defer j.mu.Unlock() j.count++ return nil } func (j *stubJob) Count() int { j.mu.Lock() defer j.mu.Unlock() return j.count }