Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions tests/Unit/HorizonLogs/HorizonNewLogsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,87 @@
->and($logs[3]->message)->toBe('App\Jobs\TestJob')
->and($logs[3]->context['duration'])->toBe('25.14ms');
});

it('detects Horizon logs without preamble', function () {
$file = generateLogFile('horizon_no_preamble.log', content: <<<EOF
2023-07-22 16:13:33 App\Jobs\ProcessOrder ............................... RUNNING
2023-07-22 16:13:34 App\Jobs\ProcessOrder ............................... 1.2s DONE
2023-07-22 16:13:35 App\Jobs\SendInvoice ................................ RUNNING
2023-07-22 16:13:36 App\Jobs\SendInvoice ................................ 850ms DONE
EOF);
$file = new LogFile($file->path);

expect($file->type()->value)->toBe(LogType::HORIZON);

$logReader = $file->logs()->scan();

expect($logs = $logReader->get())->toHaveCount(4)
->and($logs[0]->datetime->toDateTimeString())->toBe('2023-07-22 16:13:33')
->and($logs[0]->level)->toBe('RUNNING')
->and($logs[0]->message)->toBe('App\Jobs\ProcessOrder')
->and($logs[1]->datetime->toDateTimeString())->toBe('2023-07-22 16:13:34')
->and($logs[1]->level)->toBe('DONE')
->and($logs[1]->context['duration'])->toBe('1.2s')
->and($logs[2]->level)->toBe('RUNNING')
->and($logs[3]->level)->toBe('DONE')
->and($logs[3]->context['duration'])->toBe('850ms');
});

it('handles various duration formats', function () {
$file = generateLogFile('horizon_durations.log', content: <<<EOF
2022-11-11 13:50:44 App\Jobs\FirstJob ............... 40.12ms DONE
2022-11-11 13:50:44 App\Jobs\SecondJob .............. 35.42ms DONE
2022-11-11 13:50:44 App\Jobs\ThirdJob ............... 71.48ms DONE
2022-11-11 13:50:44 App\Jobs\FourthJob .............. 55.35ms DONE
2022-11-11 13:50:45 App\Jobs\FifthJob ............... 3.67ms FAIL
2022-11-11 13:50:46 App\Jobs\SixthJob ............... 2m 30s DONE
EOF);
$file = new LogFile($file->path);

expect($file->type()->value)->toBe(LogType::HORIZON);

$logReader = $file->logs()->scan();

expect($logs = $logReader->get())->toHaveCount(6)
->and($logs[0]->context['duration'])->toBe('40.12ms')
->and($logs[0]->level)->toBe('DONE')
->and($logs[1]->context['duration'])->toBe('35.42ms')
->and($logs[1]->level)->toBe('DONE')
->and($logs[2]->context['duration'])->toBe('71.48ms')
->and($logs[2]->level)->toBe('DONE')
->and($logs[3]->context['duration'])->toBe('55.35ms')
->and($logs[3]->level)->toBe('DONE')
->and($logs[4]->context['duration'])->toBe('3.67ms')
->and($logs[4]->level)->toBe('FAIL')
->and($logs[5]->context['duration'])->toBe('2m 30s')
->and($logs[5]->level)->toBe('DONE');
});

it('handles concurrent job output with garbled lines', function () {
$file = generateLogFile('horizon_concurrent.log', content: <<<EOF
2022-11-11 13:50:44 App\Jobs\SlowJob ................ RUNNING
2022-11-11 13:50:44 App\Jobs\QuickJob ............... 40.12ms DONE
2022-11-11 13:50:44 App\Jobs\AnotherJob ............. 35.42ms DONE
2022-11-11 13:50:44 App\Jobs\SlowJob 2022-11-11 13:50:44 App\Notifications\SomeNotification
2022-11-11 13:50:44 App\Notifications\SomeNotification ..................... 71.48ms DONE
2022-11-11 13:50:44 App\Jobs\SlowJob ..................... 55.35ms DONE
EOF);
$file = new LogFile($file->path);

expect($file->type()->value)->toBe(LogType::HORIZON);

$logReader = $file->logs()->scan();

$logs = $logReader->get();

// File should be detected as Horizon type
expect($file->type()->value)->toBe(LogType::HORIZON);

// Valid lines should be parsed correctly (garbled line might be skipped or partially parsed)
expect($logs)->toBeArray()
->and(count($logs))->toBeGreaterThanOrEqual(4);

// Check that at least some valid logs were parsed
$validLogs = array_filter($logs, fn ($log) => $log->datetime !== null);
expect(count($validLogs))->toBeGreaterThanOrEqual(4);
});
30 changes: 30 additions & 0 deletions tests/Unit/HorizonLogs/HorizonOldLogsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,33 @@
->and($logs[1]->message)->toBe('App\Notifications\BookingUpdated')
->and($logs[1]->context['uuid'])->toBe('13acffa6-fd25-4d36-876a-b48e968302a4');
});

it('can process old Horizon logs with failed status', function () {
$file = generateLogFile('horizon_old_failed.log', content: <<<EOF
[2022-10-07 09:41:00][a1b2c3d4-e5f6-7890-1234-567890abcdef] Processing: App\Jobs\SendEmailJob
[2022-10-07 09:41:01][a1b2c3d4-e5f6-7890-1234-567890abcdef] Failed: App\Jobs\SendEmailJob
[2022-10-07 09:42:00][b2c3d4e5-f6a7-8901-2345-67890abcdef1] Processing: App\Events\UserRegistered
[2022-10-07 09:42:00][b2c3d4e5-f6a7-8901-2345-67890abcdef1] Processed: App\Events\UserRegistered
EOF);
$file = new LogFile($file->path);

expect($file->type()->value)->toBe(LogType::HORIZON_OLD); // HorizonOldLog

$logReader = $file->logs()->scan();

expect($logs = $logReader->get())->toHaveCount(4)
->and($logs[0]->datetime->toDateTimeString())->toBe('2022-10-07 09:41:00')
->and($logs[0]->level)->toBe('Processing')
->and($logs[0]->message)->toBe('App\Jobs\SendEmailJob')
->and($logs[0]->context['uuid'])->toBe('a1b2c3d4-e5f6-7890-1234-567890abcdef')
->and($logs[1]->datetime->toDateTimeString())->toBe('2022-10-07 09:41:01')
->and($logs[1]->level)->toBe('Failed')
->and($logs[1]->message)->toBe('App\Jobs\SendEmailJob')
->and($logs[1]->context['uuid'])->toBe('a1b2c3d4-e5f6-7890-1234-567890abcdef')
->and($logs[2]->datetime->toDateTimeString())->toBe('2022-10-07 09:42:00')
->and($logs[2]->level)->toBe('Processing')
->and($logs[2]->message)->toBe('App\Events\UserRegistered')
->and($logs[3]->datetime->toDateTimeString())->toBe('2022-10-07 09:42:00')
->and($logs[3]->level)->toBe('Processed')
->and($logs[3]->message)->toBe('App\Events\UserRegistered');
});
Loading