func TestSeriesZeroOrOne(t *testing.T) { task := &fakeTask{} if tasks.SeriesTasks() != tasks.NilTask() { t.Error("Expected zero series tasks to be nil task.") } if tasks.SeriesTasks(task) != task { t.Error("Expected one series task to be that task.") } }
func (a *Action) asTask(setter Setter, lights []int) tasks.Task { if len(a.Lights) > 0 { lights = a.Lights } if len(a.Parallel) > 0 { parallelTasks := make([]tasks.Task, len(a.Parallel)) for i := range parallelTasks { parallelTasks[i] = a.Parallel[i].AsTask(setter, lights) } return tasks.ParallelTasks(parallelTasks...) } if len(a.Series) > 0 { seriesTasks := make([]tasks.Task, len(a.Series)) for i := range seriesTasks { seriesTasks[i] = a.Series[i].AsTask(setter, lights) } return tasks.SeriesTasks(seriesTasks...) } if a.G != nil { if len(a.G.Cds) == 0 || a.G.Cds[0].D != 0 { panic("D of first ColorDuration element must be 0.") } return tasks.TaskFunc(func(e *tasks.Execution) { a.doGradient(setter, lights, e) }) } if a.C.Valid || a.Bri.Valid || a.On || a.Off { return tasks.TaskFunc(func(e *tasks.Execution) { a.doOnOff(setter, lights, e) }) } return tasks.TaskFunc(func(e *tasks.Execution) { e.Sleep(a.Sleep) }) }
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 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 TestPauseSeriesItself(t *testing.T) { starting := make(chan bool, 100) defer close(starting) task := &pauseTask{Starting: starting} se := tasks.NewSingleExecutor() defer se.Close() // Tasks in executor must support equality e := se.Start(&taskStruct{tasks.SeriesTasks(task, task)}) waitForStarts(starting, 1) se.Pause() select { case <-e.Done(): t.Error("Pause not working") case <-time.After(10 * time.Millisecond): } e.End() <-e.Done() }
func TestPauseSeries(t *testing.T) { starting := make(chan bool, 100) defer close(starting) task := &pauseTask{Starting: starting} se := tasks.NewSingleExecutor() defer se.Close() ts := make([]tasks.Task, 20) for i := range ts { ts[i] = tasks.RepeatingTask(task, 2147483647) } // Tasks in executor must support equality e := se.Start(&taskStruct{tasks.SeriesTasks(ts...)}) waitForStarts(starting, 1) se.Pause() expected := 1 if out := task.Count(); out != expected { t.Errorf("Expected count of %d, got %d", expected, out) expected = out } time.Sleep(5 * time.Millisecond) assertNoStarting(t, starting) se.Resume() se.Resume() waitForStarts(starting, 1) se.Pause() expected += 1 if out := task.Count(); out != expected { t.Errorf("Expected count of %d, got %d", expected, out) expected = out } time.Sleep(5 * time.Millisecond) assertNoStarting(t, starting) se.Resume() e.End() <-e.Done() }