From 414ea3ecd6b0fadfaaa43ac081d553446105f1af Mon Sep 17 00:00:00 2001 From: chaz6chez Date: Fri, 11 Oct 2024 13:53:37 +0800 Subject: [PATCH] fix #16 --- src/Utils/Coroutine/Handlers/SwooleCoroutine.php | 5 +++-- src/Utils/Coroutine/Handlers/SwowCoroutine.php | 2 +- tests/UtilsCase/Coroutine/SwowCoroutineTest.php | 4 ++++ 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/Utils/Coroutine/Handlers/SwooleCoroutine.php b/src/Utils/Coroutine/Handlers/SwooleCoroutine.php index 1aa77ba..d035c1b 100644 --- a/src/Utils/Coroutine/Handlers/SwooleCoroutine.php +++ b/src/Utils/Coroutine/Handlers/SwooleCoroutine.php @@ -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; } diff --git a/src/Utils/Coroutine/Handlers/SwowCoroutine.php b/src/Utils/Coroutine/Handlers/SwowCoroutine.php index b9bb7f4..dbeb2c6 100644 --- a/src/Utils/Coroutine/Handlers/SwowCoroutine.php +++ b/src/Utils/Coroutine/Handlers/SwowCoroutine.php @@ -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; } diff --git a/tests/UtilsCase/Coroutine/SwowCoroutineTest.php b/tests/UtilsCase/Coroutine/SwowCoroutineTest.php index 2bf87fb..00ad4a3 100644 --- a/tests/UtilsCase/Coroutine/SwowCoroutineTest.php +++ b/tests/UtilsCase/Coroutine/SwowCoroutineTest.php @@ -32,6 +32,7 @@ public function testConstruct() return $coroutineMock; }); + $coroutineMock->shouldReceive('getCurrent')->andReturnSelf(); $coroutineMock->shouldReceive('getId')->andReturn(123); // 构造 $coroutine = new SwowCoroutine($func); @@ -64,6 +65,7 @@ public function testDestruct() return $coroutineMock; }); + $coroutineMock->shouldReceive('getCurrent')->andReturnSelf(); $coroutineMock->shouldReceive('getId')->andReturn(123); // 构造 $coroutine = new SwowCoroutine($func); @@ -90,6 +92,7 @@ public function testOrigin() return $coroutineMock; }); + $coroutineMock->shouldReceive('getCurrent')->andReturnSelf(); $coroutineMock->shouldReceive('getId')->andReturn(123); // 构造 $coroutine = new SwowCoroutine($func); @@ -116,6 +119,7 @@ public function testId() return $coroutineMock; }); + $coroutineMock->shouldReceive('getCurrent')->andReturnSelf(); $coroutineMock->shouldReceive('getId')->andReturn(123); // 构造 $coroutine = new SwowCoroutine($func);