Skip to content

Commit

Permalink
Fix issue with writing the index file
Browse files Browse the repository at this point in the history
When the new content of the ppq index file was shorter than the old
content, it wrote the new content starting from the beginning of the
file and the part of the old content that was longer than the new
content, remained at the end of the file. Fixed it be truncating the
file before writing the new content.
  • Loading branch information
otsch committed Jun 21, 2023
1 parent 082a6af commit 0fd7ff7
Show file tree
Hide file tree
Showing 31 changed files with 150 additions and 88 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [0.1.1] - 2022-06-22

### Fixed
* When the new content of the ppq index file was shorter than the old content, it wrote the new content starting from the beginning of the file and the part of the old content that was longer than the new content, remained at the end of the file. Fixed it be truncating the file before writing the new content.

## [0.1.0] - 2022-06-21
2 changes: 2 additions & 0 deletions src/Drivers/FileDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,8 @@ protected function getUnserializedFileContent(string $filepath, mixed $handle =
*/
protected function saveIndex(array $index, mixed $handle): void
{
ftruncate($handle, 0);

fwrite($handle, serialize($index));
}

Expand Down
2 changes: 1 addition & 1 deletion tests/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

function helper_configFilePath(string $configFile = 'min.php'): string
{
return __DIR__ . '/_testdata/config/' . $configFile;
return helper_testConfigPath($configFile);
}

test(
Expand Down
32 changes: 28 additions & 4 deletions tests/Drivers/FileDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Stubs\TestJob;

beforeEach(function () {
Config::setPath(__DIR__ . '/../_testdata/config/ppq.php');
Config::setPath(helper_testConfigPath('ppq.php'));

helper_cleanUpDataPathQueueFiles();
});
Expand Down Expand Up @@ -76,7 +76,7 @@
});

test('the end of a previously longer content does not remain in a file when a shorter content is written', function () {
expect(file_get_contents(__DIR__ . '/../_testdata/datapath/queue-default'))->toBe('a:0:{}');
expect(file_get_contents(helper_testDataPath('queue-default')))->toBe('a:0:{}');

$driver = new FileDriver();

Expand All @@ -86,7 +86,7 @@

$strlenId = strlen($job->id);

expect(file_get_contents(__DIR__ . '/../_testdata/datapath/queue-default'))->toBe(
expect(file_get_contents(helper_testDataPath('queue-default')))->toBe(
'a:1:{s:' . $strlenId . ':"' . $job->id .'";a:7:{s:2:"id";s:' . $strlenId .':"' . $job->id . '";s:5:"queue";' .
's:7:"default";s:8:"jobClass";s:13:"Stubs\TestJob";s:6:"status";s:7:"waiting";s:4:"args";a:0:{}s:3:"pid";N;' .
's:8:"doneTime";N;}}'
Expand All @@ -96,13 +96,37 @@

$driver->update($job);

expect(file_get_contents(__DIR__ . '/../_testdata/datapath/queue-default'))->toBe(
expect(file_get_contents(helper_testDataPath('queue-default')))->toBe(
'a:1:{s:' . $strlenId . ':"' . $job->id .'";a:7:{s:2:"id";s:' . $strlenId .':"' . $job->id . '";s:5:"queue";' .
's:7:"default";s:8:"jobClass";s:13:"Stubs\TestJob";s:6:"status";s:4:"lost";s:4:"args";a:0:{}s:3:"pid";N;' .
's:8:"doneTime";N;}}'
);
});

test('there are also no remainders in the index file, e.g. when flushing a queue', function () {
expect(file_get_contents(helper_testDataPath('index')))->toBe('a:0:{}');

$driver = new FileDriver();

$job = new QueueRecord('default', TestJob::class);

$driver->add($job);

$contentAfterAdd = file_get_contents(helper_testDataPath('index'));

if (!$contentAfterAdd) {
$contentAfterAdd = '';
}

expect(strlen($contentAfterAdd))->toBeGreaterThan(10);

$driver->flush('default');

$contentAfterFlush = file_get_contents(helper_testDataPath('index'));

expect($contentAfterFlush)->toBe('a:0:{}');
});

it('forgets a job', function () {
$driver = new FileDriver();

Expand Down
2 changes: 1 addition & 1 deletion tests/KernelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
/** @var TestCase $this */

beforeEach(function () {
Config::setPath(__DIR__ . '/_testdata/config/ppq.php');
Config::setPath(helper_testConfigPath('ppq.php'));
});

it('returns an existing ppqPath', function () {
Expand Down
4 changes: 2 additions & 2 deletions tests/ListCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@

it('lists all the waiting and running jobs in all queues', function () {
// temporarily switch config, so the driver instance is reset.
Config::setPath(__DIR__ . '/_testdata/config/min.php');
Config::setPath(helper_testConfigPath('min.php'));

Config::setPath(__DIR__ . '/_testdata/config/ppq.php');
Config::setPath(helper_testConfigPath('ppq.php'));

$driver = Config::getDriver();

Expand Down
2 changes: 1 addition & 1 deletion tests/Loggers/FileLoggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

function helper_testLogFilePath(): string
{
return __DIR__ . '/../_testdata/datapath/logfile';
return helper_testDataPath('logfile');
}

function helper_getLogFileContent(): string
Expand Down
10 changes: 5 additions & 5 deletions tests/LogsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use Stubs\LogTestJob;

beforeAll(function () {
Config::setPath(__DIR__ . '/_testdata/config/filesystem-ppq.php');
Config::setPath(helper_testConfigPath('filesystem-ppq.php'));

helper_cleanUpDataPathQueueFiles();

Expand Down Expand Up @@ -103,21 +103,21 @@
});

it('tells you the log base path', function () {
expect(realpath(Logs::logPath()))->toBe(__DIR__ . '/_testdata/datapath/logs');
expect(realpath(Logs::logPath()))->toBe(helper_testDataPath('logs'));
});

it('tells you the log path for a certain queue', function () {
expect(Logs::queueLogPath('other_queue'))->toBe(__DIR__ . '/_testdata/config/../datapath/logs/other_queue');
expect(realpath(Logs::queueLogPath('other_queue')))->toBe(helper_testDataPath('logs/other_queue'));
});

it('forgets the log file for a queue job', function () {
$job = new QueueRecord('default', LogTestJob::class, id: '123abc');

expect(file_exists(__DIR__ . '/_testdata/datapath/logs/default/' . $job->id . '.log'))->toBeTrue();
expect(file_exists(helper_testDataPath('logs/default/' . $job->id . '.log')))->toBeTrue();

Logs::forget($job);

expect(file_exists(__DIR__ . '/_testdata/datapath/logs/default/' . $job->id . '.log'))->toBeFalse();
expect(file_exists(helper_testDataPath('logs/default/' . $job->id . '.log')))->toBeFalse();
});

it('creates the log dirs if they don\'t exist yet', function () {
Expand Down
57 changes: 42 additions & 15 deletions tests/Pest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,46 +39,73 @@
|
*/

function helper_testDataPath(string $withinPath = ''): string
{
if (!empty($withinPath)) {
$withinPath = str_starts_with($withinPath, '/') ? $withinPath : '/' . $withinPath;
}

return __DIR__ . '/_testdata/datapath' . $withinPath;
}

function helper_testConfigPath(string $withinPath = ''): string
{
if (!empty($withinPath)) {
$withinPath = str_starts_with($withinPath, '/') ? $withinPath : '/' . $withinPath;
}

return __DIR__ . '/_testdata/config' . $withinPath;
}

function helper_testScriptPath(string $withinPath = ''): string
{
if (!empty($withinPath)) {
$withinPath = str_starts_with($withinPath, '/') ? $withinPath : '/' . $withinPath;
}

return __DIR__ . '/_testdata/scripts' . $withinPath;
}

function helper_cleanUpDataPathQueueFiles(): void
{
if (file_exists(__DIR__ . '/_testdata/datapath/index')) {
file_put_contents(__DIR__ . '/_testdata/datapath/index', 'a:0:{}');
if (file_exists(helper_testDataPath('index'))) {
file_put_contents(helper_testDataPath('index'), 'a:0:{}');
}

if (file_exists(__DIR__ . '/_testdata/datapath/queue-default')) {
file_put_contents(__DIR__ . '/_testdata/datapath/queue-default', 'a:0:{}');
if (file_exists(helper_testDataPath('queue-default'))) {
file_put_contents(helper_testDataPath('queue-default'), 'a:0:{}');
}

if (file_exists(__DIR__ . '/_testdata/datapath/queue-other_queue')) {
file_put_contents(__DIR__ . '/_testdata/datapath/queue-other_queue', 'a:0:{}');
if (file_exists(helper_testDataPath('queue-other_queue'))) {
file_put_contents(helper_testDataPath('queue-other_queue'), 'a:0:{}');
}

if (file_exists(__DIR__ . '/_testdata/datapath/queue-infinite_waiting_jobs_queue')) {
file_put_contents(__DIR__ . '/_testdata/datapath/queue-infinite_waiting_jobs_queue', 'a:0:{}');
if (file_exists(helper_testDataPath('queue-infinite_waiting_jobs_queue'))) {
file_put_contents(helper_testDataPath('queue-infinite_waiting_jobs_queue'), 'a:0:{}');
}

// clean up logs
if (file_exists(__DIR__ . '/_testdata/datapath/logs')) {
if (file_exists(helper_testDataPath('logs'))) {
$queues = ['default', 'other_queue', 'infinite_waiting_jobs_queue'];

foreach ($queues as $queue) {
if (file_exists(__DIR__ . '/_testdata/datapath/logs/' . $queue)) {
$filesInDir = scandir(__DIR__ . '/_testdata/datapath/logs/' . $queue);
if (file_exists(helper_testDataPath('logs/' . $queue))) {
$filesInDir = scandir(helper_testDataPath('logs/' . $queue));

if (is_array($filesInDir)) {
foreach ($filesInDir as $file) {
if (str_ends_with($file, '.log')) {
unlink(__DIR__ . '/_testdata/datapath/logs/' . $queue . '/' . $file);
unlink(helper_testDataPath('logs/' . $queue . '/' . $file));
}
}
}

rmdir(__DIR__ . '/_testdata/datapath/logs/' . $queue);
rmdir(helper_testDataPath('logs/' . $queue));
}
}

if (file_exists(__DIR__ . '/_testdata/datapath/logs')) {
rmdir(__DIR__ . '/_testdata/datapath/logs');
if (file_exists(helper_testDataPath('logs'))) {
rmdir(helper_testDataPath('logs'));
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions tests/ProcessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
/** @var TestCase $this */

it('finishes a process that was successfully finished', function () {
Config::setPath(__DIR__ . '/_testdata/config/ppq.php');
Config::setPath(helper_testConfigPath('ppq.php'));

$queueRecord = new QueueRecord('default', TestJob::class);

Expand All @@ -35,7 +35,7 @@
});

it('finishes a process that failed', function () {
Config::setPath(__DIR__ . '/_testdata/config/ppq.php');
Config::setPath(helper_testConfigPath('ppq.php'));

$queueRecord = new QueueRecord('default', TestJob::class, QueueJobStatus::running);

Expand Down
6 changes: 3 additions & 3 deletions tests/ProcessesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

it('checks if a running process with a certain pid exists', function () {
$process = \Symfony\Component\Process\Process::fromShellCommandline(
'php ' . __DIR__ . '/_testdata/scripts/do-something.php'
'php ' . helper_testScriptPath('do-something.php')
);

$process->start();
Expand All @@ -26,7 +26,7 @@
expect(Processes::processContainingStringsExists(['counting.php']))->toBeFalse();

$process = \Symfony\Component\Process\Process::fromShellCommandline(
'php ' . __DIR__ . '/_testdata/scripts/counting.php'
'php ' . helper_testScriptPath('counting.php')
);

$process->start();
Expand All @@ -36,7 +36,7 @@

test('checking if a running process containing certain strings exists, exclude the current process', function () {
$process = \Symfony\Component\Process\Process::fromShellCommandline(
'php ' . __DIR__ . '/_testdata/scripts/check-process-already-running.php'
'php ' . helper_testScriptPath('check-process-already-running.php')
);

$process->run();
Expand Down
2 changes: 1 addition & 1 deletion tests/QueueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use Stubs\TestJob;

beforeEach(function () {
Config::setPath(__DIR__ . '/_testdata/config/filesystem-ppq.php');
Config::setPath(helper_testConfigPath('filesystem-ppq.php'));

helper_cleanUpDataPathQueueFiles();
});
Expand Down
6 changes: 3 additions & 3 deletions tests/SignalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
use Otsch\Ppq\Signal;

beforeEach(function () {
Config::setPath(__DIR__ . '/_testdata/config/ppq.php');
Config::setPath(helper_testConfigPath('ppq.php'));

if (file_exists(__DIR__ . '/_testdata/datapath/signal')) {
file_put_contents(__DIR__ . '/_testdata/datapath/signal', '');
if (file_exists(helper_testDataPath('signal'))) {
file_put_contents(helper_testDataPath('signal'), '');
}
});

Expand Down
5 changes: 2 additions & 3 deletions tests/Stubs/Listeners/DefaultCancelled.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

namespace Stubs\Listeners;

use Otsch\Ppq\Contracts\QueueEventListener;
use Otsch\Ppq\Entities\QueueRecord;

class DefaultCancelled implements QueueEventListener
class DefaultCancelled extends TestQueueEventListener
{
public function invoke(QueueRecord $queueRecord): void
{
file_put_contents(__DIR__ . '/../../_testdata/datapath/event-listeners-check-file', 'default cancelled called');
file_put_contents($this->dataPath('event-listeners-check-file'), 'default cancelled called');
}
}
5 changes: 2 additions & 3 deletions tests/Stubs/Listeners/DefaultFailed.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

namespace Stubs\Listeners;

use Otsch\Ppq\Contracts\QueueEventListener;
use Otsch\Ppq\Entities\QueueRecord;

class DefaultFailed implements QueueEventListener
class DefaultFailed extends TestQueueEventListener
{
public function invoke(QueueRecord $queueRecord): void
{
file_put_contents(__DIR__ . '/../../_testdata/datapath/event-listeners-check-file', 'default failed called');
file_put_contents($this->dataPath('event-listeners-check-file'), 'default failed called');
}
}
5 changes: 2 additions & 3 deletions tests/Stubs/Listeners/DefaultFinished.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

namespace Stubs\Listeners;

use Otsch\Ppq\Contracts\QueueEventListener;
use Otsch\Ppq\Entities\QueueRecord;

class DefaultFinished implements QueueEventListener
class DefaultFinished extends TestQueueEventListener
{
public function invoke(QueueRecord $queueRecord): void
{
file_put_contents(__DIR__ . '/../../_testdata/datapath/event-listeners-check-file', 'default finished called');
file_put_contents($this->dataPath('event-listeners-check-file'), 'default finished called');
}
}
5 changes: 2 additions & 3 deletions tests/Stubs/Listeners/DefaultLost.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

namespace Stubs\Listeners;

use Otsch\Ppq\Contracts\QueueEventListener;
use Otsch\Ppq\Entities\QueueRecord;

class DefaultLost implements QueueEventListener
class DefaultLost extends TestQueueEventListener
{
public function invoke(QueueRecord $queueRecord): void
{
file_put_contents(__DIR__ . '/../../_testdata/datapath/event-listeners-check-file', 'default lost called');
file_put_contents($this->dataPath('event-listeners-check-file'), 'default lost called');
}
}
5 changes: 2 additions & 3 deletions tests/Stubs/Listeners/DefaultRunning.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

namespace Stubs\Listeners;

use Otsch\Ppq\Contracts\QueueEventListener;
use Otsch\Ppq\Entities\QueueRecord;

class DefaultRunning implements QueueEventListener
class DefaultRunning extends TestQueueEventListener
{
public function invoke(QueueRecord $queueRecord): void
{
file_put_contents(__DIR__ . '/../../_testdata/datapath/event-listeners-check-file', 'default running called');
file_put_contents($this->dataPath('event-listeners-check-file'), 'default running called');
}
}
Loading

0 comments on commit 0fd7ff7

Please sign in to comment.