|
12 | 12 | use React\Promise\PromiseInterface; |
13 | 13 | use React\Promise\Timer\TimeoutException; |
14 | 14 | use ReactParallel\Factory; |
| 15 | +use ReactParallel\ObjectProxy\Generated\WyriHaximus__Metrics_RegistryProxy; |
15 | 16 | use ReactParallel\ObjectProxy\NonExistentInterface; |
16 | 17 | use ReactParallel\ObjectProxy\Proxy; |
17 | 18 | use stdClass; |
|
26 | 27 | use function explode; |
27 | 28 | use function range; |
28 | 29 | use function React\Promise\all; |
| 30 | +use function Safe\sleep; |
29 | 31 | use function time; |
30 | 32 |
|
31 | 33 | final class ProxyTest extends AsyncTestCase |
@@ -169,4 +171,46 @@ public function metricsDestructionTesting(): void |
169 | 171 | (int) explode("\n", explode('react_parallel_object_proxy_destruct_total{class="WyriHaximus\Metrics\InMemory\Registry",interface="WyriHaximus\Metrics\Registry"}', $txt)[1])[0] |
170 | 172 | ); |
171 | 173 | } |
| 174 | + |
| 175 | + /** |
| 176 | + * @test |
| 177 | + */ |
| 178 | + public function destructionEdgeCases(): void |
| 179 | + { |
| 180 | + $loop = EventLoopFactory::create(); |
| 181 | + $factory = new Factory($loop); |
| 182 | + $registry = new InMemmoryRegistry(); |
| 183 | + $proxy = (new Proxy($factory))->withMetrics($registry); |
| 184 | + $limitedPool = $factory->limitedPool(13); |
| 185 | + $registryProxy = $proxy->create($registry, Registry::class); |
| 186 | + $fn = static function (int $int, WyriHaximus__Metrics_RegistryProxy $registryProxy, int $sleep): int { |
| 187 | + $registryProxy->notifyMainThreadAboutOurExistence(); |
| 188 | + sleep($sleep); |
| 189 | + $registryProxy->counter('counter', 'bla bla bla', new Label\Name('name'))->counter(new Label('name', 'value'))->incr(); |
| 190 | + |
| 191 | + return $int; |
| 192 | + }; |
| 193 | + |
| 194 | + $promises = []; |
| 195 | + foreach (range(0, 3) as $i) { |
| 196 | + $promises[] = $limitedPool->run($fn, [$i, $registryProxy, 0]); |
| 197 | + } |
| 198 | + |
| 199 | + $leet = $this->await( |
| 200 | + // @phpstan-ignore-next-line |
| 201 | + all($promises)->then(static function (array $v) use ($factory): PromiseInterface { |
| 202 | + return new Promise(static function (callable $resolve) use ($v, $factory): void { |
| 203 | + $factory->loop()->addTimer(3, static function () use ($resolve, $v): void { |
| 204 | + $resolve($v); |
| 205 | + }); |
| 206 | + }); |
| 207 | + })->then(static function () use ($fn, $registryProxy, $limitedPool): PromiseInterface { |
| 208 | + return $limitedPool->run($fn, [1337, $registryProxy, 20]); |
| 209 | + })->always(static function () use ($limitedPool): void { |
| 210 | + $limitedPool->close(); |
| 211 | + }), |
| 212 | + $loop |
| 213 | + ); |
| 214 | + self::assertSame(1337, $leet); |
| 215 | + } |
172 | 216 | } |
0 commit comments