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

Commit

Permalink
Merge pull request #7 from lemonde/WEB-1282/refactor/circuit-breaker
Browse files Browse the repository at this point in the history
feat(cb): ajoute un trait pour l'utilisation du CircuitBreaker
  • Loading branch information
ludovic-gonthier authored Mar 21, 2017
2 parents bb3b680 + a9d4837 commit 0079b6a
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Service/CircuitBreakerService.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ class CircuitBreakerService
const CLOSED = 'closed';
const HALFOPEN = 'half-open';

const STATUS_UP = true;
const STATUS_DOWN = false;

/**
* @var AbstractAdapter
*/
Expand Down
79 changes: 79 additions & 0 deletions Tests/Traits/CircuitBreakerAwareTraitTest.php
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');
}
}
65 changes: 65 additions & 0 deletions Traits/CircuitBreakerAwareTrait.php
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)
);
}
}

0 comments on commit 0079b6a

Please sign in to comment.