def test_execute_one_event(self): event_scheduler = EventScheduler(TEST_THREAD, TestTimer.monotonic, TestTimer) event_scheduler.start() result_list = [] event_scheduler.enter(0, 0, insert_into_list, ('A', result_list)) event_scheduler.stop() self.assertTrue(result_list) self.assertEqual(result_list[0], 'A')
def test_event_scheduler_stopped(self): event_scheduler = EventScheduler(TEST_THREAD, TestTimer.monotonic, TestTimer) result_list = [] event_scheduler.enter(0, 0, insert_into_list, ('A', result_list)) TestTimer.advance_time(0) self.assertFalse(result_list) event_scheduler.start() event_scheduler.stop() event_scheduler.enter(0, 0, insert_into_list, ('A', result_list)) TestTimer.advance_time(0) self.assertFalse(result_list)
def test_relative_delay(self): event_scheduler = EventScheduler(TEST_THREAD, TestTimer.monotonic, TestTimer) TestTimer.set_event_scheduler(event_scheduler) event_scheduler.start() result_list = [] item = 'A' event_scheduler.enter(3, 0, insert_into_list, (item, result_list)) TestTimer.advance_time(2) self.assertListEqual(result_list, []) TestTimer.advance_time(1) self.assertListEqual(result_list, [item]) event_scheduler.stop()
def test_cancel_all_events(self): event_scheduler = EventScheduler(TEST_THREAD, TestTimer.monotonic, TestTimer) TestTimer.set_event_scheduler(event_scheduler) event_scheduler.start() result_list = [] event_scheduler.enter_recurring(5, 0, insert_into_list, ('A', result_list)) event_scheduler.enter_recurring(2, 0, insert_into_list, ('B', result_list)) event_scheduler.enter(0, 0, insert_into_list, ('C', result_list)) TestTimer.advance_time(1) self.assertListEqual(result_list, ['C']) event_scheduler.enter(1, 0, insert_into_list, ('D', result_list)) TestTimer.advance_time(0.2) event_scheduler.cancel_all() self.assertListEqual(result_list, ['C']) event_scheduler.stop(False)
def test_event_scheduler_raises_invalid_priority(self): event_scheduler = EventScheduler(TEST_THREAD, TestTimer.monotonic, TestTimer) result_list = [] # priority less than 0 with self.assertRaises(ValueError): event_scheduler.enter(0, -1, insert_into_list, ('A', result_list)) with self.assertRaises(ValueError): event_scheduler.enterabs(0, -1, insert_into_list, ('A', result_list)) with self.assertRaises(ValueError): event_scheduler.enter_recurring(0, -1, insert_into_list, ('A', result_list)) # priority > sys.maxsize with self.assertRaises(ValueError): event_scheduler.enter(0, sys.maxsize, insert_into_list, ('A', result_list)) with self.assertRaises(ValueError): event_scheduler.enterabs(0, sys.maxsize, insert_into_list, ('A', result_list)) with self.assertRaises(ValueError): event_scheduler.enter_recurring(0, sys.maxsize, insert_into_list, ('A', result_list))
def test_cancel_event_after_execution(self): event_scheduler = EventScheduler(TEST_THREAD, TestTimer.monotonic, TestTimer) TestTimer.set_event_scheduler(event_scheduler) event_scheduler.start() result_list = [] event = event_scheduler.enter(2, 4, insert_into_list, (), { 'item': 'B', 'list_obj': result_list }) TestTimer.advance_time(2.5) # cancelling an already executed event should be a no-op event_scheduler.cancel(event) self.assertListEqual(result_list, ['B']) event_scheduler.stop(True)