WorkerTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. <?php
  2. namespace think\test\queue;
  3. use Carbon\Carbon;
  4. use Mockery as m;
  5. use Mockery\MockInterface;
  6. use RuntimeException;
  7. use think\Cache;
  8. use think\Event;
  9. use think\exception\Handle;
  10. use think\Queue;
  11. use think\queue\connector\Sync;
  12. use think\queue\event\JobExceptionOccurred;
  13. use think\queue\event\JobFailed;
  14. use think\queue\event\JobProcessed;
  15. use think\queue\event\JobProcessing;
  16. use think\queue\exception\MaxAttemptsExceededException;
  17. class WorkerTest extends TestCase
  18. {
  19. /** @var Handle|MockInterface */
  20. protected $handle;
  21. /** @var Event|MockInterface */
  22. protected $event;
  23. /** @var Cache|MockInterface */
  24. protected $cache;
  25. /** @var Queue|MockInterface */
  26. protected $queue;
  27. protected function setUp()
  28. {
  29. parent::setUp();
  30. $this->queue = m::mock(Queue::class);
  31. $this->handle = m::spy(Handle::class);
  32. $this->event = m::spy(Event::class);
  33. $this->cache = m::spy(Cache::class);
  34. }
  35. public function testJobCanBeFired()
  36. {
  37. $worker = $this->getWorker(['default' => [$job = new WorkerFakeJob]]);
  38. $this->event->shouldReceive('trigger')->with(m::type(JobProcessing::class))->once();
  39. $this->event->shouldReceive('trigger')->with(m::type(JobProcessed::class))->once();
  40. $worker->runNextJob('sync', 'default');
  41. }
  42. public function testWorkerCanWorkUntilQueueIsEmpty()
  43. {
  44. $worker = $this->getWorker(['default' => [
  45. $firstJob = new WorkerFakeJob,
  46. $secondJob = new WorkerFakeJob,
  47. ]]);
  48. $this->expectException(LoopBreakerException::class);
  49. $worker->daemon('sync', 'default');
  50. $this->assertTrue($firstJob->fired);
  51. $this->assertTrue($secondJob->fired);
  52. $this->assertSame(0, $worker->stoppedWithStatus);
  53. $this->event->shouldHaveReceived('trigger')->with(m::type(JobProcessing::class))->twice();
  54. $this->event->shouldHaveReceived('trigger')->with(m::type(JobProcessed::class))->twice();
  55. }
  56. public function testJobCanBeFiredBasedOnPriority()
  57. {
  58. $worker = $this->getWorker([
  59. 'high' => [
  60. $highJob = new WorkerFakeJob,
  61. $secondHighJob = new WorkerFakeJob,
  62. ],
  63. 'low' => [$lowJob = new WorkerFakeJob],
  64. ]);
  65. $worker->runNextJob('sync', 'high,low');
  66. $this->assertTrue($highJob->fired);
  67. $this->assertFalse($secondHighJob->fired);
  68. $this->assertFalse($lowJob->fired);
  69. $worker->runNextJob('sync', 'high,low');
  70. $this->assertTrue($secondHighJob->fired);
  71. $this->assertFalse($lowJob->fired);
  72. $worker->runNextJob('sync', 'high,low');
  73. $this->assertTrue($lowJob->fired);
  74. }
  75. public function testExceptionIsReportedIfConnectionThrowsExceptionOnJobPop()
  76. {
  77. $e = new RuntimeException();
  78. $sync = m::mock(Sync::class);
  79. $sync->shouldReceive('pop')->andReturnUsing(function () use ($e) {
  80. throw $e;
  81. });
  82. $this->queue->shouldReceive('driver')->with('sync')->andReturn($sync);
  83. $worker = new Worker($this->queue, $this->event, $this->handle);
  84. $worker->runNextJob('sync', 'default');
  85. $this->handle->shouldHaveReceived('report')->with($e);
  86. }
  87. public function testWorkerSleepsWhenQueueIsEmpty()
  88. {
  89. $worker = $this->getWorker(['default' => []]);
  90. $worker->runNextJob('sync', 'default', 0, 5);
  91. $this->assertEquals(5, $worker->sleptFor);
  92. }
  93. public function testJobIsReleasedOnException()
  94. {
  95. $e = new RuntimeException;
  96. $job = new WorkerFakeJob(function () use ($e) {
  97. throw $e;
  98. });
  99. $worker = $this->getWorker(['default' => [$job]]);
  100. $worker->runNextJob('sync', 'default', 10);
  101. $this->assertEquals(10, $job->releaseAfter);
  102. $this->assertFalse($job->deleted);
  103. $this->handle->shouldHaveReceived('report')->with($e);
  104. $this->event->shouldHaveReceived('trigger')->with(m::type(JobExceptionOccurred::class))->once();
  105. $this->event->shouldNotHaveReceived('trigger', [m::type(JobProcessed::class)]);
  106. }
  107. public function testJobIsNotReleasedIfItHasExceededMaxAttempts()
  108. {
  109. $e = new RuntimeException;
  110. $job = new WorkerFakeJob(function ($job) use ($e) {
  111. // In normal use this would be incremented by being popped off the queue
  112. $job->attempts++;
  113. throw $e;
  114. });
  115. $job->attempts = 1;
  116. $worker = $this->getWorker(['default' => [$job]]);
  117. $worker->runNextJob('sync', 'default', 0, 3, 1);
  118. $this->assertNull($job->releaseAfter);
  119. $this->assertTrue($job->deleted);
  120. $this->assertEquals($e, $job->failedWith);
  121. $this->handle->shouldHaveReceived('report')->with($e);
  122. $this->event->shouldHaveReceived('trigger')->with(m::type(JobExceptionOccurred::class))->once();
  123. $this->event->shouldHaveReceived('trigger')->with(m::type(JobFailed::class))->once();
  124. $this->event->shouldNotHaveReceived('trigger', [m::type(JobProcessed::class)]);
  125. }
  126. public function testJobIsNotReleasedIfItHasExpired()
  127. {
  128. $e = new RuntimeException;
  129. $job = new WorkerFakeJob(function ($job) use ($e) {
  130. // In normal use this would be incremented by being popped off the queue
  131. $job->attempts++;
  132. throw $e;
  133. });
  134. $job->timeoutAt = Carbon::now()->addSeconds(1)->getTimestamp();
  135. $job->attempts = 0;
  136. Carbon::setTestNow(
  137. Carbon::now()->addSeconds(1)
  138. );
  139. $worker = $this->getWorker(['default' => [$job]]);
  140. $worker->runNextJob('sync', 'default');
  141. $this->assertNull($job->releaseAfter);
  142. $this->assertTrue($job->deleted);
  143. $this->assertEquals($e, $job->failedWith);
  144. $this->handle->shouldHaveReceived('report')->with($e);
  145. $this->event->shouldHaveReceived('trigger')->with(m::type(JobExceptionOccurred::class))->once();
  146. $this->event->shouldHaveReceived('trigger')->with(m::type(JobFailed::class))->once();
  147. $this->event->shouldNotHaveReceived('trigger', [m::type(JobProcessed::class)]);
  148. }
  149. public function testJobIsFailedIfItHasAlreadyExceededMaxAttempts()
  150. {
  151. $job = new WorkerFakeJob(function ($job) {
  152. $job->attempts++;
  153. });
  154. $job->attempts = 2;
  155. $worker = $this->getWorker(['default' => [$job]]);
  156. $worker->runNextJob('sync', 'default', 0, 3, 1);
  157. $this->assertNull($job->releaseAfter);
  158. $this->assertTrue($job->deleted);
  159. $this->assertInstanceOf(MaxAttemptsExceededException::class, $job->failedWith);
  160. $this->handle->shouldHaveReceived('report')->with(m::type(MaxAttemptsExceededException::class));
  161. $this->event->shouldHaveReceived('trigger')->with(m::type(JobExceptionOccurred::class))->once();
  162. $this->event->shouldHaveReceived('trigger')->with(m::type(JobFailed::class))->once();
  163. $this->event->shouldNotHaveReceived('trigger', [m::type(JobProcessed::class)]);
  164. }
  165. public function testJobIsFailedIfItHasAlreadyExpired()
  166. {
  167. $job = new WorkerFakeJob(function ($job) {
  168. $job->attempts++;
  169. });
  170. $job->timeoutAt = Carbon::now()->addSeconds(2)->getTimestamp();
  171. $job->attempts = 1;
  172. Carbon::setTestNow(
  173. Carbon::now()->addSeconds(3)
  174. );
  175. $worker = $this->getWorker(['default' => [$job]]);
  176. $worker->runNextJob('sync', 'default');
  177. $this->assertNull($job->releaseAfter);
  178. $this->assertTrue($job->deleted);
  179. $this->assertInstanceOf(MaxAttemptsExceededException::class, $job->failedWith);
  180. $this->handle->shouldHaveReceived('report')->with(m::type(MaxAttemptsExceededException::class));
  181. $this->event->shouldHaveReceived('trigger')->with(m::type(JobExceptionOccurred::class))->once();
  182. $this->event->shouldHaveReceived('trigger')->with(m::type(JobFailed::class))->once();
  183. $this->event->shouldNotHaveReceived('trigger', [m::type(JobProcessed::class)]);
  184. }
  185. public function testJobBasedMaxRetries()
  186. {
  187. $job = new WorkerFakeJob(function ($job) {
  188. $job->attempts++;
  189. });
  190. $job->attempts = 2;
  191. $job->maxTries = 10;
  192. $worker = $this->getWorker(['default' => [$job]]);
  193. $worker->runNextJob('sync', 'default', 0, 3, 1);
  194. $this->assertFalse($job->deleted);
  195. $this->assertNull($job->failedWith);
  196. }
  197. protected function getWorker($jobs)
  198. {
  199. $sync = m::mock(Sync::class);
  200. $sync->shouldReceive('pop')->andReturnUsing(function ($queue) use (&$jobs) {
  201. return array_shift($jobs[$queue]);
  202. });
  203. $this->queue->shouldReceive('driver')->with('sync')->andReturn($sync);
  204. return new Worker($this->queue, $this->event, $this->handle, $this->cache);
  205. }
  206. }
  207. class WorkerFakeConnector
  208. {
  209. public $jobs = [];
  210. public function __construct($jobs)
  211. {
  212. $this->jobs = $jobs;
  213. }
  214. public function pop($queue)
  215. {
  216. return array_shift($this->jobs[$queue]);
  217. }
  218. }
  219. class Worker extends \think\queue\Worker
  220. {
  221. public $sleptFor;
  222. public $stoppedWithStatus;
  223. public function sleep($seconds)
  224. {
  225. $this->sleptFor = $seconds;
  226. }
  227. public function stop($status = 0)
  228. {
  229. $this->stoppedWithStatus = $status;
  230. throw new LoopBreakerException;
  231. }
  232. protected function stopIfNecessary($job, $lastRestart, $memory)
  233. {
  234. if (is_null($job)) {
  235. $this->stop();
  236. } else {
  237. parent::stopIfNecessary($job, $lastRestart, $memory);
  238. }
  239. }
  240. }
  241. class WorkerFakeJob
  242. {
  243. public $fired = false;
  244. public $callback;
  245. public $deleted = false;
  246. public $releaseAfter;
  247. public $released = false;
  248. public $maxTries;
  249. public $timeoutAt;
  250. public $attempts = 0;
  251. public $failedWith;
  252. public $failed = false;
  253. public $connectionName;
  254. public function __construct($callback = null)
  255. {
  256. $this->callback = $callback ?: function () {
  257. //
  258. };
  259. }
  260. public function fire()
  261. {
  262. $this->fired = true;
  263. $this->callback->__invoke($this);
  264. }
  265. public function payload()
  266. {
  267. return [];
  268. }
  269. public function maxTries()
  270. {
  271. return $this->maxTries;
  272. }
  273. public function timeoutAt()
  274. {
  275. return $this->timeoutAt;
  276. }
  277. public function delete()
  278. {
  279. $this->deleted = true;
  280. }
  281. public function isDeleted()
  282. {
  283. return $this->deleted;
  284. }
  285. public function release($delay)
  286. {
  287. $this->released = true;
  288. $this->releaseAfter = $delay;
  289. }
  290. public function isReleased()
  291. {
  292. return $this->released;
  293. }
  294. public function attempts()
  295. {
  296. return $this->attempts;
  297. }
  298. public function markAsFailed()
  299. {
  300. $this->failed = true;
  301. }
  302. public function failed($e)
  303. {
  304. $this->markAsFailed();
  305. $this->failedWith = $e;
  306. }
  307. public function hasFailed()
  308. {
  309. return $this->failed;
  310. }
  311. public function timeout()
  312. {
  313. return time() + 60;
  314. }
  315. public function getName()
  316. {
  317. return 'WorkerFakeJob';
  318. }
  319. }
  320. class LoopBreakerException extends RuntimeException
  321. {
  322. //
  323. }