diff --git a/common.py b/common.py index eb84a41b..d421a651 100644 --- a/common.py +++ b/common.py @@ -358,7 +358,10 @@ def create_task(queue, delay=None, **params): # return app.view_functions[endpoint](**args) body = urllib.parse.urlencode(sorted(params.items())).encode() - traceparent = request.headers.get('traceparent', '') + try: + traceparent = request.headers.get('traceparent', '') + except RuntimeError: # not currently in a request context + traceparent = '' task = { 'app_engine_http_request': { 'http_method': 'POST', diff --git a/tests/test_common.py b/tests/test_common.py index c225ea9e..cd3fc650 100644 --- a/tests/test_common.py +++ b/tests/test_common.py @@ -274,3 +274,10 @@ def test_as2_request_type(self): app.test_request_context('/', headers={'Accept': accept})): self.assertEqual(expected, common.as2_request_type()) + + @patch('oauth_dropins.webutil.appengine_config.tasks_client.create_task') + def test_create_task_no_request_context(self, mock_create_task): + common.RUN_TASKS_INLINE = False + self.request_context.pop() + common.create_task('foo') + mock_create_task.assert_called() diff --git a/tests/testutil.py b/tests/testutil.py index 376ccf7e..1dde4132 100644 --- a/tests/testutil.py +++ b/tests/testutil.py @@ -367,7 +367,11 @@ def tearDown(self): super().tearDown() # this breaks if it's before super().tearDown(). why?! - self.request_context.pop() + try: + self.request_context.pop() + except LookupError as e: + # the test probably popped this already + logger.warning('', exc_info=e) def run(self, result=None): """Override to hide stdlib and virtualenv lines in tracebacks.