Skip to content

Commit

Permalink
Tests for retry strategy extension
Browse files Browse the repository at this point in the history
  • Loading branch information
JanMikes committed Jul 19, 2023
1 parent 18f6fc8 commit 0f03932
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
/composer.lock

# Tests
/coverage.xml
/coverage.xml
/coverage.html
108 changes: 108 additions & 0 deletions tests/Cases/DI/MessengerExtension.retryStrategy.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php declare(strict_types = 1);

namespace Tests\Cases\DI;

use Contributte\Messenger\DI\MessengerExtension;
use Contributte\Tester\Toolkit;
use Nette\DI\Compiler;
use ReflectionProperty;
use Symfony\Component\Messenger\Bridge\Redis\Transport\RedisTransport;
use Symfony\Component\Messenger\Exception\InvalidArgumentException;
use Symfony\Component\Messenger\Middleware\MiddlewareInterface;
use Symfony\Component\Messenger\Retry\MultiplierRetryStrategy;
use Symfony\Component\Messenger\Transport\InMemory\InMemoryTransport;
use Symfony\Component\Messenger\Transport\Serialization\PhpSerializer;
use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface;
use Symfony\Component\Messenger\Transport\Sync\SyncTransport;
use Symfony\Component\Messenger\Transport\TransportFactoryInterface;
use Symfony\Component\Messenger\Transport\TransportInterface;
use Tester\Assert;
use Tests\Mocks\RetryStrategy\CustomRetryStrategy;
use Tests\Toolkit\Container;
use Tests\Toolkit\Helpers;

require_once __DIR__ . '/../../bootstrap.php';

// Default retry strategy is used and can be disabled
Toolkit::test(function (): void {
$container = Container::of()
->withDefaults()
->withCompiler(function (Compiler $compiler): void {
$compiler->addConfig(Helpers::neon(<<<'NEON'
messenger:
transport:
with_default_retry:
dsn: amqp://
without_retry:
dsn: amqp://
retryStrategy: null
NEON
));
})
->build();

Assert::type(MultiplierRetryStrategy::class, $container->getByName('messenger.transport.with_default_retry.retry_strategy'));
Assert::false($container->hasService('messenger.transport.without_retry.retry_strategy'));
});


// Test retry strategy options
Toolkit::test(static function () {
$container = Container::of()
->withDefaults()
->withCompiler(function (Compiler $compiler): void {
$compiler->addConfig(Helpers::neon(<<<'NEON'
messenger:
transport:
with_retry:
dsn: amqp://
retryStrategy:
maxRetries: 10
delay: 10000
multiplier: 20
maxDelay: 2000000
NEON
));
})
->build();

$strategy = $container->getByName('messenger.transport.with_retry.retry_strategy');

Assert::type(MultiplierRetryStrategy::class, $strategy);

$maxRetriesReflection = new ReflectionProperty(MultiplierRetryStrategy::class, 'maxRetries');
Assert::same(10, $maxRetriesReflection->getValue($strategy));

$delayReflection = new ReflectionProperty(MultiplierRetryStrategy::class, 'delayMilliseconds');
Assert::same(10000, $delayReflection->getValue($strategy));

$multiplierReflection = new ReflectionProperty(MultiplierRetryStrategy::class, 'multiplier');
Assert::same(20.0, $multiplierReflection->getValue($strategy));

$maxDelayReflection = new ReflectionProperty(MultiplierRetryStrategy::class, 'maxDelayMilliseconds');
Assert::same(2000000, $maxDelayReflection->getValue($strategy));
});

// Test strategy with custom params
Toolkit::test(static function () {
$container = Container::of()
->withDefaults()
->withCompiler(function (Compiler $compiler): void {
$compiler->addConfig(Helpers::neon(<<<'NEON'
services:
customStrategy: Tests\Mocks\RetryStrategy\CustomRetryStrategy
messenger:
transport:
custom_strategy:
dsn: amqp://
retryStrategy:
service: @customStrategy
NEON
));
})
->build();

Assert::type(CustomRetryStrategy::class, $container->getByName('messenger.transport.custom_strategy.retry_strategy'));
});
20 changes: 20 additions & 0 deletions tests/Mocks/RetryStrategy/CustomRetryStrategy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
declare(strict_types=1);

namespace Tests\Mocks\RetryStrategy;

use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Retry\RetryStrategyInterface;

final class CustomRetryStrategy implements RetryStrategyInterface
{
public function isRetryable(Envelope $message, \Throwable $throwable = null): bool
{
return true;
}

public function getWaitingTime(Envelope $message, \Throwable $throwable = null): int
{
return 1;
}
}

0 comments on commit 0f03932

Please sign in to comment.