def test_task_headers_are_available_in_tests(self): defer(deferred_func) self.process_task_queues() # Check nothing lingers self.assertFalse("HTTP_X_APPENGINE_TASKNAME" in os.environ) self.assertFalse("HTTP_X_APPENGINE_QUEUENAME" in os.environ) self.assertFalse("HTTP_X_APPENGINE_TASKEXECUTIONCOUNT" in os.environ)
def test_task_headers_are_available_in_tests(self): defer(deferred_func) self.process_task_queues() # Check nothing lingers self.assertFalse(task_name()) self.assertFalse(task_queue_name()) self.assertFalse(task_execution_count())
def cleanup_locks(request): """ Delete all Lock objects that are older than 10 minutes. """ from djangae.tasks.deferred import defer logger.info("Deferring djangae.contrib.lock cleanup task") defer(cleanup_locks_task, _queue=QUEUE) return HttpResponse("Cleanup locks task is running")
def test_get_task_count(self): deferred.defer(my_task) deferred.defer(my_task, _queue='another') # We don't use self.assertNumTasksEquals here because we want to flush. task_count = self.get_task_count("default") self.assertEqual(1, task_count) task_count = self.get_task_count("another") self.assertEqual(1, task_count)
def test_task_default_routing(self): gae_version = 'demo' os.environ['GAE_VERSION'] = gae_version with sleuth.watch('google.cloud.tasks_v2.CloudTasksClient.create_task' ) as _create_task: defer(test_task) self.assertTrue(_create_task.called) routing = _create_task.calls[0].args[2]['app_engine_http_request'][ 'app_engine_routing'] self.assertFalse('service' in routing) self.assertFalse('instance' in routing) self.assertEqual(routing['version'], gae_version) del os.environ['GAE_VERSION']
def test_large_task(self): random_file = os.path.join(os.path.dirname(__file__), "random_data") with open(random_file, "r") as f: big_string = f.read() try: defer(process_argument, big_string) except Exception: # noqa self.fail("A large task couldn't be deferred") self.process_task_queues() self.assertTrue(DeferModelC.objects.exists()) instance = DeferModelC.objects.get() self.assertEqual(instance.text, big_string)
def test_wipe_related_caches(self): b = DeferModelB.objects.create() a = DeferModelA.objects.create(b=b) a.b # Make sure we access it cache_name = DeferModelA._meta.get_field("b").get_cache_name() self.assertTrue(getattr(a, cache_name)) defer(assert_cache_wiped, a) # Should raise an assertion error if the cache existed try: self.process_task_queues() except TaskFailedError as e: raise e.original_exception # Should not have wiped the cache for us! self.assertIsNotNone(getattr(a, cache_name, None))
def test_task_queue_processing_control(self): deferred.defer(throw_once) self.process_task_queues( failure_behaviour=TaskFailedBehaviour.RETRY_TASK) # Should've retried self.assertEqual(throw_once.counter, 2) throw_once.counter = 0 deferred.defer(throw_once) self.assertRaises(TaskFailedError, self.process_task_queues, failure_behaviour=TaskFailedBehaviour.RAISE_ERROR) self.assertEqual(throw_once.counter, 1)
def test_task_routing(self): service = 'service123' version = 'version456' instance = 'instance789' os.environ['GAE_VERSION'] = 'demo' with sleuth.watch('google.cloud.tasks_v2.CloudTasksClient.create_task' ) as _create_task: defer(test_task, _service=service, _version=version, _instance=instance) self.assertTrue(_create_task.called) routing = _create_task.calls[0].args[2]['app_engine_http_request'][ 'app_engine_routing'] self.assertEqual(routing['service'], service) self.assertEqual(routing['version'], version) self.assertEqual(routing['instance'], instance) del os.environ['GAE_VERSION']
def test_transactional_defer(self): try: with transaction.atomic(): defer(create_defer_model_b, 1, _transactional=True) raise ValueError() # Rollback the transaction except ValueError: pass self.process_task_queues() # Shouldn't have created anything self.assertEqual(0, DeferModelB.objects.count()) with transaction.atomic(): defer(create_defer_model_b, 1, _transactional=True) self.process_task_queues() self.assertEqual(0, DeferModelB.objects.count()) # Still nothing # Now we should be good! self.process_task_queues() self.assertEqual(1, DeferModelB.objects.count())
def test_queues_task(self): initial_count = self.get_task_count() defer(test_task) self.assertEqual(self.get_task_count(), initial_count + 1)