Skip to content

Commit

Permalink
refactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
freekmurze committed Mar 8, 2024
2 parents 172b865 + e4e269f commit 39899d5
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 79 deletions.
12 changes: 7 additions & 5 deletions src/LongRunningTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace Clickonmedia\Monitor;

use Carbon\Carbon;
use Clickonmedia\Monitor\Enums\TaskResult;
use Clickonmedia\Monitor\Enums\LogItemStatus;
use Clickonmedia\Monitor\Enums\TaskResult;
use Clickonmedia\Monitor\Jobs\RunLongRunningTaskJob;
use Clickonmedia\Monitor\Models\LongRunningTaskLogItem;
use Exception;
Expand All @@ -13,6 +13,12 @@ abstract class LongRunningTask
{
protected array $meta = [];

abstract public function check(LongRunningTaskLogItem $logItem): TaskResult;

public function onFail(LongRunningTaskLogItem $logItem, Exception $exception): ?TaskResult {
return TaskResult::StopChecking;
}

public static function make()
{
return new static();
Expand Down Expand Up @@ -55,8 +61,4 @@ public function stopCheckingAt(): Carbon
{
return now()->addSeconds(config('long-running-tasks-monitor.keep_checking_for_in_seconds'));
}

abstract public function check(LongRunningTaskLogItem $logItem): TaskResult;

abstract public function onFail(LongRunningTaskLogItem $logItem, Exception $exception): ?TaskResult;
}
63 changes: 0 additions & 63 deletions tests/Jobs/RunLongingTaskJobTest.php

This file was deleted.

78 changes: 78 additions & 0 deletions tests/LongRunningTaskTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?php

use Clickonmedia\Monitor\Enums\LogItemStatus;
use Clickonmedia\Monitor\Enums\TaskResult;
use Clickonmedia\Monitor\Jobs\RunLongRunningTaskJob;
use Clickonmedia\Monitor\LongRunningTask;
use Clickonmedia\Monitor\Models\LongRunningTaskLogItem;
use Clickonmedia\Monitor\Tests\TestSupport\LongRunningTasks\LongRunningTestTask;
use Illuminate\Support\Facades\Queue;
Expand All @@ -22,3 +24,79 @@

Queue::assertPushed(RunLongRunningTaskJob::class);
});

it('can handle a pending task that will complete', function () {
$task = new class extends LongRunningTask {
public function check(LongRunningTaskLogItem $logItem): TaskResult
{
return TaskResult::StopChecking;
}
};

$task->start();

expect(LongRunningTaskLogItem::first())
->status->toBe(LogItemStatus::Completed)
->attempt->toBe(1)
->run_count->toBe(1);
});

it('can handle a pending task that needs a couple of runs to complete', function () {
$task = new class extends LongRunningTask {
public function check(LongRunningTaskLogItem $logItem): TaskResult
{
return $logItem->run_count < 5
? TaskResult::ContinueChecking
: TaskResult::StopChecking;
}
};

$task->start();

expect(LongRunningTaskLogItem::first())
->run_count->toBe(5)
->status->toBe(LogItemStatus::Completed)
->last_check_started_at->not()->toBeNull()
->last_check_ended_at->not()->toBeNull();
});

it('will can handle a task that always fails', function () {
$task = new class extends LongRunningTask {
public function check(LongRunningTaskLogItem $logItem): TaskResult
{
throw new Exception();
}
};

$task->start();

expect(LongRunningTaskLogItem::first())
->status->toBe(LogitemStatus::Failed)
->run_count->toBe(1)
->latest_exception->toHaveKeys(['message', 'trace']);
});

it('can handle a task that will recover', function () {
$task = new class extends LongRunningTask {
public function check(LongRunningTaskLogItem $logItem): TaskResult
{
if ($logItem->run_count < 3) {
throw new Exception();
}

return TaskResult::StopChecking;
}

public function onFail(LongRunningTaskLogItem $logItem, Exception $exception): ?TaskResult
{
return TaskResult::ContinueChecking;
}
};

$task->start();

expect(LongRunningTaskLogItem::first())
->status->toBe(LogitemStatus::Completed)
->run_count->toBe(3)
->latest_exception->toBeNull();
});
2 changes: 1 addition & 1 deletion tests/Pest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
use Clickonmedia\Monitor\Tests\TestCase;

uses(TestCase::class)
->beforeEach(fn() => ray()->newScreen($this->name()))
->beforeEach(fn () => ray()->newScreen($this->name()))
->in(__DIR__);
10 changes: 0 additions & 10 deletions tests/TestSupport/LongRunningTasks/LongRunningTestTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,8 @@

class LongRunningTestTask extends LongRunningTask
{
public static ?Closure $checkClosure = null;

public function check(LongRunningTaskLogItem $logItem): TaskResult
{
if (self::$checkClosure) {
return (self::$checkClosure)($logItem);
}

return TaskResult::StopChecking;
}

public function onFail(LongRunningTaskLogItem $logItem, Exception $exception): TaskResult
{
return TaskResult::StopChecking;
}
Expand Down

0 comments on commit 39899d5

Please sign in to comment.