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_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_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)