mirror of
https://github.com/GSA/notifications-api.git
synced 2026-01-30 14:31:57 -05:00
Move request_id injection into send_task override
This applies the same change we made in other apps [1][2]. Adding the override here is special, though, because it means the others will now get triggered, since this app is the start of the chain of tasks for a request. We will also retain existing request_id tracing for tasks within this app, since "apply_async" calls the "send_task" method internally, which is the one we're overriding. [1]:6f3c118a1e[2]:2e08b7aa95
This commit is contained in:
@@ -85,33 +85,33 @@ def test_call_exports_request_id_from_kwargs(mocker, celery_task):
|
||||
assert g.request_id == '1234'
|
||||
|
||||
|
||||
def test_apply_async_injects_global_request_id_into_kwargs(mocker, celery_task):
|
||||
super_apply = mocker.patch('celery.app.task.Task.apply_async')
|
||||
def test_send_task_injects_global_request_id_into_kwargs(mocker, notify_api):
|
||||
super_apply = mocker.patch('celery.Celery.send_task')
|
||||
g.request_id = '1234'
|
||||
celery_task.apply_async()
|
||||
super_apply.assert_called_with(None, {'request_id': '1234'})
|
||||
notify_celery.send_task('some-task')
|
||||
super_apply.assert_called_with('some-task', None, {'request_id': '1234'})
|
||||
|
||||
|
||||
def test_apply_async_inject_request_id_with_other_kwargs(mocker, celery_task):
|
||||
super_apply = mocker.patch('celery.app.task.Task.apply_async')
|
||||
def test_send_task_injects_request_id_with_other_kwargs(mocker, notify_api):
|
||||
super_apply = mocker.patch('celery.Celery.send_task')
|
||||
g.request_id = '1234'
|
||||
celery_task.apply_async(kwargs={'something': 'else'})
|
||||
super_apply.assert_called_with(None, {'request_id': '1234', 'something': 'else'})
|
||||
notify_celery.send_task('some-task', kwargs={'something': 'else'})
|
||||
super_apply.assert_called_with('some-task', None, {'request_id': '1234', 'something': 'else'})
|
||||
|
||||
|
||||
def test_apply_async_inject_request_id_with_positional_args(mocker, celery_task):
|
||||
super_apply = mocker.patch('celery.app.task.Task.apply_async')
|
||||
def test_send_task_injects_request_id_with_positional_args(mocker, notify_api):
|
||||
super_apply = mocker.patch('celery.Celery.send_task')
|
||||
g.request_id = '1234'
|
||||
celery_task.apply_async(['args'], {'something': 'else'})
|
||||
super_apply.assert_called_with(['args'], {'request_id': '1234', 'something': 'else'})
|
||||
notify_celery.send_task('some-task', ['args'], {'kw': 'args'})
|
||||
super_apply.assert_called_with('some-task', ['args'], {'request_id': '1234', 'kw': 'args'})
|
||||
|
||||
|
||||
def test_apply_async_injects_id_into_kwargs_from_request(mocker, notify_api, celery_task):
|
||||
super_apply = mocker.patch('celery.app.task.Task.apply_async')
|
||||
def test_send_task_injects_id_into_kwargs_from_request(mocker, notify_api):
|
||||
super_apply = mocker.patch('celery.Celery.send_task')
|
||||
request_id_header = notify_api.config['NOTIFY_TRACE_ID_HEADER']
|
||||
request_headers = {request_id_header: '1234'}
|
||||
|
||||
with notify_api.test_request_context(headers=request_headers):
|
||||
celery_task.apply_async()
|
||||
notify_celery.send_task('some-task')
|
||||
|
||||
super_apply.assert_called_with(None, {'request_id': '1234'})
|
||||
super_apply.assert_called_with('some-task', None, {'request_id': '1234'})
|
||||
|
||||
Reference in New Issue
Block a user