Beispiel #1
0
func TestParallelZeroOrOne(t *testing.T) {
	task := &fakeTask{}
	if tasks.ParallelTasks() != tasks.NilTask() {
		t.Error("Expected zero parallel tasks to be nil task.")
	}
	if tasks.ParallelTasks(task) != task {
		t.Error("Expected one parallel task to be that task.")
	}
}
Beispiel #2
0
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)
	})
}
Beispiel #3
0
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.")
	}
}
Beispiel #4
0
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.")
		}
	}
}
Beispiel #5
0
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.")
		}
	}
}
Beispiel #6
0
func TestPauseNotSupportedParallelPauseEarly(t *testing.T) {
	starting := make(chan bool, 100)
	defer close(starting)
	task1 := &pauseTask{Starting: starting}
	ts := make([]tasks.Task, 20)
	for i := range ts {
		ts[i] = task1
	}
	se := tasks.NewSingleExecutor()
	defer se.Close()
	se.Start(&taskStruct{tasks.ParallelTasks(ts...)})
	se.Pause()

	// Acknowledge the tasks that completed while pausing
	waitForStarts(starting, task1.Count())
	time.Sleep(time.Millisecond)

	// No new tasks should start
	assertNoStarting(t, starting)
}
Beispiel #7
0
func TestPauseNotSupportedParallel(t *testing.T) {
	starting := make(chan bool, 100)
	defer close(starting)
	task1 := &pauseTask{Starting: starting}
	ts := make([]tasks.Task, 20)
	for i := range ts {
		ts[i] = task1
	}
	se := tasks.NewSingleExecutor()
	defer se.Close()
	e := se.Start(&taskStruct{tasks.ParallelTasks(ts...)})
	waitForStarts(starting, len(ts))

	// Since these parallel tasks don't support pause, this Pause() call
	// won't return until all the tasks have finished.
	se.Pause()
	if out := task1.Count(); out != len(ts) {
		t.Errorf("Expected %d, got %d", len(ts), out)
	}
	// A paused task can still end on its own. The only
	// guarantee is that the paused task won't do any additional work.
	<-e.Done()
}
Beispiel #8
0
func TestPauseParallel(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.ParallelTasks(ts...)})
	waitForStarts(starting, len(ts))
	se.Pause()
	expected := len(ts)
	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()
	waitForStarts(starting, len(ts))
	se.Pause()
	// no-op
	se.Pause()
	expected += len(ts)
	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)
	e.End()
	<-e.Done()
}