diff --git a/src/Events/SwooleEvent.php b/src/Events/SwooleEvent.php index 98bff2e..190756c 100644 --- a/src/Events/SwooleEvent.php +++ b/src/Events/SwooleEvent.php @@ -82,7 +82,7 @@ public function add($fd, $flag, $func, $args = []) // @codeCoverageIgnoreEnd } }); - $res = is_int($res) ? (string)$res : false; + $res = is_int($res) ? (string) $res : false; } if ($res === false) { @@ -95,7 +95,6 @@ public function add($fd, $flag, $func, $args = []) return $timerId; case EventInterface::EV_READ: if (!\is_resource($fd)) { - return false; } @@ -104,7 +103,6 @@ public function add($fd, $flag, $func, $args = []) : Event::add($fd, $func, null, SWOOLE_EVENT_READ); case EventInterface::EV_WRITE: if (!\is_resource($fd)) { - return false; } @@ -148,23 +146,20 @@ public function del($fd, $flag) return false; case EventInterface::EV_READ: if (!\is_resource($fd)) { - return false; } if (!Event::isset($fd, SWOOLE_EVENT_WRITE)) { - return Event::del($fd); } + return Event::set($fd, null, null, SWOOLE_EVENT_WRITE); case EventInterface::EV_WRITE: if (!\is_resource($fd)) { - return false; } if (!Event::isset($fd, SWOOLE_EVENT_READ)) { - return Event::del($fd); } diff --git a/src/Utils/Pool/Debugger.php b/src/Utils/Pool/Debugger.php index fe06b7e..c0215ca 100644 --- a/src/Utils/Pool/Debugger.php +++ b/src/Utils/Pool/Debugger.php @@ -13,15 +13,15 @@ class Debugger { - const ERROR_TYPE_NON = 1; - const ERROR_TYPE_NORMAL = 0; - const ERROR_TYPE_STATIC_ARRAY = -1; - const ERROR_TYPE_RESOURCE = -2; + public const ERROR_TYPE_NON = 1; + public const ERROR_TYPE_NORMAL = 0; + public const ERROR_TYPE_STATIC_ARRAY = -1; + public const ERROR_TYPE_RESOURCE = -2; protected static array $_errorMap = [ self::ERROR_TYPE_STATIC_ARRAY => 'static array', self::ERROR_TYPE_RESOURCE => 'resource', - self::ERROR_TYPE_NORMAL => 'normal' + self::ERROR_TYPE_NORMAL => 'normal', ]; /** @@ -71,6 +71,7 @@ public static function validate(mixed $value): bool { $debugger = new static(); $res = $debugger->cloneValidate($value); + return $res->getReturn(); } @@ -89,8 +90,9 @@ public function cloneValidate(mixed $value, int $level = 0): Generator foreach ($value as $v) { yield from $this->cloneValidate($v, $level - 1); } + return true; - // 对象递归检查 + // 对象递归检查 case 'object': // 是否在调试容器中出现过 if (!static::$_seen->offsetExists($value)) { @@ -110,18 +112,20 @@ public function cloneValidate(mixed $value, int $level = 0): Generator // 静态数组不可控,所以返回异常 case 'array': throw new PoolDebuggerException( - "Value can not be cloned [static array]. ", static::ERROR_TYPE_STATIC_ARRAY + 'Value can not be cloned [static array]. ', + static::ERROR_TYPE_STATIC_ARRAY ); - // 资源不可拷贝,所以返回异常 + // 资源不可拷贝,所以返回异常 case 'resource': // weak map 临时保存避免生命周期内的重复检查 static::$_seen->offsetSet($value, static::ERROR_TYPE_RESOURCE); throw new PoolDebuggerException( - "Value can not be cloned [resource]. ", static::ERROR_TYPE_RESOURCE + 'Value can not be cloned [resource]. ', + static::ERROR_TYPE_RESOURCE ); - // 其他类型 - // 使用生成器递归检查,避免内存溢出 - // 使用throw=false忽略标量数据的抛出 + // 其他类型 + // 使用生成器递归检查,避免内存溢出 + // 使用throw=false忽略标量数据的抛出 default: yield from $this->cloneValidate($v, $level - 1); break; @@ -133,7 +137,8 @@ public function cloneValidate(mixed $value, int $level = 0): Generator if (is_resource($v)) { static::$_seen->offsetSet($value, static::ERROR_TYPE_RESOURCE); throw new PoolDebuggerException( - "Value can not be cloned [resource]. ", static::ERROR_TYPE_RESOURCE + 'Value can not be cloned [resource]. ', + static::ERROR_TYPE_RESOURCE ); } // 其他类型,其中非静态数组是安全的安全 @@ -146,6 +151,7 @@ public function cloneValidate(mixed $value, int $level = 0): Generator } // 不存在非法值 static::$_seen->offsetSet($value, static::ERROR_TYPE_NON); + return true; } // 如果生命周期内存在检查通过的则返回true @@ -154,12 +160,13 @@ public function cloneValidate(mixed $value, int $level = 0): Generator } $info = static::$_errorMap[$errorType]; throw new PoolDebuggerException("Value can not be cloned [$info]. ", $errorType); - // 资源不可拷贝,返回异常 + // 资源不可拷贝,返回异常 case 'resource': throw new PoolDebuggerException( - "Value can not be cloned [resource]. ", static::ERROR_TYPE_RESOURCE + 'Value can not be cloned [resource]. ', + static::ERROR_TYPE_RESOURCE ); - // 其他 + // 其他 default: // 允许内层 if ($level < 0) { @@ -167,8 +174,9 @@ public function cloneValidate(mixed $value, int $level = 0): Generator } // 不允许外层 throw new PoolDebuggerException( - "Value can not be cloned [$type]. ", static::ERROR_TYPE_NORMAL + "Value can not be cloned [$type]. ", + static::ERROR_TYPE_NORMAL ); } } -} \ No newline at end of file +} diff --git a/src/Utils/Pool/Pool.php b/src/Utils/Pool/Pool.php index 58e3cea..354f2a2 100644 --- a/src/Utils/Pool/Pool.php +++ b/src/Utils/Pool/Pool.php @@ -9,8 +9,8 @@ use WeakReference; use Workbunny\WebmanCoroutine\Exceptions\PoolException; - use Workbunny\WebmanCoroutine\Exceptions\TimeoutException; + use function Workbunny\WebmanCoroutine\wait_for; use Workerman\Worker; @@ -86,6 +86,7 @@ public static function create(string $name, int $count, mixed $element, bool $cl foreach (range(1, $count) as $i) { static::append($name, $i, $element); } + return self::$pools[$name]; } @@ -208,6 +209,7 @@ public static function getIdle(string $name, int $timeout = -1): Pool return $pool !== null; }, $timeout); $pool->setIdle(false); + return $pool; } @@ -224,6 +226,7 @@ public static function waitForIdle(string $name, \Closure $closure, int $timeout { try { $pool = static::getIdle($name, $timeout); + return call_user_func($closure, $pool); } finally { if (isset($pool)) { diff --git a/src/Utils/Worker/AbstractWorker.php b/src/Utils/Worker/AbstractWorker.php index 39755be..aead7eb 100644 --- a/src/Utils/Worker/AbstractWorker.php +++ b/src/Utils/Worker/AbstractWorker.php @@ -7,9 +7,6 @@ namespace Workbunny\WebmanCoroutine\Utils\Worker; -use Workbunny\WebmanCoroutine\Exceptions\WorkerException; -use Workbunny\WebmanCoroutine\Factory; -use Workbunny\WebmanCoroutine\Handlers\HandlerInterface; use Workerman\Worker; abstract class AbstractWorker extends Worker diff --git a/tests/EventsCase/SwooleEventTest.php b/tests/EventsCase/SwooleEventTest.php index 5a82bc3..2636430 100644 --- a/tests/EventsCase/SwooleEventTest.php +++ b/tests/EventsCase/SwooleEventTest.php @@ -101,8 +101,6 @@ public function testAddTimer() echo 'Timer triggered'; }); $this->assertEquals(1, $result); - - } public function testAddOnceTimer() @@ -268,7 +266,6 @@ public function testAddAndDelRead() // Event::set $eventMock->shouldReceive('isset')->andReturn(false); - $result = $swooleEvent->del($stream2, EventInterface::EV_READ); $this->assertTrue($result); // false @@ -371,7 +368,7 @@ public function testDestroy() $this->assertTrue(is_int($id)); }); $eventMock->shouldReceive('listCoroutines')->andReturn([ - 1, 2 + 1, 2, ]); $swooleEvent->destroy(); diff --git a/tests/UtilsCase/Pool/DebuggerTest.php b/tests/UtilsCase/Pool/DebuggerTest.php index 573dfa1..a29c1ca 100644 --- a/tests/UtilsCase/Pool/DebuggerTest.php +++ b/tests/UtilsCase/Pool/DebuggerTest.php @@ -22,6 +22,7 @@ protected function _generateNestedArray($depth): array if ($depth <= 0) { return []; } + return ['level' => $depth, 'next' => $this->_generateNestedArray($depth - 1)]; } @@ -34,9 +35,9 @@ public function testCloneValidateWithArray() [ 3.1, [ - 4 - ] - ] + 4, + ], + ], ]; $result = Debugger::validate($array); $this->assertTrue($result); @@ -48,9 +49,9 @@ public function testCloneValidateWithArray() [ 3.1, [ - 4 - ] - ] + 4, + ], + ], ]; $result = Debugger::validate($array); $this->assertTrue($result); @@ -65,9 +66,9 @@ public function testCloneValidateWithArray() 'c' => [ 3.1, [ - 'd' => 4 - ] - ] + 'd' => 4, + ], + ], ]; $result = Debugger::validate($array); $this->assertTrue($result); @@ -79,9 +80,9 @@ public function testCloneValidateWithArray() 'c' => [ 3.1, [ - 'd' => 4 - ] - ] + 'd' => 4, + ], + ], ]; $result = Debugger::validate($array); $this->assertTrue($result); @@ -99,9 +100,9 @@ public function testCloneValidateWithArrayException() [ 3.1, [ - 4 - ] - ] + 4, + ], + ], ]; try { Debugger::validate($array); @@ -117,9 +118,9 @@ public function testCloneValidateWithArrayException() [ fopen('php://memory', 'w+'), [ - 4 - ] - ] + 4, + ], + ], ]; try { Debugger::validate($array); @@ -137,9 +138,9 @@ public function testCloneValidateWithArrayException() [ $object, [ - 4 - ] - ] + 4, + ], + ], ]; try { Debugger::validate($array); @@ -164,9 +165,9 @@ public function testCloneValidateWithArrayException() [ $object, [ - 4 - ] - ] + 4, + ], + ], ]; try { Debugger::validate($array); @@ -270,7 +271,7 @@ public function testCloneValidateWithObjectResourceException() public function testCloneValidateWithObjectStaticProperty() { // string - $object = new class { + $object = new class () { public static $string = '1'; }; $res = Debugger::validate($object); @@ -280,7 +281,7 @@ public function testCloneValidateWithObjectStaticProperty() $this->assertTrue($res); // int - $object = new class { + $object = new class () { public static $int = 1; }; $res = Debugger::validate($object); @@ -290,7 +291,7 @@ public function testCloneValidateWithObjectStaticProperty() $this->assertTrue($res); // float - $object = new class { + $object = new class () { public static $float = 1.1; }; $res = Debugger::validate($object); @@ -300,7 +301,7 @@ public function testCloneValidateWithObjectStaticProperty() $this->assertTrue($res); // bool - $object = new class { + $object = new class () { public static $bool = true; }; $res = Debugger::validate($object); @@ -310,7 +311,7 @@ public function testCloneValidateWithObjectStaticProperty() $this->assertTrue($res); // null - $object = new class { + $object = new class () { public static $null = null; }; $res = Debugger::validate($object); @@ -323,7 +324,7 @@ public function testCloneValidateWithObjectStaticProperty() public function testCloneValidateWithObjectStaticPropertyException() { // array - $object = new class { + $object = new class () { public static $arr = [1, 2, 3]; }; try { @@ -334,7 +335,7 @@ public function testCloneValidateWithObjectStaticPropertyException() } // resource - $object = new class { + $object = new class () { public static $resource = null; public function __construct()