def test_dedupe_inside_request_response_cycle(self): request_started.send(sender=self) fake_task.delay() fake_task.delay() assert fake_task_func.call_count == 0 request_finished.send_robust(sender=self) assert fake_task_func.call_count == 1
def __call__(self, environ): from django.conf import settings # Set up middleware if needed. We couldn't do this earlier, because # settings weren't available. if self._request_middleware is None: self.load_middleware() request_started.disconnect(close_old_connections) request_started.send(sender=self.__class__) request_started.connect(close_old_connections) request = WSGIRequest(environ) # sneaky little hack so that we can easily get round # CsrfViewMiddleware. This makes life easier, and is probably # required for backwards compatibility with external tests against # admin views. request._dont_enforce_csrf_checks = not self.enforce_csrf_checks response = self.get_response(request) # We're emulating a WSGI server; we must call the close method # on completion. if response.streaming: response.streaming_content = closing_iterator_wrapper(response.streaming_content, response.close) else: request_finished.disconnect(close_old_connections) response.close() # will fire request_finished request_finished.connect(close_old_connections) return response
def test_deduplication(self): """Test the same task added multiple times is de-duped.""" request_started.send(sender=self) test_task.delay() test_task.delay() self._verify_one_task_queued()
def push_messages(self): try: request_started.send(None, environ=None) self._push_messages() except Exception as e: self.logger.exception(e) raise finally: request_finished.send(None)
def test_request_finished(self, _mock): request_started.send(sender=self) test_task.delay() self._verify_task_filled() request_finished.send(sender=self) self._verify_task_empty() # Assert the original `apply_async` called. assert _mock.called, ( 'Expected PostRequestTask.original_apply_async call')
def test_request_failed(self, _mock): request_started.send(sender=self) test_task.delay() self._verify_task_filled() # Simulate a request exception. _discard_tasks() self._verify_task_empty() # Assert the original `apply_async` was not called. assert not _mock.called, ( 'Unexpected PostRequestTask.original_apply_async call')
def test_tasks_deduplication_with_different_arguments(self): """Test arguments sent to the task are used when de-duping.""" request_started.send(sender=self) test_task_with_args_and_kwargs.delay(42) test_task_with_args_and_kwargs.delay(42, bar='rista') test_task_with_args_and_kwargs.delay(42, bar='rista') self._verify_x_tasks_queued( 2, # 2 tasks should have been kept. ((42,), (42,)), # Each of those tasks sent "42" as the only arg... ({}, {'bar': 'rista'}), # ... Only the second one sent kwargs. test_task=test_task_with_args_and_kwargs)
def test_task_applied_once_request_finished( self, original_apply_async_mock): request_started.send(sender=self) test_task.delay() self._verify_one_task_queued() request_finished.send(sender=self) self._verify_task_empty() # Assert the original `apply_async` called. self.assertEqual( original_apply_async_mock.call_count, 1, 'Expected PostRequestTask.original_apply_async call')
def test_task_discarded_when_request_failed( self, original_apply_async_mock): request_started.send(sender=self) test_task.delay() self._verify_one_task_queued() # Simulate a request exception. got_request_exception.send(sender=self) self._verify_task_empty() # Assert the original `apply_async` was not called. self.assertEqual( original_apply_async_mock.call_count, 0, 'Unexpected PostRequestTask.original_apply_async call')
def test_tasks_deduplication_with_different_keyword_arguments(self): """Test keyword arguments sent to the task are used when de-duping.""" request_started.send(sender=self) test_task_with_args_and_kwargs.delay(42, bar='rista') test_task_with_args_and_kwargs.delay(42, bar='rista') test_task_with_args_and_kwargs.delay(42, bar='atheon') test_task_with_args_and_kwargs.delay(42, bar='rage') test_task_with_args_and_kwargs.delay(42, bar='rista') test_task_with_args_and_kwargs.delay(42, bar='rage') self._verify_x_tasks_queued( 3, ((42,), (42,), (42, )), ({'bar': 'rista'}, {'bar': 'atheon'}, {'bar': 'rage'}), test_task=test_task_with_args_and_kwargs)
def test_context_cache_cleared_after_request(self): """ The context cache should be cleared between requests. """ CachingTestModel.objects.create(field1="test") with sleuth.watch("google.appengine.api.datastore.Query.Run") as query: CachingTestModel.objects.get(field1="test") self.assertEqual(query.call_count, 0) # Now start a new request, which should clear the cache request_started.send(HttpRequest(), keep_disabled_flags=True) CachingTestModel.objects.get(field1="test") self.assertEqual(query.call_count, 1) # Now do another call, which should use the cache (because it would have been # populated by the previous call) CachingTestModel.objects.get(field1="test") self.assertEqual(query.call_count, 1) # Now clear the cache again by *finishing* a request request_finished.send(HttpRequest(), keep_disabled_flags=True) CachingTestModel.objects.get(field1="test") self.assertEqual(query.call_count, 2)
def test_tasks_deduplication_with_celery_keywords(self): """Test celery-specific keyword arguments are used when de-duping.""" request_started.send(sender=self) # Note that we are using apply_async() instead of delay() to allow us # to pass celery-specific arguments, as a result we have to send the # positional arguments with args=() and the keyword arguments with # kwargs={}. Everything else is an argument to the apply_async() call # itself. test_task_with_args_and_kwargs.apply_async( args=(42,), kwargs={'bar': 'rista'}, retry=3) test_task_with_args_and_kwargs.apply_async( args=(42,), kwargs={'bar': 'rista'}, retry=1) test_task_with_args_and_kwargs.apply_async( args=(42,), kwargs={'bar': 'rista'}, retry=3) self._verify_x_tasks_queued( 2, ((42, ), (42, )), ({'bar': 'rista'}, {'bar': 'rista'}), ({'retry': 3}, {'retry': 1}), test_task=test_task_with_args_and_kwargs)
def __call__(self, environ): # Set up middleware if needed. We couldn't do this earlier, because # settings weren't available. if self._middleware_chain is None: self.load_middleware() request_started.disconnect(close_old_connections) request_started.send(sender=self.__class__, environ=environ) request_started.connect(close_old_connections) request = WSGIRequest(environ) # sneaky little hack so that we can easily get round # CsrfViewMiddleware. This makes life easier, and is probably # required for backwards compatibility with external tests against # admin views. request._dont_enforce_csrf_checks = not self.enforce_csrf_checks # Request goes through middleware. response = self.get_response(request) # Simulate behaviors of most Web servers. conditional_content_removal(request, response) # Attach the originating request to the response so that it could be # later retrieved. response.wsgi_request = request # We're emulating a WSGI server; we must call the close method # on completion. if response.streaming: response.streaming_content = closing_iterator_wrapper( response.streaming_content, response.close) else: request_finished.disconnect(close_old_connections) response.close() # will fire request_finished request_finished.connect(close_old_connections) return response
def __call__(self, environ): # Set up middleware if needed. We couldn't do this earlier, because # settings weren't available. if self._middleware_chain is None: self.load_middleware() request_started.disconnect(close_old_connections) request_started.send(sender=self.__class__, environ=environ) request_started.connect(close_old_connections) request = WSGIRequest(environ) # sneaky little hack so that we can easily get round # CsrfViewMiddleware. This makes life easier, and is probably # required for backwards compatibility with external tests against # admin views. request._dont_enforce_csrf_checks = not self.enforce_csrf_checks # Request goes through middleware. response = self.get_response(request) # Simulate behaviors of most Web servers. conditional_content_removal(request, response) # Attach the originating request to the response so that it could be # later retrieved. response.wsgi_request = request # Emulate a WSGI server by calling the close method on completion. if response.streaming: response.streaming_content = closing_iterator_wrapper( response.streaming_content, response.close) else: request_finished.disconnect(close_old_connections) response.close() # will fire request_finished request_finished.connect(close_old_connections) return response
def test_can_be_switched_off(self): Switch.objects.filter(name='activate-django-post-request').delete() request_started.send(sender=self) fake_task.delay() fake_task.delay() assert fake_task_func.call_count == 2
def test_queued_inside_request_response_cycle(self): request_started.send(sender=self) fake_task.delay() assert fake_task_func.call_count == 0 request_finished.send_robust(sender=self) assert fake_task_func.call_count == 1
def test_task_in_request_should_not_be_called_immediately(self): request_started.send(sender=self) result = test_task.delay() self.assertEqual(result, None) self.assertEqual(task_mock.call_count, 0)
def test_can_be_switched_off(self): Switch.objects.filter(name='activate-django-post-request').delete() request_started.send(sender=self) fake_task.delay() fake_task.delay() assert fake_task_func.call_count == 2
def test_task_in_request_should_not_be_called_immediately(self): request_started.send(sender=self) result = test_task.delay() self.assertEqual(result, None) self.assertEqual(task_mock.call_count, 0)
def test_task_in_request_with_args_and_kwargs(self): request_started.send(sender=self) test_task.delay(42, foo='bar') self.assertEqual(task_mock.call_count, 0)
def test_task_in_request_with_args_and_kwargs(self): request_started.send(sender=self) test_task.delay(42, foo='bar') self.assertEqual(task_mock.call_count, 0)
from __future__ import unicode_literals
def test_task_in_request(self): request_started.send(sender=self) test_task.delay() assert not task_mock.called