Skip to content

Commit

Permalink
feat: Coroutine增加回调函数coroutineId入参,结构优化及测试完善
Browse files Browse the repository at this point in the history
  • Loading branch information
chaz6chez committed Oct 9, 2024
1 parent bb659fd commit 08048fb
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 59 deletions.
3 changes: 2 additions & 1 deletion src/Utils/Coroutine/Coroutine.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public function __call(string $name, array $arguments): mixed
if (!method_exists($this->_interface, $name)) {
throw new \BadMethodCallException("Method $name not exists. ");
}

return $this->_interface->$name(...$arguments);
}
}
}
2 changes: 1 addition & 1 deletion src/Utils/Coroutine/Handlers/CoroutineInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ public function origin(): mixed;
* @return string|int|null
*/
public function id(): string|int|null;
}
}
1 change: 0 additions & 1 deletion src/Utils/Coroutine/Handlers/DefaultCoroutine.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

class DefaultCoroutine implements CoroutineInterface
{

/**
* @var string|null
*/
Expand Down
15 changes: 4 additions & 11 deletions src/Utils/Coroutine/Handlers/RippleCoroutine.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,17 @@

namespace Workbunny\WebmanCoroutine\Utils\Coroutine\Handlers;

use Psc\Core\Coroutine\Promise;
use function Co\async;

use Psc\Core\Coroutine\Promise;

class RippleCoroutine implements CoroutineInterface
{
/**
* @var null|Promise
*/
protected ?Promise $_promise = null;

/**
* @var null|string
*/
protected ?string $_id = null;

/** @inheritdoc
* @param \Closure $func
*/
Expand All @@ -31,12 +27,10 @@ public function __construct(\Closure $func)
try {
call_user_func($func, spl_object_hash($this->_promise));
} finally {
// 移除协程id及promise
// 移除协程promise
$this->_promise = null;
$this->_id = null;
}
});
$this->_id = spl_object_hash($this->_promise);
}

/** @inheritdoc */
Expand All @@ -48,14 +42,13 @@ public function __destruct()
/** @inheritdoc */
public function origin(): ?Promise
{

return $this->_promise;
}

/** @inheritdoc */
public function id(): ?string
{
return $this->_id;
return $this->_promise ? spl_object_hash($this->_promise) : null;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Utils/Coroutine/Handlers/SwooleCoroutine.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function origin(): ?int
}

/** @inheritdoc */
public function id(): int
public function id(): ?int
{
return $this->_id;
}
Expand Down
9 changes: 1 addition & 8 deletions src/Utils/Coroutine/Handlers/SwowCoroutine.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@ class SwowCoroutine implements CoroutineInterface
*/
protected ?Coroutine $_coroutine = null;

/**
* @var int|null
*/
protected ?int $_id = null;

/** @inheritDoc */
public function __construct(\Closure $func)
{
Expand All @@ -29,10 +24,8 @@ public function __construct(\Closure $func)
call_user_func($func, $this->_coroutine->getId());
} finally {
$this->_coroutine = null;
$this->_id = null;
}
});
$this->_id = $this->_coroutine->getId();
}

/** @inheritdoc */
Expand All @@ -50,6 +43,6 @@ public function origin(): ?Coroutine
/** @inheritdoc */
public function id(): ?int
{
return $this->_id;
return $this->_coroutine?->getId();
}
}
10 changes: 5 additions & 5 deletions tests/UtilsCase/Coroutine/CoroutineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace Workbunny\Tests\UtilsCase\Coroutine;

use PHPUnit\Framework\TestCase;
use Mockery;
use PHPUnit\Framework\TestCase;
use Workbunny\WebmanCoroutine\Utils\Coroutine\Coroutine;
use Workbunny\WebmanCoroutine\Utils\Coroutine\Handlers\CoroutineInterface;

Expand All @@ -19,15 +19,15 @@ protected function tearDown(): void
public function testConstruct()
{
$executed = false;
$func = function() use (&$executed) {
$func = function () use (&$executed) {
$executed = true;
};

$mockInterface = Mockery::mock(CoroutineInterface::class);
$mockInterface->shouldReceive('__construct')
->with($func);

$channel = Mockery::mock( Coroutine::class)
$channel = Mockery::mock(Coroutine::class)
->makePartial()
->shouldAllowMockingProtectedMethods();
$channel->shouldReceive('__destruct')
Expand All @@ -46,15 +46,15 @@ public function testConstruct()

public function testDestruct()
{
$func = function() {
$func = function () {
// 模拟闭包函数的执行
};

$mockInterface = Mockery::mock(CoroutineInterface::class);
$mockInterface->shouldReceive('__construct')
->with($func);

$channel = Mockery::mock( Coroutine::class)
$channel = Mockery::mock(Coroutine::class)
->makePartial()
->shouldAllowMockingProtectedMethods();
$channel->shouldReceive('__destruct')
Expand Down
4 changes: 2 additions & 2 deletions tests/UtilsCase/Coroutine/DefaultCoroutineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function testConstruct()

public function testOrigin()
{
$func = function() {
$func = function () {
// 模拟闭包函数的执行
};
$coroutine = new DefaultCoroutine($func);
Expand All @@ -34,7 +34,7 @@ public function testOrigin()

public function testId()
{
$func = function() {
$func = function () {
// 模拟闭包函数的执行
};
$coroutine = new DefaultCoroutine($func);
Expand Down
28 changes: 17 additions & 11 deletions tests/UtilsCase/Coroutine/RippleCoroutineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ public function testConstruct()
$promiseMock = Mockery::mock('Psc\Core\Coroutine\Promise');
$coroutine = Mockery::mock(RippleCoroutine::class)->makePartial();
$coroutine->shouldAllowMockingProtectedMethods()->shouldReceive('_async')
->andReturnUsing(function($closure) use (&$callback, $promiseMock) {
->andReturnUsing(function ($closure) use (&$callback, $promiseMock) {
$callback = $closure;

return $promiseMock;
});
// 模拟构造
Expand All @@ -39,9 +40,9 @@ public function testConstruct()

$this->assertFalse($executed);
$this->assertInstanceOf('Psc\Core\Coroutine\Promise', $coroutine->origin());
$this->assertIsString($coroutine->id());
$this->assertIsString($getId = $coroutine->id());
$this->assertEquals(spl_object_hash($promiseMock), $coroutine->id());
$this->assertEquals($coroutine->id(), $id);
$this->assertNull($id);

// 模拟发生协程执行
call_user_func($callback);
Expand All @@ -50,12 +51,12 @@ public function testConstruct()
$this->assertNull($coroutine->origin());
$this->assertNull($coroutine->id());
$this->assertNotNull($id);

$this->assertEquals($getId, $id);
}

public function testDestruct()
{
$func = function() {
$func = function () {
// 模拟闭包函数的执行
};

Expand All @@ -64,8 +65,9 @@ public function testDestruct()
$promiseMock = Mockery::mock('Psc\Core\Coroutine\Promise');
$coroutine = Mockery::mock(RippleCoroutine::class)->makePartial();
$coroutine->shouldAllowMockingProtectedMethods()->shouldReceive('_async')
->andReturnUsing(function($closure) use (&$callback, $promiseMock) {
->andReturnUsing(function ($closure) use (&$callback, $promiseMock) {
$callback = $closure;

return $promiseMock;
});
// 模拟构造函数执行
Expand All @@ -81,39 +83,43 @@ public function testDestruct()

public function testOrigin()
{
$func = function() {
$func = function () {
// 模拟闭包函数的执行
};
// mock async
$callback = null;
$promiseMock = Mockery::mock('Psc\Core\Coroutine\Promise');
$coroutine = Mockery::mock(RippleCoroutine::class)->makePartial();
$coroutine->shouldAllowMockingProtectedMethods()->shouldReceive('_async')
->andReturnUsing(function($closure) use (&$callback, $promiseMock) {
->andReturnUsing(function ($closure) use (&$callback, $promiseMock) {
$callback = $closure;

return $promiseMock;
});
// 模拟构造函数执行
$constructor = new \ReflectionMethod(RippleCoroutine::class, '__construct');
$constructor->invoke($coroutine, $func);

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

$this->assertInstanceOf('Psc\Core\Coroutine\Promise', $coroutine->origin());
$this->assertNull($coroutine->origin());
}

public function testId()
{
$func = function() {
$func = function () {
// 模拟闭包函数的执行
};
// mock async
$callback = null;
$promiseMock = Mockery::mock('Psc\Core\Coroutine\Promise');
$coroutine = Mockery::mock(RippleCoroutine::class)->makePartial();
$coroutine->shouldAllowMockingProtectedMethods()->shouldReceive('_async')
->andReturnUsing(function($closure) use (&$callback, $promiseMock) {
->andReturnUsing(function ($closure) use (&$callback, $promiseMock) {
$callback = $closure;

return $promiseMock;
});
// 模拟构造函数执行
Expand Down
21 changes: 12 additions & 9 deletions tests/UtilsCase/Coroutine/SwooleCoroutineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

class SwooleCoroutineTest extends TestCase
{

protected function tearDown(): void
{
Mockery::close();
Expand All @@ -28,8 +27,9 @@ public function testConstruct()
// mock 协程创建
$callback = null;
$coroutineMock = Mockery::mock('alias:Swoole\Coroutine');
$coroutineMock->shouldReceive('create')->andReturnUsing(function($closure) use (&$callback, &$executed) {
$coroutineMock->shouldReceive('create')->andReturnUsing(function ($closure) use (&$callback, &$executed) {
$callback = $closure;

return 123;
});
// 构造
Expand All @@ -38,7 +38,7 @@ public function testConstruct()
$this->assertFalse($executed);
$this->assertEquals(123, $coroutine->origin());
$this->assertEquals(123, $coroutine->id());
$this->assertEquals(123, $id);
$this->assertNull($id);

// 模拟构造后发生协程执行
call_user_func($callback);
Expand All @@ -51,15 +51,16 @@ public function testConstruct()

public function testDestruct()
{
$func = function() {
$func = function () {
// 模拟闭包函数的执行
};

// mock 协程创建
$callback = null;
$coroutineMock = Mockery::mock('alias:Swoole\Coroutine');
$coroutineMock->shouldReceive('create')->andReturnUsing(function($closure) use (&$callback, &$executed) {
$coroutineMock->shouldReceive('create')->andReturnUsing(function ($closure) use (&$callback, &$executed) {
$callback = $closure;

return 123;
});
// 构造
Expand All @@ -75,15 +76,16 @@ public function testDestruct()

public function testOrigin()
{
$func = function() {
$func = function () {
// 模拟闭包函数的执行
};

// mock 协程创建
$callback = null;
$coroutineMock = Mockery::mock('alias:Swoole\Coroutine');
$coroutineMock->shouldReceive('create')->andReturnUsing(function($closure) use (&$callback, &$executed) {
$coroutineMock->shouldReceive('create')->andReturnUsing(function ($closure) use (&$callback, &$executed) {
$callback = $closure;

return 123;
});
// 构造
Expand All @@ -98,15 +100,16 @@ public function testOrigin()

public function testId()
{
$func = function() {
$func = function () {
// 模拟闭包函数的执行
};

// mock 协程创建
$callback = null;
$coroutineMock = Mockery::mock('alias:Swoole\Coroutine');
$coroutineMock->shouldReceive('create')->andReturnUsing(function($closure) use (&$callback, &$executed) {
$coroutineMock->shouldReceive('create')->andReturnUsing(function ($closure) use (&$callback, &$executed) {
$callback = $closure;

return 123;
});
// 构造
Expand Down
Loading

0 comments on commit 08048fb

Please sign in to comment.