|
2 | 2 |
|
3 | 3 | declare(strict_types=1);
|
4 | 4 |
|
| 5 | +/* |
| 6 | + * This file is part of the Neo4j PHP Client and Driver package. |
| 7 | + * |
| 8 | + * (c) Nagels <https://nagels.tech> |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view the LICENSE |
| 11 | + * file that was distributed with this source code. |
| 12 | + */ |
| 13 | + |
5 | 14 | namespace Laudis\Neo4j\Tests\Unit;
|
6 | 15 |
|
| 16 | +use Generator; |
| 17 | +use Laudis\Neo4j\Authentication\Authenticate; |
7 | 18 | use Laudis\Neo4j\Bolt\ConnectionPool;
|
8 |
| -use Monolog\Test\TestCase; |
| 19 | +use Laudis\Neo4j\Common\GeneratorHelper; |
| 20 | +use Laudis\Neo4j\Common\Uri; |
| 21 | +use Laudis\Neo4j\Contracts\ConnectionFactoryInterface; |
| 22 | +use Laudis\Neo4j\Contracts\ConnectionInterface; |
| 23 | +use Laudis\Neo4j\Contracts\SemaphoreInterface; |
| 24 | +use Laudis\Neo4j\Databags\ConnectionRequestData; |
| 25 | +use Laudis\Neo4j\Databags\SessionConfiguration; |
| 26 | +use Laudis\Neo4j\Databags\SslConfiguration; |
| 27 | +use function microtime; |
| 28 | +use PHPUnit\Framework\MockObject\MockObject; |
| 29 | +use PHPUnit\Framework\TestCase; |
| 30 | +use function sleep; |
9 | 31 |
|
10 | 32 | class BoltConnectionPoolTest extends TestCase
|
11 | 33 | {
|
12 | 34 | private ConnectionPool $pool;
|
| 35 | + /** @var SemaphoreInterface&MockObject */ |
| 36 | + private SemaphoreInterface $semaphore; |
| 37 | + /** @var ConnectionFactoryInterface&MockObject */ |
| 38 | + private ConnectionFactoryInterface $factory; |
13 | 39 |
|
14 | 40 | protected function setUp(): void
|
15 | 41 | {
|
16 | 42 | parent::setUp();
|
17 | 43 |
|
18 |
| - $this->pool = new ConnectionPool() |
| 44 | + $this->setupPool((function (): Generator { |
| 45 | + yield 1.0; |
| 46 | + |
| 47 | + return true; |
| 48 | + })()); |
| 49 | + } |
| 50 | + |
| 51 | + public function testSimpleAcquire(): void |
| 52 | + { |
| 53 | + $generator = $this->pool->acquire(SessionConfiguration::default()); |
| 54 | + |
| 55 | + $connection = GeneratorHelper::getReturnFromGenerator($generator); |
| 56 | + |
| 57 | + self::assertInstanceOf(ConnectionInterface::class, $connection); |
| 58 | + } |
| 59 | + |
| 60 | + public function testTimingAcquire(): void |
| 61 | + { |
| 62 | + $generator = $this->pool->acquire(SessionConfiguration::default()); |
| 63 | + $time = microtime(true); |
| 64 | + |
| 65 | + sleep(1); |
| 66 | + |
| 67 | + $result = $generator->current(); |
| 68 | + $delta = microtime(true) - $time; |
| 69 | + |
| 70 | + $generator->next(); |
| 71 | + $generator->getReturn(); |
| 72 | + |
| 73 | + self::assertEqualsWithDelta($delta, $result, 0.05); |
| 74 | + self::assertEqualsWithDelta(1.0, $result, 0.05); |
| 75 | + } |
| 76 | + |
| 77 | + public function testMultipleWaits(): void |
| 78 | + { |
| 79 | + $this->setupPool((function (): Generator { |
| 80 | + yield 1.0; |
| 81 | + yield 2.0; |
| 82 | + yield 3.0; |
| 83 | + |
| 84 | + return true; |
| 85 | + })()); |
| 86 | + |
| 87 | + $generator = $this->pool->acquire(SessionConfiguration::default()); |
| 88 | + $count = 0; |
| 89 | + while ($generator->valid()) { |
| 90 | + ++$count; |
| 91 | + $generator->next(); |
| 92 | + } |
| 93 | + |
| 94 | + static::assertEquals(3, $count); |
| 95 | + } |
| 96 | + |
| 97 | + public function testRelease(): void |
| 98 | + { |
| 99 | + $this->semaphore->expects(self::once())->method('post'); |
| 100 | + |
| 101 | + $this->pool->release($this->createMock(ConnectionInterface::class)); |
| 102 | + } |
| 103 | + |
| 104 | + public function testReleaseReference(): void |
| 105 | + { |
| 106 | + $connection = $this->pool->acquire(SessionConfiguration::default()); |
| 107 | + $connection->next(); |
| 108 | + $connection = $connection->getReturn(); |
| 109 | + |
| 110 | + static::assertInstanceOf(ConnectionInterface::class, $connection); |
| 111 | + |
| 112 | + // We use a refCount instead of checking for garbage collection as |
| 113 | + // the underlying libraries for mocking keep references throughout the system |
| 114 | + $refCount = $this->refCount($connection); |
| 115 | + |
| 116 | + $this->pool->release($connection); |
| 117 | + |
| 118 | + static::assertEquals($refCount - 1, $this->refCount($connection)); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * @param object $var |
| 123 | + */ |
| 124 | + private function refCount($var): int |
| 125 | + { |
| 126 | + ob_start(); |
| 127 | + debug_zval_dump($var); |
| 128 | + $dump = ob_get_clean(); |
| 129 | + |
| 130 | + $matches = []; |
| 131 | + preg_match('/refcount\(([0-9]+)/', $dump, $matches); |
| 132 | + |
| 133 | + $count = (int) ($matches[1] ?? '0'); |
| 134 | + |
| 135 | + // 3 references are added, including when calling debug_zval_dump() |
| 136 | + return $count - 3; |
| 137 | + } |
| 138 | + |
| 139 | + private function setupPool(Generator $semaphoreGenerator): void |
| 140 | + { |
| 141 | + $this->semaphore = $this->createMock(SemaphoreInterface::class); |
| 142 | + $this->semaphore->method('wait') |
| 143 | + ->willReturn($semaphoreGenerator); |
| 144 | + |
| 145 | + $this->factory = $this->createMock(ConnectionFactoryInterface::class); |
| 146 | + $this->factory->method('createConnection') |
| 147 | + ->willReturn($this->createMock(ConnectionInterface::class)); |
| 148 | + |
| 149 | + $this->pool = new ConnectionPool( |
| 150 | + $this->semaphore, $this->factory, new ConnectionRequestData( |
| 151 | + Uri::create(''), |
| 152 | + Authenticate::disabled(), |
| 153 | + '', |
| 154 | + SslConfiguration::default() |
| 155 | + ) |
| 156 | + ); |
19 | 157 | }
|
20 | 158 | }
|
0 commit comments