Skip to content

Task/tracker component #197

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 16, 2023
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
23 changes: 19 additions & 4 deletions src/SplitIO/Component/Common/Di.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,16 @@
*/
class Di
{
const SAME_APIKEY = "Factory Instantiation: You already have %s factory/factories with this API Key. "
. "We recommend keeping only one instance of the factory at all times (Singleton pattern) and reusing it throughout your application.";

const MULTIPLE_INSTANCES = "Factory Instantiation: You already have an instance of the Split factory. " .
"Make sure you definitely want this additional instance. We recommend keeping only one instance of the factory at all times " .
"(Singleton pattern) and reusing it throughout your application.";

private \SplitIO\Component\Log\Logger $logger;

private int $factoryTracker = 0;
private array $factoryTracker = array();

private string $ipAddress = "";

Expand Down Expand Up @@ -63,12 +70,20 @@ public function __wakeup()
}

/**
* @param string $apiKey
* @return int
*/
public static function trackFactory()
public static function trackFactory($apiKey)
{
self::getInstance()->factoryTracker += 1;
return self::getInstance()->factoryTracker;
$current = self::getInstance()->factoryTracker[$apiKey] ?? 0;
if ($current == 0) {
self::getInstance()->getLogger()->warning(self::MULTIPLE_INSTANCES);
} else {
self::getInstance()->getLogger()->warning(sprintf(self::SAME_APIKEY, $current));
}
$current += 1;
self::getInstance()->factoryTracker[$apiKey] = $current;
return $current;
}

/**
Expand Down
14 changes: 1 addition & 13 deletions src/SplitIO/Sdk.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static function factory($apiKey = 'localhost', array $options = array())
$options['apiKey'] = $apiKey;

//Tracking Factory Instantiation
self::registerInstance();
Di::trackFactory($apiKey);

//Register Logger
self::registerLogger((isset($options['log'])) ? $options['log'] : array());
Expand Down Expand Up @@ -90,16 +90,4 @@ private static function setIP($ip)
{
\SplitIO\Component\Common\Di::setIPAddress($ip);
}

/**
* Register factory instance
*/
private static function registerInstance()
{
if (Di::trackFactory() > 1) {
Di::getLogger()->warning("Factory Instantiation: You already have an instance of the Split factory. "
. "Make sure you definitely want this additional instance. We recommend keeping only one instance of "
. "the factory at all times (Singleton pattern) and reusing it throughout your application.");
}
}
}
59 changes: 35 additions & 24 deletions tests/Suite/InputValidation/FactoryTrackerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,6 @@

class FactoryTrackerTest extends \PHPUnit\Framework\TestCase
{
private function getFactoryClient()
{
$parameters = array('scheme' => 'redis', 'host' => REDIS_HOST, 'port' => REDIS_PORT, 'timeout' => 881);
$options = array('prefix' => TEST_PREFIX);

$sdkConfig = array(
'log' => array('adapter' => 'stdout'),
'cache' => array('adapter' => 'predis', 'parameters' => $parameters, 'options' => $options)
);

//Initializing the SDK instance.
$splitFactory = \SplitIO\Sdk::factory('asdqwe123456', $sdkConfig);

return $splitFactory;
}

private function getMockedLogger()
{
//Initialize mock logger
Expand All @@ -38,18 +22,45 @@ private function getMockedLogger()

public function testMultipleClientInstantiation()
{
$splitFactory = $this->getFactoryClient();
ReflectiveTools::overrideTracker();
$parameters = array('scheme' => 'redis', 'host' => REDIS_HOST, 'port' => REDIS_PORT, 'timeout' => 881);
$options = array('prefix' => TEST_PREFIX);

$sdkConfig = array(
'log' => array('adapter' => 'stdout'),
'cache' => array('adapter' => 'predis', 'parameters' => $parameters, 'options' => $options)
);

//Initializing the SDK instance.
$splitFactory = \SplitIO\Sdk::factory('asdqwe123456', $sdkConfig);
$this->assertNotNull($splitFactory->client());

$logger = $this->getMockedLogger();

$logger->expects($this->once())
$logger->expects($this->any())
->method('warning')
->with($this->equalTo("Factory Instantiation: You already have an instance of the Split factory. "
. "Make sure you definitely want this additional instance. We recommend keeping only one instance "
. "of the factory at all times (Singleton pattern) and reusing it throughout your application."));

$splitFactory2 = $this->getFactoryClient();
->with($this->logicalOr(
$this->equalTo("Factory Instantiation: You already have 1 factory/factories with this API Key. "
. "We recommend keeping only one instance of the factory at all times (Singleton pattern) and reusing "
. "it throughout your application."),
$this->equalTo("Factory Instantiation: You already have 2 factory/factories with this API Key. "
. "We recommend keeping only one instance of the factory at all times (Singleton pattern) and reusing "
. "it throughout your application."),
$this->equalTo("Factory Instantiation: You already have an instance of the Split factory. "
. "Make sure you definitely want this additional instance. We recommend keeping only one instance "
. "of the factory at all times (Singleton pattern) and reusing it throughout your application.")
)
);

//Initializing the SDK instance2.
$splitFactory2 = \SplitIO\Sdk::factory('asdqwe123456', $sdkConfig);
$this->assertNotNull($splitFactory2->client());

//Initializing the SDK instance3.
$splitFactory3 = \SplitIO\Sdk::factory('asdqwe123456', $sdkConfig);
$this->assertNotNull($splitFactory3->client());

//Initializing the SDK instance4.
$splitFactory4 = \SplitIO\Sdk::factory('other', $sdkConfig);
$this->assertNotNull($splitFactory4->client());
}
}
1 change: 1 addition & 0 deletions tests/Suite/InputValidation/GetTreatmentValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class GetTreatmentValidationTest extends \PHPUnit\Framework\TestCase
{
private function getFactoryClient()
{
ReflectiveTools::overrideTracker();
$parameters = array('scheme' => 'redis', 'host' => REDIS_HOST, 'port' => REDIS_PORT, 'timeout' => 881);
$options = array('prefix' => TEST_PREFIX);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class GetTreatmentsValidationTest extends \PHPUnit\Framework\TestCase
{
private function getFactoryClient()
{
ReflectiveTools::overrideTracker();
$parameters = array('scheme' => 'redis', 'host' => REDIS_HOST, 'port' => REDIS_PORT, 'timeout' => 881);
$options = array('prefix' => TEST_PREFIX);

Expand Down
1 change: 1 addition & 0 deletions tests/Suite/InputValidation/ManagerValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class ManagerValidationTest extends \PHPUnit\Framework\TestCase
{
private function getFactoryClient()
{
ReflectiveTools::overrideTracker();
$parameters = array('scheme' => 'redis', 'host' => REDIS_HOST, 'port' => REDIS_PORT, 'timeout' => 881);
$options = array('prefix' => TEST_PREFIX);

Expand Down
1 change: 1 addition & 0 deletions tests/Suite/InputValidation/TrackValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class TrackValidationTest extends \PHPUnit\Framework\TestCase
{
private function getFactoryClient()
{
ReflectiveTools::overrideTracker();
$parameters = array('scheme' => 'redis', 'host' => REDIS_HOST, 'port' => REDIS_PORT, 'timeout' => 881);
$options = array('prefix' => TEST_PREFIX);

Expand Down
9 changes: 9 additions & 0 deletions tests/Suite/Redis/ReflectiveTools.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,13 @@ public static function resetIPAddress()
$property->setAccessible(true);
$property->setValue($di, "");
}

public static function overrideTracker()
{
$di = Di::getInstance();
$reflection = new \ReflectionClass('SplitIO\Component\Common\Di');
$property = $reflection->getProperty('factoryTracker');
$property->setAccessible(true);
$property->setValue($di, array());
}
}