From 5686208255034f2f76496330d10eed6d3b56a171 Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Wed, 3 Apr 2024 11:01:06 +0800 Subject: [PATCH] Add test case --- tests/MainTest.php | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/tests/MainTest.php b/tests/MainTest.php index ef849b6..cabb5ea 100644 --- a/tests/MainTest.php +++ b/tests/MainTest.php @@ -6,7 +6,7 @@ final class MainTest extends TestCase { - public function test(): void + public function testSubscribe(): void { $func = function () { $sub = new \Mix\Redis\Subscriber\Subscriber('127.0.0.1', 6379, '', 5); @@ -31,6 +31,42 @@ public function test(): void } break; } + $this->assertEquals($data->channel, 'foo1'); + $this->assertEquals($data->payload, 'foo1data'); + break; + } + $sub->close(); + }; + run($func); + } + + public function testPsubscribe(): void + { + $func = function () { + $sub = new \Mix\Redis\Subscriber\Subscriber('127.0.0.1', 6379, '', 5); + $sub->psubscribe('foo.*', 'bar'); // 订阅失败将抛出异常 + $sub->psubscribe('foo1.*', 'bar1'); + $sub->punsubscribe('foo.*', 'bar'); + + go(function () { + $redis = new \Redis(); + $redis->connect('127.0.0.1', 6379); + $redis->publish('foo.1', 'foodata'); + $redis->publish('foo1.1', 'foo1data'); + }); + + $chan = $sub->channel(); + while (true) { + $data = $chan->pop(); + if (empty($data)) { // 手动close与redis异常断开都会导致返回false + if (!$sub->closed) { + // redis异常断开处理 + var_dump('Redis connection is disconnected abnormally'); + } + break; + } + $this->assertEquals($data->pattern, 'foo1.*'); + $this->assertEquals($data->channel, 'foo1.1'); $this->assertEquals($data->payload, 'foo1data'); break; }