diff --git a/src/Adapter/Swoole/EventLoop.php b/src/Adapter/Swoole/EventLoop.php index 26c8797..05efb73 100644 --- a/src/Adapter/Swoole/EventLoop.php +++ b/src/Adapter/Swoole/EventLoop.php @@ -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(); diff --git a/src/Adapter/Swoole/Internal/SwoolePromise.php b/src/Adapter/Swoole/Internal/SwoolePromise.php index 4a01df2..cf24b53 100644 --- a/src/Adapter/Swoole/Internal/SwoolePromise.php +++ b/src/Adapter/Swoole/Internal/SwoolePromise.php @@ -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); diff --git a/tests/EventLoopTest.php b/tests/EventLoopTest.php index 1233e28..7ad4870 100644 --- a/tests/EventLoopTest.php +++ b/tests/EventLoopTest.php @@ -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) { @@ -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); }