diff --git a/composer.json b/composer.json index 7b7e205..9fc9cc7 100644 --- a/composer.json +++ b/composer.json @@ -20,11 +20,19 @@ "wyrihaximus/async-test-utilities": "^5 || ^7.2", "wyrihaximus/pool-info": "^1.1 || ^2.0" }, + "require-dev": { + "react-parallel/runtime": "^3" + }, "autoload": { "psr-4": { "ReactParallel\\Tests\\": "src/" } }, + "autoload-dev": { + "psr-4": { + "ReactParallel\\Tests\\Tests\\": "tests/" + } + }, "config": { "allow-plugins": { "dealerdirect/phpcodesniffer-composer-installer": true, diff --git a/composer.lock b/composer.lock index cb39a6a..6af0f09 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "4d62bac725a9acb4ffa4711a01d06c51", + "content-hash": "535fdb13e75702c6498b2522460f1aef", "packages": [ { "name": "amphp/amp", @@ -11218,7 +11218,65 @@ "time": "2023-01-07T21:34:04+00:00" } ], - "packages-dev": [], + "packages-dev": [ + { + "name": "react-parallel/runtime", + "version": "3.0.0", + "source": { + "type": "git", + "url": "https://github.com/reactphp-parallel/runtime.git", + "reference": "724b2e6c2296435f8ad7eb2b9047647aa7908e0f" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/reactphp-parallel/runtime/zipball/724b2e6c2296435f8ad7eb2b9047647aa7908e0f", + "reference": "724b2e6c2296435f8ad7eb2b9047647aa7908e0f", + "shasum": "" + }, + "require": { + "ext-parallel": "*", + "php": "^8.1", + "react-parallel/event-loop": "^2.0.0", + "react/event-loop": "^1.5", + "react/promise": "^2.9 || ^3.2", + "wyrihaximus/constants": "^1.6" + }, + "require-dev": { + "react-parallel/stubs": "^1.2", + "wyrihaximus/async-test-utilities": "^5 || ^7.2", + "wyrihaximus/ticking-promise": "^3" + }, + "type": "library", + "autoload": { + "psr-4": { + "ReactParallel\\Runtime\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Cees-Jan Kiewiet", + "email": "ceesjank@gmail.com", + "homepage": "http://wyrihaximus.net/" + } + ], + "description": "💨 Convinence wrapper around ext-parallel Runtime for and ReactPHP", + "support": { + "issues": "https://github.com/reactphp-parallel/runtime/issues", + "source": "https://github.com/reactphp-parallel/runtime/tree/3.0.0" + }, + "funding": [ + { + "url": "https://github.com/WyriHaximus", + "type": "github" + } + ], + "time": "2025-02-09T17:04:03+00:00" + } + ], "aliases": [], "minimum-stability": "stable", "stability-flags": {}, diff --git a/tests/AbstractPoolTestTest.php b/tests/AbstractPoolTestTest.php new file mode 100644 index 0000000..899bfbc --- /dev/null +++ b/tests/AbstractPoolTestTest.php @@ -0,0 +1,17 @@ + */ + private array $runtimes = []; + + public function __construct(private readonly EventLoopBridge $eventLoopBridge) + { + } + + /** + * {@inheritDoc} + */ + public function info(): iterable + { + yield Info::TOTAL => $this->activeThreads; + yield Info::BUSY => $this->activeThreads; + yield Info::CALLS => 0; + yield Info::IDLE => 0; + yield Info::SIZE => $this->activeThreads; + } + + /** + * {@inheritDoc} + */ + public function run(\Closure $callable, array $args = []): mixed + { + if ($this->closed === true) { + throw ClosedException::create(); + } + + $runtime = Runtime::create($this->eventLoopBridge); + $this->runtimes[\spl_object_id($runtime)] = $runtime; + $this->activeThreads++; + try { + $result = $runtime->run($callable, $args); + } finally { + unset($this->runtimes[\spl_object_id($runtime)]); + } + $this->activeThreads--; + + return $result; + } + + public function close(): bool + { + $this->closed = true; + + foreach ($this->runtimes as $runtime) { + $runtime->close(); + } + + return true; + } + + public function kill(): bool + { + $this->closed = true; + + foreach ($this->runtimes as $runtime) { + $runtime->kill(); + } + + return true; + } +}