|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Illuminate\Tests\Integration\Queue; |
| 4 | + |
| 5 | +use Exception; |
| 6 | +use Illuminate\Bus\Dispatcher; |
| 7 | +use Illuminate\Bus\Queueable; |
| 8 | +use Illuminate\Contracts\Queue\Job; |
| 9 | +use Illuminate\Queue\CallQueuedHandler; |
| 10 | +use Illuminate\Queue\InteractsWithQueue; |
| 11 | +use Illuminate\Queue\Middleware\CircuitBreaker; |
| 12 | +use Mockery as m; |
| 13 | +use Orchestra\Testbench\TestCase; |
| 14 | + |
| 15 | +/** |
| 16 | + * @group integration |
| 17 | + */ |
| 18 | +class CircuitBreakerTest extends TestCase |
| 19 | +{ |
| 20 | + protected function tearDown(): void |
| 21 | + { |
| 22 | + parent::tearDown(); |
| 23 | + |
| 24 | + m::close(); |
| 25 | + } |
| 26 | + |
| 27 | + public function testCircuitIsOpenedForJobErrors() |
| 28 | + { |
| 29 | + $this->assertJobWasReleasedImmediately(CircuitBreakerTestJob::class); |
| 30 | + $this->assertJobWasReleasedImmediately(CircuitBreakerTestJob::class); |
| 31 | + $this->assertJobWasReleasedWithDelay(CircuitBreakerTestJob::class); |
| 32 | + } |
| 33 | + |
| 34 | + public function testCircuitStaysClosedForSuccessfulJobs() |
| 35 | + { |
| 36 | + $this->assertJobRanSuccessfully(CircuitBreakerSuccessfulJob::class); |
| 37 | + $this->assertJobRanSuccessfully(CircuitBreakerSuccessfulJob::class); |
| 38 | + $this->assertJobRanSuccessfully(CircuitBreakerSuccessfulJob::class); |
| 39 | + } |
| 40 | + |
| 41 | + public function testCircuitResetsAfterSuccess() |
| 42 | + { |
| 43 | + $this->assertJobWasReleasedImmediately(CircuitBreakerTestJob::class); |
| 44 | + $this->assertJobRanSuccessfully(CircuitBreakerSuccessfulJob::class); |
| 45 | + $this->assertJobWasReleasedImmediately(CircuitBreakerTestJob::class); |
| 46 | + $this->assertJobWasReleasedImmediately(CircuitBreakerTestJob::class); |
| 47 | + $this->assertJobWasReleasedWithDelay(CircuitBreakerTestJob::class); |
| 48 | + } |
| 49 | + |
| 50 | + protected function assertJobWasReleasedImmediately($class) |
| 51 | + { |
| 52 | + $class::$handled = false; |
| 53 | + $instance = new CallQueuedHandler(new Dispatcher($this->app), $this->app); |
| 54 | + |
| 55 | + $job = m::mock(Job::class); |
| 56 | + |
| 57 | + $job->shouldReceive('hasFailed')->once()->andReturn(false); |
| 58 | + $job->shouldReceive('release')->with(0)->once(); |
| 59 | + $job->shouldReceive('isReleased')->andReturn(true); |
| 60 | + $job->shouldReceive('isDeletedOrReleased')->once()->andReturn(true); |
| 61 | + |
| 62 | + $instance->call($job, [ |
| 63 | + 'command' => serialize($command = new $class), |
| 64 | + ]); |
| 65 | + |
| 66 | + $this->assertTrue($class::$handled); |
| 67 | + } |
| 68 | + |
| 69 | + protected function assertJobWasReleasedWithDelay($class) |
| 70 | + { |
| 71 | + $class::$handled = false; |
| 72 | + $instance = new CallQueuedHandler(new Dispatcher($this->app), $this->app); |
| 73 | + |
| 74 | + $job = m::mock(Job::class); |
| 75 | + |
| 76 | + $job->shouldReceive('hasFailed')->once()->andReturn(false); |
| 77 | + $job->shouldReceive('release')->withArgs(function ($delay) { |
| 78 | + return $delay >= 600; |
| 79 | + })->once(); |
| 80 | + $job->shouldReceive('isReleased')->andReturn(true); |
| 81 | + $job->shouldReceive('isDeletedOrReleased')->once()->andReturn(true); |
| 82 | + |
| 83 | + $instance->call($job, [ |
| 84 | + 'command' => serialize($command = new $class), |
| 85 | + ]); |
| 86 | + |
| 87 | + $this->assertFalse($class::$handled); |
| 88 | + } |
| 89 | + |
| 90 | + protected function assertJobRanSuccessfully($class) |
| 91 | + { |
| 92 | + $class::$handled = false; |
| 93 | + $instance = new CallQueuedHandler(new Dispatcher($this->app), $this->app); |
| 94 | + |
| 95 | + $job = m::mock(Job::class); |
| 96 | + |
| 97 | + $job->shouldReceive('hasFailed')->once()->andReturn(false); |
| 98 | + $job->shouldReceive('isReleased')->andReturn(false); |
| 99 | + $job->shouldReceive('isDeletedOrReleased')->once()->andReturn(false); |
| 100 | + $job->shouldReceive('delete')->once(); |
| 101 | + |
| 102 | + $instance->call($job, [ |
| 103 | + 'command' => serialize($command = new $class), |
| 104 | + ]); |
| 105 | + |
| 106 | + $this->assertTrue($class::$handled); |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +class CircuitBreakerTestJob |
| 111 | +{ |
| 112 | + use InteractsWithQueue, Queueable; |
| 113 | + |
| 114 | + public static $handled = false; |
| 115 | + |
| 116 | + public function handle() |
| 117 | + { |
| 118 | + static::$handled = true; |
| 119 | + |
| 120 | + throw new Exception; |
| 121 | + } |
| 122 | + |
| 123 | + public function middleware() |
| 124 | + { |
| 125 | + return [new CircuitBreaker(2, 10, 0, 'test')]; |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +class CircuitBreakerSuccessfulJob |
| 130 | +{ |
| 131 | + use InteractsWithQueue, Queueable; |
| 132 | + |
| 133 | + public static $handled = false; |
| 134 | + |
| 135 | + public function handle() |
| 136 | + { |
| 137 | + static::$handled = true; |
| 138 | + } |
| 139 | + |
| 140 | + public function middleware() |
| 141 | + { |
| 142 | + return [new CircuitBreaker(2, 10, 0, 'test')]; |
| 143 | + } |
| 144 | +} |
0 commit comments