Exemplo n.º 1
0
    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())
Exemplo n.º 2
0
    def test_exeption_in_finally_execute(self):
        waiter = Event()

        # noinspection PyUnusedLocal
        def execute(*args, **kwargs):
            waiter.set()

        execute_mock = Mock(side_effect=execute)
        self.trackers_manager.execute = execute_mock
        clients_manager = ClientsManager({})
        logger = Logger()
        logger.finished = Mock(side_effect=Exception("Failed to save"))
        engine_runner = EngineRunner(logger,
                                     self.trackers_manager,
                                     clients_manager,
                                     interval=0.1)
        waiter.wait(1)
        self.assertTrue(waiter.is_set)
        self.assertTrue(engine_runner.is_alive())

        engine_runner.stop()
        engine_runner.join(1)
        self.assertFalse(engine_runner.is_alive())

        self.assertEqual(1, execute_mock.call_count)
Exemplo n.º 3
0
    def test_manual_execute_with_ids_ignored_while_in_execute(self):
        waiter = Event()

        long_execute_waiter = Event()

        # noinspection PyUnusedLocal
        def execute(*args, **kwargs):
            waiter.set()
            long_execute_waiter.wait(1)
            self.assertTrue(long_execute_waiter.is_set)

        execute_mock = Mock(side_effect=execute)
        self.trackers_manager.execute = execute_mock
        clients_manager = ClientsManager({})
        engine_runner = EngineRunner(Logger(),
                                     self.trackers_manager,
                                     clients_manager,
                                     interval=1)
        engine_runner.execute(None)
        waiter.wait(0.3)
        waiter.clear()
        ids = [1, 2, 3]
        engine_runner.execute(ids)
        long_execute_waiter.set()
        waiter.wait(0.3)
        self.assertTrue(waiter.is_set)
        engine_runner.stop()
        engine_runner.join(1)
        self.assertFalse(engine_runner.is_alive())

        execute_mock.assert_called_once_with(ANY, None)
Exemplo n.º 4
0
    def test_update_interval_during_execute(self, test_interval):
        waiter = Event()
        scope = self.Bunch()
        scope.first_execute = True

        # noinspection PyUnusedLocal
        def execute(*args, **kwargs):
            engine_runner.interval = test_interval
            if scope.first_execute:
                scope.start = time()
                scope.first_execute = False
            else:
                scope.end = time()
                waiter.set()

        execute_mock = Mock(side_effect=execute)
        self.trackers_manager.execute = execute_mock
        clients_manager = ClientsManager({})
        engine_runner = EngineRunner(Logger(),
                                     self.trackers_manager,
                                     clients_manager,
                                     interval=0.1)
        waiter.wait(2)
        self.assertTrue(waiter.is_set)
        engine_runner.stop()
        engine_runner.join(1)
        self.assertFalse(engine_runner.is_alive())

        self.assertEqual(2, execute_mock.call_count)

        # noinspection PyUnresolvedReferences
        delta = scope.end - scope.start

        self.assertLessEqual(abs(delta - test_interval), 0.02)
Exemplo n.º 5
0
    def test_stop_after_multiple_execute(self, value):
        waiter = Event()
        scope = self.Bunch()
        scope.execute_count = 0

        # noinspection PyUnusedLocal
        def execute(*args, **kwargs):
            scope.execute_count += 1
            if scope.execute_count < value:
                return
            waiter.set()

        execute_mock = Mock(side_effect=execute)
        self.trackers_manager.execute = execute_mock
        clients_manager = ClientsManager({})
        engine_runner = EngineRunner(Logger(),
                                     self.trackers_manager,
                                     clients_manager,
                                     interval=0.1)
        waiter.wait(2)
        self.assertTrue(waiter.is_set)
        engine_runner.stop()
        engine_runner.join(1)
        self.assertFalse(engine_runner.is_alive())

        self.assertEqual(value, execute_mock.call_count)
Exemplo n.º 6
0
 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)
Exemplo n.º 7
0
 def create_runner(self, logger=None, interval=0.1):
     self.settings_manager = Mock()
     self.clients_manager = ClientsManager({})
     self.notifier_manager = NotifierManager(self.settings_manager, {})
     self.engine_runner = EngineRunner(
         Logger() if logger is None else logger,
         self.settings_manager,
         self.trackers_manager,
         self.clients_manager,
         self.notifier_manager,
         interval=interval)
Exemplo n.º 8
0
    def test_exeption_in_finally_execute(self):
        waiter = Event()

        # noinspection PyUnusedLocal
        def execute(*args, **kwargs):
            waiter.set()

        execute_mock = Mock(side_effect=execute)
        self.trackers_manager.execute = execute_mock

        logger = Logger()
        logger.finished = Mock(side_effect=Exception("Failed to save"))

        self.create_runner(logger=logger)
        waiter.wait(1)
        self.assertTrue(waiter.is_set)
        self.assertTrue(self.engine_runner.is_alive())

        self.stop_runner()

        self.assertEqual(1, execute_mock.call_count)
Exemplo n.º 9
0
    def test_exeption_in_finally_execute(self):
        waiter = Event()

        # noinspection PyUnusedLocal
        def execute(*args, **kwargs):
            waiter.set()

        execute_mock = Mock(side_effect=execute)
        self.trackers_manager.execute = execute_mock
        clients_manager = ClientsManager({})
        logger = Logger()
        logger.finished = Mock(side_effect=Exception("Failed to save"))
        engine_runner = EngineRunner(logger, self.trackers_manager, clients_manager, interval=0.1)
        waiter.wait(1)
        self.assertTrue(waiter.is_set)
        self.assertTrue(engine_runner.is_alive())

        engine_runner.stop()
        engine_runner.join(1)
        self.assertFalse(engine_runner.is_alive())

        self.assertEqual(1, execute_mock.call_count)
Exemplo n.º 10
0
    def test_set_interval(self, value):
        execute_mock = Mock()
        self.trackers_manager.execute = execute_mock
        clients_manager = ClientsManager({})
        engine_runner = DBEngineRunner(Logger(), self.trackers_manager,
                                       clients_manager)

        self.assertEqual(7200, engine_runner.interval)

        engine_runner.interval = value

        engine_runner.stop()
        engine_runner.join(1)
        self.assertFalse(engine_runner.is_alive())
        engine_runner = DBEngineRunner(Logger(), self.trackers_manager,
                                       clients_manager)

        self.assertEqual(value, engine_runner.interval)

        engine_runner.stop()
        engine_runner.join(1)
        self.assertFalse(engine_runner.is_alive())
Exemplo n.º 11
0
    def test_stop_bofore_execute(self):
        execute_mock = MagicMock()
        self.trackers_manager.execute = execute_mock
        clients_manager = ClientsManager({})
        engine_runner = EngineRunner(Logger(),
                                     self.trackers_manager,
                                     clients_manager,
                                     interval=0.1)
        engine_runner.stop()
        engine_runner.join(1)
        self.assertFalse(engine_runner.is_alive())

        execute_mock.assert_not_called()
Exemplo n.º 12
0
    def setUp(self):
        super(EngineTest, self).setUp()

        self.log_mock = Logger()
        self.log_info_mock = MagicMock()
        self.log_downloaded_mock = MagicMock()
        self.log_failed_mock = MagicMock()

        self.log_mock.info = self.log_info_mock
        self.log_mock.downloaded = self.log_downloaded_mock
        self.log_mock.failed = self.log_failed_mock

        self.clients_manager = ClientsManager()
        self.engine = Engine(self.log_mock, self.clients_manager)
Exemplo n.º 13
0
    def test_exeption_in_finally_execute(self):
        waiter = Event()

        # noinspection PyUnusedLocal
        def execute(*args, **kwargs):
            waiter.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}

        logger = Logger()
        logger.finished = Mock(side_effect=Exception("Failed to save"))

        self.create_runner(logger=logger)
        self.assertTrue(waiter.wait(1))
        self.assertTrue(self.engine_runner.is_alive())

        self.stop_runner()

        self.assertEqual(1, execute_mock.call_count)
Exemplo n.º 14
0
    def setUp(self):
        super(EngineTest, self).setUp()

        self.log_mock = Logger()
        self.log_info_mock = MagicMock()
        self.log_downloaded_mock = MagicMock()
        self.log_failed_mock = MagicMock()

        self.log_mock.info = self.log_info_mock
        self.log_mock.downloaded = self.log_downloaded_mock
        self.log_mock.failed = self.log_failed_mock

        self.clients_manager = ClientsManager()
        self.settings_manager = Mock()
        self.trackers_manager = TrackersManager(self.settings_manager, {})
        self.notifier_manager = NotifierManager({})
        self.engine = Engine(self.log_mock, self.settings_manager,
                             self.trackers_manager, self.clients_manager,
                             self.notifier_manager)
Exemplo n.º 15
0
    def test_stop_after_execute(self):
        waiter = Event()

        # noinspection PyUnusedLocal
        def execute(*args, **kwargs):
            waiter.set()

        execute_mock = Mock(side_effect=execute)
        self.trackers_manager.execute = execute_mock
        clients_manager = ClientsManager({})
        engine_runner = EngineRunner(Logger(),
                                     self.trackers_manager,
                                     clients_manager,
                                     interval=0.1)
        waiter.wait(1)
        self.assertTrue(waiter.is_set)
        engine_runner.stop()
        engine_runner.join(1)
        self.assertFalse(engine_runner.is_alive())

        self.assertEqual(1, execute_mock.call_count)
class EngineMock(object):
    log = Logger()

    def add_torrent(self, filename, torrent, old_hash):
        return datetime.datetime.now(pytz.utc)