Skip to content
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

Propagate request start/end lazily #1687

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions esrally/client/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,19 @@ async def __aenter__(self):

@property
def request_start(self):
return self.ctx["request_start"]
return self.ctx.get("request_start")

@property
def request_end(self):
return self.ctx["request_end"]
return self.ctx.get("request_end")

async def __aexit__(self, exc_type, exc_val, exc_tb):
# propagate earliest request start and most recent request end to parent
request_start = self.request_start
request_end = self.request_end
self.ctx_holder.restore_context(self.token)
# don't attempt to restore these values on the top-level context as they don't exist
if self.token.old_value != contextvars.Token.MISSING:
self.ctx_holder.update_request_start(request_start)
self.ctx_holder.update_request_end(request_end)
# propagate earliest request start and most recent request end to parent
self.ctx_holder.update_request_start(self.request_start)
self.ctx_holder.update_request_end(self.request_end)
self.token = None
return False

Expand Down
13 changes: 13 additions & 0 deletions tests/client/factory_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,19 @@ def test_client_cert(self, tmp_path_factory: pytest.TempPathFactory):


class TestRequestContextManager:
@pytest.mark.asyncio
async def test_does_not_propagates_empty_toplevel_context(self):
test_client = client.RequestContextHolder()
async with test_client.new_request_context() as top_level_ctx:
# Simulate that we started a request context but did not issue a request.
# This can happen when a runner throws an exception before it issued an
# API request. The most typical case is that a mandatory parameter is
# missing.
pass

assert top_level_ctx.request_start is None
assert top_level_ctx.request_end is None

@pytest.mark.asyncio
async def test_propagates_nested_context(self):
test_client = client.RequestContextHolder()
Expand Down