Skip to content

Commit d514aad

Browse files
authored
Merge pull request #197 from splitio/task/trackerComponent
Task/tracker component
2 parents a5f3d30 + 7dcc25e commit d514aad

File tree

8 files changed

+68
-41
lines changed

8 files changed

+68
-41
lines changed

src/SplitIO/Component/Common/Di.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,16 @@
99
*/
1010
class Di
1111
{
12+
const SAME_APIKEY = "Factory Instantiation: You already have %s factory/factories with this API Key. "
13+
. "We recommend keeping only one instance of the factory at all times (Singleton pattern) and reusing it throughout your application.";
14+
15+
const MULTIPLE_INSTANCES = "Factory Instantiation: You already have an instance of the Split factory. " .
16+
"Make sure you definitely want this additional instance. We recommend keeping only one instance of the factory at all times " .
17+
"(Singleton pattern) and reusing it throughout your application.";
18+
1219
private \SplitIO\Component\Log\Logger $logger;
1320

14-
private int $factoryTracker = 0;
21+
private array $factoryTracker = array();
1522

1623
private string $ipAddress = "";
1724

@@ -63,12 +70,20 @@ public function __wakeup()
6370
}
6471

6572
/**
73+
* @param string $apiKey
6674
* @return int
6775
*/
68-
public static function trackFactory()
76+
public static function trackFactory($apiKey)
6977
{
70-
self::getInstance()->factoryTracker += 1;
71-
return self::getInstance()->factoryTracker;
78+
$current = self::getInstance()->factoryTracker[$apiKey] ?? 0;
79+
if ($current == 0) {
80+
self::getInstance()->getLogger()->warning(self::MULTIPLE_INSTANCES);
81+
} else {
82+
self::getInstance()->getLogger()->warning(sprintf(self::SAME_APIKEY, $current));
83+
}
84+
$current += 1;
85+
self::getInstance()->factoryTracker[$apiKey] = $current;
86+
return $current;
7287
}
7388

7489
/**

src/SplitIO/Sdk.php

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public static function factory($apiKey = 'localhost', array $options = array())
3232
$options['apiKey'] = $apiKey;
3333

3434
//Tracking Factory Instantiation
35-
self::registerInstance();
35+
Di::trackFactory($apiKey);
3636

3737
//Register Logger
3838
self::registerLogger((isset($options['log'])) ? $options['log'] : array());
@@ -90,16 +90,4 @@ private static function setIP($ip)
9090
{
9191
\SplitIO\Component\Common\Di::setIPAddress($ip);
9292
}
93-
94-
/**
95-
* Register factory instance
96-
*/
97-
private static function registerInstance()
98-
{
99-
if (Di::trackFactory() > 1) {
100-
Di::getLogger()->warning("Factory Instantiation: You already have an instance of the Split factory. "
101-
. "Make sure you definitely want this additional instance. We recommend keeping only one instance of "
102-
. "the factory at all times (Singleton pattern) and reusing it throughout your application.");
103-
}
104-
}
10593
}

tests/Suite/InputValidation/FactoryTrackerTest.php

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,6 @@
55

66
class FactoryTrackerTest extends \PHPUnit\Framework\TestCase
77
{
8-
private function getFactoryClient()
9-
{
10-
$parameters = array('scheme' => 'redis', 'host' => REDIS_HOST, 'port' => REDIS_PORT, 'timeout' => 881);
11-
$options = array('prefix' => TEST_PREFIX);
12-
13-
$sdkConfig = array(
14-
'log' => array('adapter' => 'stdout'),
15-
'cache' => array('adapter' => 'predis', 'parameters' => $parameters, 'options' => $options)
16-
);
17-
18-
//Initializing the SDK instance.
19-
$splitFactory = \SplitIO\Sdk::factory('asdqwe123456', $sdkConfig);
20-
21-
return $splitFactory;
22-
}
23-
248
private function getMockedLogger()
259
{
2610
//Initialize mock logger
@@ -38,18 +22,45 @@ private function getMockedLogger()
3822

3923
public function testMultipleClientInstantiation()
4024
{
41-
$splitFactory = $this->getFactoryClient();
25+
ReflectiveTools::overrideTracker();
26+
$parameters = array('scheme' => 'redis', 'host' => REDIS_HOST, 'port' => REDIS_PORT, 'timeout' => 881);
27+
$options = array('prefix' => TEST_PREFIX);
28+
29+
$sdkConfig = array(
30+
'log' => array('adapter' => 'stdout'),
31+
'cache' => array('adapter' => 'predis', 'parameters' => $parameters, 'options' => $options)
32+
);
33+
34+
//Initializing the SDK instance.
35+
$splitFactory = \SplitIO\Sdk::factory('asdqwe123456', $sdkConfig);
4236
$this->assertNotNull($splitFactory->client());
4337

4438
$logger = $this->getMockedLogger();
45-
46-
$logger->expects($this->once())
39+
$logger->expects($this->any())
4740
->method('warning')
48-
->with($this->equalTo("Factory Instantiation: You already have an instance of the Split factory. "
49-
. "Make sure you definitely want this additional instance. We recommend keeping only one instance "
50-
. "of the factory at all times (Singleton pattern) and reusing it throughout your application."));
51-
52-
$splitFactory2 = $this->getFactoryClient();
41+
->with($this->logicalOr(
42+
$this->equalTo("Factory Instantiation: You already have 1 factory/factories with this API Key. "
43+
. "We recommend keeping only one instance of the factory at all times (Singleton pattern) and reusing "
44+
. "it throughout your application."),
45+
$this->equalTo("Factory Instantiation: You already have 2 factory/factories with this API Key. "
46+
. "We recommend keeping only one instance of the factory at all times (Singleton pattern) and reusing "
47+
. "it throughout your application."),
48+
$this->equalTo("Factory Instantiation: You already have an instance of the Split factory. "
49+
. "Make sure you definitely want this additional instance. We recommend keeping only one instance "
50+
. "of the factory at all times (Singleton pattern) and reusing it throughout your application.")
51+
)
52+
);
53+
54+
//Initializing the SDK instance2.
55+
$splitFactory2 = \SplitIO\Sdk::factory('asdqwe123456', $sdkConfig);
5356
$this->assertNotNull($splitFactory2->client());
57+
58+
//Initializing the SDK instance3.
59+
$splitFactory3 = \SplitIO\Sdk::factory('asdqwe123456', $sdkConfig);
60+
$this->assertNotNull($splitFactory3->client());
61+
62+
//Initializing the SDK instance4.
63+
$splitFactory4 = \SplitIO\Sdk::factory('other', $sdkConfig);
64+
$this->assertNotNull($splitFactory4->client());
5465
}
5566
}

tests/Suite/InputValidation/GetTreatmentValidationTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class GetTreatmentValidationTest extends \PHPUnit\Framework\TestCase
1010
{
1111
private function getFactoryClient()
1212
{
13+
ReflectiveTools::overrideTracker();
1314
$parameters = array('scheme' => 'redis', 'host' => REDIS_HOST, 'port' => REDIS_PORT, 'timeout' => 881);
1415
$options = array('prefix' => TEST_PREFIX);
1516

tests/Suite/InputValidation/GetTreatmentsValidationTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class GetTreatmentsValidationTest extends \PHPUnit\Framework\TestCase
1010
{
1111
private function getFactoryClient()
1212
{
13+
ReflectiveTools::overrideTracker();
1314
$parameters = array('scheme' => 'redis', 'host' => REDIS_HOST, 'port' => REDIS_PORT, 'timeout' => 881);
1415
$options = array('prefix' => TEST_PREFIX);
1516

tests/Suite/InputValidation/ManagerValidationTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ class ManagerValidationTest extends \PHPUnit\Framework\TestCase
77
{
88
private function getFactoryClient()
99
{
10+
ReflectiveTools::overrideTracker();
1011
$parameters = array('scheme' => 'redis', 'host' => REDIS_HOST, 'port' => REDIS_PORT, 'timeout' => 881);
1112
$options = array('prefix' => TEST_PREFIX);
1213

tests/Suite/InputValidation/TrackValidationTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class TrackValidationTest extends \PHPUnit\Framework\TestCase
1010
{
1111
private function getFactoryClient()
1212
{
13+
ReflectiveTools::overrideTracker();
1314
$parameters = array('scheme' => 'redis', 'host' => REDIS_HOST, 'port' => REDIS_PORT, 'timeout' => 881);
1415
$options = array('prefix' => TEST_PREFIX);
1516

tests/Suite/Redis/ReflectiveTools.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,13 @@ public static function resetIPAddress()
7373
$property->setAccessible(true);
7474
$property->setValue($di, "");
7575
}
76+
77+
public static function overrideTracker()
78+
{
79+
$di = Di::getInstance();
80+
$reflection = new \ReflectionClass('SplitIO\Component\Common\Di');
81+
$property = $reflection->getProperty('factoryTracker');
82+
$property->setAccessible(true);
83+
$property->setValue($di, array());
84+
}
7685
}

0 commit comments

Comments
 (0)