-
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 #47 from josegonzalez/4-predis-support
Add PredisEngine. Closes #4.
- Loading branch information
Showing
6 changed files
with
315 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
namespace josegonzalez\Queuesadilla\Engine; | ||
|
||
use Exception; | ||
use josegonzalez\Queuesadilla\Engine\RedisEngine; | ||
use Predis\Client; | ||
use Predis\Connection\ConnectionException; | ||
|
||
class PredisEngine extends RedisEngine | ||
{ | ||
protected $persistentEnabled = false; | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function connect() | ||
{ | ||
try { | ||
$return = parent::connect(); | ||
} catch (Exception $e) { | ||
return false; | ||
} | ||
|
||
return !($return === false); | ||
} | ||
|
||
protected function evalSha($scriptSha, $item) | ||
{ | ||
return (bool)$this->connection()->evalSha( | ||
$scriptSha, | ||
3, | ||
$item['queue'], | ||
rand(), | ||
$item['id'] | ||
); | ||
} | ||
|
||
protected function redisInstance() | ||
{ | ||
return new Client([ | ||
'host' => $this->config('host'), | ||
'port' => $this->config('port'), | ||
'timeout' => (int)$this->config('timeout'), | ||
]); | ||
} | ||
} |
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
233 changes: 233 additions & 0 deletions
233
tests/josegonzalez/Queuesadilla/Engine/PredisEngineTest.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,233 @@ | ||
<?php | ||
|
||
namespace josegonzalez\Queuesadilla\Engine; | ||
|
||
use josegonzalez\Queuesadilla\Engine\PredisEngine; | ||
use josegonzalez\Queuesadilla\FixtureData; | ||
use josegonzalez\Queuesadilla\TestCase; | ||
use Psr\Log\NullLogger; | ||
use Exception as RedisException; | ||
|
||
class PredisEngineTest extends TestCase | ||
{ | ||
public function setUp() | ||
{ | ||
$this->url = getenv('REDIS_URL'); | ||
$this->config = ['url' => $this->url]; | ||
$this->Logger = new NullLogger; | ||
$this->engineClass = 'josegonzalez\Queuesadilla\Engine\PredisEngine'; | ||
$this->Engine = $this->mockEngine(); | ||
$this->Fixtures = new FixtureData; | ||
$this->clearEngine(); | ||
} | ||
|
||
public function tearDown() | ||
{ | ||
$this->clearEngine(); | ||
unset($this->Engine); | ||
} | ||
|
||
/** | ||
* @covers josegonzalez\Queuesadilla\Engine\PredisEngine::__construct | ||
*/ | ||
public function testConstruct() | ||
{ | ||
$Engine = new PredisEngine($this->Logger, []); | ||
$this->assertNotNull($Engine->connection()); | ||
|
||
$Engine = new PredisEngine($this->Logger, $this->url); | ||
$this->assertNotNull($Engine->connection()); | ||
|
||
$Engine = new PredisEngine($this->Logger, $this->config); | ||
$this->assertNotNull($Engine->connection()); | ||
} | ||
|
||
/** | ||
* @covers josegonzalez\Queuesadilla\Engine\PredisEngine::connect | ||
* @covers josegonzalez\Queuesadilla\Engine\PredisEngine::redisInstance | ||
*/ | ||
public function testConnect() | ||
{ | ||
$this->assertTrue($this->Engine->connect()); | ||
|
||
$config = $this->config; | ||
$Engine = $this->mockEngine(null, $config); | ||
$Engine->config('database', 1); | ||
$this->assertTrue($Engine->connect()); | ||
|
||
$config = $this->config; | ||
$Engine = $this->mockEngine(null, $config); | ||
$Engine->config('persistent', false); | ||
$this->assertTrue($Engine->connect()); | ||
} | ||
|
||
/** | ||
* @covers josegonzalez\Queuesadilla\Engine\PredisEngine::connect | ||
* @covers josegonzalez\Queuesadilla\Engine\PredisEngine::redisInstance | ||
*/ | ||
public function testConnectAuth() | ||
{ | ||
$this->markTestIncomplete( | ||
'Predis does not return false on invalid connection' | ||
); | ||
|
||
$config = $this->config; | ||
$Engine = $this->mockEngine(null, $config); | ||
$Engine->config('pass', 'some_password'); | ||
$this->assertFalse($Engine->connect()); | ||
} | ||
|
||
/** | ||
* @covers josegonzalez\Queuesadilla\Engine\PredisEngine::connect | ||
*/ | ||
public function testConnectionException() | ||
{ | ||
$Engine = $this->mockEngine(['redisInstance']); | ||
$Engine->expects($this->once()) | ||
->method('redisInstance') | ||
->will($this->throwException(new RedisException)); | ||
|
||
$this->assertFalse($Engine->connect()); | ||
} | ||
|
||
/** | ||
* @covers josegonzalez\Queuesadilla\Engine\Base::getJobClass | ||
*/ | ||
public function testGetJobClass() | ||
{ | ||
$this->assertEquals('\\josegonzalez\\Queuesadilla\\Job\\Base', $this->Engine->getJobClass()); | ||
} | ||
|
||
/** | ||
* @covers josegonzalez\Queuesadilla\Engine\PredisEngine::acknowledge | ||
* @covers josegonzalez\Queuesadilla\Engine\PredisEngine::evalSha | ||
*/ | ||
public function testAcknowledge() | ||
{ | ||
$this->assertFalse($this->Engine->acknowledge(null)); | ||
$this->assertFalse($this->Engine->acknowledge(false)); | ||
$this->assertFalse($this->Engine->acknowledge(1)); | ||
$this->assertFalse($this->Engine->acknowledge('string')); | ||
$this->assertFalse($this->Engine->acknowledge(['key' => 'value'])); | ||
$this->assertFalse($this->Engine->acknowledge($this->Fixtures->default['first'])); | ||
|
||
$this->assertTrue($this->Engine->push($this->Fixtures->default['first'])); | ||
$this->assertTrue($this->Engine->push($this->Fixtures->other['third'])); | ||
$this->assertTrue($this->Engine->acknowledge($this->Fixtures->default['first'])); | ||
} | ||
|
||
/** | ||
* @covers josegonzalez\Queuesadilla\Engine\PredisEngine::reject | ||
*/ | ||
public function testReject() | ||
{ | ||
$this->assertFalse($this->Engine->reject(null)); | ||
$this->assertFalse($this->Engine->reject(false)); | ||
$this->assertFalse($this->Engine->reject(1)); | ||
$this->assertFalse($this->Engine->reject('string')); | ||
$this->assertFalse($this->Engine->reject(['key' => 'value'])); | ||
$this->assertFalse($this->Engine->reject($this->Fixtures->default['first'])); | ||
|
||
$this->assertTrue($this->Engine->push($this->Fixtures->default['first'])); | ||
$this->assertTrue($this->Engine->push($this->Fixtures->other['third'])); | ||
$this->assertTrue($this->Engine->reject($this->Fixtures->default['first'])); | ||
} | ||
|
||
/** | ||
* @covers josegonzalez\Queuesadilla\Engine\PredisEngine::pop | ||
*/ | ||
public function testPop() | ||
{ | ||
$this->assertNull($this->Engine->pop('default')); | ||
$this->assertTrue($this->Engine->push($this->Fixtures->default['first'], 'default')); | ||
$this->assertEquals($this->Fixtures->default['first'], $this->Engine->pop('default')); | ||
} | ||
|
||
/** | ||
* @covers josegonzalez\Queuesadilla\Engine\PredisEngine::push | ||
*/ | ||
public function testPush() | ||
{ | ||
$this->assertTrue($this->Engine->push($this->Fixtures->default['first'], 'default')); | ||
$this->assertTrue($this->Engine->push($this->Fixtures->default['second'], [ | ||
'delay' => 30, | ||
])); | ||
$this->assertTrue($this->Engine->push($this->Fixtures->other['third'], [ | ||
'expires_in' => 1, | ||
])); | ||
$this->assertTrue($this->Engine->push($this->Fixtures->default['fourth'], 'default')); | ||
|
||
sleep(2); | ||
|
||
$pop1 = $this->Engine->pop(); | ||
$pop2 = $this->Engine->pop(); | ||
$pop3 = $this->Engine->pop(); | ||
$pop4 = $this->Engine->pop(); | ||
|
||
$this->assertNull($pop1['class']); | ||
$this->assertEmpty($pop1['args']); | ||
|
||
$this->markTestIncomplete( | ||
'PredisEngine does not yet implement delay or expires_in (tbd sorted sets)' | ||
); | ||
|
||
$this->assertEquals('yet_another_function', $pop2['class']); | ||
$this->assertNull($pop3); | ||
$this->assertNull($pop4); | ||
} | ||
|
||
/** | ||
* @covers josegonzalez\Queuesadilla\Engine\PredisEngine::pop | ||
* @covers josegonzalez\Queuesadilla\Engine\PredisEngine::release | ||
*/ | ||
public function testRelease() | ||
{ | ||
$this->assertTrue($this->Engine->push($this->Fixtures->default['first'], 'default')); | ||
$this->assertEquals($this->Fixtures->default['first'], $this->Engine->pop('default')); | ||
|
||
$this->assertFalse($this->Engine->release(null, 'default')); | ||
|
||
$this->assertEquals(1, $this->Engine->release($this->Fixtures->default['second'], 'default')); | ||
$this->assertEquals($this->Fixtures->default['second'], $this->Engine->pop('default')); | ||
} | ||
|
||
/** | ||
* @covers josegonzalez\Queuesadilla\Engine\PredisEngine::queues | ||
* @covers josegonzalez\Queuesadilla\Engine\PredisEngine::requireQueue | ||
*/ | ||
public function testQueues() | ||
{ | ||
$this->assertEquals([], $this->Engine->queues()); | ||
$this->Engine->push($this->Fixtures->default['first']); | ||
$this->assertEquals(['default'], $this->Engine->queues()); | ||
|
||
$this->Engine->push($this->Fixtures->other['second'], ['queue' => 'other']); | ||
$queues = $this->Engine->queues(); | ||
sort($queues); | ||
$this->assertEquals(['default', 'other'], $queues); | ||
|
||
$this->Engine->pop(); | ||
$this->Engine->pop(); | ||
$queues = $this->Engine->queues(); | ||
sort($queues); | ||
$this->assertEquals(['default', 'other'], $queues); | ||
} | ||
|
||
protected function clearEngine() | ||
{ | ||
$this->Engine->connection()->flushdb(); | ||
$this->Engine->connection()->script('flush'); | ||
} | ||
|
||
protected function mockEngine($methods = null, $config = null) | ||
{ | ||
if ($config === null) { | ||
$config = $this->config; | ||
} | ||
|
||
return $this->getMockBuilder($this->engineClass) | ||
->setMethods($methods) | ||
->setConstructorArgs([$this->Logger, $config]) | ||
->getMock(); | ||
} | ||
} |
Oops, something went wrong.