Skip to content

Commit 5686208

Browse files
committed
Add test case
1 parent c6705a2 commit 5686208

File tree

1 file changed

+37
-1
lines changed

1 file changed

+37
-1
lines changed

tests/MainTest.php

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
final class MainTest extends TestCase
77
{
88

9-
public function test(): void
9+
public function testSubscribe(): void
1010
{
1111
$func = function () {
1212
$sub = new \Mix\Redis\Subscriber\Subscriber('127.0.0.1', 6379, '', 5);
@@ -31,6 +31,42 @@ public function test(): void
3131
}
3232
break;
3333
}
34+
$this->assertEquals($data->channel, 'foo1');
35+
$this->assertEquals($data->payload, 'foo1data');
36+
break;
37+
}
38+
$sub->close();
39+
};
40+
run($func);
41+
}
42+
43+
public function testPsubscribe(): void
44+
{
45+
$func = function () {
46+
$sub = new \Mix\Redis\Subscriber\Subscriber('127.0.0.1', 6379, '', 5);
47+
$sub->psubscribe('foo.*', 'bar'); // 订阅失败将抛出异常
48+
$sub->psubscribe('foo1.*', 'bar1');
49+
$sub->punsubscribe('foo.*', 'bar');
50+
51+
go(function () {
52+
$redis = new \Redis();
53+
$redis->connect('127.0.0.1', 6379);
54+
$redis->publish('foo.1', 'foodata');
55+
$redis->publish('foo1.1', 'foo1data');
56+
});
57+
58+
$chan = $sub->channel();
59+
while (true) {
60+
$data = $chan->pop();
61+
if (empty($data)) { // 手动close与redis异常断开都会导致返回false
62+
if (!$sub->closed) {
63+
// redis异常断开处理
64+
var_dump('Redis connection is disconnected abnormally');
65+
}
66+
break;
67+
}
68+
$this->assertEquals($data->pattern, 'foo1.*');
69+
$this->assertEquals($data->channel, 'foo1.1');
3470
$this->assertEquals($data->payload, 'foo1data');
3571
break;
3672
}

0 commit comments

Comments
 (0)