def test_on_task_prerun(self): task = Mock() with self.fixup_context(self.app) as (f, _, _): task.request.is_eager = False with patch.object(f, 'close_database'): f.on_task_prerun(task) f.close_database.assert_called_with() task.request.is_eager = True with patch.object(f, 'close_database'): f.on_task_prerun(task) f.close_database.assert_not_called()
def test_create_broadcast_cursor(self): import pymongo with patch.object(pymongo, 'version_tuple', (2, )): self.channel._create_broadcast_cursor( 'fanout_exchange', 'foo', '*', 'foobar', ) self.assert_collection_accessed('messages.broadcast') self.assert_operation_called_with( 'broadcast', 'find', tailable=True, query={'queue': 'fanout_exchange'}, ) if pymongo.version_tuple >= (3, ): self.channel._create_broadcast_cursor( 'fanout_exchange1', 'foo', '*', 'foobar', ) self.assert_collection_accessed('messages.broadcast') self.assert_operation_called_with( 'broadcast', 'find', cursor_type=pymongo.CursorType.TAILABLE, filter={'queue': 'fanout_exchange1'}, )
def test_open_rc_version(self): import pymongo def server_info(self): return {'version': '3.6.0-rc'} with patch.object(pymongo.MongoClient, 'server_info', server_info): self.channel._open()
def test_on_task_postrun(self): task = Mock() with self.fixup_context(self.app) as (f, _, _): with patch.object(f, 'close_cache'): task.request.is_eager = False with patch.object(f, 'close_database'): f.on_task_postrun(task) f.close_database.assert_called() f.close_cache.assert_called() # when a task is eager, don't close connections with patch.object(f, 'close_cache'): task.request.is_eager = True with patch.object(f, 'close_database'): f.on_task_postrun(task) f.close_database.assert_not_called() f.close_cache.assert_not_called()
def test_task_with_result(self): with patch.object(self.app, 'send_task') as send_task: self.mytask.apply_async() expected_args, expected_kwargs = self.common_send_task_arguments() send_task.assert_called_once_with( *expected_args, **expected_kwargs )
def test_set_same_timeout(self): # Checks that context manager does not set timeout when # it is same as currently set. with patch.object(self.t, 'sock') as sock_mock: sock_mock.gettimeout.return_value = 5 with self.t.having_timeout(5) as actual_sock: assert actual_sock == self.t.sock sock_mock.gettimeout.assert_called() sock_mock.settimeout.assert_not_called()
def test_task_with_result_ignoring_on_call(self): with patch.object(self.app, 'send_task') as send_task: self.mytask.apply_async(ignore_result=True) expected_args, expected_kwargs = self.common_send_task_arguments() expected_kwargs['ignore_result'] = True send_task.assert_called_once_with( *expected_args, **expected_kwargs )
def test_on_worker_process_init(self, patching): with self.fixup_context(self.app) as (f, _, _): with patch('celery.fixups.django._maybe_close_fd') as mcf: _all = f._db.connections.all = Mock() conns = _all.return_value = [ Mock(), Mock(), ] conns[0].connection = None with patch.object(f, 'close_cache'): with patch.object(f, '_close_database'): f.on_worker_process_init() mcf.assert_called_with(conns[1].connection) f.close_cache.assert_called_with() f._close_database.assert_called_with() f.validate_models = Mock(name='validate_models') patching.setenv('FORKED_BY_MULTIPROCESSING', '1') f.on_worker_process_init() f.validate_models.assert_called_with()
def test_method_called(self): from kombu.transport.redis import SentinelChannel with patch.object(SentinelChannel, '_sentinel_managed_pool') as p: connection = Connection( 'sentinel://localhost:65534/', transport_options={ 'master_name': 'not_important', }, ) connection.channel() p.assert_called()
def test_autoretry_backoff_jitter(self, randrange): task = self.autoretry_backoff_jitter_task task.max_retries = 3 task.iterations = 0 with patch.object(task, 'retry', wraps=task.retry) as fake_retry: task.apply(("http://httpbin.org/error",)) assert task.iterations == 4 retry_call_countdowns = [ call[1]['countdown'] for call in fake_retry.call_args_list ] assert retry_call_countdowns == [0, 1, 3, 7]
def test_set_timeout(self): # Checks that context manager sets and reverts timeout properly with patch.object(self.t, 'sock') as sock_mock: sock_mock.gettimeout.return_value = 3 with self.t.having_timeout(5) as actual_sock: assert actual_sock == self.t.sock sock_mock.gettimeout.assert_called() sock_mock.settimeout.assert_has_calls( [ call(5), call(3), ] )
def test_connect_multiple_addr_entries_succeed(self): with patch('socket.socket', return_value=MockSocket()) as sock_mock, \ patch('socket.getaddrinfo', return_value=[ (socket.AF_INET, 1, socket.IPPROTO_TCP, '', ('127.0.0.1', 5672)), (socket.AF_INET, 1, socket.IPPROTO_TCP, '', ('127.0.0.2', 5672)) ]): self.t.sock = Mock() self.t.close() with patch.object(sock_mock.return_value, 'connect', side_effect=(socket.error, None)): self.t.connect()
def test_set_timeout_exception_raised(self): # Checks that context manager sets and reverts timeout properly # when exception is raised. with patch.object(self.t, 'sock') as sock_mock: sock_mock.gettimeout.return_value = 3 with pytest.raises(DummyException): with self.t.having_timeout(5) as actual_sock: assert actual_sock == self.t.sock raise DummyException() sock_mock.gettimeout.assert_called() sock_mock.settimeout.assert_has_calls( [ call(5), call(3), ] )
def test_connect_short_curcuit_on_INET_fails(self): with patch('socket.socket', return_value=MockSocket()) as sock_mock, \ patch('socket.getaddrinfo', side_effect=[ [(socket.AF_INET, 1, socket.IPPROTO_TCP, '', ('127.0.0.1', 5672))], [(socket.AF_INET6, 1, socket.IPPROTO_TCP, '', ('::1', 5672))] ]) as getaddrinfo: self.t.sock = Mock() self.t.close() with patch.object(sock_mock.return_value, 'connect', side_effect=(socket.error, None)): self.t.connect() getaddrinfo.assert_has_calls( [call('localhost', 5672, addr_type, ANY, ANY) for addr_type in (socket.AF_INET, socket.AF_INET6)])
def test_set_timeout_ewouldblock_exc(self): # We expect EWOULDBLOCK to be handled as a timeout. with patch.object(self.t, 'sock') as sock_mock: sock_mock.gettimeout.return_value = 3 with pytest.raises(socket.timeout): with self.t.having_timeout(5): err = socket.error() err.errno = errno.EWOULDBLOCK raise err class DummySocketError(socket.error): pass # Other socket errors shouldn't be converted. with pytest.raises(DummySocketError): with self.t.having_timeout(5): raise DummySocketError()
def test_close_database(self): with self.fixup_context(self.app) as (f, _, _): with patch.object(f, '_close_database') as _close: f.db_reuse_max = None f.close_database() _close.assert_called_with() _close.reset_mock() f.db_reuse_max = 10 f._db_recycles = 3 f.close_database() _close.assert_not_called() assert f._db_recycles == 4 _close.reset_mock() f._db_recycles = 20 f.close_database() _close.assert_called_with() assert f._db_recycles == 1