Skip to content

Commit b878f89

Browse files
dlkakbsteknium1
authored andcommitted
test(msgraph): cover concurrent token cache reuse
1 parent a152c70 commit b878f89

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

tests/tools/test_microsoft_graph_auth.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22

33
from __future__ import annotations
44

5+
import asyncio
6+
57
import httpx
68
import pytest
79

810
from tools.microsoft_graph_auth import (
11+
CachedAccessToken,
912
DEFAULT_GRAPH_SCOPE,
1013
GraphCredentials,
1114
MicrosoftGraphConfigError,
@@ -66,6 +69,33 @@ def handler(request: httpx.Request) -> httpx.Response:
6669
assert second == "token-1"
6770
assert len(calls) == 1
6871

72+
async def test_concurrent_calls_share_one_token_fetch(self):
73+
calls: list[int] = []
74+
75+
provider = MicrosoftGraphTokenProvider(
76+
GraphCredentials("tenant", "client", "secret"),
77+
)
78+
79+
async def _fake_fetch():
80+
calls.append(1)
81+
await asyncio.sleep(0)
82+
return CachedAccessToken(
83+
access_token="token-1",
84+
token_type="Bearer",
85+
expires_at=9_999_999_999,
86+
)
87+
88+
provider._fetch_access_token = _fake_fetch # type: ignore[method-assign]
89+
90+
first, second = await asyncio.gather(
91+
provider.get_access_token(),
92+
provider.get_access_token(),
93+
)
94+
95+
assert first == "token-1"
96+
assert second == "token-1"
97+
assert len(calls) == 1
98+
6999
async def test_refreshes_when_cached_token_is_expired(self):
70100
calls: list[int] = []
71101

0 commit comments

Comments
 (0)