Skip to content

Commit b2ec459

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 8445068 commit b2ec459

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
@@ -699,6 +699,47 @@ async def test_auth_flow_with_no_tokens(self, oauth_provider: OAuthClientProvide
699699
assert oauth_provider.context.current_tokens.access_token == "new_access_token"
700700
assert oauth_provider.context.token_expiry_time is not None
701701

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

703744
@pytest.mark.parametrize(
704745
(

0 commit comments

Comments
 (0)