mirror of
https://github.com/pestphp/pest.git
synced 2026-12-06 07:42:17 +01:00
fix(tia): stop ReadCapture from keeping a wrapper per filesystem call (#2057)
* fix(tia): free the ReadCapture wrapper each time it steps aside stream_wrapper_restore() takes a user wrapper out of the wrapper table but does not free it, so every filesystem call made while a test records kept one until the process ended. Unregistering it first frees it. * fix(tia): skip the unregister when file:// is already gone A test can unregister file:// itself. stream_wrapper_unregister() then warns, where stream_wrapper_restore() alone did not, so check first. Also run the per-test memory check over 5,000 tests and update the snapshot and parallel totals for the new tests. * Use fopen() in the memory regression test --------- Co-authored-by: Mohannad Elemary <20709195+MohannadElemary@users.noreply.github.com>
This commit is contained in:
@@ -238,6 +238,78 @@ describe('the file wrapper', function (): void {
|
||||
]);
|
||||
});
|
||||
|
||||
it('keeps no memory for each filesystem call while a test records', function (): void {
|
||||
$growth = 0;
|
||||
|
||||
tiaReadCaptureLinks(function () use (&$growth): void {
|
||||
fclose(fopen(__FILE__, 'rb'));
|
||||
|
||||
$before = memory_get_usage();
|
||||
|
||||
for ($i = 0; $i < 10_000; $i++) {
|
||||
fclose(fopen(__FILE__, 'rb'));
|
||||
}
|
||||
|
||||
$growth = memory_get_usage() - $before;
|
||||
});
|
||||
|
||||
expect($growth)->toBeLessThan(100_000);
|
||||
});
|
||||
|
||||
it('keeps no memory for each test it records', function (): void {
|
||||
tiaReadCaptureLinks(fn (): bool => true);
|
||||
|
||||
$before = memory_get_usage();
|
||||
|
||||
for ($i = 0; $i < 5_000; $i++) {
|
||||
tiaReadCaptureLinks(fn (): bool => true);
|
||||
}
|
||||
|
||||
expect(memory_get_usage() - $before)->toBeLessThan(100_000);
|
||||
});
|
||||
|
||||
it('is restored quietly when the test already restored it', function (): void {
|
||||
$errors = [];
|
||||
|
||||
set_error_handler(function (int $level, string $message) use (&$errors): bool {
|
||||
$errors[] = $message;
|
||||
|
||||
return true;
|
||||
});
|
||||
|
||||
try {
|
||||
tiaReadCaptureLinks(fn (): bool => stream_wrapper_restore('file'));
|
||||
} finally {
|
||||
restore_error_handler();
|
||||
}
|
||||
|
||||
expect($errors)->toBeEmpty()
|
||||
->and(tiaReadCaptureWrapperType())->toBe('plainfile');
|
||||
});
|
||||
|
||||
it('is restored quietly when the test unregistered it', function (): void {
|
||||
$errors = [];
|
||||
|
||||
set_error_handler(function (int $level, string $message) use (&$errors): bool {
|
||||
$errors[] = $message;
|
||||
|
||||
return true;
|
||||
});
|
||||
|
||||
try {
|
||||
tiaReadCaptureLinks(fn (): bool => stream_wrapper_unregister('file'));
|
||||
} finally {
|
||||
restore_error_handler();
|
||||
|
||||
if (! in_array('file', stream_get_wrappers(), true)) {
|
||||
stream_wrapper_restore('file');
|
||||
}
|
||||
}
|
||||
|
||||
expect($errors)->toBeEmpty()
|
||||
->and(tiaReadCaptureWrapperType())->toBe('plainfile');
|
||||
});
|
||||
|
||||
it('reports symlinks as links while a test records', function (bool $targetExists, bool $expectedExists): void {
|
||||
$directory = sys_get_temp_dir().DIRECTORY_SEPARATOR.'pest-read-capture-'.bin2hex(random_bytes(8));
|
||||
mkdir($directory);
|
||||
|
||||
Reference in New Issue
Block a user