func TestRepeatingZeroOrOne(t *testing.T) { task := &fakeTask{} if tasks.RepeatingTask(task, 0) != tasks.NilTask() { t.Error("Expect zero repeating task to be nil task.") } if tasks.RepeatingTask(task, 1) != task { t.Error("Expected one repeating task to be that task.") } }
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 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 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 TestSingleExecutorPauseFromBeginning(t *testing.T) { starting := make(chan bool, 100) defer close(starting) task := &pauseTask{Starting: starting} se := tasks.NewSingleExecutor() defer se.Close() se.Pause() e := se.Start(&taskStruct{tasks.RepeatingTask(task, 2147483647)}) time.Sleep(5 * time.Millisecond) assertNoStarting(t, starting) 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 } 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() }
// AsTask returns a Task from this instance. setter is what changes the // lightbulb. lights is the default set of lights empty means all lights. // The returned Task does not get its own deep copy of this instance. The // results of using it after making further changes to this instance are // undefined. func (a *Action) AsTask(setter Setter, lights []int) tasks.Task { if a.Repeat < 2 { return a.asTask(setter, lights) } return tasks.RepeatingTask(a.asTask(setter, lights), a.Repeat) }