def test_run_with_network_positive_workers_count(self):
     with mock.patch('source.redirect_checker.spawn_workers') as workers:
         child = mock.Mock()
         redirect_checker.active_children = lambda: [child]
         redirect_checker.main_loop(self.config)
         assert redirect_checker.sleep.called
         self.assertEqual(workers.call_count, 1, 'only one iteration')
    def test_main_loop_network_status_is_not_fine(self):
        pid = 23
        config = Config()
        config.SLEEP = 8
        config.CHECK_URL = 'test_url'
        config.HTTP_TIMEOUT = 1
        config.WORKER_POOL_SIZE = 2
        mock_spawn_workers = mock.Mock()
        mock_check_network_status = mock.Mock(return_value=False)

        def break_run(*args, **kwargs):
            redirect_checker.run_checker = False

        test_active_children = mock.Mock()
        mock_sleep = mock.Mock(side_effect=break_run)
        with mock.patch('source.redirect_checker.check_network_status', mock_check_network_status):
            with mock.patch('os.getpid', mock.Mock(return_value=pid)):
                with mock.patch('source.redirect_checker.active_children',
                                mock.Mock(return_value=[test_active_children])):
                        with mock.patch('source.redirect_checker.sleep', mock_sleep):
                            redirect_checker.main_loop(config)
        assert mock_spawn_workers.call_count == 0
        test_active_children.terminate.assert_called_once()
        mock_sleep.assert_called_once_with(config.SLEEP)
        redirect_checker.run_checker = True
    def test_main_loop_dfs(self):
        """
        try to go deeper
        """
        pid = 42
        config = Config()
        config.SLEEP = 1
        config.CHECK_URL = 'url'
        config.HTTP_TIMEOUT = 1
        config.WORKER_POOL_SIZE = 50
        mock_spawn_workers = mock.Mock()
        mock_check_network_status = mock.Mock(return_value=True)

        children = TestChildren(1)
        count = config.WORKER_POOL_SIZE - len(children())
        mock_sleep = mock.Mock(side_effect=stop_loop)
        with mock.patch('source.redirect_checker.check_network_status', mock_check_network_status),\
             mock.patch('os.getpid', mock.Mock(return_value=pid)),\
             mock.patch('source.redirect_checker.spawn_workers', mock_spawn_workers),\
             mock.patch('source.redirect_checker.active_children', children),\
             mock.patch('source.redirect_checker.sleep', mock_sleep):
                            redirect_checker.main_loop(config)
        assert mock_spawn_workers.call_args[1]['num'] == count
        assert mock_spawn_workers.call_args[1]['parent_pid'] == pid
        redirect_checker.run_checker = True
Exemplo n.º 4
0
    def test_spawn_worker_called_if_network_status_is_true_and_active_children_count_greater_than_worker_pool_size(
            self):
        parent_pid = 1
        network_status = True
        active_children_count = 16
        active_child = mock.MagicMock()
        active_child.terminate = mock.Mock()
        active_children = [active_child] * active_children_count
        config = mock.MagicMock()
        config.WORKER_POOL_SIZE = 10
        config.SLEEP = 1

        # spawn_workers_mock = mock.Mock()

        def break_run(*args, **kwargs):
            redirect_checker.is_running = False

        with mock.patch('source.redirect_checker.check_network_status',
                        mock.Mock(return_value=network_status)):
            with mock.patch('os.getpid', mock.Mock(return_value=parent_pid)):
                with mock.patch('source.redirect_checker.active_children',
                                mock.Mock(return_value=active_children)):
                    with mock.patch('source.redirect_checker.spawn_workers',
                                    mock.Mock()) as spawn_workers_mock:
                        with mock.patch('source.redirect_checker.sleep',
                                        mock.Mock(side_effect=break_run)):
                            redirect_checker.main_loop(config)
        self.assertEqual(0, spawn_workers_mock.call_count)
        redirect_checker.is_running = True
    def test_main_loop_network_status_is_fine_and_workers_exist(self):
        pid = 23
        config = Config()
        config.SLEEP = 8
        config.CHECK_URL = 'test_url'
        config.HTTP_TIMEOUT = 1
        config.WORKER_POOL_SIZE = 50
        mock_spawn_workers = mock.Mock()
        mock_check_network_status = mock.Mock(return_value=True)

        def break_run(*args, **kwargs):
            redirect_checker.run_checker = False

        active_children = TestActiveChildren(2)
        required_workers_count = config.WORKER_POOL_SIZE - len(active_children())
        mock_sleep = mock.Mock(side_effect=break_run)
        with mock.patch('source.redirect_checker.check_network_status', mock_check_network_status):
            with mock.patch('os.getpid', mock.Mock(return_value=pid)):
                with mock.patch('source.redirect_checker.active_children', active_children):
                    with mock.patch('source.redirect_checker.spawn_workers', mock_spawn_workers):
                        with mock.patch('source.redirect_checker.sleep', mock_sleep):
                            redirect_checker.main_loop(config)
        num = mock_spawn_workers.call_args[1]['num']
        parent_id = mock_spawn_workers.call_args[1]['parent_pid']
        mock_sleep.assert_called_once_with(config.SLEEP)
        mock_spawn_workers.assert_called_once()
        assert num == required_workers_count
        assert pid == parent_id
        redirect_checker.run_checker = True
Exemplo n.º 6
0
    def test_spawn_worker_called_if_network_status_is_true(self):
        parent_pid = 1
        network_status = True
        active_children_count = 6
        active_child = mock.MagicMock()
        active_child.terminate = mock.Mock()
        active_children = [active_child] * active_children_count
        config = mock.MagicMock()
        config.WORKER_POOL_SIZE = 10
        config.SLEEP = 1

        # spawn_workers_mock = mock.Mock()

        def break_run(*args, **kwargs):
            redirect_checker.is_running = False

        with mock.patch('source.redirect_checker.check_network_status',
                        mock.Mock(return_value=network_status)):
            with mock.patch('os.getpid', mock.Mock(return_value=parent_pid)):
                with mock.patch('source.redirect_checker.active_children',
                                mock.Mock(return_value=active_children)):
                    with mock.patch('source.redirect_checker.spawn_workers',
                                    mock.Mock()) as spawn_workers_mock:
                        with mock.patch('source.redirect_checker.sleep',
                                        mock.Mock(side_effect=break_run)):
                            redirect_checker.main_loop(config)
        spawn_workers_mock.assert_called_once_with(
            num=config.WORKER_POOL_SIZE - active_children_count,
            target=mock.ANY,
            args=mock.ANY,
            parent_pid=parent_pid)
        redirect_checker.is_running = True
Exemplo n.º 7
0
    def test_active_children_terminated_if_network_status_is_false(self):
        parent_pid = 1
        network_status = False

        config = mock.MagicMock()
        config.WORKER_POOL_SIZE = 10
        config.SLEEP = 1

        active_children_count = 6
        active_child = mock.MagicMock()
        active_child.terminate = mock.Mock()
        active_children = [active_child] * active_children_count

        def break_run(*args, **kwargs):
            redirect_checker.is_running = False

        with mock.patch('source.redirect_checker.active_children',
                        mock.Mock(return_value=active_children)):
            with mock.patch('source.redirect_checker.check_network_status',
                            mock.Mock(return_value=network_status)):
                with mock.patch('os.getpid',
                                mock.Mock(return_value=parent_pid)):
                    with mock.patch('source.redirect_checker.sleep',
                                    mock.Mock(side_effect=break_run)):
                        redirect_checker.main_loop(config)
        for c in active_children:
            c.terminate.assert_call_once()
        redirect_checker.is_running = True
 def test_run_with_network_negative_workers_cnt(self):
     with mock.patch('source.redirect_checker.spawn_workers') as workers:
         self.config.WORKER_POOL_SIZE = 1
         redirect_checker.active_children = lambda: [1, 2, 3]
         redirect_checker.main_loop(self.config)
         assert redirect_checker.sleep.called
         assert not workers.called, 'no workers can be instantiated'
 def test_run_without_network(self):
     child = mock.Mock()
     child_count = 12
     redirect_checker.active_children = lambda: [child] * child_count
     redirect_checker.main_loop(self.config)
     self.assertEqual(child.terminate.call_count, child_count, 'all childs must terminate')
     assert redirect_checker.sleep.called
 def test_main_loop_with_network_status_fail_and_normal_worker_counts(self):
     pid = 42
     children_mock = mock.Mock()
     with mock.patch('os.getpid', mock.Mock(return_value=pid)):
         with mock.patch('source.redirect_checker.active_children', mock.Mock(return_value=[children_mock])):
             with mock.patch('source.redirect_checker.check_network_status', mock.Mock(return_value=False)):
                 redirect_checker.main_loop(self.config)
     assert children_mock.terminate.called
 def test_main_loop_with_network_and_not_normal_worker_counts(self):
     pid = 42
     children_mock = mock.Mock()
     setattr(self.config, "WORKER_POOL_SIZE", 0)
     with mock.patch('os.getpid', mock.Mock(return_value=pid)):
         with mock.patch('source.redirect_checker.active_children', mock.Mock(return_value=[children_mock])):
             with mock.patch('source.redirect_checker.check_network_status', mock.Mock(return_value=True)):
                 with mock.patch('source.redirect_checker.spawn_workers', mock.Mock()) as workd:
                     redirect_checker.main_loop(self.config)
     assert not workd.called
 def test_main_loop_with_network_status_fail_and_normal_worker_counts(self):
     pid = 42
     children_mock = mock.Mock()
     with mock.patch('os.getpid', mock.Mock(return_value=pid)):
         with mock.patch('source.redirect_checker.active_children',
                         mock.Mock(return_value=[children_mock])):
             with mock.patch('source.redirect_checker.check_network_status',
                             mock.Mock(return_value=False)):
                 redirect_checker.main_loop(self.config)
     assert children_mock.terminate.called
 def test_redirect_checker_mainloop_network_is_down(self):
     """
     тестирование основного цикла с выключенной сетью
     :return:
     """
     my_active_child = mock.Mock()
     with mock.patch('source.redirect_checker.active_children', mock.Mock(return_value=[my_active_child])):
         self.config.WORKER_POOL_SIZE = 10
         redirect_checker.main_loop(self.config)
         assert my_active_child.terminate.called
         assert redirect_checker.sleep.called
 def test_redirect_checker_mainloop_network_is_up_required_worker_0(self):
     """
     тестирование основного цикла с включенной сетью
     required_workers_count <= 0
     :return:
     """
     with mock.patch('source.redirect_checker.spawn_workers', mock.Mock()) as spawn_worwkers:
         self.config.WORKER_POOL_SIZE = 1
         redirect_checker.main_loop(self.config)
         assert redirect_checker.sleep.called
         assert spawn_worwkers.not_called
    def test_main_loop_check_network_status_fail(self):
        pid = 50
        children_mock = mock.Mock()
        setattr(self.config, "SLEEP", 1)
        setattr(self.config, "WORKER_POOL_SIZE", 10)
        setattr(self.config, "CHECK_URL", "URL")
        setattr(self.config, "HTTP_TIMEOUT", 20)
        with mock.patch('os.getpid', mock.Mock(return_value=pid)):
            with mock.patch('source.redirect_checker.active_children', mock.Mock(return_value=[children_mock])):
                with mock.patch('source.redirect_checker.check_network_status', mock.Mock(return_value=False)):
                    redirect_checker.main_loop(self.config)

        assert children_mock.terminate.called
 def test_main_loop_with_network_and_not_normal_worker_counts(self):
     pid = 42
     children_mock = mock.Mock()
     setattr(self.config, "WORKER_POOL_SIZE", 0)
     with mock.patch('os.getpid', mock.Mock(return_value=pid)):
         with mock.patch('source.redirect_checker.active_children',
                         mock.Mock(return_value=[children_mock])):
             with mock.patch('source.redirect_checker.check_network_status',
                             mock.Mock(return_value=True)):
                 with mock.patch('source.redirect_checker.spawn_workers',
                                 mock.Mock()) as workd:
                     redirect_checker.main_loop(self.config)
     assert not workd.called
    def test_main_loop(self):
        config = mock.Mock()
        config.SLEEP = 42

        def break_run(*args, **kwargs):
            redirect_checker.run_application = False

        with mock.patch('source.redirect_checker.main_loop_iteration', mock.MagicMock()) as main_loop_iter:
            with mock.patch('source.redirect_checker.sleep', mock.Mock(side_effect=break_run)) as main_loop_sleep:
                redirect_checker.main_loop(config)
        self.assertTrue(main_loop_iter.called)
        self.assertEqual(main_loop_iter.call_count, 1)
        main_loop_sleep.assert_called_once_with(config.SLEEP)
    def test_main_loop_check_network_status_ok_with_bad_number_of_workres(self):
        pid = 50
        children_mock = mock.Mock()
        setattr(self.config, "SLEEP", 1)
        setattr(self.config, "WORKER_POOL_SIZE", 0)
        setattr(self.config, "CHECK_URL", "URL")
        setattr(self.config, "HTTP_TIMEOUT", 20)
        with mock.patch('os.getpid', mock.Mock(return_value=pid)):
            with mock.patch('source.redirect_checker.active_children', mock.Mock(return_value=[children_mock])):
                with mock.patch('source.redirect_checker.check_network_status', mock.Mock(return_value=True)):
                    with mock.patch('source.redirect_checker.spawn_workers', mock.Mock()) as spworkd:
                        redirect_checker.main_loop(self.config)

        assert not spworkd.called
 def test_redirect_checker_mainloop_network_is_up_required_worker_not_0(self):
     """
     тестирование основного цикла с включенной сетью
     required_workers_count > 0
     :return:
     """
     pid = 123
     with mock.patch('os.getpid', mock.Mock(return_value=pid)):
         with mock.patch('source.redirect_checker.spawn_workers', mock.Mock()) as spawn_workers:
             self.config.WORKER_POOL_SIZE = 2
             required_workers_count = self.config.WORKER_POOL_SIZE - len(redirect_checker.active_children())
             redirect_checker.main_loop(self.config)
             assert redirect_checker.sleep.called
             spawn_workers.assert_called_once()
             num = spawn_workers.call_args[1]['num']
             parent_id = spawn_workers.call_args[1]['parent_pid']
             assert num == required_workers_count
             assert pid == parent_id
 def test_main_loop_no_workers(self):
     pid = 42
     config = Config()
     config.SLEEP = 8
     config.CHECK_URL = 'url'
     config.HTTP_TIMEOUT = 1
     config.WORKER_POOL_SIZE = 2
     active_children = [mock.Mock() for _ in range(6)]
     mock_spawn_workers = mock.Mock()
     mock_check_network_status = mock.Mock(return_value=True)
     mock_sleep = mock.Mock(side_effect=stop_main_loop)
     with mock.patch('source.redirect_checker.check_network_status', mock_check_network_status),\
              mock.patch('os.getpid', mock.Mock(return_value=pid)),\
              mock.patch('source.redirect_checker.spawn_workers', mock_spawn_workers),\
              mock.patch('source.redirect_checker.active_children', mock.Mock(return_value=active_children)),\
              mock.patch('source.redirect_checker.sleep', mock_sleep):
         redirect_checker.main_loop(config)
     self.assertEqual(mock_spawn_workers.call_count, 0)
     mock_sleep.assert_called_once_with(config.SLEEP)
     redirect_checker.run = True
 def test_main_loop_network_status_is_not_fine(self):
     pid = 42
     config = Config()
     config.SLEEP = 8
     config.CHECK_URL = 'url'
     config.HTTP_TIMEOUT = 1
     config.WORKER_POOL_SIZE = 2
     mock_spawn_workers = mock.Mock()
     mock_check_network_status = mock.Mock(return_value=False)
     test_active_children = mock.Mock()
     mock_sleep = mock.Mock(side_effect=stop_loop)
     with mock.patch('source.redirect_checker.check_network_status', mock_check_network_status),\
          mock.patch('os.getpid', mock.Mock(return_value=pid)),\
          mock.patch('source.redirect_checker.active_children', mock.Mock(return_value=[test_active_children])),\
          mock.patch('source.redirect_checker.sleep', mock_sleep):
         redirect_checker.main_loop(config)
         self.assertEqual(mock_spawn_workers.call_count, 0)
         test_active_children.terminate.assert_called_once()
         mock_sleep.assert_called_once_with(config.SLEEP)
         redirect_checker.run_checker = True
Exemplo n.º 22
0
 def test_main_loop_no_workers(self):
     pid = 42
     config = Config()
     config.SLEEP = 8
     config.CHECK_URL = 'url'
     config.HTTP_TIMEOUT = 1
     config.WORKER_POOL_SIZE = 2
     active_children = [mock.Mock() for _ in range(6)]
     mock_spawn_workers = mock.Mock()
     mock_check_network_status = mock.Mock(return_value=True)
     mock_sleep = mock.Mock(side_effect=stop_main_loop)
     with mock.patch('source.redirect_checker.check_network_status', mock_check_network_status),\
              mock.patch('os.getpid', mock.Mock(return_value=pid)),\
              mock.patch('source.redirect_checker.spawn_workers', mock_spawn_workers),\
              mock.patch('source.redirect_checker.active_children', mock.Mock(return_value=active_children)),\
              mock.patch('source.redirect_checker.sleep', mock_sleep):
         redirect_checker.main_loop(config)
     self.assertEqual(mock_spawn_workers.call_count, 0)
     mock_sleep.assert_called_once_with(config.SLEEP)
     redirect_checker.run = True
Exemplo n.º 23
0
 def test_main_loop_network_status_is_not_fine(self):
     pid = 42
     config = Config()
     config.SLEEP = 8
     config.CHECK_URL = 'url'
     config.HTTP_TIMEOUT = 1
     config.WORKER_POOL_SIZE = 2
     mock_spawn_workers = mock.Mock()
     mock_check_network_status = mock.Mock(return_value=False)
     test_active_children = mock.Mock()
     mock_sleep = mock.Mock(side_effect=stop_main_loop)
     with mock.patch('source.redirect_checker.check_network_status', mock_check_network_status),\
             mock.patch('os.getpid', mock.Mock(return_value=pid)),\
             mock.patch('source.redirect_checker.active_children', mock.Mock(return_value=[test_active_children])),\
             mock.patch('source.redirect_checker.sleep', mock_sleep):
         redirect_checker.main_loop(config)
     self.assertEqual(mock_spawn_workers.call_count, 0)
     self.assertEqual(test_active_children.terminate.call_count, 1,
                      "Expected only one call")
     mock_sleep.assert_called_once_with(config.SLEEP)
     redirect_checker.run = True
Exemplo n.º 24
0
    def test_active_children_terminated_if_network_status_is_false(self):
        parent_pid = 1
        network_status = False

        config = mock.MagicMock()
        config.WORKER_POOL_SIZE = 10
        config.SLEEP = 1

        active_children_count = 6
        active_child = mock.MagicMock()
        active_child.terminate = mock.Mock()
        active_children = [active_child] * active_children_count

        def break_run(*args, **kwargs):
            redirect_checker.is_running = False

        with mock.patch('source.redirect_checker.active_children', mock.Mock(return_value=active_children)):
            with mock.patch('source.redirect_checker.check_network_status', mock.Mock(return_value=network_status)):
                with mock.patch('os.getpid', mock.Mock(return_value=parent_pid)):
                    with mock.patch('source.redirect_checker.sleep', mock.Mock(side_effect=break_run)):
                        redirect_checker.main_loop(config)
        for c in active_children:
            c.terminate.assert_call_once()
        redirect_checker.is_running = True
Exemplo n.º 25
0
 def test_main_loop_dfs(self):
     """
     try to go deeper
     """
     pid = 42
     config = Config()
     config.SLEEP = 1
     config.CHECK_URL = 'url'
     config.HTTP_TIMEOUT = 1
     config.WORKER_POOL_SIZE = 50
     mock_spawn_workers = mock.Mock()
     mock_check_network_status = mock.Mock(return_value=True)
     children = [mock.Mock()]
     count = config.WORKER_POOL_SIZE - len(children)
     mock_sleep = mock.Mock(side_effect=stop_main_loop)
     with mock.patch('source.redirect_checker.check_network_status', mock_check_network_status),\
              mock.patch('os.getpid', mock.Mock(return_value=pid)),\
              mock.patch('source.redirect_checker.spawn_workers', mock_spawn_workers),\
              mock.patch('source.redirect_checker.active_children', mock.Mock(return_value=children)),\
              mock.patch('source.redirect_checker.sleep', mock_sleep):
         redirect_checker.main_loop(config)
     self.assert_(mock_spawn_workers.call_args[1]['num'] == count)
     self.assert_(mock_spawn_workers.call_args[1]['parent_pid'] == pid)
     redirect_checker.run = True
Exemplo n.º 26
0
    def test_spawn_worker_called_if_network_status_is_true(self):
        parent_pid = 1
        network_status = True
        active_children_count = 6
        active_child = mock.MagicMock()
        active_child.terminate = mock.Mock()
        active_children = [active_child] * active_children_count
        config = mock.MagicMock()
        config.WORKER_POOL_SIZE = 10
        config.SLEEP = 1
        # spawn_workers_mock = mock.Mock()

        def break_run(*args, **kwargs):
            redirect_checker.is_running = False

        with mock.patch('source.redirect_checker.check_network_status', mock.Mock(return_value=network_status)):
            with mock.patch('os.getpid', mock.Mock(return_value=parent_pid)):
                with mock.patch('source.redirect_checker.active_children', mock.Mock(return_value=active_children)):
                    with mock.patch('source.redirect_checker.spawn_workers', mock.Mock()) as spawn_workers_mock:
                        with mock.patch('source.redirect_checker.sleep', mock.Mock(side_effect=break_run)):
                            redirect_checker.main_loop(config)
        spawn_workers_mock.assert_called_once_with(num=config.WORKER_POOL_SIZE - active_children_count, target=mock.ANY,
                                                   args=mock.ANY, parent_pid=parent_pid)
        redirect_checker.is_running = True
Exemplo n.º 27
0
    def test_spawn_worker_called_if_network_status_is_true_and_active_children_count_greater_than_worker_pool_size(
            self):
        parent_pid = 1
        network_status = True
        active_children_count = 16
        active_child = mock.MagicMock()
        active_child.terminate = mock.Mock()
        active_children = [active_child] * active_children_count
        config = mock.MagicMock()
        config.WORKER_POOL_SIZE = 10
        config.SLEEP = 1
        # spawn_workers_mock = mock.Mock()

        def break_run(*args, **kwargs):
            redirect_checker.is_running = False

        with mock.patch('source.redirect_checker.check_network_status', mock.Mock(return_value=network_status)):
            with mock.patch('os.getpid', mock.Mock(return_value=parent_pid)):
                with mock.patch('source.redirect_checker.active_children', mock.Mock(return_value=active_children)):
                    with mock.patch('source.redirect_checker.spawn_workers', mock.Mock()) as spawn_workers_mock:
                        with mock.patch('source.redirect_checker.sleep', mock.Mock(side_effect=break_run)):
                            redirect_checker.main_loop(config)
        self.assertEqual(0, spawn_workers_mock.call_count)
        redirect_checker.is_running = True