Пример #1
0
func main() {
	const context = "main"

	// Create a task value for execution.
	t := Task{
		Name: "test task",
	}

	// Start the job running with a specified duration.
	if err := runner.Run(context, time.Second, &t); err != nil {
		switch err {
		case runner.ErrTimeout:

			// The task did not finish within the specified duration.
			log.Error(context, "main", err, "Task timeout")

		case runner.ErrSignaled:

			// The user hit <control> c and we shutdown early.
			log.Error(context, "main", err, "Shutdown early")

		default:

			// An error occurred in the processing of the task.
			log.Error(context, "main", err, "Processing error")
		}

		os.Exit(1)
	}

	log.User(context, "main", "Completed")
}
Пример #2
0
// TestSignaled tests when jobs is requested to shutdown.
func TestSignaled(t *testing.T) {
	tests.ResetLog()
	defer tests.DisplayLog()

	t.Log("Given the need to test a task that is requested to shutdown.")
	{
		t.Log("\tWhen using a task that should see the signal.")
		{
			var job task
			job.KillAfter(100 * time.Millisecond)

			// Need the job method to quit as soon as we are done.
			defer job.Kill()

			go func() {
				time.Sleep(50 * time.Millisecond)
				syscall.Kill(syscall.Getpid(), syscall.SIGINT)
			}()

			if err := runner.Run(tests.Context, 3*time.Second, &job); err != runner.ErrSignaled {
				t.Fatalf("\t%s\tShould receive a signaled error : %v", tests.Failed, err)
			}
			t.Logf("\t%s\tShould receive a signaled error.", tests.Success)
		}
	}
}
Пример #3
0
// TestCompleted tests when jobs complete properly.
func TestCompleted(t *testing.T) {
	tests.ResetLog()
	defer tests.DisplayLog()

	t.Log("Given the need to test a successful task run.")
	{
		t.Log("\tWhen using a task that will complete in time.")
		{
			var job task
			job.KillAfter(time.Millisecond)

			if err := runner.Run(tests.Context, time.Second, &job); err != nil {
				t.Fatalf("\t%s\tShould not receive an error : %v", tests.Failed, err)
			}
			t.Logf("\t%s\tShould not receive an error.", tests.Success)
		}
	}
}
Пример #4
0
// TestError tests when jobs complete properly but with errors.
func TestError(t *testing.T) {
	tests.ResetLog()
	defer tests.DisplayLog()

	t.Log("Given the need to test a successful task run with error.")
	{
		t.Log("\tWhen using a task that will complete in time.")
		{
			Err := errors.New("An error")
			job := task{
				err: Err,
			}
			job.KillAfter(time.Millisecond)

			if err := runner.Run(tests.Context, time.Second, &job); err != Err {
				t.Fatalf("\t%s\tShould receive our error : %v", tests.Failed, err)
			}
			t.Logf("\t%s\tShould receive our error.", tests.Success)
		}
	}
}
Пример #5
0
// TestTimeout tests when jobs timeout.
func TestTimeout(t *testing.T) {
	tests.ResetLog()
	defer tests.DisplayLog()

	t.Log("Given the need to test a task that timesout.")
	{
		t.Log("\tWhen using a task that will timeout.")
		{
			var job task
			job.KillAfter(time.Second)

			// Need the job method to quit as soon as we are done.
			defer job.Kill()

			if err := runner.Run(tests.Context, time.Millisecond, &job); err != runner.ErrTimeout {
				t.Fatalf("\t%s\tShould receive a timeout error : %v", tests.Failed, err)
			}
			t.Logf("\t%s\tShould receive a timeout error.", tests.Success)
		}
	}
}