Skip to content

Commit

Permalink
fix #16
Browse files Browse the repository at this point in the history
  • Loading branch information
chaz6chez committed Oct 11, 2024
1 parent 8f05ca3 commit 414ea3e
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/Utils/Coroutine/Handlers/SwooleCoroutine.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ class SwooleCoroutine implements CoroutineInterface
public function __construct(\Closure $func)
{
while (1) {
if ($id = Coroutine::create(function () use ($func) {
if ($id = Coroutine::create(function () use (&$id, $func) {
try {
call_user_func($func, $this->_id);
$this->_id = $id;
call_user_func($func, $id);
} finally {
$this->_id = null;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Utils/Coroutine/Handlers/SwowCoroutine.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function __construct(\Closure $func)
{
$this->_coroutine = Coroutine::run(function () use ($func) {
try {
call_user_func($func, $this->_coroutine->getId());
call_user_func($func, Coroutine::getCurrent()->getId());
} finally {
$this->_coroutine = null;
}
Expand Down
4 changes: 4 additions & 0 deletions tests/UtilsCase/Coroutine/SwowCoroutineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public function testConstruct()

return $coroutineMock;
});
$coroutineMock->shouldReceive('getCurrent')->andReturnSelf();
$coroutineMock->shouldReceive('getId')->andReturn(123);
// 构造
$coroutine = new SwowCoroutine($func);
Expand Down Expand Up @@ -64,6 +65,7 @@ public function testDestruct()

return $coroutineMock;
});
$coroutineMock->shouldReceive('getCurrent')->andReturnSelf();
$coroutineMock->shouldReceive('getId')->andReturn(123);
// 构造
$coroutine = new SwowCoroutine($func);
Expand All @@ -90,6 +92,7 @@ public function testOrigin()

return $coroutineMock;
});
$coroutineMock->shouldReceive('getCurrent')->andReturnSelf();
$coroutineMock->shouldReceive('getId')->andReturn(123);
// 构造
$coroutine = new SwowCoroutine($func);
Expand All @@ -116,6 +119,7 @@ public function testId()

return $coroutineMock;
});
$coroutineMock->shouldReceive('getCurrent')->andReturnSelf();
$coroutineMock->shouldReceive('getId')->andReturn(123);
// 构造
$coroutine = new SwowCoroutine($func);
Expand Down

0 comments on commit 414ea3e

Please sign in to comment.