|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Sentry\Tests\Util; |
| 6 | + |
| 7 | +use PHPUnit\Framework\TestCase; |
| 8 | +use Sentry\Util\TelemetryStorage; |
| 9 | + |
| 10 | +final class TelemetryStorageTest extends TestCase |
| 11 | +{ |
| 12 | + public function testUnboundedPushAndToArray(): void |
| 13 | + { |
| 14 | + $storage = TelemetryStorage::unbounded(); |
| 15 | + $storage->push('foo'); |
| 16 | + $storage->push('bar'); |
| 17 | + |
| 18 | + $result = $storage->toArray(); |
| 19 | + $this->assertSame(2, $storage->count()); |
| 20 | + $this->assertEquals(['foo', 'bar'], $result); |
| 21 | + } |
| 22 | + |
| 23 | + public function testUnboundedDrainClearsStorage(): void |
| 24 | + { |
| 25 | + $storage = TelemetryStorage::unbounded(); |
| 26 | + $storage->push('foo'); |
| 27 | + $storage->push('bar'); |
| 28 | + |
| 29 | + $this->assertSame(2, $storage->count()); |
| 30 | + $result = $storage->drain(); |
| 31 | + $this->assertTrue($storage->isEmpty()); |
| 32 | + $this->assertEquals(['foo', 'bar'], $result); |
| 33 | + } |
| 34 | + |
| 35 | + public function testUnboundedIsEmpty(): void |
| 36 | + { |
| 37 | + $storage = TelemetryStorage::unbounded(); |
| 38 | + $this->assertTrue($storage->isEmpty()); |
| 39 | + |
| 40 | + $storage->push('foo'); |
| 41 | + |
| 42 | + $this->assertFalse($storage->isEmpty()); |
| 43 | + } |
| 44 | + |
| 45 | + public function testBoundedCapacityOverwritesOldestItems(): void |
| 46 | + { |
| 47 | + $storage = TelemetryStorage::bounded(2); |
| 48 | + $storage->push('foo'); |
| 49 | + $storage->push('bar'); |
| 50 | + $storage->push('baz'); |
| 51 | + |
| 52 | + $this->assertSame(2, $storage->count()); |
| 53 | + $this->assertEquals(['bar', 'baz'], $storage->toArray()); |
| 54 | + } |
| 55 | + |
| 56 | + public function testBoundedDrainReturnsLogicalOrderAndClearsStorage(): void |
| 57 | + { |
| 58 | + $storage = TelemetryStorage::bounded(2); |
| 59 | + $storage->push('foo'); |
| 60 | + $storage->push('bar'); |
| 61 | + $storage->push('baz'); |
| 62 | + |
| 63 | + $this->assertSame(2, $storage->count()); |
| 64 | + $result = $storage->drain(); |
| 65 | + $this->assertTrue($storage->isEmpty()); |
| 66 | + $this->assertEquals(['bar', 'baz'], $result); |
| 67 | + } |
| 68 | + |
| 69 | + public function testBoundedCapacityOneKeepsLatestItem(): void |
| 70 | + { |
| 71 | + $storage = TelemetryStorage::bounded(1); |
| 72 | + $storage->push('foo'); |
| 73 | + $storage->push('bar'); |
| 74 | + |
| 75 | + $this->assertCount(1, $storage); |
| 76 | + $this->assertEquals(['bar'], $storage->toArray()); |
| 77 | + } |
| 78 | +} |
0 commit comments