You've already forked ugly-queue
More tests and minor updates.
This commit is contained in:
@@ -192,22 +192,22 @@ HTML;
|
||||
return false;
|
||||
|
||||
// Find number of lines in the queue file
|
||||
$line_count = FileHelper::getLineCount($this->queueGroupDirPath.'queue.txt');
|
||||
$lineCount = FileHelper::getLineCount($this->queueGroupDirPath.'queue.txt');
|
||||
|
||||
// If queue line count is 0, assume empty
|
||||
if ($line_count === 0)
|
||||
if ($lineCount === 0)
|
||||
return false;
|
||||
|
||||
// Try to open the file for reading / writing.
|
||||
$queue_file_handle = fopen($this->queueGroupDirPath.'queue.txt', 'r+');
|
||||
if ($queue_file_handle === false)
|
||||
$queueFileHandle = fopen($this->queueGroupDirPath.'queue.txt', 'r+');
|
||||
if ($queueFileHandle === false)
|
||||
$this->unlock();
|
||||
|
||||
// Get an array of the oldest $count data in the queue
|
||||
$data = array();
|
||||
$start_line = $line_count - $count;
|
||||
$start_line = $lineCount - $count;
|
||||
$i = 0;
|
||||
while (($line = fscanf($queue_file_handle, "%s\t%s\n")) !== false && $i < $line_count)
|
||||
while (($line = fscanf($queueFileHandle, "%s\t%s\n")) !== false && $i < $lineCount)
|
||||
{
|
||||
if ($i++ >= $start_line)
|
||||
{
|
||||
@@ -217,20 +217,20 @@ HTML;
|
||||
}
|
||||
|
||||
// If we have consumed the rest of the file
|
||||
if ($count >= $line_count)
|
||||
if ($count >= $lineCount)
|
||||
{
|
||||
rewind($queue_file_handle);
|
||||
ftruncate($queue_file_handle, 0);
|
||||
fclose($queue_file_handle);
|
||||
rewind($queueFileHandle);
|
||||
ftruncate($queueFileHandle, 0);
|
||||
fclose($queueFileHandle);
|
||||
$this->unlock();
|
||||
}
|
||||
// Otherwise, create new queue file minus the processed lines.
|
||||
else
|
||||
{
|
||||
$tmp = fopen($this->queueGroupDirPath.'queue.tmp', 'w+');
|
||||
rewind($queue_file_handle);
|
||||
rewind($queueFileHandle);
|
||||
$i = 0;
|
||||
while (($line = fgets($queue_file_handle)) !== false && $i < $start_line)
|
||||
while (($line = fgets($queueFileHandle)) !== false && $i < $start_line)
|
||||
{
|
||||
if ($line !== "\n" || $line !== "")
|
||||
fwrite($tmp, $line);
|
||||
@@ -238,7 +238,7 @@ HTML;
|
||||
$i++;
|
||||
}
|
||||
|
||||
fclose($queue_file_handle);
|
||||
fclose($queueFileHandle);
|
||||
fclose($tmp);
|
||||
unlink($this->queueGroupDirPath.'queue.txt');
|
||||
rename($this->queueGroupDirPath.'queue.tmp', $this->queueGroupDirPath.'queue.txt');
|
||||
@@ -283,20 +283,20 @@ HTML;
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function _populateQueue()
|
||||
public function _populateQueue()
|
||||
{
|
||||
if (is_resource($this->_tmpHandle))
|
||||
{
|
||||
if (file_exists($this->queueGroupDirPath.'queue.txt'))
|
||||
{
|
||||
$queue_file_handle = fopen($this->queueGroupDirPath.'queue.txt', 'r+');
|
||||
while (($line = fgets($queue_file_handle)) !== false)
|
||||
$queueFileHandle = fopen($this->queueGroupDirPath.'queue.txt', 'r+');
|
||||
while (($line = fgets($queueFileHandle)) !== false)
|
||||
{
|
||||
if ($line !== "\n" && $line !== "")
|
||||
fwrite($this->_tmpHandle, $line);
|
||||
}
|
||||
|
||||
fclose($queue_file_handle);
|
||||
fclose($queueFileHandle);
|
||||
unlink($this->queueGroupDirPath.'queue.txt');
|
||||
}
|
||||
|
||||
@@ -306,9 +306,7 @@ HTML;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method will always return n+1, as there is always a final newline at the end of the file
|
||||
*
|
||||
* @return int|null
|
||||
* @return int
|
||||
* @throws \RuntimeException
|
||||
*/
|
||||
public function getQueueItemCount()
|
||||
@@ -334,25 +332,23 @@ HTML;
|
||||
if ($this->init === false)
|
||||
throw new \RuntimeException('UglyQueue::keyExistsInQueue - Must first initialize queue');
|
||||
|
||||
// If we don't have a lock, assume issue and move on.
|
||||
if ($this->haveLock === false)
|
||||
throw new \RuntimeException('UglyQueue::keyExistsInQueue - Must first acquire queue lock');
|
||||
|
||||
$key = (string)$key;
|
||||
|
||||
// Try to open the file for reading / writing.
|
||||
$queue_file_handle = fopen($this->queueGroupDirPath.'queue.txt', 'r');
|
||||
$queueFileHandle = fopen($this->queueGroupDirPath.'queue.txt', 'r');
|
||||
|
||||
while(($line = fscanf($queue_file_handle, "%s\t")) !== false)
|
||||
while(($line = fscanf($queueFileHandle, "%s\t")) !== false)
|
||||
{
|
||||
if (strpos($line, $key) === 0)
|
||||
list($queueKey) = $line;
|
||||
|
||||
if ($key === $queueKey)
|
||||
{
|
||||
fclose($queue_file_handle);
|
||||
fclose($queueFileHandle);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
fclose($queue_file_handle);
|
||||
fclose($queueFileHandle);
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -158,6 +158,18 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase
|
||||
$itemCount = $uglyQueue->getQueueItemCount();
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \DCarbone\UglyQueue::keyExistsInQueue
|
||||
* @uses \DCarbone\UglyQueue
|
||||
* @depends testCanConstructUglyQueueWithValidParameter
|
||||
* @expectedException \RuntimeException
|
||||
* @param \DCarbone\UglyQueue $uglyQueue
|
||||
*/
|
||||
public function testExceptionThrownWhenTryingToFindKeyBeforeInitialization(\DCarbone\UglyQueue $uglyQueue)
|
||||
{
|
||||
$keyExists = $uglyQueue->keyExistsInQueue(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \DCarbone\UglyQueue::initialize
|
||||
* @covers \DCarbone\UglyQueue::getInit
|
||||
@@ -175,14 +187,27 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase
|
||||
return $uglyQueue;
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \DCarbone\UglyQueue::keyExistsInQueue
|
||||
* @uses \DCarbone\UglyQueue
|
||||
* @depends testCanInitializeNewUglyQueue
|
||||
* @param \DCarbone\UglyQueue $uglyQueue
|
||||
*/
|
||||
public function testKeyExistsInQueueReturnsFalseWithEmptyQueueAfterInitialization(\DCarbone\UglyQueue $uglyQueue)
|
||||
{
|
||||
$exists = $uglyQueue->keyExistsInQueue(0);
|
||||
|
||||
$this->assertFalse($exists);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \DCarbone\UglyQueue::addToQueue
|
||||
* @uses \DCarbone\UglyQueue
|
||||
* @depends testCanConstructUglyQueueWithValidParameter
|
||||
* @depends testCanInitializeNewUglyQueue
|
||||
* @expectedException \RuntimeException
|
||||
* @param \DCarbone\UglyQueue $uglyQueue
|
||||
*/
|
||||
public function testExceptionThrownWhenTryingToAddItemsToQueueWithoutLock(\DCarbone\UglyQueue $uglyQueue)
|
||||
public function testExceptionThrownWhenTryingToAddItemsToQueueWithoutLockAfterInitialization(\DCarbone\UglyQueue $uglyQueue)
|
||||
{
|
||||
$addToQueue = $uglyQueue->addToQueue('test', 'value');
|
||||
}
|
||||
@@ -439,17 +464,76 @@ class UglyQueueTest extends PHPUnit_Framework_TestCase
|
||||
/**
|
||||
* @covers \DCarbone\UglyQueue::addToQueue
|
||||
* @uses \DCarbone\UglyQueue
|
||||
* @uses \DCarbone\Helpers\FileHelper
|
||||
* @depends testCanLockQueueWithValidIntegerValue
|
||||
* @param \DCarbone\UglyQueue $uglyQueue
|
||||
* @return \DCarbone\UglyQueue
|
||||
*/
|
||||
public function testCanPopulateLockedQueue(\DCarbone\UglyQueue $uglyQueue)
|
||||
public function testCanPopulateQueueTempFileAfterInitializationAndAcquiringLock(\DCarbone\UglyQueue $uglyQueue)
|
||||
{
|
||||
foreach($this->tastySandwich as $k=>$v)
|
||||
{
|
||||
$added = $uglyQueue->addToQueue($k, $v);
|
||||
$this->assertTrue($added);
|
||||
}
|
||||
|
||||
$groupDir = $uglyQueue->getQueueGroupDirPath();
|
||||
|
||||
$this->assertFileExists(
|
||||
$groupDir.'queue.tmp',
|
||||
'queue.tmp file was not created!');
|
||||
|
||||
$lineCount = \DCarbone\Helpers\FileHelper::getLineCount($groupDir.'queue.tmp');
|
||||
|
||||
$this->assertEquals(11, $lineCount);
|
||||
|
||||
return $uglyQueue;
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \DCarbone\UglyQueue::_populateQueue
|
||||
* @uses \DCarbone\UglyQueue
|
||||
* @depends testCanPopulateQueueTempFileAfterInitializationAndAcquiringLock
|
||||
* @param \DCarbone\UglyQueue $uglyQueue
|
||||
* @return \DCarbone\UglyQueue
|
||||
*/
|
||||
public function testCanForciblyUpdateQueueFileFromTempFile(\DCarbone\UglyQueue $uglyQueue)
|
||||
{
|
||||
$uglyQueue->_populateQueue();
|
||||
|
||||
$groupDir = $uglyQueue->getQueueGroupDirPath();
|
||||
|
||||
$this->assertFileNotExists($groupDir.'queue.tmp');
|
||||
|
||||
$uglyQueue->_populateQueue();
|
||||
|
||||
return $uglyQueue;
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \DCarbone\UglyQueue::getQueueItemCount
|
||||
* @uses \DCarbone\UglyQueue
|
||||
* @uses \DCarbone\Helpers\FileHelper
|
||||
* @depends testCanPopulateQueueTempFileAfterInitializationAndAcquiringLock
|
||||
* @param \DCarbone\UglyQueue $uglyQueue
|
||||
*/
|
||||
public function testCanGetCountOfItemsInPopulatedQueue(\DCarbone\UglyQueue $uglyQueue)
|
||||
{
|
||||
$itemCount = $uglyQueue->getQueueItemCount();
|
||||
|
||||
$this->assertEquals(10, $itemCount);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \DCarbone\UglyQueue::keyExistsInQueue
|
||||
* @uses \DCarbone\UglyQueue
|
||||
* @depends testCanPopulateQueueTempFileAfterInitializationAndAcquiringLock
|
||||
* @param \DCarbone\UglyQueue $uglyQueue
|
||||
*/
|
||||
public function testKeyExistsReturnsTrueWithPopulatedQueue(\DCarbone\UglyQueue $uglyQueue)
|
||||
{
|
||||
$exists = $uglyQueue->keyExistsInQueue(5);
|
||||
|
||||
$this->assertTrue($exists);
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,13 @@
|
||||
<?php
|
||||
|
||||
if (is_dir(__DIR__.'/tasty-sandwich'))
|
||||
\DCarbone\Helpers\FileHelper::superUnlink(__DIR__.'/tasty-sandwich');
|
||||
{
|
||||
foreach(glob(__DIR__.'/tasty-sandwich/*') as $file)
|
||||
{
|
||||
if (substr($file, -1) === '.')
|
||||
continue;
|
||||
|
||||
unlink($file);
|
||||
}
|
||||
rmdir(__DIR__.'/tasty-sandwich');
|
||||
}
|
||||
Reference in New Issue
Block a user