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_not_running(self, info_mock, configure_mock):
        config_mock = Mock(None)
        configure_mock.return_value = Mock(None), Mock(None), Mock(None)

        np.main_loop(config_mock)

        info_mock.assert_called_once_with('Stop application loop.')
Example #3
0
    def test_main_loop_not_if_task(self, done_with_processed_tasks, tarantool_queue, 
                    gevent_queue, gevent_pool, gevent_sleep, logger):
        config_mock = mock.MagicMock()
        config_mock.QUEUE_HOST = 1
        config_mock.QUEUE_PORT = 1
        config_mock.QUEUE_SPACE = 1
        config_mock.QUEUE_TUBE = 1
        config_mock.SLEEP = 1
        config_mock.WORKER_POOL_SIZE = 1

        #STOP WHILE
        def change_state(*args, **kwargs):
            notification_pusher.run_application = False
        gevent_sleep.side_effect = change_state

        worker_pool_mock = mock.MagicMock()
        worker_pool_mock.free_count.return_value = 1

        tube_mock = mock.MagicMock()
        tube_mock.take.return_value = False

        queue = Mock()
        queue.tube.return_value = tube_mock

        tarantool_queue.return_value = queue

        gevent_pool.return_value = worker_pool_mock

        processed_task_mock = Mock()
        gevent_queue.return_value = processed_task_mock

        notification_pusher.main_loop(config_mock)
        done_with_processed_tasks.assert_called_once_with(processed_task_mock)

        notification_pusher.run_application = True
 def test_main_mainloop_success(self):
     config = self.make_config()
     args = ['','-c', './source/tests/test_config']
     with mock.patch('source.notification_pusher.load_config_from_pyfile', Mock(return_value=config)):
         with patch('source.notification_pusher.main_loop', Mock(side_effect=stop_app())):
             notification_pusher.main(args)
             notification_pusher.main_loop(config)
 def test_mainloop_config_fail(self):
     from tarantool_queue import Queue
     config = self.make_config()
     config.QUEUE_HOST = ""
     patch('gevent.queue.Queue', Mock())
     with self.assertRaises(Queue.BadConfigException):
         notification_pusher.main_loop(config)
	def test_main_loop_stopped(self, m_gevent_queue, m_pool, m_queue):
		notification_pusher.run_application = False
		
		notification_pusher.main_loop(self.config)
	
		m_queue.assert_called_once_with(
			host=self.config.QUEUE_HOST, 
			port=self.config.QUEUE_PORT,
			space=self.config.QUEUE_SPACE
		)
		m_pool.assert_called_once_with(self.config.WORKER_POOL_SIZE)
		m_gevent_queue.assert_called_once()
		self.logger.info.assert_called_with('Stop application loop.')
Example #7
0
    def test_main_loop_run_app_with_task(self):
        config = mock.MagicMock()
        config.QUEUE_HOST = 'queue_host'
        config.QUEUE_PORT = 8080
        config.QUEUE_SPACE = 1
        config.QUEUE_TUBE = 'queue_tube'
        config.QUEUE_TAKE_TIMEOUT = 1
        config.WORKER_POOL_SIZE = 1
        config.SLEEP = 1
        config.HTTP_CONNECTION_TIMEOUT = 2

        task = mock.MagicMock()
        tube = mock.MagicMock()
        tube.take = mock.Mock(return_value=task)
        queue = mock.MagicMock()
        queue.tube = mock.Mock(return_value=tube)

        processed_task_queue = mock.MagicMock()

        worker_pool = mock.Mock()
        worker_pool.free_count = mock.Mock(return_value=1)

        mock_done_with_processed_tasks = mock.Mock()
        mock_logger = mock.Mock()
        mock_sleep = mock.Mock(side_effect=stop_main_loop)

        with mock.patch("notification_pusher.logger", mock_logger):
            with mock.patch("notification_pusher.tarantool_queue.Queue",
                            mock.Mock(return_value=queue)):
                with mock.patch("notification_pusher.Pool",
                                mock.Mock(return_value=worker_pool)):
                    with mock.patch(
                            "notification_pusher.gevent_queue.Queue",
                            mock.Mock(return_value=processed_task_queue)):
                        with mock.patch(
                                "notification_pusher.done_with_processed_tasks",
                                mock_done_with_processed_tasks):
                            with mock.patch("notification_pusher.sleep",
                                            mock_sleep):
                                with mock.patch(
                                        "notification_pusher.run_application",
                                        True):
                                    notification_pusher.main_loop(config)

        worker_pool.free_count.assert_called_once_with()
        tube.take.assert_called_once_with(config.QUEUE_TAKE_TIMEOUT)
        mock_done_with_processed_tasks.assert_called_once_with(
            processed_task_queue)
        mock_sleep.assert_called_once_with(config.SLEEP)
        self.assertTrue(mock_logger.debug.call_count == 2)
        self.assertTrue(worker_pool.add.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_main_loop_run_app_with_task(self):
        config = mock.MagicMock()
        config.QUEUE_HOST = 'queue_host'
        config.QUEUE_PORT = 8080
        config.QUEUE_SPACE = 1
        config.QUEUE_TUBE = 'queue_tube'
        config.QUEUE_TAKE_TIMEOUT = 1
        config.WORKER_POOL_SIZE = 1
        config.SLEEP = 1
        config.HTTP_CONNECTION_TIMEOUT = 2

        task = mock.MagicMock()
        tube = mock.MagicMock()
        tube.take = mock.Mock(return_value=task)
        queue = mock.MagicMock()
        queue.tube = mock.Mock(return_value=tube)

        processed_task_queue = mock.MagicMock()

        worker_pool = mock.Mock()
        worker_pool.free_count = mock.Mock(return_value=1)

        mock_done_with_processed_tasks = mock.Mock()
        mock_logger = mock.Mock()
        mock_sleep = mock.Mock(side_effect=stop_main_loop)

        with mock.patch("notification_pusher.logger", mock_logger):
            with mock.patch("notification_pusher.tarantool_queue.Queue", mock.Mock(return_value=queue)):
                with mock.patch("notification_pusher.Pool", mock.Mock(return_value=worker_pool)):
                    with mock.patch("notification_pusher.gevent_queue.Queue", mock.Mock(return_value=processed_task_queue)):
                        with mock.patch("notification_pusher.done_with_processed_tasks", mock_done_with_processed_tasks):
                            with mock.patch("notification_pusher.sleep", mock_sleep):
                                with mock.patch("notification_pusher.run_application", True):
                                    notification_pusher.main_loop(config)

        worker_pool.free_count.assert_called_once_with()
        tube.take.assert_called_once_with(config.QUEUE_TAKE_TIMEOUT)
        mock_done_with_processed_tasks.assert_called_once_with(processed_task_queue)
        mock_sleep.assert_called_once_with(config.SLEEP)
        self.assertTrue(mock_logger.debug.call_count == 2)
        self.assertTrue(worker_pool.add.called)
	def test_main_loop_run(self, m_gevent_queue, m_pool, m_queue):
		notification_pusher.run_application = True
		free_workers_count = 5
		
		m_pool().free_count = mock.Mock(return_value=free_workers_count) 

		class WorkerTask:
			def __init__(self, task_id):
				self.task_id = task_id

		m_queue().tube = mock.Mock()
		m_queue().tube().take = mock.Mock(return_value=WorkerTask(task_id=42))

		def stop_application(*args, **kwargs):
			notification_pusher.run_application = False

		with mock.patch('notification_pusher.sleep', mock.Mock(side_effect=stop_application)):
			with mock.patch('notification_pusher.done_with_processed_tasks', mock.Mock()):
				notification_pusher.main_loop(self.config)	

		self.assertEquals(m_queue().tube().take.call_count, free_workers_count)
		self.assertEquals(m_pool().add.call_count, free_workers_count)
    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)