Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
1. 修复Channel方法bug
  • Loading branch information
chaz6chez committed Nov 18, 2023
1 parent 478eadf commit 3b02409
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/Traits/ChannelMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* @method static array GetChannel(string $key) Channel 获取
* @method static bool ChPublish(string $key, mixed $message, bool $store = true, null|string|int $workerId = null) Channel 发布消息
* @method static bool|int ChCreateListener(string $key, string|int $workerId, Closure $listener) Channel 监听器创建
* @method static void ChRemoveListener(string $key, string|int $workerId, bool $remove = true) Channel 监听器移除
* @method static void ChRemoveListener(string $key, string|int $workerId, bool $remove = false) Channel 监听器移除
*/
trait ChannelMethods
{
Expand Down
57 changes: 50 additions & 7 deletions tests/ChannelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,35 @@ public function testChannelRemoveListener(): void
apcu_delete(Cache::GetChannelKey($channel));
}

public function testChannelRemoveListenerUseRemove(): void
{
Future::$debug = true;
$channel = __FUNCTION__;
// 初始化确认
$this->assertEquals([], Cache::GetChannel($channel));
$this->assertEquals([], Cache::LockInfo());
// 当前进程执行
Cache::ChCreateListener($channel, '1', function (string $key, string|int $workerId, mixed $message) {
dump($key, $workerId, $message);
});
// 确认数据
$this->assertEquals([
'1' => [
'futureId' => 1,
'value' => []
]
], apcu_fetch(Cache::GetChannelKey($channel)));
// 确认无锁
$this->assertEquals([], Cache::LockInfo());
Cache::ChRemoveListener($channel, '1', true);
// 确认数据
$this->assertEquals([], apcu_fetch(Cache::GetChannelKey($channel)));
// 确认无锁
$this->assertEquals([], Cache::LockInfo());
// 清理
apcu_delete(Cache::GetChannelKey($channel));
}

public function testChannelRemoveListenerByChild(): void
{
Future::$debug = true;
Expand All @@ -211,6 +240,7 @@ public function testChannelRemoveListenerByChild(): void
Cache::ChCreateListener($channel, '1', function (string $key, string|int $workerId, mixed $message) {
dump($key, $workerId, $message);
});
Cache::ChRemoveListener($channel, '1', false);
}, $channel, $message);
// 确认数据
$this->assertEquals([
Expand All @@ -221,14 +251,27 @@ public function testChannelRemoveListenerByChild(): void
], apcu_fetch(Cache::GetChannelKey($channel)));
// 确认无锁
$this->assertEquals([], Cache::LockInfo());
Cache::ChRemoveListener($channel, '1', false);
// 清理
apcu_delete(Cache::GetChannelKey($channel));
}

public function testChannelRemoveListenerUseRemoveByChild(): void
{
Future::$debug = true;
$channel = __FUNCTION__;
$message = 'test';
// 初始化确认
$this->assertEquals([], Cache::GetChannel($channel));
$this->assertEquals([], Cache::LockInfo());
// 子进程执行
$this->childExec(static function (string $channel, string $message) {
Cache::ChCreateListener($channel, '1', function (string $key, string|int $workerId, mixed $message) {
dump($key, $workerId, $message);
});
Cache::ChRemoveListener($channel, '1', true);
}, $channel, $message);
// 确认数据
$this->assertEquals([
'1' => [
'futureId' => 1,
'value' => []
]
], apcu_fetch(Cache::GetChannelKey($channel)));
$this->assertEquals([], apcu_fetch(Cache::GetChannelKey($channel)));
// 确认无锁
$this->assertEquals([], Cache::LockInfo());
// 清理
Expand Down

0 comments on commit 3b02409

Please sign in to comment.