2014-08-08 15:06:11 -05:00
|
|
|
<?php
|
|
|
|
|
|
2014-08-10 10:52:59 -05:00
|
|
|
date_default_timezone_set('UTC');
|
|
|
|
|
|
|
|
|
|
require_once __DIR__.'/../misc/cleanup.php';
|
|
|
|
|
|
2014-08-08 15:06:11 -05:00
|
|
|
/**
|
|
|
|
|
* Class UglyQueueTest
|
|
|
|
|
*/
|
|
|
|
|
class UglyQueueTest extends PHPUnit_Framework_TestCase
|
|
|
|
|
{
|
2015-09-29 11:35:48 -05:00
|
|
|
protected $baseDir;
|
|
|
|
|
|
2014-08-08 18:09:45 -05:00
|
|
|
/**
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
|
|
|
|
protected $tastySandwich = array(
|
|
|
|
|
'0' => 'unsalted butter',
|
|
|
|
|
'1' => 'all-purpose flour',
|
|
|
|
|
'2' => 'hot milk',
|
|
|
|
|
'3' => 'kosher salt',
|
|
|
|
|
'4' => 'freshly ground black pepper',
|
|
|
|
|
'5' => 'nutmeg',
|
|
|
|
|
'6' => 'grated Gruyere',
|
|
|
|
|
'7' => 'freshly grated Parmesan',
|
|
|
|
|
'8' => 'white sandwich bread, crust removed',
|
|
|
|
|
'9' => 'Dijon mustard',
|
|
|
|
|
'10' => 'Virginia baked ham, sliced',
|
|
|
|
|
);
|
|
|
|
|
|
2015-09-29 11:35:48 -05:00
|
|
|
protected function setUp()
|
|
|
|
|
{
|
2015-09-29 12:08:32 -05:00
|
|
|
$this->baseDir = realpath(__DIR__.'/../misc/queues');
|
2015-09-29 11:35:48 -05:00
|
|
|
}
|
|
|
|
|
|
2014-08-08 15:06:11 -05:00
|
|
|
/**
|
2015-09-29 11:35:48 -05:00
|
|
|
* @covers \DCarbone\UglyQueue::__construct
|
2014-08-08 15:06:11 -05:00
|
|
|
* @uses \DCarbone\UglyQueue
|
|
|
|
|
* @return \DCarbone\UglyQueue
|
|
|
|
|
*/
|
2015-09-29 11:35:48 -05:00
|
|
|
public function testCanInitializeObjectWithValidParameters()
|
2014-08-08 15:06:11 -05:00
|
|
|
{
|
2015-09-29 11:35:48 -05:00
|
|
|
$uglyQueue = new \DCarbone\UglyQueue($this->baseDir, 'tasty-sandwich');
|
2014-08-08 15:06:11 -05:00
|
|
|
|
2014-08-08 18:09:45 -05:00
|
|
|
$this->assertInstanceOf('\\DCarbone\\UglyQueue', $uglyQueue);
|
|
|
|
|
|
2014-08-08 15:06:11 -05:00
|
|
|
return $uglyQueue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-09-29 11:35:48 -05:00
|
|
|
* @covers \DCarbone\UglyQueue::retrieveItems
|
2014-08-10 14:27:00 -05:00
|
|
|
* @uses \DCarbone\UglyQueue
|
2015-09-29 11:35:48 -05:00
|
|
|
* @depends testCanInitializeObjectWithValidParameters
|
2014-08-10 14:27:00 -05:00
|
|
|
* @expectedException \RuntimeException
|
|
|
|
|
* @param \DCarbone\UglyQueue $uglyQueue
|
|
|
|
|
*/
|
|
|
|
|
public function testExceptionThrownWhenTryingToProcessQueueAfterInitializationBeforeLock(\DCarbone\UglyQueue $uglyQueue)
|
|
|
|
|
{
|
2015-09-30 08:55:18 -05:00
|
|
|
|
2015-09-29 11:35:48 -05:00
|
|
|
$uglyQueue->retrieveItems();
|
2014-08-10 14:27:00 -05:00
|
|
|
}
|
|
|
|
|
|
2014-08-10 13:56:15 -05:00
|
|
|
/**
|
|
|
|
|
* @covers \DCarbone\UglyQueue::keyExistsInQueue
|
|
|
|
|
* @uses \DCarbone\UglyQueue
|
2015-09-29 11:35:48 -05:00
|
|
|
* @depends testCanInitializeObjectWithValidParameters
|
2014-08-10 13:56:15 -05:00
|
|
|
* @param \DCarbone\UglyQueue $uglyQueue
|
|
|
|
|
*/
|
|
|
|
|
public function testKeyExistsInQueueReturnsFalseWithEmptyQueueAfterInitialization(\DCarbone\UglyQueue $uglyQueue)
|
|
|
|
|
{
|
2015-09-30 08:55:18 -05:00
|
|
|
|
2014-08-10 13:56:15 -05:00
|
|
|
$exists = $uglyQueue->keyExistsInQueue(0);
|
|
|
|
|
|
|
|
|
|
$this->assertFalse($exists);
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-10 11:46:06 -05:00
|
|
|
/**
|
2015-09-29 11:35:48 -05:00
|
|
|
* @covers \DCarbone\UglyQueue::addItem
|
2014-08-10 11:46:06 -05:00
|
|
|
* @uses \DCarbone\UglyQueue
|
2015-09-29 11:35:48 -05:00
|
|
|
* @depends testCanInitializeObjectWithValidParameters
|
2014-08-10 11:46:06 -05:00
|
|
|
* @expectedException \RuntimeException
|
|
|
|
|
* @param \DCarbone\UglyQueue $uglyQueue
|
|
|
|
|
*/
|
2014-09-29 17:02:28 -05:00
|
|
|
public function testExceptionThrownWhenTryingToAddItemsToQueueWithoutLock(\DCarbone\UglyQueue $uglyQueue)
|
2014-08-10 11:46:06 -05:00
|
|
|
{
|
2015-09-29 11:35:48 -05:00
|
|
|
$uglyQueue->addItem('test', 'value');
|
2014-08-10 11:46:06 -05:00
|
|
|
}
|
|
|
|
|
|
2014-08-10 10:52:59 -05:00
|
|
|
/**
|
2015-09-29 11:35:48 -05:00
|
|
|
* @covers \DCarbone\UglyQueue::getPath
|
2014-08-10 10:52:59 -05:00
|
|
|
* @uses \DCarbone\UglyQueue
|
2015-09-29 11:35:48 -05:00
|
|
|
* @depends testCanInitializeObjectWithValidParameters
|
2014-08-10 10:52:59 -05:00
|
|
|
* @param \DCarbone\UglyQueue $uglyQueue
|
|
|
|
|
*/
|
2014-09-29 17:02:28 -05:00
|
|
|
public function testCanGetQueueDirectory(\DCarbone\UglyQueue $uglyQueue)
|
2014-08-10 10:52:59 -05:00
|
|
|
{
|
2015-09-29 11:35:48 -05:00
|
|
|
$queuePath = $uglyQueue->getPath();
|
2014-08-10 10:52:59 -05:00
|
|
|
|
2014-09-29 17:02:28 -05:00
|
|
|
$this->assertFileExists($queuePath);
|
2014-08-10 10:52:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-09-29 11:35:48 -05:00
|
|
|
* @covers \DCarbone\UglyQueue::getName
|
2014-08-10 10:52:59 -05:00
|
|
|
* @uses \DCarbone\UglyQueue
|
2015-09-29 11:35:48 -05:00
|
|
|
* @depends testCanInitializeObjectWithValidParameters
|
2014-08-10 10:52:59 -05:00
|
|
|
* @param \DCarbone\UglyQueue $uglyQueue
|
|
|
|
|
*/
|
2014-09-29 17:02:28 -05:00
|
|
|
public function testCanGetQueueName(\DCarbone\UglyQueue $uglyQueue)
|
2014-08-10 10:52:59 -05:00
|
|
|
{
|
2015-09-29 11:35:48 -05:00
|
|
|
$queueName = $uglyQueue->getName();
|
2014-08-10 10:52:59 -05:00
|
|
|
|
2014-09-29 17:02:28 -05:00
|
|
|
$this->assertEquals('tasty-sandwich', $queueName);
|
2014-08-10 10:52:59 -05:00
|
|
|
}
|
|
|
|
|
|
2015-09-30 08:55:18 -05:00
|
|
|
/**
|
|
|
|
|
* @covers \DCarbone\UglyQueue::getMode
|
|
|
|
|
* @depends testCanInitializeObjectWithValidParameters
|
|
|
|
|
* @param \DCarbone\UglyQueue $uglyQueue
|
|
|
|
|
*/
|
|
|
|
|
public function testCanGetQueueMode(\DCarbone\UglyQueue $uglyQueue)
|
|
|
|
|
{
|
|
|
|
|
$mode = $uglyQueue->getMode();
|
|
|
|
|
$this->assertEquals(\DCarbone\UglyQueue::QUEUE_READWRITE, $mode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers \DCarbone\UglyQueue::getBaseDir
|
|
|
|
|
* @depends testCanInitializeObjectWithValidParameters
|
|
|
|
|
* @param \DCarbone\UglyQueue $uglyQueue
|
|
|
|
|
*/
|
|
|
|
|
public function testCanGetBaseDirectory(\DCarbone\UglyQueue $uglyQueue)
|
|
|
|
|
{
|
|
|
|
|
$baseDir = $uglyQueue->getBaseDir();
|
|
|
|
|
$this->assertEquals($this->baseDir, $baseDir);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers \DCarbone\UglyQueue::getLockFile
|
|
|
|
|
* @depends testCanInitializeObjectWithValidParameters
|
|
|
|
|
* @param \DCarbone\UglyQueue $uglyQueue
|
|
|
|
|
*/
|
|
|
|
|
public function testCanGetLockFilePath(\DCarbone\UglyQueue $uglyQueue)
|
|
|
|
|
{
|
|
|
|
|
$lockFile = $uglyQueue->getLockFile();
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
sprintf('%s%s%s%squeue.lock',
|
|
|
|
|
$this->baseDir,
|
|
|
|
|
DIRECTORY_SEPARATOR,
|
|
|
|
|
$uglyQueue->getName(),
|
|
|
|
|
DIRECTORY_SEPARATOR),
|
|
|
|
|
$lockFile
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers \DCarbone\UglyQueue::getQueueFile
|
|
|
|
|
* @depends testCanInitializeObjectWithValidParameters
|
|
|
|
|
* @param \DCarbone\UglyQueue $uglyQueue
|
|
|
|
|
*/
|
|
|
|
|
public function testCanGetQueueFilePath(\DCarbone\UglyQueue $uglyQueue)
|
|
|
|
|
{
|
|
|
|
|
$queueFile = $uglyQueue->getQueueFile();
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
sprintf('%s%s%s%squeue.txt',
|
|
|
|
|
$this->baseDir,
|
|
|
|
|
DIRECTORY_SEPARATOR,
|
|
|
|
|
$uglyQueue->getName(),
|
|
|
|
|
DIRECTORY_SEPARATOR),
|
|
|
|
|
$queueFile
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers \DCarbone\UglyQueue::getQueueTmpFile
|
|
|
|
|
* @depends testCanInitializeObjectWithValidParameters
|
|
|
|
|
* @param \DCarbone\UglyQueue $uglyQueue
|
|
|
|
|
*/
|
|
|
|
|
public function testCanGetQueueTmpFilePath(\DCarbone\UglyQueue $uglyQueue)
|
|
|
|
|
{
|
|
|
|
|
$queueTmpFile = $uglyQueue->getQueueTmpFile();
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
sprintf(
|
|
|
|
|
'%s%s%s%squeue.tmp',
|
|
|
|
|
$this->baseDir,
|
|
|
|
|
DIRECTORY_SEPARATOR,
|
|
|
|
|
$uglyQueue->getName(),
|
|
|
|
|
DIRECTORY_SEPARATOR),
|
|
|
|
|
$queueTmpFile
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers \DCarbone\UglyQueue::getSerializeFile
|
|
|
|
|
* @depends testCanInitializeObjectWithValidParameters
|
|
|
|
|
* @param \DCarbone\UglyQueue $uglyQueue
|
|
|
|
|
*/
|
|
|
|
|
public function testCanGetSerializeFilePath(\DCarbone\UglyQueue $uglyQueue)
|
|
|
|
|
{
|
|
|
|
|
$serializeFile = $uglyQueue->getSerializeFile();
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
sprintf(
|
|
|
|
|
'%s%s%s%sugly-queue.obj',
|
|
|
|
|
$this->baseDir,
|
|
|
|
|
DIRECTORY_SEPARATOR,
|
|
|
|
|
$uglyQueue->getName(),
|
|
|
|
|
DIRECTORY_SEPARATOR),
|
|
|
|
|
$serializeFile
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-29 17:18:14 -05:00
|
|
|
/**
|
2015-09-30 15:12:35 -05:00
|
|
|
* @covers \DCarbone\isAlreadyLocked::isAlreadyLocked
|
2014-09-29 17:18:14 -05:00
|
|
|
* @uses \DCarbone\UglyQueue
|
2015-09-29 11:35:48 -05:00
|
|
|
* @depends testCanInitializeObjectWithValidParameters
|
2014-09-29 17:18:14 -05:00
|
|
|
* @param \DCarbone\UglyQueue $uglyQueue
|
|
|
|
|
*/
|
|
|
|
|
public function testCanGetQueueLockedStatus(\DCarbone\UglyQueue $uglyQueue)
|
|
|
|
|
{
|
2015-09-30 15:12:35 -05:00
|
|
|
$locked = $uglyQueue->isAlreadyLocked();
|
2014-09-29 17:18:14 -05:00
|
|
|
$this->assertFalse($locked);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-09-29 11:35:48 -05:00
|
|
|
* @covers \DCarbone\UglyQueue::count
|
2014-08-10 12:28:26 -05:00
|
|
|
* @uses \DCarbone\UglyQueue
|
2015-09-29 11:35:48 -05:00
|
|
|
* @depends testCanInitializeObjectWithValidParameters
|
2014-08-10 12:28:26 -05:00
|
|
|
* @param \DCarbone\UglyQueue $uglyQueue
|
|
|
|
|
*/
|
2014-09-29 17:02:28 -05:00
|
|
|
public function testGetQueueItemCountReturnsZeroWithEmptyQueue(\DCarbone\UglyQueue $uglyQueue)
|
2014-08-10 12:28:26 -05:00
|
|
|
{
|
2015-09-29 11:35:48 -05:00
|
|
|
$itemCount = count($uglyQueue);
|
2014-08-10 12:28:26 -05:00
|
|
|
$this->assertEquals(0, $itemCount);
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-10 10:52:59 -05:00
|
|
|
/**
|
2015-09-29 11:35:48 -05:00
|
|
|
* @covers \DCarbone\UglyQueue::__construct
|
2014-08-10 10:52:59 -05:00
|
|
|
* @uses \DCarbone\UglyQueue
|
2014-08-10 11:46:06 -05:00
|
|
|
* @return \DCarbone\UglyQueue
|
2014-08-08 18:09:45 -05:00
|
|
|
*/
|
2014-08-10 10:52:59 -05:00
|
|
|
public function testCanInitializeExistingQueue()
|
2014-08-08 18:09:45 -05:00
|
|
|
{
|
2015-09-29 11:35:48 -05:00
|
|
|
$uglyQueue = new \DCarbone\UglyQueue($this->baseDir, 'tasty-sandwich');
|
2014-08-10 10:52:59 -05:00
|
|
|
|
|
|
|
|
$this->assertInstanceOf('\\DCarbone\\UglyQueue', $uglyQueue);
|
|
|
|
|
|
2014-08-10 11:46:06 -05:00
|
|
|
return $uglyQueue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers \DCarbone\UglyQueue::lock
|
|
|
|
|
* @uses \DCarbone\UglyQueue
|
2015-09-29 11:35:48 -05:00
|
|
|
* @depends testCanInitializeObjectWithValidParameters
|
2014-08-10 11:46:06 -05:00
|
|
|
* @expectedException \InvalidArgumentException
|
|
|
|
|
* @param \DCarbone\UglyQueue $uglyQueue
|
|
|
|
|
*/
|
|
|
|
|
public function testExceptionThrownWhenPassingNonIntegerValueToLock(\DCarbone\UglyQueue $uglyQueue)
|
|
|
|
|
{
|
|
|
|
|
$uglyQueue->lock('7 billion');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers \DCarbone\UglyQueue::lock
|
|
|
|
|
* @uses \DCarbone\UglyQueue
|
2015-09-29 11:35:48 -05:00
|
|
|
* @depends testCanInitializeObjectWithValidParameters
|
2014-08-10 11:46:06 -05:00
|
|
|
* @expectedException \InvalidArgumentException
|
|
|
|
|
* @param \DCarbone\UglyQueue $uglyQueue
|
|
|
|
|
*/
|
|
|
|
|
public function testExceptionThrownWhenPassingNegativeIntegerValueToLock(\DCarbone\UglyQueue $uglyQueue)
|
|
|
|
|
{
|
|
|
|
|
$uglyQueue->lock(-73);
|
2014-08-10 10:52:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers \DCarbone\UglyQueue::lock
|
2015-09-30 15:12:35 -05:00
|
|
|
* @covers \DCarbone\isAlreadyLocked::isAlreadyLocked
|
2014-09-29 17:02:28 -05:00
|
|
|
* @covers \DCarbone\UglyQueue::createLockFile
|
2014-08-10 10:52:59 -05:00
|
|
|
* @uses \DCarbone\UglyQueue
|
2015-09-29 11:35:48 -05:00
|
|
|
* @depends testCanInitializeObjectWithValidParameters
|
2014-08-10 10:52:59 -05:00
|
|
|
* @param \DCarbone\UglyQueue $uglyQueue
|
|
|
|
|
* @return \DCarbone\UglyQueue
|
|
|
|
|
*/
|
|
|
|
|
public function testCanLockUglyQueueWithDefaultTTL(\DCarbone\UglyQueue $uglyQueue)
|
|
|
|
|
{
|
|
|
|
|
$locked = $uglyQueue->lock();
|
2014-08-08 18:09:45 -05:00
|
|
|
|
2014-08-10 10:52:59 -05:00
|
|
|
$this->assertTrue($locked);
|
2014-08-08 18:09:45 -05:00
|
|
|
|
2015-09-29 11:35:48 -05:00
|
|
|
$this->assertFileExists($uglyQueue->getLockFile());
|
2014-08-10 10:52:59 -05:00
|
|
|
|
2015-09-29 11:35:48 -05:00
|
|
|
$decode = @json_decode(file_get_contents($uglyQueue->getLockFile()));
|
2014-08-10 10:52:59 -05:00
|
|
|
|
|
|
|
|
$this->assertTrue((json_last_error() === JSON_ERROR_NONE));
|
|
|
|
|
$this->assertObjectHasAttribute('ttl', $decode);
|
|
|
|
|
$this->assertObjectHasAttribute('born', $decode);
|
|
|
|
|
$this->assertEquals(250, $decode->ttl);
|
|
|
|
|
|
|
|
|
|
return $uglyQueue;
|
2014-08-08 18:09:45 -05:00
|
|
|
}
|
2014-08-10 10:52:59 -05:00
|
|
|
|
2014-08-10 11:46:06 -05:00
|
|
|
/**
|
|
|
|
|
* @covers \DCarbone\UglyQueue::lock
|
2015-09-30 15:12:35 -05:00
|
|
|
* @covers \DCarbone\isAlreadyLocked::isAlreadyLocked
|
2014-08-10 11:46:06 -05:00
|
|
|
* @uses \DCarbone\UglyQueue
|
|
|
|
|
* @depends testCanInitializeExistingQueue
|
|
|
|
|
* @param \DCarbone\UglyQueue $uglyQueue
|
|
|
|
|
*/
|
2014-09-29 17:02:28 -05:00
|
|
|
public function testCannotLockQueueThatIsAlreadyLocked(\DCarbone\UglyQueue $uglyQueue)
|
2014-08-10 11:46:06 -05:00
|
|
|
{
|
2015-09-30 08:55:18 -05:00
|
|
|
|
2014-08-10 11:46:06 -05:00
|
|
|
$lock = $uglyQueue->lock();
|
|
|
|
|
|
|
|
|
|
$this->assertFalse($lock);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-09-30 15:12:35 -05:00
|
|
|
* @covers \DCarbone\isAlreadyLocked::isAlreadyLocked
|
2014-08-10 11:46:06 -05:00
|
|
|
* @uses \DCarbone\UglyQueue
|
|
|
|
|
* @depends testCanLockUglyQueueWithDefaultTTL
|
|
|
|
|
* @param \DCarbone\UglyQueue $uglyQueue
|
|
|
|
|
*/
|
2014-09-29 17:02:28 -05:00
|
|
|
public function testIsLockedReturnsTrueAfterLocking(\DCarbone\UglyQueue $uglyQueue)
|
2014-08-10 11:46:06 -05:00
|
|
|
{
|
2015-09-30 08:55:18 -05:00
|
|
|
|
2015-09-30 15:12:35 -05:00
|
|
|
$isLocked = $uglyQueue->isAlreadyLocked();
|
2014-08-10 11:46:06 -05:00
|
|
|
$this->assertTrue($isLocked);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers \DCarbone\UglyQueue::unlock
|
|
|
|
|
* @uses \DCarbone\UglyQueue
|
|
|
|
|
* @uses \DCarbone\Helpers\FileHelper
|
|
|
|
|
* @depends testCanLockUglyQueueWithDefaultTTL
|
|
|
|
|
* @param \DCarbone\UglyQueue $uglyQueue
|
|
|
|
|
* @return \DCarbone\UglyQueue
|
|
|
|
|
*/
|
|
|
|
|
public function testCanUnlockLockedQueue(\DCarbone\UglyQueue $uglyQueue)
|
|
|
|
|
{
|
2015-09-30 08:55:18 -05:00
|
|
|
|
2014-08-10 11:46:06 -05:00
|
|
|
$uglyQueue->unlock();
|
|
|
|
|
|
2015-09-29 11:35:48 -05:00
|
|
|
$this->assertFileNotExists($uglyQueue->getLockFile());
|
2014-08-10 11:46:06 -05:00
|
|
|
|
|
|
|
|
return $uglyQueue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-09-30 15:12:35 -05:00
|
|
|
* @covers \DCarbone\isAlreadyLocked::isAlreadyLocked
|
2014-08-10 11:46:06 -05:00
|
|
|
* @uses \DCarbone\UglyQueue
|
|
|
|
|
* @depends testCanUnlockLockedQueue
|
|
|
|
|
* @param \DCarbone\UglyQueue $uglyQueue
|
|
|
|
|
*/
|
|
|
|
|
public function testIsLockedReturnsFalseAfterUnlockingQueue(\DCarbone\UglyQueue $uglyQueue)
|
|
|
|
|
{
|
2015-09-30 08:55:18 -05:00
|
|
|
|
2015-09-30 15:12:35 -05:00
|
|
|
$isLocked = $uglyQueue->isAlreadyLocked();
|
2014-08-10 11:46:06 -05:00
|
|
|
|
|
|
|
|
$this->assertFalse($isLocked);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers \DCarbone\UglyQueue::lock
|
2015-09-30 15:12:35 -05:00
|
|
|
* @covers \DCarbone\isAlreadyLocked::isAlreadyLocked
|
2014-08-10 11:46:06 -05:00
|
|
|
* @uses \DCarbone\UglyQueue
|
|
|
|
|
* @uses \DCarbone\Helpers\FileHelper
|
|
|
|
|
* @depends testCanUnlockLockedQueue
|
|
|
|
|
* @param \DCarbone\UglyQueue $uglyQueue
|
|
|
|
|
*/
|
|
|
|
|
public function testIsLockedReturnsFalseWithStaleQueueLockFile(\DCarbone\UglyQueue $uglyQueue)
|
|
|
|
|
{
|
2015-09-30 08:55:18 -05:00
|
|
|
|
2014-08-10 11:46:06 -05:00
|
|
|
$uglyQueue->lock(2);
|
2015-09-30 15:12:35 -05:00
|
|
|
$isLocked = $uglyQueue->isAlreadyLocked();
|
2014-08-10 11:46:06 -05:00
|
|
|
$this->assertTrue($isLocked);
|
|
|
|
|
|
|
|
|
|
sleep(3);
|
|
|
|
|
|
2015-09-30 15:12:35 -05:00
|
|
|
$isLocked = $uglyQueue->isAlreadyLocked();
|
2014-08-10 11:46:06 -05:00
|
|
|
$this->assertFalse($isLocked);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers \DCarbone\UglyQueue::lock
|
2015-09-30 15:12:35 -05:00
|
|
|
* @covers \DCarbone\isAlreadyLocked::isAlreadyLocked
|
2014-08-10 11:46:06 -05:00
|
|
|
* @uses \DCarbone\UglyQueue
|
|
|
|
|
* @depends testCanUnlockLockedQueue
|
|
|
|
|
* @param \DCarbone\UglyQueue $uglyQueue
|
|
|
|
|
* @return \DCarbone\UglyQueue
|
|
|
|
|
*/
|
|
|
|
|
public function testCanLockQueueWithValidIntegerValue(\DCarbone\UglyQueue $uglyQueue)
|
|
|
|
|
{
|
2015-09-30 08:55:18 -05:00
|
|
|
|
2014-08-10 11:46:06 -05:00
|
|
|
$locked = $uglyQueue->lock(200);
|
|
|
|
|
|
|
|
|
|
$this->assertTrue($locked);
|
|
|
|
|
|
2015-09-29 11:35:48 -05:00
|
|
|
$this->assertFileExists($uglyQueue->getLockFile());
|
2014-08-10 11:46:06 -05:00
|
|
|
|
2015-09-29 11:35:48 -05:00
|
|
|
$decode = @json_decode(file_get_contents($uglyQueue->getLockFile()));
|
2014-08-10 11:46:06 -05:00
|
|
|
|
|
|
|
|
$this->assertTrue((json_last_error() === JSON_ERROR_NONE));
|
|
|
|
|
$this->assertObjectHasAttribute('ttl', $decode);
|
|
|
|
|
$this->assertObjectHasAttribute('born', $decode);
|
|
|
|
|
$this->assertEquals(200, $decode->ttl);
|
|
|
|
|
|
|
|
|
|
return $uglyQueue;
|
|
|
|
|
}
|
2014-08-10 12:28:26 -05:00
|
|
|
|
|
|
|
|
/**
|
2015-09-29 11:35:48 -05:00
|
|
|
* @covers \DCarbone\UglyQueue::addItem
|
2014-08-10 12:28:26 -05:00
|
|
|
* @uses \DCarbone\UglyQueue
|
2014-08-10 13:56:15 -05:00
|
|
|
* @uses \DCarbone\Helpers\FileHelper
|
2014-08-10 12:28:26 -05:00
|
|
|
* @depends testCanLockQueueWithValidIntegerValue
|
|
|
|
|
* @param \DCarbone\UglyQueue $uglyQueue
|
|
|
|
|
* @return \DCarbone\UglyQueue
|
|
|
|
|
*/
|
2014-08-10 13:56:15 -05:00
|
|
|
public function testCanPopulateQueueTempFileAfterInitializationAndAcquiringLock(\DCarbone\UglyQueue $uglyQueue)
|
2014-08-10 12:28:26 -05:00
|
|
|
{
|
2015-09-30 08:55:18 -05:00
|
|
|
|
2014-08-10 14:27:00 -05:00
|
|
|
foreach(array_reverse($this->tastySandwich, true) as $k=>$v)
|
2014-08-10 12:28:26 -05:00
|
|
|
{
|
2015-09-29 11:35:48 -05:00
|
|
|
$added = $uglyQueue->addItem($k, $v);
|
2014-08-10 12:28:26 -05:00
|
|
|
$this->assertTrue($added);
|
|
|
|
|
}
|
2014-08-10 13:56:15 -05:00
|
|
|
|
|
|
|
|
$this->assertFileExists(
|
2015-09-29 11:35:48 -05:00
|
|
|
$uglyQueue->getQueueTmpFile(),
|
2014-08-10 13:56:15 -05:00
|
|
|
'queue.tmp file was not created!');
|
|
|
|
|
|
2015-09-29 11:35:48 -05:00
|
|
|
$lineCount = \DCarbone\Helpers\FileHelper::getLineCount($uglyQueue->getQueueTmpFile());
|
2014-08-10 13:56:15 -05:00
|
|
|
|
|
|
|
|
$this->assertEquals(11, $lineCount);
|
|
|
|
|
|
2014-08-10 12:28:26 -05:00
|
|
|
return $uglyQueue;
|
|
|
|
|
}
|
2014-08-10 13:56:15 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers \DCarbone\UglyQueue::_populateQueue
|
|
|
|
|
* @uses \DCarbone\UglyQueue
|
|
|
|
|
* @depends testCanPopulateQueueTempFileAfterInitializationAndAcquiringLock
|
|
|
|
|
* @param \DCarbone\UglyQueue $uglyQueue
|
|
|
|
|
* @return \DCarbone\UglyQueue
|
|
|
|
|
*/
|
|
|
|
|
public function testCanForciblyUpdateQueueFileFromTempFile(\DCarbone\UglyQueue $uglyQueue)
|
|
|
|
|
{
|
2015-09-30 08:55:18 -05:00
|
|
|
|
2014-08-10 13:56:15 -05:00
|
|
|
$uglyQueue->_populateQueue();
|
|
|
|
|
|
2015-09-29 11:35:48 -05:00
|
|
|
$this->assertFileNotExists($uglyQueue->getQueueTmpFile());
|
2014-08-10 13:56:15 -05:00
|
|
|
|
|
|
|
|
$uglyQueue->_populateQueue();
|
|
|
|
|
|
|
|
|
|
return $uglyQueue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-09-30 08:55:18 -05:00
|
|
|
* @covers \DCarbone\UglyQueue::count
|
2014-08-10 13:56:15 -05:00
|
|
|
* @uses \DCarbone\UglyQueue
|
|
|
|
|
* @uses \DCarbone\Helpers\FileHelper
|
|
|
|
|
* @depends testCanPopulateQueueTempFileAfterInitializationAndAcquiringLock
|
|
|
|
|
* @param \DCarbone\UglyQueue $uglyQueue
|
|
|
|
|
*/
|
|
|
|
|
public function testCanGetCountOfItemsInPopulatedQueue(\DCarbone\UglyQueue $uglyQueue)
|
|
|
|
|
{
|
2015-09-29 11:35:48 -05:00
|
|
|
$itemCount = count($uglyQueue);
|
2014-08-10 13:56:15 -05:00
|
|
|
|
2014-08-10 14:27:00 -05:00
|
|
|
$this->assertEquals(11, $itemCount);
|
2014-08-10 13:56:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers \DCarbone\UglyQueue::keyExistsInQueue
|
|
|
|
|
* @uses \DCarbone\UglyQueue
|
|
|
|
|
* @depends testCanPopulateQueueTempFileAfterInitializationAndAcquiringLock
|
|
|
|
|
* @param \DCarbone\UglyQueue $uglyQueue
|
|
|
|
|
*/
|
|
|
|
|
public function testKeyExistsReturnsTrueWithPopulatedQueue(\DCarbone\UglyQueue $uglyQueue)
|
|
|
|
|
{
|
2015-09-30 08:55:18 -05:00
|
|
|
|
2014-08-10 13:56:15 -05:00
|
|
|
$exists = $uglyQueue->keyExistsInQueue(5);
|
|
|
|
|
|
|
|
|
|
$this->assertTrue($exists);
|
|
|
|
|
}
|
2014-08-10 14:27:00 -05:00
|
|
|
|
|
|
|
|
/**
|
2015-09-29 11:35:48 -05:00
|
|
|
* @covers \DCarbone\UglyQueue::retrieveItems
|
2014-08-10 14:27:00 -05:00
|
|
|
* @uses \DCarbone\UglyQueue
|
|
|
|
|
* @depends testCanPopulateQueueTempFileAfterInitializationAndAcquiringLock
|
|
|
|
|
* @expectedException \InvalidArgumentException
|
|
|
|
|
* @param \DCarbone\UglyQueue $uglyQueue
|
|
|
|
|
*/
|
|
|
|
|
public function testExceptionThrownWhenTryingToProcessLockedQueueWithNonInteger(\DCarbone\UglyQueue $uglyQueue)
|
|
|
|
|
{
|
2015-09-30 08:55:18 -05:00
|
|
|
|
2015-09-29 11:35:48 -05:00
|
|
|
$uglyQueue->retrieveItems('Eleventy Billion');
|
2014-08-10 14:27:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-09-29 11:35:48 -05:00
|
|
|
* @covers \DCarbone\UglyQueue::retrieveItems
|
2014-08-10 14:27:00 -05:00
|
|
|
* @uses \DCarbone\UglyQueue
|
|
|
|
|
* @depends testCanPopulateQueueTempFileAfterInitializationAndAcquiringLock
|
|
|
|
|
* @expectedException \InvalidArgumentException
|
|
|
|
|
* @param \DCarbone\UglyQueue $uglyQueue
|
|
|
|
|
*/
|
|
|
|
|
public function testExceptionThrownWhenTryingToProcessLockedQueueWithIntegerLessThan1(\DCarbone\UglyQueue $uglyQueue)
|
|
|
|
|
{
|
2015-09-30 08:55:18 -05:00
|
|
|
|
2015-09-29 11:35:48 -05:00
|
|
|
$uglyQueue->retrieveItems(0);
|
2014-08-10 14:27:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-09-29 11:35:48 -05:00
|
|
|
* @covers \DCarbone\UglyQueue::retrieveItems
|
2015-09-30 08:55:18 -05:00
|
|
|
* @covers \DCarbone\UglyQueue::count
|
2014-08-10 14:27:00 -05:00
|
|
|
* @uses \DCarbone\UglyQueue
|
|
|
|
|
* @uses \DCarbone\Helpers\FileHelper
|
|
|
|
|
* @depends testCanPopulateQueueTempFileAfterInitializationAndAcquiringLock
|
|
|
|
|
* @param \DCarbone\UglyQueue $uglyQueue
|
|
|
|
|
* @return \DCarbone\UglyQueue
|
|
|
|
|
*/
|
|
|
|
|
public function testCanGetPartialQueueContents(\DCarbone\UglyQueue $uglyQueue)
|
|
|
|
|
{
|
2015-09-30 08:55:18 -05:00
|
|
|
|
2015-09-29 11:35:48 -05:00
|
|
|
$process = $uglyQueue->retrieveItems(5);
|
2014-08-10 14:27:00 -05:00
|
|
|
|
|
|
|
|
$this->assertEquals(5, count($process));
|
|
|
|
|
|
|
|
|
|
$this->assertArrayHasKey('0', $process);
|
|
|
|
|
$this->assertArrayHasKey('4', $process);
|
|
|
|
|
|
2015-09-29 11:35:48 -05:00
|
|
|
$this->assertEquals(6, count($uglyQueue));
|
2014-08-10 14:27:00 -05:00
|
|
|
|
|
|
|
|
return $uglyQueue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-09-29 11:35:48 -05:00
|
|
|
* @covers \DCarbone\UglyQueue::retrieveItems
|
2015-09-30 08:55:18 -05:00
|
|
|
* @covers \DCarbone\UglyQueue::count
|
2014-08-10 14:27:00 -05:00
|
|
|
* @uses \DCarbone\UglyQueue
|
|
|
|
|
* @uses \DCarbone\Helpers\FileHelper
|
|
|
|
|
* @depends testCanGetPartialQueueContents
|
|
|
|
|
* @param \DCarbone\UglyQueue $uglyQueue
|
|
|
|
|
* @return \DCarbone\UglyQueue
|
|
|
|
|
*/
|
|
|
|
|
public function testCanGetFullQueueContents(\DCarbone\UglyQueue $uglyQueue)
|
|
|
|
|
{
|
2015-09-29 11:35:48 -05:00
|
|
|
$process = $uglyQueue->retrieveItems(6);
|
2014-08-10 14:27:00 -05:00
|
|
|
|
|
|
|
|
$this->assertEquals(6, count($process));
|
|
|
|
|
|
|
|
|
|
$this->assertArrayHasKey('10', $process);
|
|
|
|
|
$this->assertArrayHasKey('5', $process);
|
|
|
|
|
|
2015-09-29 11:35:48 -05:00
|
|
|
$this->assertEquals(0, count($uglyQueue));
|
2014-08-10 14:27:00 -05:00
|
|
|
|
|
|
|
|
return $uglyQueue;
|
|
|
|
|
}
|
2014-08-08 18:09:45 -05:00
|
|
|
}
|