def test_get_last_execute(self): execute_mock = Mock() self.trackers_manager.execute = execute_mock clients_manager = ClientsManager({}) self.TestDatetime.mock_now = datetime.now(pytz.utc) with patch('monitorrent.engine.datetime', self.TestDatetime(2015, 8, 28)): engine_runner = DBEngineRunner(Logger(), self.trackers_manager, clients_manager) self.assertIsNone(engine_runner.last_execute) engine_runner.execute(None) sleep(0.1) engine_runner.stop() engine_runner.join(1) self.assertFalse(engine_runner.is_alive()) engine_runner = DBEngineRunner(Logger(), self.trackers_manager, clients_manager) self.assertEqual(self.TestDatetime.mock_now, engine_runner.last_execute) engine_runner.stop() engine_runner.join(1) self.assertFalse(engine_runner.is_alive())
class DBEngineRunnerTest(DbTestCase, WithEngineRunnerTest): def setUp(self): super(DBEngineRunnerTest, self).setUp() self.create_trackers_manager() def create_runner(self, logger=None): self.clients_manager = ClientsManager({}) self.engine_runner = DBEngineRunner(Logger() if logger is None else logger, self.trackers_manager, self.clients_manager) @data(10, 200, 3600, 7200) def test_set_interval(self, value): self.create_runner() self.assertEqual(7200, self.engine_runner.interval) self.engine_runner.interval = value self.stop_runner() self.create_runner() self.assertEqual(value, self.engine_runner.interval) self.stop_runner() class TestDatetime(datetime): mock_now = None @classmethod def now(cls, tz=None): return cls.mock_now def test_get_last_execute(self): self.TestDatetime.mock_now = datetime.now(pytz.utc) with patch('monitorrent.engine.datetime', self.TestDatetime(2015, 8, 28)): self.create_runner() self.assertIsNone(self.engine_runner.last_execute) self.engine_runner.execute(None) sleep(0.1) self.stop_runner() self.create_runner() self.assertEqual(self.TestDatetime.mock_now, self.engine_runner.last_execute) self.stop_runner() @data(10, 200, 3600, 7200) @patch('monitorrent.engine.timer') def test_interval_set_should_update_timer_and_persisted_value(self, expected_value, create_timer_mock): # arrange cancel_mock = Mock() create_timer_mock.return_value = cancel_mock self.create_runner() # act self.engine_runner.interval = expected_value self.stop_runner() # assert self.assertEqual(2, create_timer_mock.call_count) self.assertEqual(2, cancel_mock.call_count) with DBSession() as db: settings = db.query(ExecuteSettings).first() self.assertEqual(settings.interval, expected_value) def test_last_execute_set_should_update_persisted_value(self): # arrange last_execute_expected = datetime.now(pytz.utc) self.create_runner() self.assertIsNone(self.engine_runner.last_execute) # act self.engine_runner.last_execute = last_execute_expected self.stop_runner() # assert with DBSession() as db: settings = db.query(ExecuteSettings).first() self.assertEqual(settings.last_execute, last_execute_expected) def test_manual_execute_shouldnt_reset_timeout_for_whole_execute(self): executed = Event() # noinspection PyUnusedLocal def execute(*args, **kwargs): executed.set() execute_mock = Mock(side_effect=execute) self.trackers_manager.execute = execute_mock with DBSession() as db: db.add(ExecuteSettings(interval=1, last_execute=None)) self.create_runner() sleep(0.5) # start manual self.engine_runner.execute([1, 2, 3]) executed.wait(0.3) executed.clear() sleep(0.5) executed.wait(0.3) self.assertTrue(executed.is_set) self.stop_runner() self.assertEqual(2, execute_mock.call_count)
class DBEngineRunnerTest(DbTestCase, WithEngineRunnerTest): def setUp(self): super(DBEngineRunnerTest, self).setUp() self.create_trackers_manager() def create_runner(self, logger=None): self.settings_manager = Mock() self.clients_manager = ClientsManager({}) self.notifier_manager = NotifierManager(self.settings_manager, {}) # noinspection PyTypeChecker self.engine_runner = DBEngineRunner( Logger() if logger is None else logger, self.settings_manager, self.trackers_manager, self.clients_manager, self.notifier_manager) @data(10, 200, 3600, 7200) def test_set_interval(self, value): self.create_runner() self.assertEqual(7200, self.engine_runner.interval) self.engine_runner.interval = value self.stop_runner() self.create_runner() self.assertEqual(value, self.engine_runner.interval) self.stop_runner() class TestDatetime(datetime): mock_now = None @classmethod def now(cls, tz=None): return cls.mock_now def test_get_last_execute(self): self.TestDatetime.mock_now = datetime.now(pytz.utc) with patch('monitorrent.engine.datetime', self.TestDatetime(2015, 8, 28)): self.create_runner() self.assertIsNone(self.engine_runner.last_execute) self.engine_runner.execute(None) sleep(0.1) self.stop_runner() self.create_runner() self.assertEqual(self.TestDatetime.mock_now, self.engine_runner.last_execute) self.stop_runner() @data(10, 200, 3600, 7200) @patch('monitorrent.engine.timer') def test_interval_set_should_update_timer_and_persisted_value( self, expected_value, create_timer_mock): # arrange cancel_mock = Mock() create_timer_mock.return_value = cancel_mock self.create_runner() # act self.engine_runner.interval = expected_value self.stop_runner() # assert self.assertEqual(2, create_timer_mock.call_count) self.assertEqual(2, cancel_mock.call_count) with DBSession() as db: settings = db.query(ExecuteSettings).first() self.assertEqual(settings.interval, expected_value) def test_last_execute_set_should_update_persisted_value(self): # arrange last_execute_expected = datetime.now(pytz.utc) self.create_runner() self.assertIsNone(self.engine_runner.last_execute) # act self.engine_runner.last_execute = last_execute_expected self.stop_runner() # assert with DBSession() as db: settings = db.query(ExecuteSettings).first() self.assertEqual(settings.last_execute, last_execute_expected) def test_manual_execute_shouldnt_reset_timeout_for_whole_execute(self): executed = Event() # noinspection PyUnusedLocal def execute(*args, **kwargs): executed.set() execute_mock = Mock(side_effect=execute) mock_tracker = Mock() mock_tracker.get_topics = Mock(return_value=[Topic()]) mock_tracker.execute = execute_mock self.trackers_manager.trackers = {'mock.tracker': mock_tracker} with DBSession() as db: db.add(ExecuteSettings(interval=1, last_execute=None)) self.create_runner() sleep(0.5) # start manual self.engine_runner.execute([1, 2, 3]) executed.wait(0.3) executed.clear() sleep(0.5) executed.wait(0.3) self.assertTrue(executed.is_set) self.stop_runner() self.assertEqual(2, execute_mock.call_count)