Skip to content

Commit

Permalink
fix: add eager threshold (#416)
Browse files Browse the repository at this point in the history
  • Loading branch information
bshaffer authored Aug 23, 2022
1 parent a9355c7 commit 99eb172
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/CredentialsWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ class CredentialsWrapper
private $credentialsFetcher;
private $authHttpHandler;

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

/**
* CredentialsWrapper constructor.
* @param FetchAuthTokenInterface $credentialsFetcher A credentials loader
Expand Down Expand Up @@ -297,6 +300,6 @@ private static function isExpired($token)
{
return !(self::isValid($token)
&& array_key_exists('expires_at', $token)
&& $token['expires_at'] > time());
&& $token['expires_at'] > time() + self::$eagerRefreshThresholdSeconds);
}
}
12 changes: 12 additions & 0 deletions tests/Tests/Unit/CredentialsWrapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,17 @@ public function getBearerStringData()
'access_token' => 456,
'expires_at' => time() + 1000
]);
$eagerExpiredFetcher = $this->prophesize(FetchAuthTokenInterface::class);
$eagerExpiredFetcher->getLastReceivedToken()
->willReturn([
'access_token' => 123,
'expires_at' => time() + 1
]);
$eagerExpiredFetcher->fetchAuthToken(Argument::any())
->willReturn([
'access_token' => 456,
'expires_at' => time() + 10 // within 10 second eager threshold
]);
$unexpiredFetcher = $this->prophesize(FetchAuthTokenInterface::class);
$unexpiredFetcher->getLastReceivedToken()
->willReturn([
Expand All @@ -226,6 +237,7 @@ public function getBearerStringData()
]);
return [
[$expiredFetcher->reveal(), 'Bearer 456'],
[$eagerExpiredFetcher->reveal(), 'Bearer 456'],
[$unexpiredFetcher->reveal(), 'Bearer 123'],
[$insecureFetcher->reveal(), ''],
[$nullFetcher->reveal(), '']
Expand Down
2 changes: 1 addition & 1 deletion tests/Tests/Unit/OperationResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ public function getSleeps()
return $this->sleeps;
}

public function sleepMillis($millis)
public function sleepMillis(int $millis)
{
$this->currentTime += $millis;
$this->sleeps[] = $millis;
Expand Down

0 comments on commit 99eb172

Please sign in to comment.