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

Commit

Permalink
Merge pull request #20 from loveOSS/remove-place-suffix
Browse files Browse the repository at this point in the history
Removed place suffixes
  • Loading branch information
mickaelandrieu authored Jul 28, 2019
2 parents 653b819 + 5e242cc commit cc96a31
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 49 deletions.
1 change: 0 additions & 1 deletion src/Places/AbstractPlace.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Resiliency\Contracts\CircuitBreaker;
use Resiliency\Contracts\Transaction;
use Resiliency\Contracts\Service;
use Resiliency\Contracts\Place;
use Resiliency\Contracts\Event;
use Resiliency\Exceptions\InvalidPlace;
Expand Down
2 changes: 1 addition & 1 deletion src/Places/ClosedPlace.php → src/Places/Closed.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* The circuit-breaker executes actions placed through it, measuring the failures and successes of those actions.
* If the failures exceed a certain threshold, the circuit will break (open).
*/
final class ClosedPlace extends AbstractPlace
final class Closed extends AbstractPlace
{
/**
* @var Client the client
Expand Down
5 changes: 2 additions & 3 deletions src/Places/HalfOpenPlace.php → src/Places/HalfOpened.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@
use Resiliency\Events\Closed;
use Resiliency\Events\ReOpened;
use Resiliency\Exceptions\UnavailableService;
use Resiliency\Transitions;
use Resiliency\States;

/**
* When the circuit is half-open:
* When the circuit is half-opened:
*
* the next action will be treated as a trial, to determine the circuit's health.
*
Expand All @@ -21,7 +20,7 @@
*
* If the call throws no exception, the circuit transitions back to closed.
*/
final class HalfOpenPlace extends AbstractPlace
final class HalfOpened extends AbstractPlace
{
/**
* @var Client the client
Expand Down
2 changes: 1 addition & 1 deletion src/Places/IsolatedPlace.php → src/Places/Isolated.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* This state is manually triggered to ensure the Circuit Breaker
* remains open until we reset it.
*/
class IsolatedPlace extends AbstractPlace
class Isolated extends AbstractPlace
{
public function __construct()
{
Expand Down
6 changes: 3 additions & 3 deletions src/Places/OpenPlace.php → src/Places/Opened.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
namespace Resiliency\Places;

use Resiliency\Contracts\Transaction;
use Resiliency\Events\Opened;
use Resiliency\Events\Opened as OpenedEvent;
use Resiliency\States;
use DateTime;

/**
* While the circuit is in an open state: every call to the service
* won't be executed and the fallback callback is executed.
*/
final class OpenPlace extends AbstractPlace
final class Opened extends AbstractPlace
{
/**
* @param float $threshold the Place threshold
Expand All @@ -35,7 +35,7 @@ public function getState(): string
public function call(Transaction $transaction, callable $fallback): string
{
$service = $transaction->getService();
$this->dispatch(new Opened($this->circuitBreaker, $service));
$this->dispatch(new OpenedEvent($this->circuitBreaker, $service));

if (!($transaction->getThresholdDateTime() < new DateTime())) {
return (string) $fallback();
Expand Down
25 changes: 13 additions & 12 deletions src/Systems/MainSystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,23 @@

namespace Resiliency\Systems;

use Resiliency\Contracts\Client;
use Resiliency\Places\IsolatedPlace;
use Resiliency\States;
use Resiliency\Contracts\Client;
use Resiliency\Contracts\Place;
use Resiliency\Contracts\System;
use Resiliency\Places\OpenPlace;
use Resiliency\Places\ClosedPlace;
use Resiliency\Places\HalfOpenPlace;
use Resiliency\Exceptions\InvalidSystem;
use Resiliency\Places\Opened;
use Resiliency\Places\Closed;
use Resiliency\Places\Isolated;
use Resiliency\Places\HalfOpened;

/**
* Implement the system described by the documentation.
* The main system is built with 3 places:
* The main system is built with 4 places:
* - A Closed place
* - A Half Open Place
* - An Open Place
* - A Half Opened Place
* - An Opened Place
* - An Isolated Place
*/
final class MainSystem implements System
{
Expand All @@ -40,10 +41,10 @@ public function __construct(
float $strippedTimeout,
float $threshold
) {
$closedPlace = new ClosedPlace($client, $failures, $timeout);
$halfOpenPlace = new HalfOpenPlace($client, $strippedTimeout);
$openPlace = new OpenPlace($threshold);
$isolatedPlace = new IsolatedPlace();
$closedPlace = new Closed($client, $failures, $timeout);
$halfOpenPlace = new HalfOpened($client, $strippedTimeout);
$openPlace = new Opened($threshold);
$isolatedPlace = new Isolated();

$this->places = [
$closedPlace->getState() => $closedPlace,
Expand Down
26 changes: 13 additions & 13 deletions tests/CircuitBreakerWorkflowTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
use Psr\EventDispatcher\EventDispatcherInterface;
use Resiliency\Exceptions\InvalidSystem;
use Resiliency\Contracts\Exception;
use Resiliency\Places\ClosedPlace;
use Resiliency\Places\IsolatedPlace;
use Resiliency\Places\OpenPlace;
use Resiliency\Places\Closed;
use Resiliency\Places\Isolated;
use Resiliency\Places\Opened;
use Symfony\Component\Cache\Simple\ArrayCache;
use Resiliency\Contracts\CircuitBreaker;
use Resiliency\Storages\SymfonyCache;
Expand All @@ -32,7 +32,7 @@ class CircuitBreakerWorkflowTest extends CircuitBreakerTestCase
*/
public function testCircuitBreakerIsInClosedStateAtStart(): void
{
$this->assertInstanceOf(ClosedPlace::class, $this->circuitBreaker->getState());
$this->assertInstanceOf(Closed::class, $this->circuitBreaker->getState());

$this->assertSame(
'{}',
Expand All @@ -52,12 +52,12 @@ public function testCircuitBreakerIsInClosedStateAtStart(): void
public function testCircuitBreakerWillBeOpenInCaseOfFailures(): void
{
// CLOSED
$this->assertInstanceOf(ClosedPlace::class, $this->circuitBreaker->getState());
$this->assertInstanceOf(Closed::class, $this->circuitBreaker->getState());
$response = $this->circuitBreaker->call('https://httpbin.org/get/foo', $this->createFallbackResponse());
$this->assertSame('{}', $response);

//After two failed calls switch to OPEN state
$this->assertInstanceOf(OpenPlace::class, $this->circuitBreaker->getState());
$this->assertInstanceOf(Opened::class, $this->circuitBreaker->getState());
$this->assertSame(
'{}',
$this->circuitBreaker->call(
Expand All @@ -79,15 +79,15 @@ public function testCircuitBreakerWillBeOpenInCaseOfFailures(): void
public function testOnceInHalfOpenModeServiceIsFinallyReachable(): void
{
// CLOSED - first call fails (twice)
$this->assertInstanceOf(ClosedPlace::class, $this->circuitBreaker->getState());
$this->assertInstanceOf(Closed::class, $this->circuitBreaker->getState());
$response = $this->circuitBreaker->call('https://httpbin.org/get/foo', $this->createFallbackResponse());
$this->assertSame('{}', $response);
$this->assertInstanceOf(OpenPlace::class, $this->circuitBreaker->getState());
$this->assertInstanceOf(Opened::class, $this->circuitBreaker->getState());

// OPEN - no call to client
$response = $this->circuitBreaker->call('https://httpbin.org/get/foo', $this->createFallbackResponse());
$this->assertSame('{}', $response);
$this->assertInstanceOf(OpenPlace::class, $this->circuitBreaker->getState());
$this->assertInstanceOf(Opened::class, $this->circuitBreaker->getState());
$this->waitFor(2 * self::OPEN_THRESHOLD);

// SWITCH TO HALF OPEN and retry to call the service (still in failure)
Expand All @@ -98,7 +98,7 @@ public function testOnceInHalfOpenModeServiceIsFinallyReachable(): void
$this->createFallbackResponse()
)
);
$this->assertInstanceOf(ClosedPlace::class, $this->circuitBreaker->getState());
$this->assertInstanceOf(Closed::class, $this->circuitBreaker->getState());
}

/**
Expand All @@ -115,19 +115,19 @@ public function testOnceCircuitBreakerIsIsolatedNoTrialsAreDone(): void

$response = $this->circuitBreaker->call($uri, $this->createFallbackResponse());
$this->assertSame('{}', $response);
$this->assertInstanceOf(IsolatedPlace::class, $this->circuitBreaker->getState());
$this->assertInstanceOf(Isolated::class, $this->circuitBreaker->getState());

// Let's do 5 calls!

for ($i = 0; $i < 5; ++$i) {
$this->circuitBreaker->call($uri, $this->createFallbackResponse());
$this->assertSame('{}', $response);
$this->assertInstanceOf(IsolatedPlace::class, $this->circuitBreaker->getState());
$this->assertInstanceOf(Isolated::class, $this->circuitBreaker->getState());
}

$this->circuitBreaker->reset($uri);

$this->assertInstanceOf(ClosedPlace::class, $this->circuitBreaker->getState());
$this->assertInstanceOf(Closed::class, $this->circuitBreaker->getState());
}

/**
Expand Down
10 changes: 5 additions & 5 deletions tests/Places/ClosedPlaceTest.php → tests/Places/ClosedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

use Resiliency\Contracts\Client;
use Resiliency\Exceptions\InvalidPlace;
use Resiliency\Places\ClosedPlace;
use Resiliency\Places\Closed;
use Resiliency\States;

class ClosedPlaceTest extends PlaceTestCase
class ClosedTest extends PlaceTestCase
{
/**
* @dataProvider getFixtures
Expand All @@ -19,7 +19,7 @@ class ClosedPlaceTest extends PlaceTestCase
public function testCreationWith($failures, $timeout, $threshold): void
{
$client = $this->createMock(Client::class);
$closedPlace = new ClosedPlace($client, $failures, $timeout);
$closedPlace = new Closed($client, $failures, $timeout);

$this->assertSame($failures, $closedPlace->getFailures());
$this->assertSame($timeout, $closedPlace->getTimeout());
Expand All @@ -37,13 +37,13 @@ public function testCreationWithInvalidValues($failures, $timeout): void
$this->expectException(InvalidPlace::class);
$client = $this->createMock(Client::class);

new ClosedPlace($client, $failures, $timeout);
new Closed($client, $failures, $timeout);
}

public function testGetExpectedState(): void
{
$client = $this->createMock(Client::class);
$closedPlace = new ClosedPlace($client, 1, 1.0);
$closedPlace = new Closed($client, 1, 1.0);

$this->assertSame(States::CLOSED_STATE, $closedPlace->getState());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

use Resiliency\Contracts\Client;
use Resiliency\Exceptions\InvalidPlace;
use Resiliency\Places\HalfOpenPlace;
use Resiliency\Places\HalfOpened;
use Resiliency\States;

class HalfOpenPlaceTest extends PlaceTestCase
class HalfOpenedTest extends PlaceTestCase
{
/**
* @dataProvider getFixtures
Expand All @@ -20,7 +20,7 @@ public function testCreationWith($failures, $timeout, $threshold): void
{
unset($failures, $threshold);
$client = $this->createMock(Client::class);
$halfOpenPlace = new HalfOpenPlace($client, $timeout);
$halfOpenPlace = new HalfOpened($client, $timeout);

$this->assertSame(0, $halfOpenPlace->getFailures());
$this->assertSame($timeout, $halfOpenPlace->getTimeout());
Expand All @@ -40,13 +40,13 @@ public function testCreationWithInvalidValues($failures, $timeout, $threshold):
$this->expectException(InvalidPlace::class);

$client = $this->createMock(Client::class);
new HalfOpenPlace($client, $timeout);
new HalfOpened($client, $timeout);
}

public function testGetExpectedState(): void
{
$client = $this->createMock(Client::class);
$halfOpenPlace = new HalfOpenPlace($client, 1);
$halfOpenPlace = new HalfOpened($client, 1);

$this->assertSame(States::HALF_OPEN_STATE, $halfOpenPlace->getState());
}
Expand Down
10 changes: 5 additions & 5 deletions tests/Places/OpenPlaceTest.php → tests/Places/OpenedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
namespace Tests\Resiliency\Places;

use Resiliency\Exceptions\InvalidPlace;
use Resiliency\Places\OpenPlace;
use Resiliency\Places\Opened;
use Resiliency\States;

class OpenPlaceTest extends PlaceTestCase
class OpenedTest extends PlaceTestCase
{
/**
* @dataProvider getFixtures
Expand All @@ -18,7 +18,7 @@ class OpenPlaceTest extends PlaceTestCase
public function testCreationWith($failures, $timeout, $threshold): void
{
unset($failures, $timeout);
$closedPlace = new OpenPlace($threshold);
$closedPlace = new Opened($threshold);

$this->assertSame(0, $closedPlace->getFailures());
$this->assertSame(0.0, $closedPlace->getTimeout());
Expand All @@ -38,12 +38,12 @@ public function testCreationWithInvalidValues($failures, $timeout, $threshold):

$this->expectException(InvalidPlace::class);

new OpenPlace($threshold);
new Opened($threshold);
}

public function testGetExpectedState()
{
$closedPlace = new OpenPlace(1.0);
$closedPlace = new Opened(1.0);

$this->assertSame(States::OPEN_STATE, $closedPlace->getState());
}
Expand Down

0 comments on commit cc96a31

Please sign in to comment.