|
| 1 | +--TEST-- |
| 2 | +Test that OOM handling skips the memory limit increase when current usage is already higher |
| 3 | +--INI-- |
| 4 | +memory_limit=67108864 |
| 5 | +--FILE-- |
| 6 | +<?php |
| 7 | + |
| 8 | +declare(strict_types=1); |
| 9 | + |
| 10 | +namespace Sentry { |
| 11 | + // override the function so that we can trace how often it got invoked |
| 12 | + function ini_set(string $option, string $value) |
| 13 | + { |
| 14 | + if (strtolower($option) !== 'memory_limit') { |
| 15 | + return \ini_set($option, $value); |
| 16 | + } |
| 17 | + |
| 18 | + $GLOBALS['sentry_test_ini_set_calls'] = ($GLOBALS['sentry_test_ini_set_calls'] ?? 0) + 1; |
| 19 | + |
| 20 | + return \ini_set($option, $value); |
| 21 | + } |
| 22 | + |
| 23 | + // override the function so that we can test if the memory gets increased to a value |
| 24 | + // that is lower than currently in use |
| 25 | + function memory_get_usage(bool $realUsage = false): int |
| 26 | + { |
| 27 | + return $GLOBALS['sentry_test_memory_get_usage'] ?? \memory_get_usage($realUsage); |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +namespace Sentry\Tests { |
| 32 | + use Sentry\Event; |
| 33 | + use Sentry\Transport\Result; |
| 34 | + use Sentry\Transport\ResultStatus; |
| 35 | + use Sentry\Transport\TransportInterface; |
| 36 | + |
| 37 | + $vendor = __DIR__; |
| 38 | + |
| 39 | + while (!file_exists($vendor . '/vendor')) { |
| 40 | + $vendor = \dirname($vendor); |
| 41 | + } |
| 42 | + |
| 43 | + require $vendor . '/vendor/autoload.php'; |
| 44 | + |
| 45 | + error_reporting(\E_ALL & ~\E_DEPRECATED & ~\E_USER_DEPRECATED); |
| 46 | + |
| 47 | + $GLOBALS['sentry_test_memory_get_usage'] = (64 * 1024 * 1024) + (5 * 1024 * 1024) + 1; |
| 48 | + |
| 49 | + set_error_handler(static function (int $level): bool { |
| 50 | + if (\E_WARNING !== $level) { |
| 51 | + return false; |
| 52 | + } |
| 53 | + |
| 54 | + $GLOBALS['sentry_test_warning_handler_calls'] = ($GLOBALS['sentry_test_warning_handler_calls'] ?? 0) + 1; |
| 55 | + |
| 56 | + return true; |
| 57 | + }); |
| 58 | + |
| 59 | + $transport = new class implements TransportInterface { |
| 60 | + public function send(Event $event): Result |
| 61 | + { |
| 62 | + $GLOBALS['sentry_test_transport_calls'] = ($GLOBALS['sentry_test_transport_calls'] ?? 0) + 1; |
| 63 | + |
| 64 | + return new Result(ResultStatus::success()); |
| 65 | + } |
| 66 | + |
| 67 | + public function close(?int $timeout = null): Result |
| 68 | + { |
| 69 | + return new Result(ResultStatus::success()); |
| 70 | + } |
| 71 | + }; |
| 72 | + |
| 73 | + \Sentry\init([ |
| 74 | + 'dsn' => 'http://public@example.com/sentry/1', |
| 75 | + 'transport' => $transport, |
| 76 | + 'capture_silenced_errors' => true, |
| 77 | + ]); |
| 78 | + |
| 79 | + register_shutdown_function(static function (): void { |
| 80 | + echo 'Transport calls: ' . ($GLOBALS['sentry_test_transport_calls'] ?? 0) . \PHP_EOL; |
| 81 | + echo 'Memory limit increase attempts: ' . ($GLOBALS['sentry_test_ini_set_calls'] ?? 0) . \PHP_EOL; |
| 82 | + echo 'Warning handler calls: ' . ($GLOBALS['sentry_test_warning_handler_calls'] ?? 0) . \PHP_EOL; |
| 83 | + }); |
| 84 | + |
| 85 | + $foo = str_repeat('x', 1024 * 1024 * 1024); |
| 86 | +} |
| 87 | +?> |
| 88 | +--EXPECTF-- |
| 89 | +%A |
| 90 | +Transport calls: 1 |
| 91 | +Memory limit increase attempts: 0 |
| 92 | +Warning handler calls: 0 |
0 commit comments