Skip to content

Commit

Permalink
fix: add eager refresh (#411)
Browse files Browse the repository at this point in the history
* fix: add eager refresh

* add test
  • Loading branch information
bshaffer authored Sep 1, 2022
1 parent deab935 commit da4037d
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/FetchAuthTokenCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ class FetchAuthTokenCache implements
*/
private $fetcher;

/**
* @var int
*/
private $eagerRefreshThresholdSeconds = 10;

/**
* @param FetchAuthTokenInterface $fetcher A credentials fetcher
* @param array<mixed> $cacheConfig Configuration for the cache
Expand Down Expand Up @@ -250,7 +255,7 @@ private function fetchAuthTokenFromCache($authUri = null)
// (for JwtAccess and ID tokens)
return $cached;
}
if (time() < $cached['expires_at']) {
if ((time() + $this->eagerRefreshThresholdSeconds) < $cached['expires_at']) {
// access token is not expired
return $cached;
}
Expand Down
46 changes: 45 additions & 1 deletion tests/FetchAuthTokenCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ public function testShouldReturnValueWhenNotExpired()
{
$cacheKey = 'myKey';
$token = '2/abcdef1234567890';
$expiresAt = time() + 10;
$expiresAt = time() + 20;
$cachedValue = [
'access_token' => $token,
'expires_at' => $expiresAt,
Expand Down Expand Up @@ -328,6 +328,50 @@ public function testShouldNotReturnValueWhenExpired()
$this->assertEquals($newToken, $accessToken);
}

public function testShouldNotReturnValueWhenExpiredWithinEagerThreshold()
{
$cacheKey = 'myKey';
$token = '2/abcdef1234567890';
$expiresAt = time() + 5;
$cachedValue = [
'access_token' => $token,
'expires_at' => $expiresAt,
];
$newToken = ['access_token' => '3/abcdef1234567890'];
$this->mockCacheItem->isHit()
->shouldBeCalledTimes(1)
->willReturn(true);
$this->mockCacheItem->get()
->shouldBeCalledTimes(1)
->willReturn($cachedValue);
$this->mockCacheItem->set($newToken)
->shouldBeCalledTimes(1)
->willReturn($this->mockCacheItem->reveal());
$this->mockCacheItem->expiresAfter(1500)
->shouldBeCalledTimes(1)
->willReturn($this->mockCacheItem->reveal());
$this->mockCache->getItem($cacheKey)
->shouldBeCalledTimes(2)
->willReturn($this->mockCacheItem->reveal());
$this->mockFetcher->fetchAuthToken(null)
->shouldBeCalledTimes(1)
->willReturn($newToken);
$this->mockFetcher->getCacheKey()
->shouldBeCalled()
->willReturn($cacheKey);
$this->mockCache->save($this->mockCacheItem)
->shouldBeCalledTimes(1);

// Run the test.
$cachedFetcher = new FetchAuthTokenCache(
$this->mockFetcher->reveal(),
null,
$this->mockCache->reveal()
);
$accessToken = $cachedFetcher->fetchAuthToken();
$this->assertEquals($newToken, $accessToken);
}

public function testGetsCachedAuthTokenUsingCachePrefix()
{
$prefix = 'test_prefix_';
Expand Down

0 comments on commit da4037d

Please sign in to comment.