Skip to content

Commit fb105c9

Browse files
Add proper regression test for OAuth retry performance bug
This test validates the fix for PR #1206 which resolved a critical performance issue where every request was being sent twice due to incorrect indentation of retry logic. The test: - Simulates a successful authenticated request (200 response) - Counts how many times the request is yielded - FAILS with buggy version: 2 yields (double sending) - PASSES with fix: 1 yield (correct behavior) This directly tests the root cause that made OAuth requests 2x slower and ensures this regression cannot occur again.
1 parent 32c6b9a commit fb105c9

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

tests/client/test_auth.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,47 @@ async def test_auth_flow_with_no_tokens(self, oauth_provider, mock_storage):
698698
assert oauth_provider.context.current_tokens.access_token == "new_access_token"
699699
assert oauth_provider.context.token_expiry_time is not None
700700

701+
@pytest.mark.anyio
702+
async def test_auth_flow_no_unnecessary_retry_after_oauth(self, oauth_provider, mock_storage, valid_tokens):
703+
"""Test that requests are not retried unnecessarily - the core bug that caused 2x performance degradation."""
704+
# Pre-store valid tokens so no OAuth flow is needed
705+
await mock_storage.set_tokens(valid_tokens)
706+
oauth_provider.context.current_tokens = valid_tokens
707+
oauth_provider.context.token_expiry_time = time.time() + 1800
708+
oauth_provider._initialized = True
709+
710+
test_request = httpx.Request("GET", "https://api.example.com/mcp")
711+
auth_flow = oauth_provider.async_auth_flow(test_request)
712+
713+
# Count how many times the request is yielded
714+
request_yields = 0
715+
716+
# First request - should have auth header already
717+
request = await auth_flow.__anext__()
718+
request_yields += 1
719+
assert request.headers["Authorization"] == "Bearer test_access_token"
720+
721+
# Send a successful 200 response
722+
response = httpx.Response(200, request=request)
723+
724+
# In the buggy version, this would yield the request AGAIN unconditionally
725+
# In the fixed version, this should end the generator
726+
try:
727+
extra_request = await auth_flow.asend(response)
728+
request_yields += 1
729+
# If we reach here, the bug is present
730+
pytest.fail(
731+
f"Unnecessary retry detected! Request was yielded {request_yields} times. "
732+
f"This indicates the retry logic bug that caused 2x performance degradation. "
733+
f"The request should only be yielded once for successful responses."
734+
)
735+
except StopAsyncIteration:
736+
# This is the expected behavior - no unnecessary retry
737+
pass
738+
739+
# Verify exactly one request was yielded (no double-sending)
740+
assert request_yields == 1, f"Expected 1 request yield, got {request_yields}"
741+
701742

702743
@pytest.mark.parametrize(
703744
(

0 commit comments

Comments
 (0)