WorkCommandTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. namespace Illuminate\Tests\Integration\Queue;
  3. use Illuminate\Bus\Queueable;
  4. use Illuminate\Contracts\Queue\ShouldQueue;
  5. use Illuminate\Database\Schema\Blueprint;
  6. use Illuminate\Foundation\Bus\Dispatchable;
  7. use Orchestra\Testbench\TestCase;
  8. use Queue;
  9. class WorkCommandTest extends TestCase
  10. {
  11. protected function getEnvironmentSetUp($app)
  12. {
  13. $app['db']->connection()->getSchemaBuilder()->create('jobs', function (Blueprint $table) {
  14. $table->bigIncrements('id');
  15. $table->string('queue');
  16. $table->longText('payload');
  17. $table->tinyInteger('attempts')->unsigned();
  18. $table->unsignedInteger('reserved_at')->nullable();
  19. $table->unsignedInteger('available_at');
  20. $table->unsignedInteger('created_at');
  21. $table->index(['queue', 'reserved_at']);
  22. });
  23. }
  24. protected function tearDown(): void
  25. {
  26. $this->app['db']->connection()->getSchemaBuilder()->drop('jobs');
  27. parent::tearDown();
  28. FirstJob::$ran = false;
  29. SecondJob::$ran = false;
  30. ThirdJob::$ran = false;
  31. }
  32. public function testRunningOneJob()
  33. {
  34. Queue::connection('database')->push(new FirstJob);
  35. Queue::connection('database')->push(new SecondJob);
  36. $this->artisan('queue:work', [
  37. 'connection' => 'database',
  38. '--once' => true,
  39. '--memory' => 1024,
  40. ])->assertExitCode(0);
  41. $this->assertSame(1, Queue::connection('database')->size());
  42. $this->assertTrue(FirstJob::$ran);
  43. $this->assertFalse(SecondJob::$ran);
  44. }
  45. public function testDaemon()
  46. {
  47. Queue::connection('database')->push(new FirstJob);
  48. Queue::connection('database')->push(new SecondJob);
  49. $this->artisan('queue:work', [
  50. 'connection' => 'database',
  51. '--daemon' => true,
  52. '--stop-when-empty' => true,
  53. '--memory' => 1024,
  54. ])->assertExitCode(0);
  55. $this->assertSame(0, Queue::connection('database')->size());
  56. $this->assertTrue(FirstJob::$ran);
  57. $this->assertTrue(SecondJob::$ran);
  58. }
  59. public function testMemoryExceeded()
  60. {
  61. Queue::connection('database')->push(new FirstJob);
  62. Queue::connection('database')->push(new SecondJob);
  63. $this->artisan('queue:work', [
  64. 'connection' => 'database',
  65. '--daemon' => true,
  66. '--stop-when-empty' => true,
  67. '--memory' => 0.1,
  68. ])->assertExitCode(12);
  69. // Memory limit isn't checked until after the first job is attempted.
  70. $this->assertSame(1, Queue::connection('database')->size());
  71. $this->assertTrue(FirstJob::$ran);
  72. $this->assertFalse(SecondJob::$ran);
  73. }
  74. public function testMaxJobsExceeded()
  75. {
  76. Queue::connection('database')->push(new FirstJob);
  77. Queue::connection('database')->push(new SecondJob);
  78. $this->artisan('queue:work', [
  79. 'connection' => 'database',
  80. '--daemon' => true,
  81. '--stop-when-empty' => true,
  82. '--max-jobs' => 1,
  83. ]);
  84. // Memory limit isn't checked until after the first job is attempted.
  85. $this->assertSame(1, Queue::connection('database')->size());
  86. $this->assertTrue(FirstJob::$ran);
  87. $this->assertFalse(SecondJob::$ran);
  88. }
  89. public function testMaxTimeExceeded()
  90. {
  91. Queue::connection('database')->push(new ThirdJob);
  92. Queue::connection('database')->push(new FirstJob);
  93. Queue::connection('database')->push(new SecondJob);
  94. $this->artisan('queue:work', [
  95. 'connection' => 'database',
  96. '--daemon' => true,
  97. '--stop-when-empty' => true,
  98. '--max-time' => 1,
  99. ]);
  100. // Memory limit isn't checked until after the first job is attempted.
  101. $this->assertSame(2, Queue::connection('database')->size());
  102. $this->assertTrue(ThirdJob::$ran);
  103. $this->assertFalse(FirstJob::$ran);
  104. $this->assertFalse(SecondJob::$ran);
  105. }
  106. }
  107. class FirstJob implements ShouldQueue
  108. {
  109. use Dispatchable, Queueable;
  110. public static $ran = false;
  111. public function handle()
  112. {
  113. static::$ran = true;
  114. }
  115. }
  116. class SecondJob implements ShouldQueue
  117. {
  118. use Dispatchable, Queueable;
  119. public static $ran = false;
  120. public function handle()
  121. {
  122. static::$ran = true;
  123. }
  124. }
  125. class ThirdJob implements ShouldQueue
  126. {
  127. use Dispatchable, Queueable;
  128. public static $ran = false;
  129. public function handle()
  130. {
  131. sleep(1);
  132. static::$ran = true;
  133. }
  134. }