/** * @test */ public function executes() { $filelib = new FileLibrary(new MemoryStorageAdapter(), new MemoryBackendAdapter()); $this->assertFileNotExists(ROOT_TESTS . '/data/temp/ping.txt'); $strategy = new PekkisQueueExecutionStrategy($this->queue); $strategy->attachTo($filelib); $strategy->execute('\\touchMyTrallala', [6]); $this->assertFileNotExists(ROOT_TESTS . '/data/temp/ping.txt'); $processor = new Processor(new EventDispatchingQueue($strategy->getQueue(), $filelib->getEventDispatcher())); $processor->registerHandler(new FilelibMessageHandler()); $result = $processor->process(); $this->assertTrue($result); $this->assertFileExists(ROOT_TESTS . '/data/temp/ping.txt'); }
/** * @test * @dataProvider provideData */ public function messagesAreHandled($successfulResult) { $message = Message::create('test', array('banana' => 'is not just a banaana, banaana')); $this->queue->expects($this->once())->method('dequeue')->will($this->returnValue($message)); $mockHandler2 = $this->getMock('Pekkis\\Queue\\Processor\\MessageHandler'); $mockHandler2->expects($this->never())->method('willHandle'); $mockHandler = $this->getMock('Pekkis\\Queue\\Processor\\MessageHandler'); $mockHandler->expects($this->once())->method('willHandle')->with($message)->will($this->returnValue(true)); $message2 = Message::create('test', array('banana' => 'is not just a banaana, banaana')); $message3 = Message::create('test', array('banana' => 'is not just a banaana, banaana')); $result = new Result($successfulResult); $mockHandler->expects($this->once())->method('handle')->with($message, $this->queue)->will($this->returnValue($result)); if ($successfulResult) { $this->queue->expects($this->once())->method('ack')->with($message); } else { $this->queue->expects($this->never())->method('ack'); } $this->processor->registerHandler($mockHandler2); $this->processor->registerHandler($mockHandler); $this->processor->process(); }
<?php namespace Pekkis\Queue\Example; use Pekkis\Queue\Processor\ConsoleOutputSubscriber; use Pekkis\Queue\Processor\Processor; use Pekkis\Queue\SymfonyBridge\EventDispatchingQueue; require_once __DIR__ . '/real-life-bootstrap.php'; /** @var EventDispatchingQueue $queue */ // Queue processor has it's own console output subscriber to add to our queue. $queue->addSubscriber(new ConsoleOutputSubscriber($output)); $processor = new Processor($queue); // Register our handler to handle reservation request messages $processor->registerHandler(new ReservationHandler()); // A single processor should process only n messages because PHP sometime leaks like a hamster with bladder problems. $processor->processWhile(function ($ret) { static $count = 0; $count = $count + 1; if ($count >= 1000) { return false; } if (!$ret) { sleep(2); } return true; });