Skip to content
This repository was archived by the owner on Feb 7, 2024. It is now read-only.

Commit a45c0bf

Browse files
committed
Using the Redis non-blocking client.
1 parent 21db4b3 commit a45c0bf

File tree

7 files changed

+117
-52
lines changed

7 files changed

+117
-52
lines changed

src/PubSub/Drivers/RedisClient.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ public function subscribeToApp($appId): bool
194194
{
195195
$this->subscribeClient->__call('subscribe', [$this->getTopicName($appId)]);
196196

197-
$this->redis->hincrby($this->getTopicName($appId), 'connections', 1);
197+
$this->publishClient->__call('hincrby', [$this->getTopicName($appId), 'connections', 1]);
198198

199199
return true;
200200
}
@@ -209,7 +209,7 @@ public function unsubscribeFromApp($appId): bool
209209
{
210210
$this->subscribeClient->__call('unsubscribe', [$this->getTopicName($appId)]);
211211

212-
$this->redis->hincrby($this->getTopicName($appId), 'connections', -1);
212+
$this->publishClient->__call('hincrby', [$this->getTopicName($appId), 'connections', -1]);
213213

214214
return true;
215215
}
@@ -226,7 +226,7 @@ public function unsubscribeFromApp($appId): bool
226226
*/
227227
public function joinChannel($appId, string $channel, string $socketId, string $data)
228228
{
229-
$this->redis->hset($this->getTopicName($appId, $channel), $socketId, $data);
229+
$this->publishClient->__call('hset', [$this->getTopicName($appId, $channel), $socketId, $data]);
230230

231231
DashboardLogger::log($appId, DashboardLogger::TYPE_REPLICATOR_JOINED_CHANNEL, [
232232
'channel' => $channel,
@@ -248,7 +248,7 @@ public function joinChannel($appId, string $channel, string $socketId, string $d
248248
*/
249249
public function leaveChannel($appId, string $channel, string $socketId)
250250
{
251-
$this->redis->hdel($this->getTopicName($appId, $channel), $socketId);
251+
$this->publishClient->__call('hdel', [$this->getTopicName($appId, $channel), $socketId]);
252252

253253
DashboardLogger::log($appId, DashboardLogger::TYPE_REPLICATOR_LEFT_CHANNEL, [
254254
'channel' => $channel,
@@ -307,7 +307,7 @@ public function appConnectionsCount($appId)
307307
{
308308
// Use the in-built Redis manager to avoid async run.
309309

310-
return $this->redis->hget($this->getTopicName($appId), 'connections') ?: 0;
310+
return $this->publishClient->hget($this->getTopicName($appId), 'connections') ?: 0;
311311
}
312312

313313
/**

tests/Channels/PresenceChannelReplicationTest.php

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,10 @@
44

55
use BeyondCode\LaravelWebSockets\Tests\Mocks\Message;
66
use BeyondCode\LaravelWebSockets\Tests\TestCase;
7-
use Illuminate\Support\Facades\Cache;
7+
use Illuminate\Support\Facades\Redis;
88

99
class PresenceChannelReplicationTest extends TestCase
1010
{
11-
/**
12-
* The Redis manager instance.
13-
*
14-
* @var \Illuminate\Redis\RedisManager
15-
*/
16-
protected $redis;
17-
1811
/**
1912
* {@inheritdoc}
2013
*/
@@ -23,8 +16,6 @@ public function setUp(): void
2316
parent::setUp();
2417

2518
$this->runOnlyOnRedisReplication();
26-
27-
$this->redis = Cache::getRedis();
2819
}
2920

3021
/** @test */
@@ -55,7 +46,7 @@ public function clients_with_valid_auth_signatures_can_join_presence_channels()
5546
$this->pusherServer->onMessage($connection, $message);
5647

5748
$this->getPublishClient()
58-
->assertNotCalledWithArgs('hset', [
49+
->assertCalledWithArgs('hset', [
5950
'laravel_database_1234:presence-channel',
6051
$connection->socketId,
6152
json_encode($channelData),
@@ -64,7 +55,7 @@ public function clients_with_valid_auth_signatures_can_join_presence_channels()
6455
->assertCalled('publish');
6556

6657
$this->assertNotNull(
67-
$this->redis->hget('laravel_database_1234:presence-channel', $connection->socketId)
58+
Redis::hget('laravel_database_1234:presence-channel', $connection->socketId)
6859
);
6960
}
7061

@@ -96,7 +87,7 @@ public function clients_with_valid_auth_signatures_can_leave_presence_channels()
9687
->assertEventDispatched('message');
9788

9889
$this->getPublishClient()
99-
->assertNotCalled('hset')
90+
->assertCalled('hset')
10091
->assertCalledWithArgs('hgetall', ['laravel_database_1234:presence-channel'])
10192
->assertCalled('publish');
10293

@@ -114,7 +105,7 @@ public function clients_with_valid_auth_signatures_can_leave_presence_channels()
114105
$this->pusherServer->onMessage($connection, $message);
115106

116107
$this->getPublishClient()
117-
->assertNotCalled('hdel')
108+
->assertCalled('hdel')
118109
->assertCalled('publish');
119110
}
120111

@@ -143,8 +134,8 @@ public function clients_with_no_user_info_can_join_presence_channels()
143134
$this->pusherServer->onMessage($connection, $message);
144135

145136
$this->getPublishClient()
146-
->assertNotCalled('hset')
147-
->assertcalledWithArgs('hgetall', ['laravel_database_1234:presence-channel'])
137+
->assertCalled('hset')
138+
->assertCalledWithArgs('hgetall', ['laravel_database_1234:presence-channel'])
148139
->assertCalled('publish');
149140
}
150141
}

tests/ConnectionTest.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use BeyondCode\LaravelWebSockets\WebSockets\Exceptions\ConnectionsOverCapacity;
88
use BeyondCode\LaravelWebSockets\WebSockets\Exceptions\OriginNotAllowed;
99
use BeyondCode\LaravelWebSockets\WebSockets\Exceptions\UnknownAppKey;
10-
use Illuminate\Support\Facades\Cache;
10+
use Illuminate\Support\Facades\Redis;
1111

1212
class ConnectionTest extends TestCase
1313
{
@@ -47,15 +47,18 @@ public function app_can_not_exceed_maximum_capacity_on_redis_replication()
4747
{
4848
$this->runOnlyOnRedisReplication();
4949

50-
$redis = Cache::getRedis();
51-
52-
$redis->hdel('laravel_database_1234', 'connections');
50+
Redis::hdel('laravel_database_1234', 'connections');
5351

5452
$this->app['config']->set('websockets.apps.0.capacity', 2);
5553

5654
$this->getConnectedWebSocketConnection(['test-channel']);
5755
$this->getConnectedWebSocketConnection(['test-channel']);
56+
57+
$this->getPublishClient()
58+
->assertCalledWithArgsCount(2, 'hincrby', ['laravel_database_1234', 'connections', 1]);
59+
5860
$this->expectException(ConnectionsOverCapacity::class);
61+
5962
$this->getConnectedWebSocketConnection(['test-channel']);
6063
}
6164

tests/HttpApi/FetchChannelsReplicationTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function replication_it_returns_the_channel_information()
4848
->assertEventDispatched('message');
4949

5050
$this->getPublishClient()
51-
->assertNotCalled('hset')
51+
->assertCalled('hset')
5252
->assertCalledWithArgs('hgetall', ['laravel_database_1234:presence-channel'])
5353
->assertCalled('publish')
5454
->assertCalled('multi')
@@ -88,7 +88,7 @@ public function replication_it_returns_the_channel_information_for_prefix()
8888
->assertEventDispatched('message');
8989

9090
$this->getPublishClient()
91-
->assertNotCalled('hset')
91+
->assertCalled('hset')
9292
->assertCalledWithArgs('hgetall', ['laravel_database_1234:presence-global.1'])
9393
->assertCalledWithArgs('hgetall', ['laravel_database_1234:presence-global.2'])
9494
->assertCalledWithArgs('hgetall', ['laravel_database_1234:presence-notglobal.2'])
@@ -133,7 +133,7 @@ public function replication_it_returns_the_channel_information_for_prefix_with_u
133133
->assertEventDispatched('message');
134134

135135
$this->getPublishClient()
136-
->assertNotCalled('hset')
136+
->assertCalled('hset')
137137
->assertCalledWithArgs('hgetall', ['laravel_database_1234:presence-global.1'])
138138
->assertCalledWithArgs('hgetall', ['laravel_database_1234:presence-global.2'])
139139
->assertCalledWithArgs('hgetall', ['laravel_database_1234:presence-notglobal.2'])

tests/Mocks/LazyClient.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
namespace BeyondCode\LaravelWebSockets\Tests\Mocks;
44

55
use Clue\React\Redis\LazyClient as BaseLazyClient;
6+
use Clue\React\Redis\Factory;
7+
use Illuminate\Support\Facades\Cache;
68
use PHPUnit\Framework\Assert as PHPUnit;
9+
use React\EventLoop\LoopInterface;
710

811
class LazyClient extends BaseLazyClient
912
{
@@ -21,13 +24,34 @@ class LazyClient extends BaseLazyClient
2124
*/
2225
protected $events = [];
2326

27+
/**
28+
* The Redis manager instance.
29+
*
30+
* @var \Illuminate\Redis\RedisManager
31+
*/
32+
protected $redis;
33+
34+
/**
35+
* {@inheritdoc}
36+
*/
37+
public function __construct($target, Factory $factory, LoopInterface $loop)
38+
{
39+
parent::__construct($target, $factory, $loop);
40+
41+
$this->redis = Cache::getRedis();
42+
}
43+
2444
/**
2545
* {@inheritdoc}
2646
*/
2747
public function __call($name, $args)
2848
{
2949
$this->calls[] = [$name, $args];
3050

51+
if (! in_array($name, ['subscribe', 'psubscribe', 'unsubscribe', 'punsubscribe', 'onMessage'])) {
52+
$this->redis->__call($name, $args);
53+
}
54+
3155
return parent::__call($name, $args);
3256
}
3357

@@ -88,6 +112,26 @@ public function assertCalledWithArgs($name, array $args)
88112
return $this;
89113
}
90114

115+
/**
116+
* Check if the method with args got called an amount of times.
117+
*
118+
* @param string $name
119+
* @param array $args
120+
* @return $this
121+
*/
122+
public function assertCalledWithArgsCount($times = 1, $name, array $args)
123+
{
124+
$total = collect($this->getCalledFunctions())->filter(function ($function) use ($name, $args) {
125+
[$calledName, $calledArgs] = $function;
126+
127+
return $calledName === $name && $calledArgs === $args;
128+
});
129+
130+
PHPUnit::assertCount($times, $total);
131+
132+
return $this;
133+
}
134+
91135
/**
92136
* Check if the method didn't call.
93137
*
@@ -135,6 +179,26 @@ public function assertNotCalledWithArgs($name, array $args)
135179
return $this;
136180
}
137181

182+
/**
183+
* Check if the method with args got called an amount of times.
184+
*
185+
* @param string $name
186+
* @param array $args
187+
* @return $this
188+
*/
189+
public function assertNotCalledWithArgsCount($times = 1, $name, array $args)
190+
{
191+
$total = collect($this->getCalledFunctions())->filter(function ($function) use ($name, $args) {
192+
[$calledName, $calledArgs] = $function;
193+
194+
return $calledName === $name && $calledArgs === $args;
195+
});
196+
197+
PHPUnit::assertNotCount($times, $total);
198+
199+
return $this;
200+
}
201+
138202
/**
139203
* Check if no function got called.
140204
*

tests/PubSub/RedisDriverTest.php

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,11 @@
55
use BeyondCode\LaravelWebSockets\PubSub\Drivers\RedisClient;
66
use BeyondCode\LaravelWebSockets\Tests\Mocks\RedisFactory;
77
use BeyondCode\LaravelWebSockets\Tests\TestCase;
8-
use Illuminate\Support\Facades\Cache;
8+
use Illuminate\Support\Facades\Redis;
99
use React\EventLoop\Factory as LoopFactory;
1010

1111
class RedisDriverTest extends TestCase
1212
{
13-
/**
14-
* The Redis manager instance.
15-
*
16-
* @var \Illuminate\Redis\RedisManager
17-
*/
18-
protected $redis;
19-
2013
/**
2114
* {@inheritdoc}
2215
*/
@@ -26,9 +19,7 @@ public function setUp(): void
2619

2720
$this->runOnlyOnRedisReplication();
2821

29-
$this->redis = Cache::getRedis();
30-
31-
$this->redis->hdel('laravel_database_1234', 'connections');
22+
Redis::hdel('laravel_database_1234', 'connections');
3223
}
3324

3425
/** @test */
@@ -104,9 +95,7 @@ public function redis_tracks_app_connections_count()
10495
->assertCalledWithArgs('subscribe', ['laravel_database_1234']);
10596

10697
$this->getPublishClient()
107-
->assertNothingCalled();
108-
109-
$this->assertEquals(1, $this->redis->hget('laravel_database_1234', 'connections'));
98+
->assertCalledWithArgs('hincrby', ['laravel_database_1234', 'connections', 1]);
11099
}
111100

112101
/** @test */
@@ -121,15 +110,13 @@ public function redis_tracks_app_connections_count_on_disconnect()
121110
->assertNotCalledWithArgs('unsubscribe', ['laravel_database_1234']);
122111

123112
$this->getPublishClient()
124-
->assertNothingCalled();
125-
126-
$this->assertEquals(1, $this->redis->hget('laravel_database_1234', 'connections'));
113+
->assertCalledWithArgs('hincrby', ['laravel_database_1234', 'connections', 1]);
127114

128115
$this->pusherServer->onClose($connection);
129116

130117
$this->getPublishClient()
131-
->assertNothingCalled();
118+
->assertCalledWithArgs('hincrby', ['laravel_database_1234', 'connections', -1]);
132119

133-
$this->assertEquals(0, $this->redis->hget('laravel_database_1234', 'connections'));
120+
$this->assertEquals(0, Redis::hget('laravel_database_1234', 'connections'));
134121
}
135122
}

0 commit comments

Comments
 (0)