Skip to content

Commit

Permalink
Update: update get raw coroutine in ripple
Browse files Browse the repository at this point in the history
  • Loading branch information
cclilshy committed Oct 14, 2024
1 parent 043edc6 commit da8a5c1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
23 changes: 13 additions & 10 deletions src/Utils/Coroutine/Handlers/RippleCoroutine.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,25 @@

namespace Workbunny\WebmanCoroutine\Utils\Coroutine\Handlers;

use Closure;
use Psc\Core\Coroutine\Promise;
use Revolt\EventLoop\Suspension;

use function Co\async;
use function Co\getSuspension;

class RippleCoroutine implements CoroutineInterface
{
protected Promise|null $promise = null;
/**
* @var \Revolt\EventLoop\Suspension
*/
protected Suspension $suspension;

/** @inheritdoc
* @param \Closure $func
*/
public function __construct(\Closure $func)
public function __construct(Closure $func)
{
$this->promise = $this->_async(function (\Closure $resolve, \Closure $reject) use ($func) {
$this->_async(function (Closure $resolve, Closure $reject) use ($func) {
$this->suspension = \Co\getSuspension();

try {
$result = call_user_func(
$func,
Expand All @@ -46,7 +49,7 @@ public function __destruct()
/** @inheritdoc */
public function origin(): Suspension
{
return getSuspension();
return $this->suspension;
}

/** @inheritdoc */
Expand All @@ -60,10 +63,10 @@ public function id(): ?string
*
* @param \Closure $closure
*
* @return mixed
* @return \Psc\Core\Coroutine\Promise
*/
protected function _async(\Closure $closure)
protected function _async(Closure $closure): Promise
{
return async($closure);
return \Co\async($closure);
}
}
12 changes: 6 additions & 6 deletions tests/UtilsCase/Coroutine/RippleCoroutineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function testConstruct()

// mock async
$callback = null;
$promiseMock = Mockery::mock('Psc\Core\Coroutine\Promise');
$promiseMock = Mockery::mock('Revolt\EventLoop\Suspension');
$coroutine = Mockery::mock(RippleCoroutine::class)->makePartial();
$coroutine->shouldAllowMockingProtectedMethods()->shouldReceive('_async')
->andReturnUsing(function ($closure) use (&$callback, $promiseMock) {
Expand All @@ -39,7 +39,7 @@ public function testConstruct()
$constructor->invoke($coroutine, $func);

$this->assertFalse($executed);
$this->assertInstanceOf('Psc\Core\Coroutine\Promise', $coroutine->origin());
$this->assertInstanceOf('Revolt\EventLoop\Suspension', $coroutine->origin());
$this->assertIsString($getId = $coroutine->id());
$this->assertEquals(spl_object_hash($promiseMock), $coroutine->id());
$this->assertNull($id);
Expand All @@ -62,7 +62,7 @@ public function testDestruct()

// mock async
$callback = null;
$promiseMock = Mockery::mock('Psc\Core\Coroutine\Promise');
$promiseMock = Mockery::mock('Revolt\EventLoop\Suspension');
$coroutine = Mockery::mock(RippleCoroutine::class)->makePartial();
$coroutine->shouldAllowMockingProtectedMethods()->shouldReceive('_async')
->andReturnUsing(function ($closure) use (&$callback, $promiseMock) {
Expand Down Expand Up @@ -90,7 +90,7 @@ public function testOrigin()
};
// mock async
$callback = null;
$promiseMock = Mockery::mock('Psc\Core\Coroutine\Promise');
$promiseMock = Mockery::mock('Revolt\EventLoop\Suspension');
$coroutine = Mockery::mock(RippleCoroutine::class)->makePartial();
$coroutine->shouldAllowMockingProtectedMethods()->shouldReceive('_async')
->andReturnUsing(function ($closure) use (&$callback, $promiseMock) {
Expand All @@ -102,7 +102,7 @@ public function testOrigin()
$constructor = new \ReflectionMethod(RippleCoroutine::class, '__construct');
$constructor->invoke($coroutine, $func);

$this->assertInstanceOf('Psc\Core\Coroutine\Promise', $coroutine->origin());
$this->assertInstanceOf('Revolt\EventLoop\Suspension', $coroutine->origin());
// 模拟构造后协程执行callback
call_user_func($callback);

Expand All @@ -116,7 +116,7 @@ public function testId()
};
// mock async
$callback = null;
$promiseMock = Mockery::mock('Psc\Core\Coroutine\Promise');
$promiseMock = Mockery::mock('Revolt\EventLoop\Suspension');
$coroutine = Mockery::mock(RippleCoroutine::class)->makePartial();
$coroutine->shouldAllowMockingProtectedMethods()->shouldReceive('_async')
->andReturnUsing(function ($closure) use (&$callback, $promiseMock) {
Expand Down

0 comments on commit da8a5c1

Please sign in to comment.