Skip to content

Commit

Permalink
gh actions setup
Browse files Browse the repository at this point in the history
  • Loading branch information
sweikenb committed Sep 29, 2023
1 parent 8821514 commit e3fe071
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 37 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/phpunit.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: PHP Composer
name: PHPUnit Tests

on:
push:
Expand Down
78 changes: 42 additions & 36 deletions tests/ProcessManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,40 +117,46 @@ public function testRunProcessWithExceptions(): void
* @covers \Sweikenb\Library\Pcntl\Api\ProcessInterface::sendMessage
* @covers \Sweikenb\Library\Pcntl\Api\ProcessInterface::getNextMessage
*/
// public function testIPC(): void
// {
// $pm = new ProcessManager();
// $factory = new MessageFactory();
//
// $childs = [];
// /* @var array<int, ChildProcessInterface> $childs */
//
// for ($i = 0; $i < 5; $i++) {
// $childs[$i] = $pm->runProcess(
// function (ChildProcessInterface $process, ParentProcessInterface $parentProcess) use ($i, $factory) {
// $message = $parentProcess->getNextMessage();
// $parentProcess->sendMessage(
// $factory->create(sprintf('answer from #%d', $i), 'hello')
// );
// return $message !== null
// && $message->getTopic() === sprintf("hello my child %d", $i)
// && $message->getPayload() === 'hello';
// }
// );
// $childs[$i]->sendMessage($factory->create(sprintf("hello my child %d", $i), 'hello'));
// }
//
// foreach ($childs as $i => $child) {
// $message = $child->getNextMessage();
// $this->assertNotNull($message);
// $this->assertSame(sprintf("answer from #%s", $i), $message->getTopic());
// $this->assertSame('hello', $message->getPayload());
//
// $this->assertNull($child->getNextMessage(false));
// }
//
// $pm->wait(fn(int $status) => $this->assertSame(0, $status));
// }
public function testIPC(): void
{
if (getenv('GITHUB_ACTIONS') === 'true') {
$this->markTestSkipped(
'IPC is not working for GitHub actions, so we have to skipp it for now.'
);
}

$pm = new ProcessManager();
$factory = new MessageFactory();

$childs = [];
/* @var array<int, ChildProcessInterface> $childs */

for ($i = 0; $i < 5; $i++) {
$childs[$i] = $pm->runProcess(
function (ChildProcessInterface $process, ParentProcessInterface $parentProcess) use ($i, $factory) {
$message = $parentProcess->getNextMessage();
$parentProcess->sendMessage(
$factory->create(sprintf('answer from #%d', $i), 'hello')
);
return $message !== null
&& $message->getTopic() === sprintf("hello my child %d", $i)
&& $message->getPayload() === 'hello';
}
);
$childs[$i]->sendMessage($factory->create(sprintf("hello my child %d", $i), 'hello'));
}

foreach ($childs as $i => $child) {
$message = $child->getNextMessage();
$this->assertNotNull($message);
$this->assertSame(sprintf("answer from #%s", $i), $message->getTopic());
$this->assertSame('hello', $message->getPayload());

$this->assertNull($child->getNextMessage(false));
}

$pm->wait(fn(int $status) => $this->assertSame(0, $status));
}

/**
* @covers \Sweikenb\Library\Pcntl\ProcessManager::wait
Expand Down Expand Up @@ -191,14 +197,14 @@ public function testOnChildExit(): void
$exitCalled = 0;
$waitCalled = 0;

$pm->onThreadExit(function () use(&$exitCalled, $waitCalled){
$pm->onThreadExit(function () use (&$exitCalled, $waitCalled) {
$exitCalled++;
$this->assertGreaterThanOrEqual($waitCalled, $exitCalled);
});
for ($i = 1; $i <= 20; $i++) {
$pm->runProcess(fn() => usleep(1000));
}
$pm->wait(function () use($exitCalled, &$waitCalled){
$pm->wait(function () use ($exitCalled, &$waitCalled) {
$waitCalled++;
$this->assertLessThan($waitCalled, $exitCalled);
});
Expand Down

0 comments on commit e3fe071

Please sign in to comment.