mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-05-14 04:02:26 +00:00
test(msgraph): cover concurrent token cache reuse
This commit is contained in:
parent
a152c706b7
commit
b878f89f66
1 changed files with 30 additions and 0 deletions
|
|
@ -2,10 +2,13 @@
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import asyncio
|
||||||
|
|
||||||
import httpx
|
import httpx
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from tools.microsoft_graph_auth import (
|
from tools.microsoft_graph_auth import (
|
||||||
|
CachedAccessToken,
|
||||||
DEFAULT_GRAPH_SCOPE,
|
DEFAULT_GRAPH_SCOPE,
|
||||||
GraphCredentials,
|
GraphCredentials,
|
||||||
MicrosoftGraphConfigError,
|
MicrosoftGraphConfigError,
|
||||||
|
|
@ -66,6 +69,33 @@ class TestMicrosoftGraphTokenProvider:
|
||||||
assert second == "token-1"
|
assert second == "token-1"
|
||||||
assert len(calls) == 1
|
assert len(calls) == 1
|
||||||
|
|
||||||
|
async def test_concurrent_calls_share_one_token_fetch(self):
|
||||||
|
calls: list[int] = []
|
||||||
|
|
||||||
|
provider = MicrosoftGraphTokenProvider(
|
||||||
|
GraphCredentials("tenant", "client", "secret"),
|
||||||
|
)
|
||||||
|
|
||||||
|
async def _fake_fetch():
|
||||||
|
calls.append(1)
|
||||||
|
await asyncio.sleep(0)
|
||||||
|
return CachedAccessToken(
|
||||||
|
access_token="token-1",
|
||||||
|
token_type="Bearer",
|
||||||
|
expires_at=9_999_999_999,
|
||||||
|
)
|
||||||
|
|
||||||
|
provider._fetch_access_token = _fake_fetch # type: ignore[method-assign]
|
||||||
|
|
||||||
|
first, second = await asyncio.gather(
|
||||||
|
provider.get_access_token(),
|
||||||
|
provider.get_access_token(),
|
||||||
|
)
|
||||||
|
|
||||||
|
assert first == "token-1"
|
||||||
|
assert second == "token-1"
|
||||||
|
assert len(calls) == 1
|
||||||
|
|
||||||
async def test_refreshes_when_cached_token_is_expired(self):
|
async def test_refreshes_when_cached_token_is_expired(self):
|
||||||
calls: list[int] = []
|
calls: list[int] = []
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue