|
| 1 | +--TEST-- |
| 2 | +Test that when handling a out of memory error the memory limit is increased with 5 MiB and the event is serialized and ready to be sent |
| 3 | +--SKIPIF-- |
| 4 | +<?php |
| 5 | +if (PHP_VERSION_ID < 80500) { |
| 6 | + die('skip - only works for PHP 8.5 and above'); |
| 7 | +} |
| 8 | +--INI-- |
| 9 | +memory_limit=67108864 |
| 10 | +--FILE-- |
| 11 | +<?php |
| 12 | + |
| 13 | +declare(strict_types=1); |
| 14 | + |
| 15 | +namespace Sentry\Tests; |
| 16 | + |
| 17 | +use Sentry\ClientBuilder; |
| 18 | +use Sentry\Event; |
| 19 | +use Sentry\Options; |
| 20 | +use Sentry\SentrySdk; |
| 21 | +use Sentry\Serializer\PayloadSerializer; |
| 22 | +use Sentry\Serializer\PayloadSerializerInterface; |
| 23 | +use Sentry\Transport\Result; |
| 24 | +use Sentry\Transport\ResultStatus; |
| 25 | +use Sentry\Transport\TransportInterface; |
| 26 | + |
| 27 | +$vendor = __DIR__; |
| 28 | + |
| 29 | +while (!file_exists($vendor . '/vendor')) { |
| 30 | + $vendor = \dirname($vendor); |
| 31 | +} |
| 32 | + |
| 33 | +require $vendor . '/vendor/autoload.php'; |
| 34 | + |
| 35 | +$options = new Options([ |
| 36 | + 'dsn' => 'http://public@example.com/sentry/1', |
| 37 | +]); |
| 38 | + |
| 39 | +$transport = new class(new PayloadSerializer($options)) implements TransportInterface { |
| 40 | + private $payloadSerializer; |
| 41 | + |
| 42 | + public function __construct(PayloadSerializerInterface $payloadSerializer) |
| 43 | + { |
| 44 | + $this->payloadSerializer = $payloadSerializer; |
| 45 | + } |
| 46 | + |
| 47 | + public function send(Event $event): Result |
| 48 | + { |
| 49 | + $serialized = $this->payloadSerializer->serialize($event); |
| 50 | + |
| 51 | + echo 'Transport called' . \PHP_EOL; |
| 52 | + |
| 53 | + return new Result(ResultStatus::success()); |
| 54 | + } |
| 55 | + |
| 56 | + public function close(?int $timeout = null): Result |
| 57 | + { |
| 58 | + return new Result(ResultStatus::success()); |
| 59 | + } |
| 60 | +}; |
| 61 | + |
| 62 | +$options->setTransport($transport); |
| 63 | + |
| 64 | +$client = (new ClientBuilder($options))->getClient(); |
| 65 | + |
| 66 | +SentrySdk::init()->bindClient($client); |
| 67 | + |
| 68 | +echo 'Before OOM memory limit: ' . \ini_get('memory_limit'); |
| 69 | + |
| 70 | +register_shutdown_function(function () { |
| 71 | + echo 'After OOM memory limit: ' . \ini_get('memory_limit'); |
| 72 | +}); |
| 73 | + |
| 74 | +$array = []; |
| 75 | +for ($i = 0; $i < 100000000; ++$i) { |
| 76 | + $array[] = 'sentry'; |
| 77 | +} |
| 78 | +--EXPECTF-- |
| 79 | +Before OOM memory limit: 67108864 |
| 80 | +Fatal error: Allowed memory size of %d bytes exhausted (tried to allocate %d bytes) in %s on line %d |
| 81 | +Stack trace: |
| 82 | +%A |
| 83 | +Transport called |
| 84 | +After OOM memory limit: 72351744 |
0 commit comments