Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/Rox/Core/Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -350,10 +350,13 @@ public function fetch($isSourcePushing = false)
$this->_targetGroupRepository->setTargetGroups($configuration->getTargetGroups());
$this->_flagSetter->setExperiments();

$hasChanges = ($this->_lastConfigurations == null || $this->_lastConfigurations->equals($result));
$hasChanges = ($this->_lastConfigurations == null || !$this->_lastConfigurations->equals($result));

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😱

$this->_lastConfigurations = $result;
$fetcherStatus = $result->isFromCache()
? FetcherStatus::AppliedFromLocalStorage
: FetcherStatus::AppliedFromNetwork;
$this->_configurationFetchedInvoker->invoke(
FetcherStatus::AppliedFromNetwork,
$fetcherStatus,
$configuration->getSignatureDate(),
$hasChanges
);
Expand Down
8 changes: 8 additions & 0 deletions src/Rox/Core/Network/AbstractHttpResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,12 @@ final function isSuccessfulStatusCode()
return $this->getStatusCode() >= 200 &&
$this->getStatusCode() <= 299;
}

/**
* @return bool
*/
function isFromCache()
{
return false;
}
}
9 changes: 9 additions & 0 deletions src/Rox/Core/Network/CdnCacheStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Rox\Core\Network;

use Kevinrob\GuzzleCache\KeyValueHttpHeader;
use Kevinrob\GuzzleCache\Strategy\GreedyCacheStrategy;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
Expand All @@ -11,6 +12,14 @@

class CdnCacheStrategy extends GreedyCacheStrategy
{
protected function getCacheKey(RequestInterface $request, KeyValueHttpHeader $varyHeaders = null)
{
// Key on path only so that per-instance query params (e.g. distinct_id)
// don't cause cache misses across containers/pods.
$request = $request->withUri($request->getUri()->withQuery(''));
return parent::getCacheKey($request, $varyHeaders);
}

protected function getCacheObject(RequestInterface $request, ResponseInterface $response)
{
if ($response->getStatusCode() == 200) {
Expand Down
17 changes: 16 additions & 1 deletion src/Rox/Core/Network/ConfigurationFetchResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,22 @@ class ConfigurationFetchResult
*/
private $_parsedData;

/**
* @var bool $_isFromCache
*/
private $_isFromCache;

/**
* ConfigurationFetchResult constructor.
* @param array $parsedData
* @param int $source
* @param bool $isFromCache
*/
public function __construct($parsedData, $source)
public function __construct($parsedData, $source, $isFromCache = false)
{
$this->_source = $source;
$this->_parsedData = $parsedData;
$this->_isFromCache = $isFromCache;
}

/**
Expand All @@ -42,6 +49,14 @@ public function getParsedData()
return $this->_parsedData;
}

/**
* @return bool
*/
public function isFromCache()
{
return $this->_isFromCache;
}

/**
* @param ConfigurationFetchResult $other
* @return bool
Expand Down
2 changes: 1 addition & 1 deletion src/Rox/Core/Network/ConfigurationFetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ function fetch()

if ($fetchResult->isSuccessfulStatusCode()) {
$responseAsString = $fetchResult->getContent()->readAsString();
$configurationFetchResult = $this->createConfigurationResult($responseAsString, $source);
$configurationFetchResult = $this->createConfigurationResult($responseAsString, $source, $fetchResult->isFromCache());

if ($configurationFetchResult == null || $configurationFetchResult->getParsedData() == null) {
return null;
Expand Down
5 changes: 3 additions & 2 deletions src/Rox/Core/Network/ConfigurationFetcherBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,11 @@ public function __construct(
/**
* @param string $data
* @param int $source
* @param bool $isFromCache
* @return ConfigurationFetchResult|null
* @see ConfigurationSource
*/
protected function createConfigurationResult($data, $source)
protected function createConfigurationResult($data, $source, $isFromCache = false)
{
if (!$data) {
$this->_configurationFetchedInvoker->invokeWithError(FetcherError::EmptyJson);
Expand All @@ -99,7 +100,7 @@ protected function createConfigurationResult($data, $source)
return null;
}

return new ConfigurationFetchResult($decoded, $source);
return new ConfigurationFetchResult($decoded, $source, $isFromCache);
}

/**
Expand Down
5 changes: 5 additions & 0 deletions src/Rox/Core/Network/HttpResponseInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ function getStatusCode();
*/
function isSuccessfulStatusCode();

/**
* @return bool
*/
function isFromCache();

/**
* @return HttpResponseContentInterface
*/
Expand Down
9 changes: 9 additions & 0 deletions src/Rox/Core/Network/Psr7ResponseWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ function getStatusCode()
return $this->_response->getStatusCode();
}

/**
* @return bool
*/
function isFromCache()
{
$header = $this->_response->getHeader('X-Kevinrob-Cache');
return !empty($header) && $header[0] === 'HIT';
}

/**
* @return HttpResponseContentInterface
*/
Expand Down
38 changes: 38 additions & 0 deletions tests/Rox/Core/Network/ConfigurationFetcherTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,44 @@ public function testWillReturnAPIDataWhenCDNFails404APIOK()
$this->assertEquals(0, $numberOfTimerCalled[0]);
}

public function testWillReturnIsFromCacheTrueWhenCDNResponseIsFromCache()
{
$confFetchInvoker = new ConfigurationFetchedInvoker(Mockery::mock(UserspaceUnhandledErrorInvokerInterface::class));

$response = new TestHttpResponse(200, "{\"a\": \"cached\"}", true);

$request = Mockery::mock(HttpClientInterface::class)
->shouldReceive('sendGet')
->andReturn($response)
->getMock();

$confFetcher = new ConfigurationFetcher($request, $this->_bu, $this->_dp, $confFetchInvoker, $this->_errorReporter, $this->_environment);
$result = $confFetcher->fetch();

$this->assertNotNull($result);
$this->assertEquals("cached", $result->getParsedData()["a"]);
$this->assertTrue($result->isFromCache());
}

public function testWillReturnIsFromCacheFalseWhenCDNResponseIsFromNetwork()
{
$confFetchInvoker = new ConfigurationFetchedInvoker(Mockery::mock(UserspaceUnhandledErrorInvokerInterface::class));

$response = new TestHttpResponse(200, "{\"a\": \"fresh\"}");

$request = Mockery::mock(HttpClientInterface::class)
->shouldReceive('sendGet')
->andReturn($response)
->getMock();

$confFetcher = new ConfigurationFetcher($request, $this->_bu, $this->_dp, $confFetchInvoker, $this->_errorReporter, $this->_environment);
$result = $confFetcher->fetch();

$this->assertNotNull($result);
$this->assertEquals("fresh", $result->getParsedData()["a"]);
$this->assertFalse($result->isFromCache());
}

public function testWillReturnNullDataWhenBothNotFound()
{
$confFetchInvoker = new ConfigurationFetchedInvoker(Mockery::mock(UserspaceUnhandledErrorInvokerInterface::class));
Expand Down
17 changes: 16 additions & 1 deletion tests/Rox/Core/Network/TestHttpResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,22 @@ class TestHttpResponse extends AbstractHttpResponse
*/
private $_content;

/**
* @var bool $_isFromCache
*/
private $_isFromCache;

/**
* TestHttpResponse constructor.
* @param int $status
* @param string $content
* @param bool $isFromCache
*/
public function __construct($status, $content = "")
public function __construct($status, $content = "", $isFromCache = false)
{
$this->_status = $status;
$this->_content = $content;
$this->_isFromCache = $isFromCache;
}

/**
Expand All @@ -33,6 +40,14 @@ function getStatusCode()
return $this->_status;
}

/**
* @return bool
*/
function isFromCache()
{
return $this->_isFromCache;
}

/**
* @return HttpResponseContentInterface
*/
Expand Down
3 changes: 2 additions & 1 deletion tests/Rox/E2E/RoxE2ETests.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ public static function setUpBeforeClass(): void

$options = new RoxOptions((new RoxOptionsBuilder())
->setConfigurationFetchedHandler(function (ConfigurationFetchedArgs $args) {
if ($args->getFetcherStatus() == FetcherStatus::AppliedFromNetwork) {
if ($args->getFetcherStatus() == FetcherStatus::AppliedFromNetwork ||
$args->getFetcherStatus() == FetcherStatus::AppliedFromLocalStorage) {
TestVars::$configurationFetchedCount++;
}
})
Expand Down
39 changes: 39 additions & 0 deletions tests/Rox/Server/Client/ServerPropertiesTests.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Rox\Server\Client;

use Rox\Core\Client\SdkSettings;
use Rox\RoxTestCase;
use Rox\Server\RoxOptions;
use Rox\Server\RoxOptionsBuilder;

class ServerPropertiesTests extends RoxTestCase
{
public function testDistinctIdUsesEnvVarWhenSet()
{
putenv(ServerProperties::INSTANCE_ID_ENV_VAR_NAME . '=my-instance-001');
$props = new ServerProperties(
new SdkSettings('test-api-key', ''),
new RoxOptions((new RoxOptionsBuilder()))
);
$this->assertEquals('my-instance-001', $props->getDistinctId());
putenv(ServerProperties::INSTANCE_ID_ENV_VAR_NAME);
}

public function testDistinctIdFallsBackToScriptPropertiesHashWhenEnvVarNotSet()
{
putenv(ServerProperties::INSTANCE_ID_ENV_VAR_NAME);
$props = new ServerProperties(
new SdkSettings('test-api-key', ''),
new RoxOptions((new RoxOptionsBuilder()))
);
$expected = md5(join('.', [
getmyuid(),
getmygid(),
get_current_user(),
getmyinode(),
getlastmod()
]));
$this->assertEquals($expected, $props->getDistinctId());
}
}
Loading