Skip to content

Commit

Permalink
Add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
huangdijia committed Apr 3, 2024
1 parent c6705a2 commit 5686208
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 additions & 1 deletion tests/MainTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
}
Expand Down

0 comments on commit 5686208

Please sign in to comment.