Skip to content

Commit 3b8f8a4

Browse files
committed
Add comprehensive Horizon log detection tests
- Test old format with Failed status - Test new format without preamble (rotated logs) - Test various duration formats and concurrent output - Strengthen content-based detection coverage
1 parent 3d04bda commit 3b8f8a4

File tree

2 files changed

+114
-0
lines changed

2 files changed

+114
-0
lines changed

tests/Unit/HorizonLogs/HorizonNewLogsTest.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,87 @@
6969
->and($logs[3]->message)->toBe('App\Jobs\TestJob')
7070
->and($logs[3]->context['duration'])->toBe('25.14ms');
7171
});
72+
73+
it('detects Horizon logs without preamble', function () {
74+
$file = generateLogFile('horizon_no_preamble.log', content: <<<EOF
75+
2023-07-22 16:13:33 App\Jobs\ProcessOrder ............................... RUNNING
76+
2023-07-22 16:13:34 App\Jobs\ProcessOrder ............................... 1.2s DONE
77+
2023-07-22 16:13:35 App\Jobs\SendInvoice ................................ RUNNING
78+
2023-07-22 16:13:36 App\Jobs\SendInvoice ................................ 850ms DONE
79+
EOF);
80+
$file = new LogFile($file->path);
81+
82+
expect($file->type()->value)->toBe(LogType::HORIZON);
83+
84+
$logReader = $file->logs()->scan();
85+
86+
expect($logs = $logReader->get())->toHaveCount(4)
87+
->and($logs[0]->datetime->toDateTimeString())->toBe('2023-07-22 16:13:33')
88+
->and($logs[0]->level)->toBe('RUNNING')
89+
->and($logs[0]->message)->toBe('App\Jobs\ProcessOrder')
90+
->and($logs[1]->datetime->toDateTimeString())->toBe('2023-07-22 16:13:34')
91+
->and($logs[1]->level)->toBe('DONE')
92+
->and($logs[1]->context['duration'])->toBe('1.2s')
93+
->and($logs[2]->level)->toBe('RUNNING')
94+
->and($logs[3]->level)->toBe('DONE')
95+
->and($logs[3]->context['duration'])->toBe('850ms');
96+
});
97+
98+
it('handles various duration formats', function () {
99+
$file = generateLogFile('horizon_durations.log', content: <<<EOF
100+
2022-11-11 13:50:44 App\Jobs\FirstJob ............... 40.12ms DONE
101+
2022-11-11 13:50:44 App\Jobs\SecondJob .............. 35.42ms DONE
102+
2022-11-11 13:50:44 App\Jobs\ThirdJob ............... 71.48ms DONE
103+
2022-11-11 13:50:44 App\Jobs\FourthJob .............. 55.35ms DONE
104+
2022-11-11 13:50:45 App\Jobs\FifthJob ............... 3.67ms FAIL
105+
2022-11-11 13:50:46 App\Jobs\SixthJob ............... 2m 30s DONE
106+
EOF);
107+
$file = new LogFile($file->path);
108+
109+
expect($file->type()->value)->toBe(LogType::HORIZON);
110+
111+
$logReader = $file->logs()->scan();
112+
113+
expect($logs = $logReader->get())->toHaveCount(6)
114+
->and($logs[0]->context['duration'])->toBe('40.12ms')
115+
->and($logs[0]->level)->toBe('DONE')
116+
->and($logs[1]->context['duration'])->toBe('35.42ms')
117+
->and($logs[1]->level)->toBe('DONE')
118+
->and($logs[2]->context['duration'])->toBe('71.48ms')
119+
->and($logs[2]->level)->toBe('DONE')
120+
->and($logs[3]->context['duration'])->toBe('55.35ms')
121+
->and($logs[3]->level)->toBe('DONE')
122+
->and($logs[4]->context['duration'])->toBe('3.67ms')
123+
->and($logs[4]->level)->toBe('FAIL')
124+
->and($logs[5]->context['duration'])->toBe('2m 30s')
125+
->and($logs[5]->level)->toBe('DONE');
126+
});
127+
128+
it('handles concurrent job output with garbled lines', function () {
129+
$file = generateLogFile('horizon_concurrent.log', content: <<<EOF
130+
2022-11-11 13:50:44 App\Jobs\SlowJob ................ RUNNING
131+
2022-11-11 13:50:44 App\Jobs\QuickJob ............... 40.12ms DONE
132+
2022-11-11 13:50:44 App\Jobs\AnotherJob ............. 35.42ms DONE
133+
2022-11-11 13:50:44 App\Jobs\SlowJob 2022-11-11 13:50:44 App\Notifications\SomeNotification
134+
2022-11-11 13:50:44 App\Notifications\SomeNotification ..................... 71.48ms DONE
135+
2022-11-11 13:50:44 App\Jobs\SlowJob ..................... 55.35ms DONE
136+
EOF);
137+
$file = new LogFile($file->path);
138+
139+
expect($file->type()->value)->toBe(LogType::HORIZON);
140+
141+
$logReader = $file->logs()->scan();
142+
143+
$logs = $logReader->get();
144+
145+
// File should be detected as Horizon type
146+
expect($file->type()->value)->toBe(LogType::HORIZON);
147+
148+
// Valid lines should be parsed correctly (garbled line might be skipped or partially parsed)
149+
expect($logs)->toBeArray()
150+
->and(count($logs))->toBeGreaterThanOrEqual(4);
151+
152+
// Check that at least some valid logs were parsed
153+
$validLogs = array_filter($logs, fn($log) => $log->datetime !== null);
154+
expect(count($validLogs))->toBeGreaterThanOrEqual(4);
155+
});

tests/Unit/HorizonLogs/HorizonOldLogsTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,33 @@
2424
->and($logs[1]->message)->toBe('App\Notifications\BookingUpdated')
2525
->and($logs[1]->context['uuid'])->toBe('13acffa6-fd25-4d36-876a-b48e968302a4');
2626
});
27+
28+
it('can process old Horizon logs with failed status', function () {
29+
$file = generateLogFile('horizon_old_failed.log', content: <<<EOF
30+
[2022-10-07 09:41:00][a1b2c3d4-e5f6-7890-1234-567890abcdef] Processing: App\Jobs\SendEmailJob
31+
[2022-10-07 09:41:01][a1b2c3d4-e5f6-7890-1234-567890abcdef] Failed: App\Jobs\SendEmailJob
32+
[2022-10-07 09:42:00][b2c3d4e5-f6a7-8901-2345-67890abcdef1] Processing: App\Events\UserRegistered
33+
[2022-10-07 09:42:00][b2c3d4e5-f6a7-8901-2345-67890abcdef1] Processed: App\Events\UserRegistered
34+
EOF);
35+
$file = new LogFile($file->path);
36+
37+
expect($file->type()->value)->toBe(LogType::HORIZON_OLD); // HorizonOldLog
38+
39+
$logReader = $file->logs()->scan();
40+
41+
expect($logs = $logReader->get())->toHaveCount(4)
42+
->and($logs[0]->datetime->toDateTimeString())->toBe('2022-10-07 09:41:00')
43+
->and($logs[0]->level)->toBe('Processing')
44+
->and($logs[0]->message)->toBe('App\Jobs\SendEmailJob')
45+
->and($logs[0]->context['uuid'])->toBe('a1b2c3d4-e5f6-7890-1234-567890abcdef')
46+
->and($logs[1]->datetime->toDateTimeString())->toBe('2022-10-07 09:41:01')
47+
->and($logs[1]->level)->toBe('Failed')
48+
->and($logs[1]->message)->toBe('App\Jobs\SendEmailJob')
49+
->and($logs[1]->context['uuid'])->toBe('a1b2c3d4-e5f6-7890-1234-567890abcdef')
50+
->and($logs[2]->datetime->toDateTimeString())->toBe('2022-10-07 09:42:00')
51+
->and($logs[2]->level)->toBe('Processing')
52+
->and($logs[2]->message)->toBe('App\Events\UserRegistered')
53+
->and($logs[3]->datetime->toDateTimeString())->toBe('2022-10-07 09:42:00')
54+
->and($logs[3]->level)->toBe('Processed')
55+
->and($logs[3]->message)->toBe('App\Events\UserRegistered');
56+
});

0 commit comments

Comments
 (0)