/** * @param callable $job_runner */ private function checkMessageForJobIsAcknowledged(callable $job_runner) { $uniqid = uniqid(); $expected_job = ['name' => "some-job-{$uniqid}", 'params' => ['value' => $uniqid], 'meta' => ['buffered_job_id' => rand(1, 10)]]; $this->database->insert('queued_jobs', $expected_job['meta']['buffered_job_id'], []); $this->worker_queue->push($expected_job['name'], $expected_job['params'], $expected_job['meta']); $this->worker_queue->runNext($job_runner); $this->message_bank->emulateReconnect(); $this->worker_queue->runNext(function () { }); }
/** * @param int $count */ private function assertJobsAreQueued($count) { $uniqid = uniqid(); for ($i = 1; $i <= $count; $i++) { $row = ['buffered_job_id' => "{$uniqid}-{$i}-id", 'queue_name' => 'default-worker', 'job_name' => "{$uniqid}-{$i}-name", 'job_params' => ["{$uniqid}-{$i}-params"], 'run_after' => date('c'), 'job_rank' => $i, 'mutex_id' => "{$uniqid}-{$i}-mutex"]; $this->database->insert('buffered_jobs', "{$uniqid}-{$i}-id", $row); } $this->superqueue->queueJobsFromDatabaseToWorkerQueue(); $confirmed_messages = 0; for ($i = 1; $i <= $count; $i++) { $mq_message = json_decode($this->message_bank->consumeMessage()->getContent(), true); if ("{$uniqid}-{$i}-name" === $mq_message['name'] && $this->database->has('queued_jobs', "{$uniqid}-{$i}-id")) { ++$confirmed_messages; } } $this->assertSame($count, $confirmed_messages); }
public function acknowledge() { $this->message_bank->acknowledgeMessage($this->message_id); }
/** * @covers ::consumeMessage * @covers ::produceMessage * @covers ::acknowledgeMessage * @covers ::emulateReconnect */ public function testOnlyAckedMessagesComeBackOnReconnect() { $message_bank = new MessageBank(); $acked_message = 'a-' . uniqid(); $unacked_message = 'b-' . uniqid(); $message_bank->produceMessage($acked_message); $message_bank->produceMessage($unacked_message); $consumed_ack_message = $message_bank->consumeMessage(); $this->assertSame($acked_message, $consumed_ack_message->getContent()); $consumed_ack_message->acknowledge(); $this->assertSame($unacked_message, $message_bank->consumeMessage()->getContent()); $message_bank->emulateReconnect(); $this->assertSame($unacked_message, $message_bank->consumeMessage()->getContent()); }
/** * @return int */ public function getMaxTimePerConsume() { return $this->message_bank->getMaxTimePerConsume(); }
/** * @param string[] $messages */ public function produceMessageBatch(array $messages) { foreach ($messages as $message) { $this->message_bank->produceMessage($message->getEncodedContent()); } }