Пример #1
0
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()
}
Пример #2
0
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)
			}
		}
	}
}
Пример #3
0
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)
			}
		}
	}
}
Пример #4
0
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)
	}
}
Пример #5
0
func TestError(t *testing.T) {
	eTask := &fakeTask{err: kSomeError}
	e := tasks.Start(eTask)
	<-e.Done()
	if e.Error() != kSomeError {
		t.Error("Expected some error.")
	}
}
Пример #6
0
func TestNoError(t *testing.T) {
	eTask := tasks.NilTask()
	e := tasks.Start(eTask)
	<-e.Done()
	if e.Error() != nil {
		t.Error("Expected no error.")
	}
}
Пример #7
0
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)
	}
}
Пример #8
0
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)
	}
}
Пример #9
0
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)
	}
}
Пример #10
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.")
	}
}
Пример #11
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.")
		}
	}
}
Пример #12
0
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)
		}
	}
}
Пример #13
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.")
		}
	}
}
Пример #14
0
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.")
	}
}