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

Commit 51f84e3

Browse files
committed
set up tests
1 parent 64b0fa8 commit 51f84e3

File tree

10 files changed

+55
-36
lines changed

10 files changed

+55
-36
lines changed

.github/workflows/run-tests.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,11 @@ jobs:
4343
composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction --no-suggest
4444
4545
- name: Execute tests
46-
run: vendor/bin/phpunit --coverage-text --coverage-clover=coverage.xml
46+
run: |
47+
REPLICATION_DRIVER=local phpunit --coverage-text --coverage-clover=coverage_local.xml
48+
REPLICATION_DRIVER=redis phpunit --coverage-text --coverage-clover=coverage_redis.xml
4749
4850
- uses: codecov/codecov-action@v1
4951
with:
5052
fail_ci_if_error: false
53+
file: '*.xml'

config/websockets.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -143,23 +143,21 @@
143143

144144
/*
145145
|--------------------------------------------------------------------------
146-
| Broadcasting Replication
146+
| Broadcasting Replication PubSub
147147
|--------------------------------------------------------------------------
148148
|
149149
| You can enable replication to publish and subscribe to
150150
| messages across the driver.
151-
|
152-
| By default, it is disabled, but you can configure it to use drivers
151+
152+
| By default, it is set to 'local', but you can configure it to use drivers
153153
| like Redis to ensure connection between multiple instances of
154-
| WebSocket servers.
154+
| WebSocket servers. Just set the driver to 'redis' to enable the PubSub using Redis.
155155
|
156156
*/
157157

158158
'replication' => [
159159

160-
'enabled' => false,
161-
162-
'driver' => 'redis',
160+
'driver' => 'local',
163161

164162
'redis' => [
165163

src/Console/StartWebSocketServer.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,9 @@ protected function startWebSocketServer()
166166

167167
protected function configurePubSubReplication()
168168
{
169-
$this->laravel->get(ReplicationInterface::class)->boot($this->loop);
169+
$this->laravel
170+
->get(ReplicationInterface::class)
171+
->boot($this->loop);
170172

171173
return $this;
172174
}

src/HttpApi/Controllers/FetchChannelsController.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@
1313
class FetchChannelsController extends Controller
1414
{
1515
/** @var ReplicationInterface */
16-
protected $replication;
16+
protected $pubsub;
1717

18-
public function __construct(ChannelManager $channelManager, ReplicationInterface $replication)
18+
public function __construct(ChannelManager $channelManager, ReplicationInterface $pubsub)
1919
{
2020
parent::__construct($channelManager);
2121

22-
$this->replication = $replication;
22+
$this->pubsub = $pubsub;
2323
}
2424

2525
public function __invoke(Request $request)
@@ -51,7 +51,7 @@ public function __invoke(Request $request)
5151

5252
// We ask the replication backend to get us the member count per channel.
5353
// We get $counts back as a key-value array of channel names and their member count.
54-
return $this->replication
54+
return $this->pubsub
5555
->channelMemberCounts($request->appId, $channelNames)
5656
->then(function (array $counts) use ($channels, $attributes) {
5757
return [

src/PubSub/Drivers/LocalClient.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ public function boot(LoopInterface $loop): ReplicationInterface
3838
*/
3939
public function publish(string $appId, string $channel, stdClass $payload): bool
4040
{
41-
// Nothing to do, nobody to publish to
4241
return true;
4342
}
4443

src/PubSub/Drivers/RedisClient.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -257,20 +257,24 @@ public function channelMemberCounts(string $appId, array $channelNames): Promise
257257
*/
258258
protected function getConnectionUri()
259259
{
260-
$name = config('websockets.replication.connection') ?? 'default';
261-
$config = config("database.redis.$name");
260+
$name = config('websockets.replication.redis.connection') ?? 'default';
261+
$config = config('database.redis')[$name];
262+
262263
$host = $config['host'];
263-
$port = $config['port'] ? (':'.$config['port']) : ':6379';
264+
$port = $config['port'] ?: 6379;
264265

265266
$query = [];
267+
266268
if ($config['password']) {
267269
$query['password'] = $config['password'];
268270
}
271+
269272
if ($config['database']) {
270273
$query['database'] = $config['database'];
271274
}
275+
272276
$query = http_build_query($query);
273277

274-
return "redis://$host$port".($query ? '?'.$query : '');
278+
return "redis://{$host}:{$port}".($query ? "?{$query}" : '');
275279
}
276280
}

src/WebSockets/Channels/Channel.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ class Channel
1515
protected $channelName;
1616

1717
/** @var ReplicationInterface */
18-
protected $replication;
18+
protected $pubsub;
1919

2020
/** @var \Ratchet\ConnectionInterface[] */
2121
protected $subscribedConnections = [];
2222

2323
public function __construct(string $channelName)
2424
{
2525
$this->channelName = $channelName;
26-
$this->replication = app(ReplicationInterface::class);
26+
$this->pubsub = app(ReplicationInterface::class);
2727
}
2828

2929
public function getChannelName(): string
@@ -68,7 +68,7 @@ public function subscribe(ConnectionInterface $connection, stdClass $payload)
6868
$this->saveConnection($connection);
6969

7070
// Subscribe to broadcasted messages from the pub/sub backend
71-
$this->replication->subscribe($connection->app->id, $this->channelName);
71+
$this->pubsub->subscribe($connection->app->id, $this->channelName);
7272

7373
$connection->send(json_encode([
7474
'event' => 'pusher_internal:subscription_succeeded',
@@ -81,7 +81,7 @@ public function unsubscribe(ConnectionInterface $connection)
8181
unset($this->subscribedConnections[$connection->socketId]);
8282

8383
// Unsubscribe from the pub/sub backend
84-
$this->replication->unsubscribe($connection->app->id, $this->channelName);
84+
$this->pubsub->unsubscribe($connection->app->id, $this->channelName);
8585

8686
if (! $this->hasConnections()) {
8787
DashboardLogger::vacated($connection, $this->channelName);
@@ -120,7 +120,7 @@ public function broadcastToEveryoneExcept($payload, ?string $socketId, string $a
120120
// in this case. If this came from TriggerEventController, then we still want
121121
// to publish to get the message out to other server instances.
122122
if ($publish) {
123-
$this->replication->publish($appId, $this->channelName, $payload);
123+
$this->pubsub->publish($appId, $this->channelName, $payload);
124124
}
125125

126126
// Performance optimization, if we don't have a socket ID,

src/WebSockets/Channels/PresenceChannel.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class PresenceChannel extends Channel
2828
public function getUsers(string $appId)
2929
{
3030
// Get the members list from the replication backend
31-
return $this->replication
31+
return $this->pubsub
3232
->channelMembers($appId, $this->channelName);
3333
}
3434

@@ -49,7 +49,7 @@ public function subscribe(ConnectionInterface $connection, stdClass $payload)
4949
$this->users[$connection->socketId] = $channelData;
5050

5151
// Add the connection as a member of the channel
52-
$this->replication
52+
$this->pubsub
5353
->joinChannel(
5454
$connection->app->id,
5555
$this->channelName,
@@ -59,7 +59,7 @@ public function subscribe(ConnectionInterface $connection, stdClass $payload)
5959

6060
// We need to pull the channel data from the replication backend,
6161
// otherwise we won't be sending the full details of the channel
62-
$this->replication
62+
$this->pubsub
6363
->channelMembers($connection->app->id, $this->channelName)
6464
->then(function ($users) use ($connection) {
6565
// Send the success event
@@ -86,7 +86,7 @@ public function unsubscribe(ConnectionInterface $connection)
8686
}
8787

8888
// Remove the connection as a member of the channel
89-
$this->replication
89+
$this->pubsub
9090
->leaveChannel(
9191
$connection->app->id,
9292
$this->channelName,
@@ -110,7 +110,7 @@ public function unsubscribe(ConnectionInterface $connection)
110110
*/
111111
public function toArray(string $appId = null)
112112
{
113-
return $this->replication
113+
return $this->pubsub
114114
->channelMembers($appId, $this->channelName)
115115
->then(function ($users) {
116116
return array_merge(parent::toArray(), [

src/WebSocketsServiceProvider.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use Illuminate\Support\ServiceProvider;
2525
use Psr\Log\LoggerInterface;
2626
use Pusher\Pusher;
27+
use React\EventLoop\Factory as LoopFactory;
2728

2829
class WebSocketsServiceProvider extends ServiceProvider
2930
{
@@ -56,19 +57,19 @@ public function boot()
5657

5758
protected function configurePubSub()
5859
{
59-
if (config('websockets.replication.enabled') !== true || config('websockets.replication.driver') !== 'redis') {
60+
if (config('websockets.replication.driver') === 'local') {
6061
$this->app->singleton(ReplicationInterface::class, function () {
61-
return new LocalClient();
62+
return new LocalClient;
6263
});
63-
64-
return;
6564
}
6665

67-
$this->app->singleton(ReplicationInterface::class, function () {
68-
return (new RedisClient())->boot($this->loop);
69-
});
66+
if (config('websockets.replication.driver') === 'redis') {
67+
$this->app->singleton(ReplicationInterface::class, function () {
68+
return (new RedisClient)->boot($this->loop ?? LoopFactory::create());
69+
});
70+
}
7071

71-
$this->app->get(BroadcastManager::class)->extend('redis-pusher', function ($app, array $config) {
72+
$this->app->get(BroadcastManager::class)->extend('websockets', function ($app, array $config) {
7273
$pusher = new Pusher(
7374
$config['key'], $config['secret'],
7475
$config['app_id'], $config['options'] ?? []

tests/TestCase.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,18 @@ protected function getEnvironmentSetUp($app)
5656
],
5757
]);
5858

59+
$app['config']->set('database.redis.default', [
60+
'host' => env('REDIS_HOST', '127.0.0.1'),
61+
'password' => env('REDIS_PASSWORD', null),
62+
'port' => env('REDIS_PORT', '6379'),
63+
'database' => env('REDIS_DB', '0'),
64+
]);
65+
66+
$app['config']->set(
67+
'websockets.replication.driver',
68+
getenv('REPLICATION_DRIVER') ?: 'local'
69+
);
70+
5971
include_once __DIR__.'/../database/migrations/create_websockets_statistics_entries_table.php.stub';
6072

6173
(new \CreateWebSocketsStatisticsEntriesTable())->up();

0 commit comments

Comments
 (0)