func TestRecurringEnded(t *testing.T) { tk := tasks.NilTask() r := recurring.AtInterval(kNow, time.Hour) e := tasks.Start(tasks.RecurringTask(tk, r)) e.End() <-e.Done() }
func TestSeriesError(t *testing.T) { // three tasks testTasks := make([]tasks.Task, 3) // second task throws an error for i := range testTasks { if i == 1 { testTasks[i] = &fakeTask{err: kSomeError} } else { testTasks[i] = &fakeTask{} } } e := tasks.Start(tasks.SeriesTasks(testTasks...)) <-e.Done() // First 2 tasks should have been but not 3rd task for i, atask := range testTasks { ft := atask.(*fakeTask) if i < 2 { if !ft.hasRun() { t.Errorf("Expected task %d to be run.", i) } } else { if ft.hasRun() { t.Errorf("Expected task %d not to be run.", i) } } } }
func TestSeriesEnded(t *testing.T) { // two tasks testTasks := make([]tasks.Task, 2) for i := range testTasks { testTasks[i] = &fakeTask{runDuration: time.Hour} } e := tasks.Start(tasks.SeriesTasks(testTasks...)) e.End() <-e.Done() // 2nd task should not be reached. for i, atask := range testTasks { ft := atask.(*fakeTask) if i < 1 { if !ft.hasRun() { t.Errorf("Expected task %d to be run.", i) } } else { if ft.hasRun() { t.Errorf("Expected task %d not to be run.", i) } } } }
func verifyRepeatingTask(t *testing.T, n int) { task := &fakeTask{} e := tasks.Start(tasks.RepeatingTask(task, n)) <-e.Done() if task.timesRun != n { t.Errorf("Expected %d, got %d", n, task.timesRun) } }
func TestError(t *testing.T) { eTask := &fakeTask{err: kSomeError} e := tasks.Start(eTask) <-e.Done() if e.Error() != kSomeError { t.Error("Expected some error.") } }
func TestNoError(t *testing.T) { eTask := tasks.NilTask() e := tasks.Start(eTask) <-e.Done() if e.Error() != nil { t.Error("Expected no error.") } }
func TestRepeatingTaskError(t *testing.T) { task := &fakeTask{err: kSomeError} e := tasks.Start(tasks.RepeatingTask(task, 5)) <-e.Done() if task.timesRun != 1 { t.Errorf("Expected 1, got %v", task.timesRun) } }
func TestRepeatingTaskEnded(t *testing.T) { task := &fakeTask{runDuration: time.Hour} e := tasks.Start(tasks.RepeatingTask(task, 5)) e.End() <-e.Done() if task.timesRun != 1 { t.Errorf("Expected 1, got %v", task.timesRun) } }
func TestNestedRepeatingTask(t *testing.T) { task := &fakeTask{} e := tasks.Start(tasks.RepeatingTask( tasks.RepeatingTask(task, 2), 3)) <-e.Done() if task.timesRun != 6 { t.Errorf("Expected 6, got %v", task.timesRun) } }
func TestParallelError(t *testing.T) { testTasks := make([]tasks.Task, 20) for i := range testTasks { if i == 5 { testTasks[i] = &fakeTask{err: kSomeError} } else { testTasks[i] = &fakeTask{} } } e := tasks.Start(tasks.ParallelTasks(testTasks...)) <-e.Done() if e.Error() != kSomeError { t.Error("Expected to get an error.") } }
func TestParallelEnded(t *testing.T) { testTasks := make([]tasks.Task, 20) for i := range testTasks { testTasks[i] = &fakeTask{runDuration: time.Hour} } e := tasks.Start(tasks.ParallelTasks(testTasks...)) e.End() <-e.Done() for _, atask := range testTasks { ft := atask.(*fakeTask) if !ft.hasRun() { t.Error("Expected task to be run.") } } }
func TestSeries(t *testing.T) { // three tasks testTasks := make([]tasks.Task, 3) // second task throws an error for i := range testTasks { testTasks[i] = &fakeTask{} } e := tasks.Start(tasks.SeriesTasks(testTasks...)) <-e.Done() for i, atask := range testTasks { ft := atask.(*fakeTask) if !ft.hasRun() { t.Errorf("Expected task %d to be run.", i) } } }
func TestParallel(t *testing.T) { testTasks := make([]tasks.Task, 20) for i := range testTasks { testTasks[i] = &fakeTask{} } e := tasks.Start(tasks.ParallelTasks(testTasks...)) <-e.Done() // Blocking here is not necessary in production code. Just testing that // this channel gets closed too. <-e.Ended() for _, atask := range testTasks { ft := atask.(*fakeTask) if !ft.hasRun() { t.Error("Expected task to be run.") } } }
func TestEndTask(t *testing.T) { longTask := &fakeTask{runDuration: time.Hour} e := tasks.Start(longTask) if e.IsEnded() { t.Error("Expected IsEnded() to be false.") } if e.IsDone() { t.Error("Expected IsDone() to be false.") } e.End() if !e.IsEnded() { t.Error("Expected IsEnded() to be true.") } <-e.Done() if !e.IsDone() { t.Error("Expected IsDone() to be true.") } if !longTask.hasRun() { t.Error("Expected task to be run.") } }