123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- <?php
- namespace Illuminate\Tests\Integration\Queue;
- use Exception;
- use Illuminate\Bus\Dispatcher;
- use Illuminate\Bus\Queueable;
- use Illuminate\Contracts\Queue\Job;
- use Illuminate\Foundation\Testing\Concerns\InteractsWithRedis;
- use Illuminate\Queue\CallQueuedHandler;
- use Illuminate\Queue\InteractsWithQueue;
- use Illuminate\Queue\Middleware\ThrottlesExceptionsWithRedis;
- use Illuminate\Support\Str;
- use Mockery as m;
- use Orchestra\Testbench\TestCase;
- class ThrottlesExceptionsWithRedisTest extends TestCase
- {
- use InteractsWithRedis;
- protected function setUp(): void
- {
- parent::setUp();
- $this->setUpRedis();
- }
- protected function tearDown(): void
- {
- parent::tearDown();
- $this->tearDownRedis();
- m::close();
- }
- public function testCircuitIsOpenedForJobErrors()
- {
- $this->assertJobWasReleasedImmediately(CircuitBreakerWithRedisTestJob::class, $key = Str::random());
- $this->assertJobWasReleasedImmediately(CircuitBreakerWithRedisTestJob::class, $key);
- $this->assertJobWasReleasedWithDelay(CircuitBreakerWithRedisTestJob::class, $key);
- }
- public function testCircuitStaysClosedForSuccessfulJobs()
- {
- $this->assertJobRanSuccessfully(CircuitBreakerWithRedisSuccessfulJob::class, $key = Str::random());
- $this->assertJobRanSuccessfully(CircuitBreakerWithRedisSuccessfulJob::class, $key);
- $this->assertJobRanSuccessfully(CircuitBreakerWithRedisSuccessfulJob::class, $key);
- }
- public function testCircuitResetsAfterSuccess()
- {
- $this->assertJobWasReleasedImmediately(CircuitBreakerWithRedisTestJob::class, $key = Str::random());
- $this->assertJobRanSuccessfully(CircuitBreakerWithRedisSuccessfulJob::class, $key);
- $this->assertJobWasReleasedImmediately(CircuitBreakerWithRedisTestJob::class, $key);
- $this->assertJobWasReleasedImmediately(CircuitBreakerWithRedisTestJob::class, $key);
- $this->assertJobWasReleasedWithDelay(CircuitBreakerWithRedisTestJob::class, $key);
- }
- protected function assertJobWasReleasedImmediately($class, $key)
- {
- $class::$handled = false;
- $instance = new CallQueuedHandler(new Dispatcher($this->app), $this->app);
- $job = m::mock(Job::class);
- $job->shouldReceive('hasFailed')->once()->andReturn(false);
- $job->shouldReceive('release')->with(0)->once();
- $job->shouldReceive('isReleased')->andReturn(true);
- $job->shouldReceive('isDeletedOrReleased')->once()->andReturn(true);
- $instance->call($job, [
- 'command' => serialize($command = new $class($key)),
- ]);
- $this->assertTrue($class::$handled);
- }
- protected function assertJobWasReleasedWithDelay($class, $key)
- {
- $class::$handled = false;
- $instance = new CallQueuedHandler(new Dispatcher($this->app), $this->app);
- $job = m::mock(Job::class);
- $job->shouldReceive('hasFailed')->once()->andReturn(false);
- $job->shouldReceive('release')->withArgs(function ($delay) {
- return $delay >= 600;
- })->once();
- $job->shouldReceive('isReleased')->andReturn(true);
- $job->shouldReceive('isDeletedOrReleased')->once()->andReturn(true);
- $instance->call($job, [
- 'command' => serialize($command = new $class($key)),
- ]);
- $this->assertFalse($class::$handled);
- }
- protected function assertJobRanSuccessfully($class, $key)
- {
- $class::$handled = false;
- $instance = new CallQueuedHandler(new Dispatcher($this->app), $this->app);
- $job = m::mock(Job::class);
- $job->shouldReceive('hasFailed')->once()->andReturn(false);
- $job->shouldReceive('isReleased')->andReturn(false);
- $job->shouldReceive('isDeletedOrReleased')->once()->andReturn(false);
- $job->shouldReceive('delete')->once();
- $instance->call($job, [
- 'command' => serialize($command = new $class($key)),
- ]);
- $this->assertTrue($class::$handled);
- }
- }
- class CircuitBreakerWithRedisTestJob
- {
- use InteractsWithQueue, Queueable;
- public static $handled = false;
- public function __construct($key)
- {
- $this->key = $key;
- }
- public function handle()
- {
- static::$handled = true;
- throw new Exception;
- }
- public function middleware()
- {
- return [(new ThrottlesExceptionsWithRedis(2, 10))->by($this->key)];
- }
- }
- class CircuitBreakerWithRedisSuccessfulJob
- {
- use InteractsWithQueue, Queueable;
- public static $handled = false;
- public function __construct($key)
- {
- $this->key = $key;
- }
- public function handle()
- {
- static::$handled = true;
- }
- public function middleware()
- {
- return [(new ThrottlesExceptionsWithRedis(2, 10))->by($this->key)];
- }
- }
|