def test_main_loop_no_task(self):
        config = Mock()
        pool_size = 4

        mocked_pool = Mock()
        mocked_pool.free_count = Mock(return_value=pool_size)

        mocked_done_with_processed_tasks = Mock(side_effect=self.main_loop_done_with_processed_tasks)

        mocked_worker = Mock()
        mocked_gevent_queue = Mock()

        mocked_task = Mock()

        mocked_tube = Mock(return_value=mocked_task)
        mocked_tube.take = Mock(return_value=None)

        mocked_tarantool_queue = Mock()
        mocked_tarantool_queue.tube = Mock(return_value=mocked_tube)

        with patch('notification_pusher.tarantool_queue.Queue', Mock(return_value=mocked_tarantool_queue)):
            with patch('notification_pusher.Pool', Mock(return_value=mocked_pool)):
                with patch('notification_pusher.done_with_processed_tasks', mocked_done_with_processed_tasks):
                    with patch('notification_pusher.Greenlet', Mock(return_value=mocked_worker)):
                        with patch('notification_pusher.gevent_queue.Queue', Mock(return_value=mocked_gevent_queue)):
                            notification_pusher.run_application = True
                            main_loop(config)

                            assert not mocked_worker.start.called
                            mocked_done_with_processed_tasks.assert_called_once_with(mocked_gevent_queue)
    def test_worker_when_no_result_and_no_exception(self, path_exs_m, get_tubes_m, get_redirect_m):
        config = Mock(None)
        config.HTTP_TIMEOUT = 100
        config.MAX_REDIRECTS = 10
        config.USER_AGENT = 'abc'
        config.QUEUE_TAKE_TIMEOUT = 50

        task_mock = Mock(None)
        task_mock.ack = Mock(None)
        in_tube_mock = Mock(None)
        out_tube_mock = Mock(None)
        in_tube_mock.take = Mock(return_value=task_mock)
        in_tube_mock.put = Mock(None)
        out_tube_mock.put = Mock(None)
        path_exs_m.side_effect = [True, False]
        get_redirect_m.return_value = None
        get_tubes_m.return_value = in_tube_mock, out_tube_mock

        wr.worker(config, 666)

        get_tubes_m.assert_called_once_with(config)
        in_tube_mock.take.assert_called_once_with(50)
        get_redirect_m.assert_called_once_with(task_mock, 100, 10, 'abc')
        self.assertEqual(in_tube_mock.put.call_count, 0)
        self.assertEqual(out_tube_mock.put.call_count, 0)
        self.assertTrue(task_mock.ack.called)
    def test_worker_when_no_task_at_all(self, path_exs_m, get_tubes_m, get_redirect_m):
        config = Mock(None)
        config.QUEUE_TAKE_TIMEOUT = 50

        in_tube_mock = Mock(None)
        out_tube_mock = Mock(None)
        in_tube_mock.take = Mock(return_value=None)
        in_tube_mock.put = Mock(None)
        out_tube_mock.put = Mock(None)
        path_exs_m.side_effect = [True, False]
        get_tubes_m.return_value = in_tube_mock, out_tube_mock

        wr.worker(config, 666)

        self.assertFalse(get_redirect_m.called)
    def test_main_loop_when_app_is_running_but_no_tasks(self, add_work_mock, done_mock, configure_mock):
        config_mock = Mock(None)
        config_mock.QUEUE_TAKE_TIMEOUT = 10
        config_mock.SLEEP = 10
        tube_mock = Mock(None)
        tube_mock.take = Mock(return_value=None)
        pool_mock = Mock(None)
        pool_mock.free_count = Mock(return_value=5)
        queue_mock = Mock(None)
        configure_mock.return_value = tube_mock, pool_mock, queue_mock
        sleep_mock = Mock(side_effect=stop_app)

        with patch('notification_pusher.sleep', sleep_mock):
            np.main_loop(config_mock)

        self.assertFalse(add_work_mock.called)
 def test_main_loop_successful_while(self):
     config = self.get_config()
     worker = Mock()
     worker.start = Mock(return_value=1)
     task = Mock()
     task.task_id = Mock(return_value=1)
     tube = Mock()
     tube.take = Mock(return_value=task)
     queue = Mock()
     queue.tube = Mock(return_value=tube)
     with patch('notification_pusher.tarantool_queue.Queue', Mock(return_value=queue)):
         with patch('notification_pusher.Greenlet', Mock(return_value=worker)):
             with patch('notification_pusher.break_func_for_test', Mock(return_value=True)):
                 with patch('notification_pusher.logger', Mock()) as logger:
                     notification_pusher.main_loop(config)
     self.assertTrue(logger.info.called)
    def test_worker_when_no_result_and_exception(self, exc_m, path_exs_m, get_tubes_m, get_redirect_m):
        config = Mock(None)
        config.HTTP_TIMEOUT = 100
        config.MAX_REDIRECTS = 10
        config.USER_AGENT = 'abc'
        config.QUEUE_TAKE_TIMEOUT = 50

        task_mock = Mock(None)
        task_mock.ack = Mock(side_effect=DatabaseError())
        in_tube_mock = Mock(None)
        out_tube_mock = Mock(None)
        in_tube_mock.take = Mock(return_value=task_mock)
        in_tube_mock.put = Mock(None)
        out_tube_mock.put = Mock(None)
        path_exs_m.side_effect = [True, False]
        get_redirect_m.return_value = None
        get_tubes_m.return_value = in_tube_mock, out_tube_mock

        wr.worker(config, 666)

        self.assertTrue(exc_m.called)
    def test_worker_when_result_putting_to_output(self, path_exs_m, get_tubes_m, get_redirect_m):
        config = Mock(None)
        config.HTTP_TIMEOUT = 100
        config.MAX_REDIRECTS = 10
        config.USER_AGENT = 'abc'
        config.QUEUE_TAKE_TIMEOUT = 50

        task_mock = Mock(None)
        task_mock.ack = Mock(None)
        in_tube_mock = Mock(None)
        out_tube_mock = Mock(None)
        in_tube_mock.take = Mock(return_value=task_mock)
        in_tube_mock.put = Mock(None)
        out_tube_mock.put = Mock(None)
        path_exs_m.side_effect = [True, False]
        get_redirect_m.return_value = False, 'data'
        get_tubes_m.return_value = in_tube_mock, out_tube_mock

        wr.worker(config, 666)

        out_tube_mock.put.assert_called_once_with('data')
        self.assertFalse(in_tube_mock.put.called)
    def test_main_loop_when_app_is_running(self, add_work_mock, done_mock, configure_mock):
        config_mock = Mock(None)
        config_mock.QUEUE_TAKE_TIMEOUT = 10
        config_mock.SLEEP = 10
        task_mock = Mock(None)
        tube_mock = Mock(None)
        tube_mock.take = Mock(return_value=task_mock)
        pool_mock = Mock(None)
        pool_mock.free_count = Mock(return_value=5)
        queue_mock = Mock(None)
        configure_mock.return_value = tube_mock, pool_mock, queue_mock
        sleep_mock = Mock(side_effect=stop_app)

        with patch('notification_pusher.sleep', sleep_mock):
            np.main_loop(config_mock)

        configure_mock.assert_called_once_with(config_mock)
        self.assertTrue(pool_mock.free_count.called)
        self.assertEqual(tube_mock.take.call_count, 5)
        tube_mock.take.assert_call_any(config_mock.QUEUE_TAKE_TIMEOUT)
        done_mock.assert_called_once_with(queue_mock)
        sleep_mock.assert_called_once_with(config_mock.SLEEP)