Skip to content

Commit

Permalink
test: process & parallel
Browse files Browse the repository at this point in the history
  • Loading branch information
cclilshy committed Aug 13, 2024
1 parent 331503c commit 464b8fa
Show file tree
Hide file tree
Showing 17 changed files with 370 additions and 170 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/phpunit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
uses: shivammathur/setup-php@v2
with:
php-version: '8.3'
extensions: posix,sockets,pcntl,openssl,parallel
extensions: posix,sockets,pcntl,openssl,parallel,curl
env:
phpts: ts

Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.idea
p-ripple-core.iml

/build/
/vendor/
composer.lock

Expand Down
8 changes: 5 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@
},
"require-dev": {
"ext-curl": "*",
"ext-parallel": "*",
"phpunit/phpunit": "^11.2",
"friendsofphp/php-cs-fixer": "*",
"ext-parallel": "*"
}
"friendsofphp/php-cs-fixer": "*"
},
"minimum-stability": "dev",
"prefer-stable": true
}
17 changes: 0 additions & 17 deletions example/parallel.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,4 @@
* 由于软件或软件的使用或其他交易而引起的任何索赔、损害或其他责任承担责任。
*/

use parallel\Runtime;

include_once __DIR__ . '/../vendor/autoload.php';


$channel = parallel\Channel::make('channel');
$thread = new Runtime();

$thread->run(static function ($channel) {
\sleep(1);
$channel->send(true);
}, [$channel]);


while(1) {
\var_dump($channel->recv());
\sleep(1);
}
25 changes: 18 additions & 7 deletions example/process.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,25 @@
* 由于软件或软件的使用或其他交易而引起的任何索赔、损害或其他责任承担责任。
*/

include_once __DIR__ .'/../vendor/autoload.php';
use P\System;

use function P\async;
use function P\await;
use function P\defer;
use function P\tick;

include_once __DIR__ . '/../vendor/autoload.php';

$task = \P\System::Process()->task(function () {
\P\repeat(function () {}, 1);
$code = \mt_rand(0, 255);
$async = async(function () use ($code) {
$task = System::Process()->task(function () use ($code) {
\P\sleep(1);
defer(function () use ($code) {
exit($code);
});
});
$runtime = $task->run();
return await($runtime->getPromise());
});

$task->run();

\P\tick();
\var_dump($code, $async->await());
tick();
4 changes: 4 additions & 0 deletions example/thread.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@
$thread = thread(static function ($context) {
return \file_get_contents(__FILE__);
});

$thread->run()->onValue(static function ($value) {
echo \strlen($value), \PHP_EOL;
});
}

tick();
12 changes: 10 additions & 2 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,21 @@
~ 由于软件或软件的使用或其他交易而引起的任何索赔、损害或其他责任承担责任。
-->

<phpunit bootstrap="vendor/autoload.php" colors="true">
<phpunit bootstrap="vendor/autoload.php"
colors="true"
stopOnFailure="false"
failOnWarning="true"
failOnRisky="true"
testdox="true">
<testsuites>
<testsuite name="Core">
<directory>tests</directory>
</testsuite>
</testsuites>

<php>
<env name="APP_ENV" value="testing"/>
<env name="DB_CONNECTION" value="sqlite"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="XDEBUG_MODE" value="coverage"/>
</php>
</phpunit>
2 changes: 1 addition & 1 deletion src/Core/Channel/Channel.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ public function close(): void
$this->writeLock->close();

if ($this->owner) {
unlink($this->path);
file_exists($this->path) && unlink($this->path);
}

$this->closed = true;
Expand Down
18 changes: 5 additions & 13 deletions src/Core/Coroutine/Coroutine.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@

use function P\delay;
use function P\registerForkHandler;
use function P\run;
use function P\tick;
use function spl_object_hash;

/**
Expand Down Expand Up @@ -211,7 +211,6 @@ public function async(Closure $closure): Promise
$this->handleEscapeException($exception);
}


if ($fiber->isTerminated()) {
try {
$result = $fiber->getReturn();
Expand Down Expand Up @@ -270,7 +269,7 @@ public function sleep(float|int $second): void
try {
// 尝试恢复Fiber运行
$fiber->resume();
} catch (EscapeException) {
} catch (EscapeException $exception) {
// 恢复运行过程发生逃逸异常
$this->handleEscapeException($exception);
} catch (Throwable $e) {
Expand Down Expand Up @@ -332,19 +331,12 @@ public function getCoroutine(): array|null
*/
public function handleEscapeException(EscapeException $exception): void
{
if (!Fiber::getCurrent()) {
if (!Fiber::getCurrent() || !$this->isCoroutine()) {
$this->fiber2callback = array();

run();
tick();
exit(0);
}

if ($this->isCoroutine()) {
throw $exception;
} else {
$this->fiber2callback = array();

Fiber::suspend();
throw $exception;
}
}
}
Loading

0 comments on commit 464b8fa

Please sign in to comment.