This repository has been archived by the owner on Aug 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from lemonde/WEB-1282/refactor/circuit-breaker
feat(cb): ajoute un trait pour l'utilisation du CircuitBreaker
- Loading branch information
Showing
3 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
|
||
namespace Tests\CircuitBreakerBundle\Traits; | ||
|
||
use CircuitBreakerBundle\Event\CircuitBreakerEvent; | ||
use CircuitBreakerBundle\Service\CircuitBreakerService; | ||
use CircuitBreakerBundle\Traits\CircuitBreakerAwareTrait; | ||
use Symfony\Component\EventDispatcher\EventDispatcherInterface; | ||
|
||
class CircuitBreakerAwareTraitTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
private $eventDispatcher; | ||
private $circuitBreaker; | ||
|
||
protected function setUp() | ||
{ | ||
$this->eventDispatcher = $this->createMock(EventDispatcherInterface::class); | ||
$this->circuitBreaker = $this->createMock(CircuitBreakerService::class); | ||
} | ||
|
||
|
||
public function testSetCircuitBreaker() | ||
{ | ||
$trait = $this->getMockForTrait(CircuitBreakerAwareTrait::class); | ||
|
||
$trait->setCircuitBreaker($this->circuitBreaker); | ||
|
||
$this->assertSame( | ||
$this->circuitBreaker, | ||
\PHPUnit_Framework_Assert::readAttribute($trait, 'circuitBreaker') | ||
); | ||
} | ||
|
||
public function testSetEventDispatcher() | ||
{ | ||
$trait = $this->getMockForTrait(CircuitBreakerAwareTrait::class); | ||
|
||
$trait->setEventDispatcher($this->eventDispatcher); | ||
|
||
$this->assertSame( | ||
$this->eventDispatcher, | ||
\PHPUnit_Framework_Assert::readAttribute($trait, 'eventDispatcher') | ||
); | ||
} | ||
|
||
public function testNotifyServiceIsDown() | ||
{ | ||
$trait = $this->getMockForTrait(CircuitBreakerAwareTrait::class); | ||
$trait->setEventDispatcher($this->eventDispatcher); | ||
|
||
$this->eventDispatcher | ||
->expects($this->once()) | ||
->method('dispatch') | ||
->with('circuit.breaker', $this->callback(function ($event) { | ||
return $event instanceof CircuitBreakerEvent | ||
&& $event->getKey() === 'test_service' | ||
&& $event->getStatus() === CircuitBreakerService::STATUS_DOWN; | ||
})); | ||
|
||
$trait->notifyServiceIsDown('test_service'); | ||
} | ||
|
||
public function testNotifyServiceIsUp() | ||
{ | ||
$trait = $this->getMockForTrait(CircuitBreakerAwareTrait::class); | ||
$trait->setEventDispatcher($this->eventDispatcher); | ||
|
||
$this->eventDispatcher | ||
->expects($this->once()) | ||
->method('dispatch') | ||
->with('circuit.breaker', $this->callback(function ($event) { | ||
return $event instanceof CircuitBreakerEvent | ||
&& $event->getKey() === 'test_service' | ||
&& $event->getStatus() === CircuitBreakerService::STATUS_UP; | ||
})); | ||
|
||
$trait->notifyServiceIsUp('test_service'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace CircuitBreakerBundle\Traits; | ||
|
||
use CircuitBreakerBundle\Service\CircuitBreakerService; | ||
use CircuitBreakerBundle\Event\CircuitBreakerEvent; | ||
use Symfony\Component\EventDispatcher\EventDispatcherInterface; | ||
|
||
trait CircuitBreakerAwareTrait | ||
{ | ||
/** | ||
* @var CircuitBreakerService | ||
*/ | ||
private $circuitBreaker; | ||
|
||
/** | ||
* @var EventDispatcherInterface | ||
*/ | ||
private $eventDispatcher; | ||
|
||
/** | ||
* Set the circuit breaker service | ||
* | ||
* @param CircuitBreakerService $circuitBreaker | ||
*/ | ||
public function setCircuitBreaker(CircuitBreakerService $circuitBreaker) | ||
{ | ||
$this->circuitBreaker = $circuitBreaker; | ||
} | ||
|
||
/** | ||
* @param EventDispatcherInterface $eventDispatcher | ||
*/ | ||
public function setEventDispatcher(EventDispatcherInterface $eventDispatcher) | ||
{ | ||
$this->eventDispatcher = $eventDispatcher; | ||
} | ||
|
||
|
||
/** | ||
* Send an event to notify the circuit breaker that a given service is down | ||
* | ||
* @param string $serviceName The service which is down | ||
*/ | ||
public function notifyServiceIsDown(string $serviceName) | ||
{ | ||
$this->eventDispatcher->dispatch( | ||
'circuit.breaker', | ||
new CircuitBreakerEvent($serviceName, CircuitBreakerService::STATUS_DOWN) | ||
); | ||
} | ||
|
||
/** | ||
* Send an event to notify the circuit breaker that a given service is up | ||
* | ||
* @param string $serviceName The service which is up | ||
*/ | ||
public function notifyServiceIsUp(string $serviceName) | ||
{ | ||
$this->eventDispatcher->dispatch( | ||
'circuit.breaker', | ||
new CircuitBreakerEvent($serviceName, CircuitBreakerService::STATUS_UP) | ||
); | ||
} | ||
} |