ThrottlesExceptionsWithRedisTest.php 4.8 KB

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