-
-
Notifications
You must be signed in to change notification settings - Fork 945
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: update request-id recipe to use contextvars #2382
Open
EricGoulart
wants to merge
12
commits into
falconry:master
Choose a base branch
from
EricGoulart:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 9 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
2727fef
refactor: request-id to use contextvars
EricGoulart 1a4b3fa
Merge branch 'master' into master
vytas7 b65571b
Merge branch 'master' into master
vytas7 c499cc2
adding test to test_recipes
EricGoulart 7510647
Merge remote-tracking branch 'origin/master'
EricGoulart 9c0f151
test(request_id): add tests for request id middleware context handling
EricGoulart 54b83b6
test(request_id): add tests for request id middleware context handling
EricGoulart 1220b21
Merge branch 'master' into master
vytas7 fd348be
Merge branch 'master' into master
vytas7 63d269e
minor changes in tests
EricGoulart 2ae9a54
Merge branch 'master' into master
vytas7 560fe6a
Merge branch 'master' into master
vytas7 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,19 @@ | ||
# context.py | ||
|
||
import threading | ||
import contextvars | ||
|
||
|
||
class _Context: | ||
def __init__(self): | ||
self._thread_local = threading.local() | ||
self._request_id_var = contextvars.ContextVar('request_id', default=None) | ||
|
||
@property | ||
def request_id(self): | ||
return getattr(self._thread_local, 'request_id', None) | ||
return self._request_id_var.get() | ||
|
||
@request_id.setter | ||
def request_id(self, value): | ||
self._thread_local.request_id = value | ||
self._request_id_var.set(value) | ||
|
||
|
||
ctx = _Context() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
import asyncio | ||
|
||
import pytest | ||
|
||
from examples.recipes.request_id_middleware import RequestIDMiddleware | ||
import falcon | ||
import falcon.testing | ||
|
||
|
@@ -141,3 +144,45 @@ def test_raw_path(self, asgi, app_kind, util): | |
) | ||
assert result2.status_code == 200 | ||
assert result2.json == {'cached': True} | ||
|
||
|
||
class TestRequestIDContext: | ||
@pytest.fixture | ||
def app(self): | ||
app = falcon.App(middleware=[RequestIDMiddleware()]) | ||
app.add_route('/test', self.RequestIDResource()) | ||
return app | ||
|
||
class RequestIDResource: | ||
def on_get(self, req, resp): | ||
resp.media = {'request_id': req.context.request_id} | ||
|
||
def test_request_id_isolated_in_async(self, app): | ||
async def make_request(): | ||
client = falcon.testing.TestClient(app) | ||
response = client.simulate_get('/test') | ||
return response.json['request_id'] | ||
|
||
loop = asyncio.get_event_loop() | ||
request_id1, request_id2 = loop.run_until_complete( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These won't run asynchronously anyway, there is no real benefit vs just running |
||
asyncio.gather(make_request(), make_request()) | ||
) | ||
assert request_id1 != request_id2 | ||
|
||
def test_request_id_persistence(self, app): | ||
client = falcon.testing.TestClient(app) | ||
|
||
response = client.simulate_get('/test') | ||
request_id1 = response.json['request_id'] | ||
|
||
response = client.simulate_get('/test') | ||
request_id2 = response.json['request_id'] | ||
|
||
assert request_id1 != request_id2 | ||
|
||
def test_request_id_in_response_header(self, app): | ||
client = falcon.testing.TestClient(app) | ||
|
||
response = client.simulate_get('/test') | ||
assert 'X-Request-ID' in response.headers | ||
assert response.headers['X-Request-ID'] == response.json['request_id'] |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not a good idea to import this way, it may not work correctly when running
pytest
from another directory, for instance.Let's import directly from the Python file, as it is done in other tests in this file.