bshaffer/phpunit-retry-annotations
Composer 安装命令:
composer require bshaffer/phpunit-retry-annotations
包简介
Traits for retrying test methods and classes in PHPUnit
README 文档
README
Traits for retrying test methods and classes in PHPUnit.
Installation
composer require --dev bshaffer/phpunit-retry-annotations
Configuring retries
Retry using a specified number of retries
/** * @retryAttempts 2 */ class MyTest extends PHPUnit\Framework\TestCase { use PHPUnitRetry\RetryTrait; public function testSomethingFlakeyTwice() { // Retry a flakey test up to two times } /** * @retryAttempts 3 */ public function testSomethingFlakeyThreeTimes() { // Retry a flakey test up to three times } }
NOTE: "Attempts" represents the number of times a test is retried. Providing "@retryAttempts" a value of 0 has no effect, and would not retry.
Retry until a specific duration has passed
/** * @retryForSeconds 90 */ class MyTest extends PHPUnit\Framework\TestCase { use PHPUnitRetry\RetryTrait; public function testSomethingFlakeyFor90Seconds() { // retries for 90 seconds } /** * @retryForSeconds 1800 */ public function testSomethingFlakeyFor30Minutes() { // retries for 30 minutes } }
Configuring retry conditions
Retry only for certain exceptions
By default, retrying happens when any exception other than
PHPUnit\Framework\IncompleteTestError and PHPUnit\Framework\SkippedTestError
is thrown.
Because you may not always want to retry, you can configure your test to only retry under certain conditions. For example, you can only retry if your tests throw a certain exception.
/** * @retryAttempts 3 * @retryIfException MyApi\ResourceExhaustedException */
You can retry for multiple exceptions.
/** * @retryAttempts 3 * @retryIfException MyApi\RateLimitExceededException * @retryIfException ServiceUnavailableException */
Retry based on a custom method
For more complex logic surrounding whether you should retry, define a custom retry method:
/** * @retryAttempts 3 * @retryIfMethod isRateLimitExceededException */ public function testWithCustomRetryMethod() { // retries only if the method `isRateLimitExceededException` returns true. } /** * @param Exception $e */ private function isRateLimitExceededException(Exception $e) { // Check if HTTP Status code is 429 "Too many requests" return ($e instanceof HttpException && $e->getStatusCode() == 429); }
Define arbitrary arguments for your retry method by passing them into the annotation:
/** * @retryAttempts 3 * @retryIfMethod exceptionStatusCode 429 */ public function testWithCustomRetryMethod() { // retries only if the method `exceptionStatusCode` returns true. } /** * @param Exception $e */ private function exceptionStatusCode(Exception $e, $statusCode) { // Check if HTTP status code is $statusCode return ($e instanceof HttpException && $e->getStatusCode() == $statusCode); }
Configuring delay
Delay for a duration between each retry
/** * @retryAttempts 3 * @retryDelaySeconds 10 */
Delay for an amount increasing exponentially based on the retry attempt
/** * @retryAttempts 3 * @retryDelayMethod exponentialBackoff */
The behavior of the exponentialBackoff method is to start at 1
second and increase to a maximum of 60 seconds. The maximum delay can be
customized by supplying a second argument to the annotation
/** * This test will delay with exponential backoff, with a maximum delay of 10 minutes. * * @retryAttempts 30 * @retryDelayMethod exponentialBackoff 600 */
Define a custom delay method
/** * @retryAttempts 3 * @retryDelayMethod myCustomDelay */ public function testWithCustomDelay() { // retries using the method `myCustomDelay`. } /** * @param int $attempt The current test attempt */ private function myCustomDelay($attempt) { // Doubles the sleep each attempt, but not longer than 10 seconds. sleep(min($attempt * 2, 10)); }
Define arbitrary arguments for your delay function by passing them into the annotation:
/** * @retryAttempts 3 * @retryDelayMethod myCustomDelay 10 60 */ public function testWithCustomDelay() { // retries using the method `myCustomDelay`. } /** * @param int $attempt The current test attempt. * @param int $multiplier Rate of exponential backoff delay. * @param int $maxDelay Maximum time to wait regardless of retry attempt. */ private function myCustomDelay($attempt, $multiplier, $maxDelay) { // Increases exponentially sleep(min($attempt * $multiplier, $max)); }
bshaffer/phpunit-retry-annotations 适用场景与选型建议
bshaffer/phpunit-retry-annotations 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 492.14k 次下载、GitHub Stars 达 23, 最近一次更新时间为 2019 年 12 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「phpunit」 「test」 「retry」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 bshaffer/phpunit-retry-annotations 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 bshaffer/phpunit-retry-annotations 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 bshaffer/phpunit-retry-annotations 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
HiDev plugin for PHPUnit
Emulated timeouts for synchronous operations.
Testing Suite For Lumen like Laravel does.
A cli tool which generates unit tests.
Retry tasks that fail due to transient faults
Polling library for conditionally retry operations based on a result checker.
统计信息
- 总下载量: 492.14k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 23
- 点击次数: 22
- 依赖项目数: 3
- 推荐数: 0
其他信息
- 授权协议: Apache-2.0
- 更新时间: 2019-12-04