|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Test\MyCLabs\Work\FunctionalTest\Beanstalkd; |
| 4 | + |
| 5 | +use MyCLabs\Work\Adapter\Beanstalkd\BeanstalkdWorkDispatcher; |
| 6 | +use MyCLabs\Work\Adapter\Beanstalkd\BeanstalkdWorker; |
| 7 | +use Pheanstalk_Pheanstalk; |
| 8 | +use PHPUnit_Framework_TestCase; |
| 9 | +use Test\MyCLabs\Work\FunctionalTest\FakeTask; |
| 10 | + |
| 11 | +/** |
| 12 | + * Test executing tasks through Beanstalkd |
| 13 | + */ |
| 14 | +class BeanstalkdTest extends PHPUnit_Framework_TestCase |
| 15 | +{ |
| 16 | + const QUEUE_PREFIX = 'myclabs_work_test'; |
| 17 | + |
| 18 | + /** |
| 19 | + * @var Pheanstalk_Pheanstalk |
| 20 | + */ |
| 21 | + private $connection; |
| 22 | + |
| 23 | + /** |
| 24 | + * @var string |
| 25 | + */ |
| 26 | + private $tube; |
| 27 | + |
| 28 | + public function setUp() |
| 29 | + { |
| 30 | + $this->connection = new Pheanstalk_Pheanstalk('127.0.0.1'); |
| 31 | + if (! $this->connection->getConnection()->isServiceListening()) { |
| 32 | + // Beanstalkd not installed, mark test skipped |
| 33 | + $this->markTestSkipped('Beanstalkd is not installed or was not found'); |
| 34 | + return; |
| 35 | + } |
| 36 | + $this->tube = self::QUEUE_PREFIX . '_' . rand(); |
| 37 | + } |
| 38 | + |
| 39 | + public function testSimpleRun() |
| 40 | + { |
| 41 | + $dispatcher = new BeanstalkdWorkDispatcher($this->connection, $this->tube); |
| 42 | + |
| 43 | + // Pile up a task to execute |
| 44 | + $task = new FakeTask(); |
| 45 | + $dispatcher->run($task); |
| 46 | + |
| 47 | + // Run the worker to execute the task |
| 48 | + $worker = new BeanstalkdWorker($this->connection, $this->tube); |
| 49 | + |
| 50 | + // Check that event methods are called |
| 51 | + $listener = $this->getMock('MyCLabs\Work\Worker\Event\WorkerEventListener'); |
| 52 | + $listener->expects($this->once()) |
| 53 | + ->method('beforeTaskExecution'); |
| 54 | + $listener->expects($this->once()) |
| 55 | + ->method('onTaskSuccess'); |
| 56 | + $worker->registerEventListener($listener); |
| 57 | + |
| 58 | + // Fake task executor |
| 59 | + $taskExecutor = $this->getMockForAbstractClass('MyCLabs\Work\TaskExecutor\TaskExecutor'); |
| 60 | + $taskExecutor->expects($this->once()) |
| 61 | + ->method('execute') |
| 62 | + ->with($task); |
| 63 | + $worker->registerTaskExecutor(get_class($task), $taskExecutor); |
| 64 | + |
| 65 | + // Work |
| 66 | + $worker->work(1); |
| 67 | + } |
| 68 | + |
| 69 | + public function testRunWithException() |
| 70 | + { |
| 71 | + $workDispatcher = new BeanstalkdWorkDispatcher($this->connection, $this->tube); |
| 72 | + |
| 73 | + // Pile up a task to execute |
| 74 | + $task = new FakeTask(); |
| 75 | + $workDispatcher->run($task); |
| 76 | + |
| 77 | + // Run the worker to execute the task |
| 78 | + $worker = new BeanstalkdWorker($this->connection, $this->tube); |
| 79 | + |
| 80 | + // Check that event methods are called |
| 81 | + $listener = $this->getMock('MyCLabs\Work\Worker\Event\WorkerEventListener'); |
| 82 | + $listener->expects($this->once()) |
| 83 | + ->method('beforeTaskExecution'); |
| 84 | + $listener->expects($this->once()) |
| 85 | + ->method('onTaskError'); |
| 86 | + $worker->registerEventListener($listener); |
| 87 | + |
| 88 | + // Fake task executor |
| 89 | + $taskExecutor = $this->getMockForAbstractClass('MyCLabs\Work\TaskExecutor\TaskExecutor'); |
| 90 | + $taskExecutor->expects($this->once()) |
| 91 | + ->method('execute') |
| 92 | + ->with($task) |
| 93 | + ->will($this->throwException(new \Exception())); |
| 94 | + $worker->registerTaskExecutor(get_class($task), $taskExecutor); |
| 95 | + |
| 96 | + // Work |
| 97 | + $worker->work(1); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Test a scenario where tasks are piling up in Beanstalkd |
| 102 | + */ |
| 103 | + public function testTaskQueuing() |
| 104 | + { |
| 105 | + $workDispatcher = new BeanstalkdWorkDispatcher($this->connection, $this->tube); |
| 106 | + |
| 107 | + // Queue 2 tasks |
| 108 | + $workDispatcher->run(new FakeTask(), 0.1); |
| 109 | + $workDispatcher->run(new FakeTask(), 0.1); |
| 110 | + |
| 111 | + $file = __DIR__ . '/worker.php'; |
| 112 | + |
| 113 | + // Process first task |
| 114 | + $status = shell_exec("php $file {$this->tube} 0 2>&1"); |
| 115 | + $this->assertSame("ok", trim($status)); |
| 116 | + |
| 117 | + // Process second task |
| 118 | + $status = shell_exec("php $file {$this->tube} 0 2>&1"); |
| 119 | + $this->assertSame("ok", trim($status)); |
| 120 | + } |
| 121 | +} |
0 commit comments