Soo, here’s the deal, Im creating a test in order to check if an improvement a made for performance is working or not, the improvements consists in making some signals logic an async task instead of an async process, I wanted to test this out but when I manually create a celery worker I get the
django.db.transaction.TransactionManagementError: An error occurred in the current transaction. You can’t execute queries until the end of the ‘atomic’ block.
To have more context.. I’m avoiding creating a worker from the start because the pipeline does not use redis by default, or at least it avoids the celery creation, so I need to make one manually, but when I create the service I get the error
def test_delete_optimal_deletion_time(self):
"""
Verifies that the `deletion logic` is under an acceptable
running time.
:return:
"""
# ... non-relevant code
# Start Celery worker in a separate context to avoid nested atomic transactions
with start_worker(celery_app, perform_ping_check=False):
data_delete_start = time.time()
og_org.delete()
data_delete_end = time.time()
data_total_test_end = time.time()
data_total_test_time = data_total_test_end - data_total_test_start
data_delete_time = data_delete_end - data_delete_start
print("time to create test data: ", data_creation_time)
print("time to delete test data: ", data_delete_time)
print("time to run all test data: ", data_total_test_time)
print("settings.CELERY_TASK_ALWAYS_EAGER: ", settings.CELERY_TASK_ALWAYS_EAGER)
this line is the “root” crash
og_org.delete()
this is because og_org’s delete method uses transaction.atomic to prevent deadlocks,
My theory is that the “with” define in order to use the agent acts as a try catch, which in newer iterations of django counts as an atomic transaction.. therefore the error, I haven’t been able to find a work around for this…
for some more context this is the delete method of og_org instance:
def delete(self, *args, **kwargs):
with transaction.atomic():
return super().delete(*args, **kwargs)
things I’ve tried
- wrapping this up in another atomic transaction
- defining the celery worker in the class setup
- trying to ran this og_org.delete() as a separate task outside the with
- tried to do it with start_worker….
pass - tried a bypass with he pytest library
I know the most simple solution would be to update some local_settings configurations and allow the agent creation at the beginning of the app creation for the tests and not just for the application itself, but I’m trying to leave this as a last resort. Any help is appreciated