This repository has been archived by the owner on Feb 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #53 from Articus/master
Custom tube names in Beanstalkd for queues
- Loading branch information
Showing
7 changed files
with
199 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace SlmQueueBeanstalkd\Options; | ||
|
||
use Zend\Stdlib\AbstractOptions; | ||
|
||
class QueueOptions extends AbstractOptions | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
protected $tube = ''; | ||
|
||
/** | ||
* Get beanstalkd tube name for queue | ||
* @return string | ||
*/ | ||
public function getTube() | ||
{ | ||
return $this->tube; | ||
} | ||
|
||
/** | ||
* Set beanstalkd tube for queue | ||
* @param string $tube | ||
*/ | ||
public function setTube($tube) | ||
{ | ||
$this->tube = $tube; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
tests/SlmQueueBeanstalkdTest/Queue/BeanstalkdQueueTubeTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
<?php | ||
|
||
namespace SlmQueueBeanstalkdTest\Queue; | ||
|
||
use Pheanstalk\Job as PheanstalkJob; | ||
use PHPUnit_Framework_TestCase as TestCase; | ||
use SlmQueueBeanstalkd\Options\QueueOptions; | ||
use SlmQueueBeanstalkd\Queue\BeanstalkdQueue; | ||
use SlmQueueBeanstalkdTest\Asset\SimpleJob; | ||
|
||
/** | ||
* BeanstalkdQueue Test for custom tube name | ||
*/ | ||
class BeanstalkdQueueTubeTest extends TestCase | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
protected $queueName; | ||
/** | ||
* @var string | ||
*/ | ||
protected $tubeName; | ||
/** | ||
* @var BeanstalkdQueue | ||
*/ | ||
protected $queue; | ||
/** | ||
* @var \Pheanstalk\Pheanstalk|\PHPUnit_Framework_MockObject_MockObject | ||
*/ | ||
protected $pheanstalk; | ||
/** | ||
* @var \SlmQueue\Job\JobPluginManager|\PHPUnit_Framework_MockObject_MockObject | ||
*/ | ||
protected $pluginManager; | ||
|
||
public function setUp() | ||
{ | ||
$this->queueName = 'testQueueName'; | ||
$this->tubeName = 'testQueueTubeName'; | ||
$this->pheanstalk = $this->getMockBuilder('Pheanstalk\Pheanstalk') | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
|
||
$this->pluginManager = $this->getMockBuilder('SlmQueue\Job\JobPluginManager') | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
|
||
$queueOptions = new QueueOptions(); | ||
$queueOptions->setTube($this->tubeName); | ||
|
||
$this->queue = new BeanstalkdQueue($this->pheanstalk, $this->queueName, $this->pluginManager, $queueOptions); | ||
} | ||
|
||
public function testTubeNameGetter() | ||
{ | ||
$tubeName = $this->tubeName; | ||
$result = $this->queue->getTubeName(); | ||
$this->assertEquals($result, $tubeName); | ||
} | ||
|
||
public function testSuccessfulKickWithSelectedTube() | ||
{ | ||
$maxKick = 10; | ||
$tubeName = $this->tubeName; | ||
$pheanstalk = $this->pheanstalk; | ||
|
||
$pheanstalk->expects($this->once()) | ||
->method('useTube') | ||
->with($this->equalTo($tubeName)) | ||
->will($this->returnValue($pheanstalk)); | ||
|
||
$pheanstalk->expects($this->once()) | ||
->method('kick') | ||
->with($this->equalTo($maxKick)) | ||
->will($this->returnValue($maxKick)); | ||
|
||
$result = $this->queue->kick($maxKick); | ||
$this->assertEquals($result, $maxKick); | ||
} | ||
|
||
public function testPopPreservesMetadata() | ||
{ | ||
$pheanstalk = $this->pheanstalk; | ||
$tubeName = $this->tubeName; | ||
$pluginManager = $this->pluginManager; | ||
|
||
$job = new SimpleJob; | ||
$job->setMetadata('foo', 'bar'); | ||
|
||
$pheanstalkJob = new PheanstalkJob(1, $this->queue->serializeJob($job)); | ||
|
||
$pheanstalk->expects($this->once()) | ||
->method('reserveFromTube') | ||
->with($this->equalTo($tubeName)) | ||
->will($this->returnValue($pheanstalkJob)); | ||
|
||
$pluginManager->expects($this->once()) | ||
->method('get') | ||
->with(get_class($job)) | ||
->will($this->returnValue($job)); | ||
|
||
$result = $this->queue->pop(); | ||
|
||
$this->assertEquals($result, $job); | ||
$this->assertEquals('bar', $job->getMetadata('foo')); | ||
} | ||
} |