Skip to content

Commit

Permalink
More tests - now at 85% covered
Browse files Browse the repository at this point in the history
  • Loading branch information
justinrainbow committed Jul 22, 2012
1 parent c725ecd commit 3b6a5c9
Show file tree
Hide file tree
Showing 8 changed files with 299 additions and 14 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"symfony/event-dispatcher": "2.1.*@dev"
},
"require-dev": {
"monolog/monolog": "1.*"
"monolog/monolog": "1.*",
"mockery/mockery": "*"
},
"autoload": {
"psr-0": {
Expand Down
6 changes: 5 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/Presque/StatusInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ interface StatusInterface
CONST RUNNING = 4;
CONST EXPIRED = 7;
CONST DYING = 8;
CONST STOPPING = 13;
CONST STOPPED = 14;
}
18 changes: 13 additions & 5 deletions src/Presque/Storage/PredisStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,27 @@ public function __construct(Client $connection, $prefix = null)
$this->connection = $connection;
}

public function setPrefix($prefix = null)
{
$this->prefix = $prefix;
}

public function getPrefix()
{
return $this->prefix;
}

public function push($listName, $payload)
{
$this->connection->lpush($this->getKey($listName), json_encode($payload));
}

public function pop($listName, $waitTimeout = null)
{
if ($payload = $this->connection->blpop($this->getKey($listName), $waitTimeout)) {
if (is_array($payload)) {
return json_decode($payload[1], true);
}
$payload = $this->connection->blpop($this->getKey($listName), $waitTimeout);

return json_decode($payload, true);
if (is_array($payload) && isset($payload[1])) {
return json_decode($payload[1], true);
}

return false;
Expand Down
36 changes: 29 additions & 7 deletions src/Presque/Worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Presque\Event\JobEvent;
use Presque\Event\WorkerEvent;
use Presque\Log\LoggerAwareInterface;
use Presque\Log\LoggerInterface;

class Worker implements WorkerInterface
class Worker implements WorkerInterface, LoggerAwareInterface
{
private $id;
private $queues;
Expand All @@ -24,7 +26,12 @@ class Worker implements WorkerInterface

public function __construct($id = null)
{
if (null === $id) {
$id = gethostname() . ':' . getmypid();
}

$this->id = $id;
$this->queues = new \SplObjectStorage();
}

/**
Expand All @@ -40,16 +47,16 @@ public function getId()
*/
public function addQueue(QueueInterface $queue)
{
$this->queues[] = $queue;
$this->queues->attach($queue);
}

/**
* {@inheritDoc}
*/
public function removeQueue(QueueInterface $queue)
{
if ($key = array_search($queue, $this->queues)) {
unset($this->queues[$queue]);
if ($this->queues->contains($queue)) {
$this->queues->detach($queue);
}
}

Expand Down Expand Up @@ -77,6 +84,14 @@ public function hasEventDispatcher()
return null !== $this->eventDispatcher;
}

/**
* {@inheritDoc}
*/
public function setLogger(LoggerInterface $logger = null)
{
$this->logger = $logger;
}

/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -106,7 +121,7 @@ public function isRunning()
*/
public function isDying()
{
return $this->getStatus() === StatusInterface::DYING;
return $this->getStatus() === StatusInterface::DYING || $this->getStatus() === StatusInterface::STOPPING;
}

/**
Expand All @@ -115,7 +130,7 @@ public function isDying()
public function start()
{
if ($this->hasEventDispatcher()) {
$event = $this->eventDispatcher(Events::WORK_STARTED, new WorkerEvent($this));
$event = $this->eventDispatcher->dispatch(Events::WORK_STARTED, new WorkerEvent($this));

if ($event->isCanceled()) {
return;
Expand All @@ -125,6 +140,13 @@ public function start()
$this->setStatus(StatusInterface::RUNNING);

$this->run();

$this->setStatus(StatusInterface::STOPPED);
}

public function stop()
{
$this->setStatus(StatusInterface::STOPPING);
}

/**
Expand All @@ -136,7 +158,7 @@ public function run()
foreach ($this->getQueues() as $queue) {
$this->process($queue);

if (!$this->isRunning() || $this->isDying()) {
if ($this->isDying() || !$this->isRunning()) {
return;
}
}
Expand Down
60 changes: 60 additions & 0 deletions tests/Presque/Tests/Log/MonologLogAdapterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

/*
* This file is part of the Presque package.
*
* (c) Justin Rainbow <justin.rainbow@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Presque\Tests;

use Presque\Tests\TestCase;
use Presque\Log\MonologLogAdapter;
use Monolog\Logger;

class MonologLogAdapterTest extends TestCase
{
protected $logger;
protected $adapter;

/**
* @dataProvider getLogLevels
*/
public function testLoggingMessages($level, $expectedLevel)
{
$this->logger
->expects($this->once())
->method('addRecord')
->with($this->equalTo($expectedLevel), $this->equalTo("My message"));

$this->adapter->log("My message", $level);
}

public function getLogLevels()
{
return array(
array(LOG_DEBUG, Logger::DEBUG),
array(LOG_INFO, Logger::INFO),
array(LOG_WARNING, Logger::WARNING),
array(LOG_ERR, Logger::ERROR),
array(LOG_CRIT, Logger::CRITICAL),
array(LOG_ALERT, Logger::ALERT),
);
}

protected function setUp()
{
$this->logger = $this->createMonologMock();
$this->adapter = new MonologLogAdapter($this->logger);
}

protected function createMonologMock()
{
return $this->getMockBuilder('Monolog\Logger')
->disableOriginalConstructor()
->getMock();
}
}
79 changes: 79 additions & 0 deletions tests/Presque/Tests/Storage/PredisStorageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

/*
* This file is part of the Presque package.
*
* (c) Justin Rainbow <justin.rainbow@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Presque\Tests\Storage;

use Presque\Tests\TestCase;
use Presque\Storage\PredisStorage;
use Mockery as m;

class PredisStorageTest extends TestCase
{
protected $predis;
protected $storage;

public function testPushingDataOntoList()
{
$payload = array(
'class' => 'test',
'args' => array()
);

$this->predis
->shouldReceive('lpush')
->with('list', json_encode($payload))
->once();

$this->storage->push('list', $payload);
}

public function testPopingDataFromList()
{
$payload = array(
'class' => 'test',
'args' => array()
);

$this->predis
->shouldReceive('blpop')->with('list', 5)->once()->andReturn(array('list', json_encode($payload)))->ordered('pop')
->shouldReceive('blpop')->with('presque:list', 5)->once()->andReturn(array('presque:list', json_encode($payload)))->ordered('pop')
->shouldReceive('blpop')->with('presque:list', 5)->once()->andReturn(false)->ordered('pop')
->shouldReceive('blpop')->with('presque:list', 5)->once()->andReturn(array())->ordered('pop');

$result = $this->storage->pop('list', 5);
$this->assertTrue(is_array($result));
$this->assertArrayHasKey('class', $result);
$this->assertArrayHasKey('args', $result);
$this->assertEquals('test', $result['class']);

$this->storage->setPrefix('presque');
$this->assertEquals('presque', $this->storage->getPrefix());
$result = $this->storage->pop('list', 5);
$this->assertTrue(is_array($result));
$this->assertArrayHasKey('class', $result);
$this->assertArrayHasKey('args', $result);
$this->assertEquals('test', $result['class']);

$this->assertFalse($this->storage->pop('list', 5));
$this->assertFalse($this->storage->pop('list', 5));
}

protected function setUp()
{
$this->predis = $this->createPredisMock();
$this->storage = new PredisStorage($this->predis);
}

protected function createPredisMock()
{
return m::mock('Predis\Client');
}
}
Loading

0 comments on commit 3b6a5c9

Please sign in to comment.