Skip to content

Commit

Permalink
🔨 fix wait with Event->exit
Browse files Browse the repository at this point in the history
  • Loading branch information
matyo91 committed Apr 19, 2021
1 parent 8ac9b6e commit 4c2aaf6
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 63 deletions.
59 changes: 32 additions & 27 deletions src/Adapter/Swoole/EventLoop.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,36 +35,41 @@ public function wait(Promise $promise)
$isRejected = false;
$promiseSettled = false;

Coroutine::create(function() use ($promise, &$value, &$isRejected, &$promiseSettled) {
$ticks = 1;
//$wg = new WaitGroup(1);
$channel = new Channel($ticks);
Internal\PromiseWrapper::toHandledPromise($promise, $this->unhandledFailingPromises)->getSwoolePromise()->then(
function ($result) use (/* $wg , */$channel, &$value, &$promiseSettled) {
$promiseSettled = true;
$value = $result;
$channel->push(true);
//$wg->done();
},
function ($result) use (/* $wg , */$channel, &$value, &$isRejected, &$promiseSettled) {
$promiseSettled = true;
$value = $result;
$isRejected = true;
$channel->push(true);
//$wg->done();
}
);
while ($ticks--) {
$channel->pop();
//Coroutine::create(function() use ($promise, &$value, &$isRejected, &$promiseSettled) {
$ticks = 1;
//$wg = new WaitGroup(1);
//$channel = new Channel($ticks);
Internal\PromiseWrapper::toHandledPromise($promise, $this->unhandledFailingPromises)->getSwoolePromise()->then(
function ($result) use (/* $wg , $channel, */&$value, &$promiseSettled) {
$promiseSettled = true;
$value = $result;
//$channel->push(true);
//$wg->done();
Event::exit();
},
function ($result) use (/* $wg , $channel, */&$value, &$isRejected, &$promiseSettled) {
$promiseSettled = true;
$value = $result;
$isRejected = true;
//$channel->push(true);
//$wg->done();
Event::exit();
}
$channel->close();
//$wg->wait();
});
while (!$promiseSettled) {
);
if (!$promiseSettled) {
Event::wait();
}
//while ($ticks--) {
//$channel->pop();
//}
//$channel->close();
//$wg->wait();
//});
//while (!$promiseSettled) {
// @codeCoverageIgnoreStart
usleep(SwoolePromise::PROMISE_WAIT);
//usleep(SwoolePromise::PROMISE_WAIT);
// @codeCoverageIgnoreEnd
}
//}
//Event::wait();
//swoole_event_wait();

Expand Down
6 changes: 3 additions & 3 deletions src/Adapter/Swoole/Internal/SwoolePromise.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,11 +232,11 @@ private function setResult($value, $state): void
};
$value->then($callable, $callable);
// resolve async locking error (code to remove)
while (!$resolved) {
//while (!$resolved) {
// @codeCoverageIgnoreStart
usleep(self::PROMISE_WAIT);
//usleep(self::PROMISE_WAIT);
// @codeCoverageIgnoreEnd
}
//}
} else if ($this->isPending()) {
$this->result = $value;
$this->setState($state);
Expand Down
34 changes: 1 addition & 33 deletions tests/EventLoopTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public function testDeferredRejected()

public function testWaitFunctionShouldReturnAsSoonAsPromiseIsResolved()
{
/*$eventLoop = $this->createEventLoop();
$eventLoop = $this->createEventLoop();
$count = 0;
$unfinishedGenerator = function (EventLoop $eventLoop, int &$count): \Generator {
while (++$count < 10) {
Expand All @@ -178,38 +178,6 @@ public function testWaitFunctionShouldReturnAsSoonAsPromiseIsResolved()
$eventLoop->async($unfinishedGenerator($eventLoop, $count));
$result = $eventLoop->wait($eventLoop->promiseFulfilled('value'));

$this->assertSame('value', $result);
$this->assertLessThanOrEqual(2, $count);*/

$wait = function($cid) {
$result = null;
$n = new Atomic();
Coroutine::create(function () use (&$result, $n) {
echo "coucou1\n";
Coroutine::sleep(0.001);
echo "coucou2\n";
$result = 'value';
$n->wakeup();
});

$n->wait(0.001);
echo "end wait***\n";

return $result;
};
$count = 0;
$cid = Coroutine::create(function() use (&$count) {
while (++$count < 10) {
echo "coucou3\n";
Coroutine::sleep(0.001);
echo "coucou4\n";
}
});
$result = $wait($cid);

//Coroutine::suspend($uid);
//Event::wait();
//Coroutine::resume($uid);
$this->assertSame('value', $result);
$this->assertLessThanOrEqual(2, $count);
}
Expand Down

0 comments on commit 4c2aaf6

Please sign in to comment.