From 5c5770255596b0cba17e9ea9c001ad2466a1a07c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micka=C3=ABl=20Andrieu?= Date: Mon, 13 May 2019 13:20:27 +0200 Subject: [PATCH] Fixed invalid state on transaction retrieval for SimpleCircuitBreaker --- src/SimpleCircuitBreaker.php | 6 +++--- src/SimpleCircuitBreakerFactory.php | 4 +++- src/Storages/SimpleArray.php | 10 +++++----- tests/CircuitBreakerWorkflowTest.php | 9 +++++++-- tests/Storages/SimpleArrayTest.php | 8 ++++---- 5 files changed, 22 insertions(+), 15 deletions(-) diff --git a/src/SimpleCircuitBreaker.php b/src/SimpleCircuitBreaker.php index 6cb9d97..3e27466 100644 --- a/src/SimpleCircuitBreaker.php +++ b/src/SimpleCircuitBreaker.php @@ -4,7 +4,7 @@ use Resiliency\Contracts\System; use Resiliency\Contracts\Client; -use Resiliency\Storages\SimpleArray; +use Resiliency\Contracts\Storage; use Resiliency\Exceptions\UnavailableService; /** @@ -12,9 +12,9 @@ */ final class SimpleCircuitBreaker extends PartialCircuitBreaker { - public function __construct(System $system, Client $client) + public function __construct(System $system, Client $client, Storage $storage) { - parent::__construct($system, $client, new SimpleArray()); + parent::__construct($system, $client, $storage); } /** diff --git a/src/SimpleCircuitBreakerFactory.php b/src/SimpleCircuitBreakerFactory.php index 1381e91..d505d27 100644 --- a/src/SimpleCircuitBreakerFactory.php +++ b/src/SimpleCircuitBreakerFactory.php @@ -6,6 +6,7 @@ use Resiliency\Contracts\Factory; use Resiliency\Clients\GuzzleClient; use Resiliency\Systems\MainSystem; +use Resiliency\Storages\SimpleArray; /** * Main implementation of Circuit Breaker Factory @@ -25,7 +26,8 @@ public function create(array $settings): CircuitBreaker return new SimpleCircuitBreaker( $mainSystem, - $client + $client, + new SimpleArray() ); } } diff --git a/src/Storages/SimpleArray.php b/src/Storages/SimpleArray.php index 65ce525..861c8e2 100644 --- a/src/Storages/SimpleArray.php +++ b/src/Storages/SimpleArray.php @@ -14,7 +14,7 @@ final class SimpleArray implements Storage /** * @var array the circuit breaker transactions */ - public static $transactions = []; + public $transactions = []; /** * {@inheritdoc} @@ -23,7 +23,7 @@ public function saveTransaction(string $service, Transaction $transaction): bool { $key = $this->getKey($service); - self::$transactions[$key] = $transaction; + $this->transactions[$key] = $transaction; return true; } @@ -36,7 +36,7 @@ public function getTransaction(string $service): Transaction $key = $this->getKey($service); if ($this->hasTransaction($service)) { - $transaction = self::$transactions[$key]; + $transaction = $this->transactions[$key]; if ($transaction instanceof Transaction) { return $transaction; @@ -53,7 +53,7 @@ public function hasTransaction(string $service): bool { $key = $this->getKey($service); - return array_key_exists($key, self::$transactions); + return array_key_exists($key, $this->transactions); } /** @@ -61,7 +61,7 @@ public function hasTransaction(string $service): bool */ public function clear(): bool { - self::$transactions = []; + $this->transactions = []; return true; } diff --git a/tests/CircuitBreakerWorkflowTest.php b/tests/CircuitBreakerWorkflowTest.php index b6c769a..be76be3 100644 --- a/tests/CircuitBreakerWorkflowTest.php +++ b/tests/CircuitBreakerWorkflowTest.php @@ -4,6 +4,7 @@ use Resiliency\Contracts\CircuitBreaker; use Resiliency\Storages\SymfonyCache; +use Resiliency\Storages\SimpleArray; use Resiliency\SymfonyCircuitBreaker; use Resiliency\SimpleCircuitBreaker; use Symfony\Component\EventDispatcher\Event; @@ -137,7 +138,7 @@ public function testOnceInHalfOpenModeServiceIsFinallyReachable(CircuitBreaker $ public function getCircuitBreakers(): array { return [ - 'simple' => [$this->createSimpleCircuitBreaker()], + 'simple' => [$this->createSimpleCircuitBreaker()], 'symfony' => [$this->createSymfonyCircuitBreaker()], ]; } @@ -147,7 +148,11 @@ public function getCircuitBreakers(): array */ private function createSimpleCircuitBreaker(): SimpleCircuitBreaker { - return new SimpleCircuitBreaker($this->getSystem(), $this->getTestClient()); + return new SimpleCircuitBreaker( + $this->getSystem(), + $this->getTestClient(), + new SimpleArray() + ); } /** diff --git a/tests/Storages/SimpleArrayTest.php b/tests/Storages/SimpleArrayTest.php index 16bda6a..cf349e8 100644 --- a/tests/Storages/SimpleArrayTest.php +++ b/tests/Storages/SimpleArrayTest.php @@ -15,7 +15,7 @@ class SimpleArrayTest extends TestCase protected function setUp(): void { $simpleArray = new SimpleArray(); - $simpleArray::$transactions = []; + $simpleArray->transactions = []; } /** @@ -25,7 +25,7 @@ public function testCreation() { $simpleArray = new SimpleArray(); - $this->assertCount(0, $simpleArray::$transactions); + $this->assertCount(0, $simpleArray->transactions); $this->assertInstanceOf(SimpleArray::class, $simpleArray); } @@ -42,7 +42,7 @@ public function testSaveTransaction() $this->createMock(Transaction::class) ); $this->assertTrue($operation); - $this->assertCount(1, $simpleArray::$transactions); + $this->assertCount(1, $simpleArray->transactions); } /** @@ -107,7 +107,7 @@ public function testClear() // We have stored 2 transactions $simpleArray->clear(); - $transactions = $simpleArray::$transactions; + $transactions = $simpleArray->transactions; $this->assertEmpty($transactions); } }