Skip to content
This repository has been archived by the owner on Aug 17, 2022. It is now read-only.

Commit

Permalink
Fixed invalid state on transaction retrieval for SimpleCircuitBreaker
Browse files Browse the repository at this point in the history
  • Loading branch information
mickaelandrieu committed May 13, 2019
1 parent 9721c28 commit 5c57702
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 15 deletions.
6 changes: 3 additions & 3 deletions src/SimpleCircuitBreaker.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@

use Resiliency\Contracts\System;
use Resiliency\Contracts\Client;
use Resiliency\Storages\SimpleArray;
use Resiliency\Contracts\Storage;
use Resiliency\Exceptions\UnavailableService;

/**
* Main implementation of Circuit Breaker.
*/
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);
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/SimpleCircuitBreakerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -25,7 +26,8 @@ public function create(array $settings): CircuitBreaker

return new SimpleCircuitBreaker(
$mainSystem,
$client
$client,
new SimpleArray()
);
}
}
10 changes: 5 additions & 5 deletions src/Storages/SimpleArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ final class SimpleArray implements Storage
/**
* @var array the circuit breaker transactions
*/
public static $transactions = [];
public $transactions = [];

/**
* {@inheritdoc}
Expand All @@ -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;
}
Expand All @@ -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;
Expand All @@ -53,15 +53,15 @@ public function hasTransaction(string $service): bool
{
$key = $this->getKey($service);

return array_key_exists($key, self::$transactions);
return array_key_exists($key, $this->transactions);
}

/**
* {@inheritdoc}
*/
public function clear(): bool
{
self::$transactions = [];
$this->transactions = [];

return true;
}
Expand Down
9 changes: 7 additions & 2 deletions tests/CircuitBreakerWorkflowTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -137,7 +138,7 @@ public function testOnceInHalfOpenModeServiceIsFinallyReachable(CircuitBreaker $
public function getCircuitBreakers(): array
{
return [
'simple' => [$this->createSimpleCircuitBreaker()],
'simple' => [$this->createSimpleCircuitBreaker()],
'symfony' => [$this->createSymfonyCircuitBreaker()],
];
}
Expand All @@ -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()
);
}

/**
Expand Down
8 changes: 4 additions & 4 deletions tests/Storages/SimpleArrayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class SimpleArrayTest extends TestCase
protected function setUp(): void
{
$simpleArray = new SimpleArray();
$simpleArray::$transactions = [];
$simpleArray->transactions = [];
}

/**
Expand All @@ -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);
}

Expand All @@ -42,7 +42,7 @@ public function testSaveTransaction()
$this->createMock(Transaction::class)
);
$this->assertTrue($operation);
$this->assertCount(1, $simpleArray::$transactions);
$this->assertCount(1, $simpleArray->transactions);
}

/**
Expand Down Expand Up @@ -107,7 +107,7 @@ public function testClear()

// We have stored 2 transactions
$simpleArray->clear();
$transactions = $simpleArray::$transactions;
$transactions = $simpleArray->transactions;
$this->assertEmpty($transactions);
}
}

0 comments on commit 5c57702

Please sign in to comment.