def test_add_already_added_task(): time_stamp = datetime(year=2018, month=8, day=13, hour=5, minute=10, second=5, microsecond=100222) t1 = Task(1, "run command example", time_stamp, name="example run", desc="this is a bologna command that does nothing", duration=500, max_attempts=3) start_time_stamp = datetime(year=2018, month=8, day=13, hour=5, minute=10, second=6, microsecond=100222) t1.attempt_task("runner", start_time_stamp) ot = OpenTasks(LOGGER) assert len(ot) == 0 ot.add_task(t1) # should be there now assert len(ot) == 1 assert ot.get_task(1) == t1 ot.add_task(t1) # should be there now assert len(ot) == 1 assert ot.get_task(1) == t1
def test_attempt_task(): task = Task("1234", "some cmd", datetime.datetime(2018, 1, 15, 12, 35, 0), max_attempts=2) # no attempts assert task.num_attempts() == 0 assert task.most_recent_attempt() is None attempt_time = datetime.datetime(2018, 1, 15, 12, 35, 30) attempt = task.attempt_task("runner 1", attempt_time) assert task.num_attempts() == 1 assert task.most_recent_attempt() == attempt attempt_time_2 = datetime.datetime(2018, 1, 15, 12, 35, 45) attempt_2 = task.attempt_task("runner 2", attempt_time_2) assert task.num_attempts() == 2 assert task.most_recent_attempt() == attempt_2 # now get another attempt (which will be greater than max_attempts so should fail. attempt_time_3 = datetime.datetime(2018, 1, 15, 12, 36, 10) attempt_3 = task.attempt_task("runner 3", attempt_time_3) assert attempt_3 is None assert task.num_attempts() == 2 assert task.most_recent_attempt() == attempt_2
def test_open_time_open_attempt(): task = Task("1234", "some cmd", datetime.datetime(2018, 1, 15, 12, 35, 0), max_attempts=2) attempt_time = datetime.datetime(2018, 1, 15, 12, 35, 30) task.attempt_task("runner 1", attempt_time) assert task.open_time() is None
def test_is_completed_open_attempt(): task = Task("1234", "some cmd", datetime.datetime(2018, 1, 15, 12, 35, 0), max_attempts=2) attempt_time = datetime.datetime(2018, 1, 15, 12, 35, 30) task.attempt_task("runner 1", attempt_time) assert task.is_completed() is False
def test_is_in_process_open_attempt(): task = Task("1234", "some cmd", datetime.datetime(2018, 1, 15, 12, 35, 0), max_attempts=2) attempt_time = datetime.datetime(2018, 1, 15, 12, 35, 30) task.attempt_task("runner 1", attempt_time) assert task.is_in_process() is True
def test_task_to_retry_first_started_retry(): time_stamp = datetime(year=2018, month=8, day=13, hour=5, minute=10, second=5, microsecond=100222) t1 = Task(1, "run command example", time_stamp, name="example run", desc="this is a bologna command that does nothing", duration=100, max_attempts=3) start_time_stamp = datetime(year=2018, month=8, day=13, hour=5, minute=10, second=6, microsecond=100222) t1.attempt_task("runner", start_time_stamp) t2 = Task(2, "run command example 2", time_stamp, name="example run 2", desc="this is a bologna command that does nothing", duration=820, max_attempts=3) start_time_stamp = datetime(year=2018, month=8, day=13, hour=5, minute=10, second=6, microsecond=100222) t2.attempt_task("runner", start_time_stamp) ot = OpenTasks(LOGGER) ot.add_task(t1) ot.add_task(t2) current_time = datetime(year=2018, month=8, day=13, hour=5, minute=12, second=8, microsecond=100222) task, failed_tasks = ot.task_to_retry(current_time) # should be no failed tasks assert task.task_id() == t1.task_id() assert task == t1 assert len(failed_tasks) == 0
def test_completed_time_multiple_completed_attempts(): task_created_time = datetime.datetime(2018, 1, 15, 12, 35, 0) task = Task("1234", "some cmd", task_created_time, max_attempts=2) attempt_time = datetime.datetime(2018, 1, 15, 12, 35, 30) attempt = task.attempt_task("runner 1", attempt_time) attempt_completed_time = datetime.datetime(2018, 1, 15, 12, 35, 45) attempt.mark_completed(attempt_completed_time) attempt_time_2 = datetime.datetime(2018, 1, 15, 12, 35, 45) attempt_2 = task.attempt_task("runner 2", attempt_time_2) attempt_2.mark_completed(datetime.datetime(2018, 1, 15, 12, 35, 48)) assert task.completed_time() == attempt_completed_time
def test_completed_time_all_failed_attempts(): task = Task("1234", "some cmd", datetime.datetime(2018, 1, 15, 12, 35, 0), max_attempts=2) attempt_time = datetime.datetime(2018, 1, 15, 12, 35, 30) attempt = task.attempt_task("runner 1", attempt_time) attempt.mark_failed(datetime.datetime(2018, 1, 15, 12, 35, 45)) attempt_time_2 = datetime.datetime(2018, 1, 15, 12, 35, 45) attempt_2 = task.attempt_task("runner 2", attempt_time_2) attempt_2.mark_failed(datetime.datetime(2018, 1, 15, 12, 35, 48)) assert task.completed_time() is None
def test_remove_task_that_doesnt_exist(): time_stamp = datetime(year=2018, month=8, day=13, hour=5, minute=10, second=5, microsecond=100222) t1 = Task(1, "run command example", time_stamp, name="example run", desc="this is a bologna command that does nothing", duration=500, max_attempts=3) start_time_stamp = datetime(year=2018, month=8, day=13, hour=5, minute=10, second=6, microsecond=100222) t1.attempt_task("runner", start_time_stamp) t2 = Task(2, "run command example 2", time_stamp, name="example run 2", desc="this is a bologna command that does nothing", duration=90, max_attempts=3) start_time_stamp = datetime(year=2018, month=8, day=13, hour=5, minute=10, second=8, microsecond=100222) t2.attempt_task("runner", start_time_stamp) ot = OpenTasks(LOGGER) ot.add_task(t1) ot.add_task(t2) assert len(ot) == 2 assert ot.get_task(1) == t1 assert ot.get_task(2) == t2 # remove non-existent task ot.remove_task("id that doesn't exist") assert len(ot) == 2 assert ot.get_task(1) == t1 assert ot.get_task(2) == t2
def test_started_both_failed_attempts(): task = Task("1234", "some cmd", datetime.datetime(2018, 1, 15, 12, 35, 0), max_attempts=2) attempt_time = datetime.datetime(2018, 1, 15, 12, 35, 30) attempt = task.attempt_task("runner 1", attempt_time) attempt.mark_failed(datetime.datetime(2018, 1, 15, 12, 35, 45)) attempt_time_2 = datetime.datetime(2018, 1, 15, 12, 35, 45) attempt_2 = task.attempt_task("runner 2", attempt_time_2) attempt_2.mark_failed(datetime.datetime(2018, 1, 15, 12, 35, 48)) assert task.is_started() is True
def test_is_in_process_all_fail(): task = Task("1234", "some cmd", datetime.datetime(2018, 1, 15, 12, 35, 0), max_attempts=2) attempt_time = datetime.datetime(2018, 1, 15, 12, 35, 30) attempt = task.attempt_task("runner 1", attempt_time) attempt.mark_failed(datetime.datetime(2018, 1, 15, 12, 35, 45)) attempt_time_2 = datetime.datetime(2018, 1, 15, 12, 35, 45) attempt_2 = task.attempt_task("runner 2", attempt_time_2) attempt_2.mark_failed(datetime.datetime(2018, 1, 15, 12, 35, 48)) assert task.is_in_process() is False
def test_all_tasks_added_in_reverse_order(): # same as test_all_tasks but the creation times are reversed time_stamp = datetime(year=2018, month=8, day=13, hour=5, minute=10, second=5, microsecond=100222) t1 = Task(1, "run command example", time_stamp, name="example run", desc="this is a bologna command that does nothing", duration=500, max_attempts=3) start_time_stamp = datetime(year=2018, month=8, day=13, hour=5, minute=10, second=8, microsecond=100222) t1.attempt_task("runner", start_time_stamp) t2 = Task(2, "run command example 2", time_stamp, name="example run 2", desc="this is a bologna command that does nothing", duration=90, max_attempts=3) start_time_stamp = datetime(year=2018, month=8, day=13, hour=5, minute=10, second=6, microsecond=100222) t2.attempt_task("runner", start_time_stamp) ot = OpenTasks(LOGGER) ot.add_task(t1) ot.add_task(t2) # should be sorted by creation time tasks = ot.all_tasks() assert len(tasks) == 2 assert tasks[0] == t1 assert tasks[1] == t2
def test_get_attempt(): task = Task("1234", "some cmd", datetime.datetime(2018, 1, 15, 12, 35, 0), max_attempts=2) attempt_time = datetime.datetime(2018, 1, 15, 12, 35, 30) attempt_1 = task.attempt_task("runner 1", attempt_time) attempt_time_2 = datetime.datetime(2018, 1, 15, 12, 35, 45) attempt_2 = task.attempt_task("runner 2", attempt_time_2) # no id should be None assert task.get_attempt("fake_attempt_id") is None assert attempt_1 == task.get_attempt(attempt_1._attempt_id) assert attempt_2 == task.get_attempt(attempt_2._attempt_id)
def test_is_completed_completed_attempt(): task = Task("1234", "some cmd", datetime.datetime(2018, 1, 15, 12, 35, 0), max_attempts=2) attempt_time = datetime.datetime(2018, 1, 15, 12, 35, 30) attempt = task.attempt_task("runner 1", attempt_time) attempt.mark_completed(datetime.datetime(2018, 1, 15, 12, 35, 45)) assert task.is_completed() is True
def test_open_time_one_attempt_failed(): task = Task("1234", "some cmd", datetime.datetime(2018, 1, 15, 12, 35, 0), max_attempts=2) attempt_time = datetime.datetime(2018, 1, 15, 12, 35, 30) attempt = task.attempt_task("runner 1", attempt_time) attempt.mark_failed(datetime.datetime(2018, 1, 15, 12, 35, 45)) assert task.open_time() is None
def test_open_time_one_completed_attempt(): task_created_time = datetime.datetime(2018, 1, 15, 12, 35, 0) task = Task("1234", "some cmd", task_created_time, max_attempts=2) attempt_time = datetime.datetime(2018, 1, 15, 12, 35, 30) attempt = task.attempt_task("runner 1", attempt_time) attempt_completed_time = datetime.datetime(2018, 1, 15, 12, 35, 45) attempt.mark_completed(attempt_completed_time) assert task.open_time() == (attempt_completed_time - task_created_time).total_seconds()
def test_started_time_open_attempt(): task_created_time = datetime.datetime(2018, 1, 15, 12, 35, 0) task = Task("1234", "some cmd", task_created_time, max_attempts=2) attempt_time = datetime.datetime(2018, 1, 15, 12, 35, 30) task.attempt_task("runner 1", attempt_time) assert task.started_time() == attempt_time