Skip to content

Commit

Permalink
fix: make proxy sdk respect cache ttl (#169)
Browse files Browse the repository at this point in the history
  • Loading branch information
sighphyre committed Aug 7, 2023
1 parent d249586 commit fe05098
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Repository/DefaultUnleashProxyRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function findFeatureByContext(string $featureName, ?Context $context = nu

if ($featureData !== null) {
if ($this->configuration->getCache() !== null) {
$this->configuration->getCache()->set($cacheKey, $featureData);
$this->configuration->getCache()->set($cacheKey, $featureData, $this->configuration->getTtl());
}

return new DefaultProxyFeature($featureData);
Expand Down
57 changes: 57 additions & 0 deletions tests/Repository/DefaultUnleashProxyRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ final class DefaultUnleashProxyRepositoryTest extends AbstractHttpClientTest
{
use FakeCacheImplementationTrait, RealCacheImplementationTrait {
FakeCacheImplementationTrait::getCache insteadof RealCacheImplementationTrait;

RealCacheImplementationTrait::getCache as getRealCache;
}

public function testNon200ResponseDegradesGracefully()
Expand Down Expand Up @@ -93,4 +95,59 @@ public function test200ResponseResolvesCorrectly()
$this->assertTrue($resolvedFeature->isEnabled());
$this->assertEquals($expectedVariant, $resolvedFeature->getVariant());
}

public function testCacheTtlIsRespected()
{
$container = [];
$history = Middleware::history($container);
$response = json_encode([
'name' => 'test',
'enabled' => true,
'variant' => [
'name' => 'some-variant',
'payload' => [
'type' => 'string',
'value' => 'some-value',
],
'enabled' => true,
],
'impression_data' => false,
]);

$mock = new MockHandler([
new Response(
200,
['ETag' => 'etag value'],
$response
),
new Response(
200,
['ETag' => 'etag value'],
$response
),
]);

$handlerStack = HandlerStack::create($mock);
$handlerStack->push($history);

$client = new Client(['handler' => $handlerStack]);
$config = (new UnleashConfiguration('', '', ''))
->setCache($this->getRealCache())
->setProxyKey('some-key')
->setTtl(1);

$requestFactory = new HttpFactory();
$repository = new DefaultUnleashProxyRepository($config, $client, $requestFactory);

$repository->findFeatureByContext('test');
//cache is still warm so this should fall back to that
$repository->findFeatureByContext('test');

sleep(1);

//ttl should have expired so this should trigger an API call
$repository->findFeatureByContext('test');

$this->assertCount(2, $container);
}
}

0 comments on commit fe05098

Please sign in to comment.