mirror of
https://github.com/predis/predis.git
synced 2026-08-24 01:49:36 +00:00
980326d5d8
* Initial work on retries * Added retry class and test coverage * Added support for standalone and cluster * Make TimeoutException instance of CommunicationException * Added pipeline, trasnaction, replication support * Fixed broken test * Marked test as relay-incompatible * Marked test as relay-incompatible * Fixed analysis errors, added missing tests * Codestyle fixes * Fixed test * Update README.md * Update README.md * Update README.md * Updated README.md * Refactor retry on read and write * Added check for timeout value * Updated README.md * Fixed README.md * Codestyle changes * Added missing coverage * Added missing test coverage * Removed comments * Added retry support for Relay connection (#1620) * Added integration test case with mocked retry * Changed client initialisation in tests * Marked test as relay-incompatible --------- Co-authored-by: Pavlo Yatsukhnenko <yatsukhnenko@users.noreply.github.com>
71 lines
2.1 KiB
PHP
71 lines
2.1 KiB
PHP
<?php
|
|
|
|
/*
|
|
* This file is part of the Predis package.
|
|
*
|
|
* (c) 2009-2020 Daniele Alessandri
|
|
* (c) 2021-2025 Till Krüss
|
|
*
|
|
* For the full copyright and license information, please view the LICENSE
|
|
* file that was distributed with this source code.
|
|
*/
|
|
|
|
namespace Predis\Retry\Strategy;
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
class ExponentialBackoffTest extends TestCase
|
|
{
|
|
/**
|
|
* @group disconnected
|
|
* @return void
|
|
*/
|
|
public function testCompute(): void
|
|
{
|
|
$backoff = new ExponentialBackoff();
|
|
|
|
// Test default cap
|
|
$this->assertLessThanOrEqual(RetryStrategyInterface::DEFAULT_CAP, $backoff->compute(100));
|
|
|
|
// Test default base
|
|
$this->assertGreaterThanOrEqual(RetryStrategyInterface::DEFAULT_BASE, $backoff->compute(0));
|
|
|
|
$interval = $backoff->compute(2);
|
|
|
|
// Test between
|
|
$this->assertGreaterThanOrEqual(RetryStrategyInterface::DEFAULT_BASE, $interval);
|
|
$this->assertLessThanOrEqual(RetryStrategyInterface::DEFAULT_CAP, $interval);
|
|
|
|
$backoff = new ExponentialBackoff(1000000, 10000000);
|
|
|
|
// Test adjusted cap
|
|
$this->assertLessThanOrEqual(10000000, $backoff->compute(100));
|
|
|
|
// Test adjusted base
|
|
$this->assertGreaterThanOrEqual(1000000, $backoff->compute(0));
|
|
|
|
$backoff = new ExponentialBackoff(RetryStrategyInterface::DEFAULT_BASE, -1);
|
|
|
|
// Test with no cap
|
|
$this->assertEquals(RetryStrategyInterface::DEFAULT_BASE * 2, $backoff->compute(1));
|
|
|
|
$backoff = new ExponentialBackoff(
|
|
RetryStrategyInterface::DEFAULT_BASE,
|
|
RetryStrategyInterface::DEFAULT_CAP,
|
|
true
|
|
);
|
|
|
|
$interval = $backoff->compute(0);
|
|
|
|
// Test with jitter - default base
|
|
$this->assertGreaterThanOrEqual(0, $interval);
|
|
$this->assertLessThanOrEqual(RetryStrategyInterface::DEFAULT_BASE, $interval);
|
|
|
|
$interval = $backoff->compute(6);
|
|
|
|
// Test with jitter - default cap
|
|
$this->assertGreaterThanOrEqual(0, $interval);
|
|
$this->assertLessThanOrEqual(RetryStrategyInterface::DEFAULT_CAP, $interval);
|
|
}
|
|
}
|