1
0
Fork 0
mirror of synced 2025-04-05 06:03:34 +03:00

tests, null for ttl

This commit is contained in:
Akolzin Dmitry 2021-04-01 15:36:12 +03:00
parent 689ca9cbad
commit 530bf56476
3 changed files with 126 additions and 2 deletions

View file

@ -39,7 +39,7 @@ class LockableMessageMiddleware implements MiddlewareInterface
$message = $envelope->getMessage();
if ($envelope->all(ReceivedStamp::class) && $message instanceof LockableMessage) {
$lock = $this->lockFactory->createLock(md5(serialize($message)));
$lock = $this->lockFactory->createLock($this->hash($message), null);
if (!$lock->acquire()) {
return $envelope;
}
@ -55,4 +55,14 @@ class LockableMessageMiddleware implements MiddlewareInterface
return $stack->next()->handle($envelope, $stack);
}
/**
* @param LockableMessage $message
*
* @return string
*/
private function hash(LockableMessage $message): string
{
return md5(serialize($message));
}
}

View file

@ -3,8 +3,9 @@
namespace RetailCrm\ServiceBundle\Tests\DataFixtures;
use RetailCrm\ServiceBundle\Messenger\CommandMessage;
use RetailCrm\ServiceBundle\Messenger\Middleware\LockableMessage;
class TestMessage extends CommandMessage
class TestMessage extends CommandMessage implements LockableMessage
{
public function __construct()
{

View file

@ -0,0 +1,113 @@
<?php
namespace RetailCrm\ServiceBundle\Tests\Messenger\Middleware;
use PHPUnit\Framework\TestCase;
use RetailCrm\ServiceBundle\Messenger\Middleware\LockableMessageMiddleware;
use RetailCrm\ServiceBundle\Tests\DataFixtures\TestMessage;
use Symfony\Component\Lock\Exception\LockConflictedException;
use Symfony\Component\Lock\Key;
use Symfony\Component\Lock\Lock;
use Symfony\Component\Lock\LockFactory;
use Symfony\Component\Lock\PersistingStoreInterface;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Middleware\MiddlewareInterface;
use Symfony\Component\Messenger\Middleware\StackInterface;
use Symfony\Component\Messenger\Stamp\ReceivedStamp;
/**
* Class LockableMessageMiddlewareTest
*
* @package RetailCrm\ServiceBundle\Tests\Messenger\Middleware
*/
class LockableMessageMiddlewareTest extends TestCase
{
/**
* @var LockFactory
*/
private $lockFactory;
protected function setUp(): void
{
$this->lockFactory = $this->createMock(LockFactory::class);
}
public function testHandle(): void
{
$store = $this->createMock(PersistingStoreInterface::class);
$key = new Key(uniqid());
$lock = new Lock($key, $store);
$this->lockFactory->expects(static::once())->method('createLock')->willReturn($lock);
$envelope = new Envelope(new TestMessage(), [new ReceivedStamp('test')]);
$next = $this->createMock(MiddlewareInterface::class);
$next->method('handle')->willReturn($envelope);
$stack = $this->createMock(StackInterface::class);
$stack->method('next')->willReturn($next);
$middleware = new LockableMessageMiddleware($this->lockFactory);
$result = $middleware->handle($envelope, $stack);
static::assertInstanceOf(Envelope::class, $result);
}
public function testLockHandle(): void
{
$store = $this->createMock(PersistingStoreInterface::class);
$store->method('save')->willThrowException(new LockConflictedException);
$key = new Key(uniqid());
$lock = new Lock($key, $store);
$this->lockFactory->expects(static::once())->method('createLock')->willReturn($lock);
$envelope = new Envelope(new TestMessage(), [new ReceivedStamp('test')]);
$next = $this->createMock(MiddlewareInterface::class);
$next->method('handle')->willReturn($envelope);
$stack = $this->createMock(StackInterface::class);
$stack->method('next')->willReturn($next);
$middleware = new LockableMessageMiddleware($this->lockFactory);
$result = $middleware->handle($envelope, $stack);
static::assertInstanceOf(Envelope::class, $result);
}
public function testNonLockableHandle(): void
{
$store = $this->createMock(PersistingStoreInterface::class);
$store->method('save')->willThrowException(new LockConflictedException);
$key = new Key(uniqid());
$lock = new Lock($key, $store);
$this->lockFactory->expects(static::never())->method('createLock')->willReturn($lock);
$envelope = new Envelope(new \stdClass(), [new ReceivedStamp('test')]);
$next = $this->createMock(MiddlewareInterface::class);
$next->method('handle')->willReturn($envelope);
$stack = $this->createMock(StackInterface::class);
$stack->method('next')->willReturn($next);
$middleware = new LockableMessageMiddleware($this->lockFactory);
$result = $middleware->handle($envelope, $stack);
static::assertInstanceOf(Envelope::class, $result);
}
public function testNonReceivedHandle(): void
{
$store = $this->createMock(PersistingStoreInterface::class);
$store->method('save')->willThrowException(new LockConflictedException);
$key = new Key(uniqid());
$lock = new Lock($key, $store);
$this->lockFactory->expects(static::never())->method('createLock')->willReturn($lock);
$envelope = new Envelope(new TestMessage());
$next = $this->createMock(MiddlewareInterface::class);
$next->method('handle')->willReturn($envelope);
$stack = $this->createMock(StackInterface::class);
$stack->method('next')->willReturn($next);
$middleware = new LockableMessageMiddleware($this->lockFactory);
$result = $middleware->handle($envelope, $stack);
static::assertInstanceOf(Envelope::class, $result);
}
}