Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
joedixon committed Nov 24, 2023
1 parent faa4fb0 commit 32a7a64
Show file tree
Hide file tree
Showing 17 changed files with 72 additions and 45 deletions.
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
"illuminate/http": "^10.0",
"illuminate/redis": "^10.0",
"illuminate/support": "^10.0",
"nesbot/carbon": "^2.64",
"ratchet/rfc6455": "^0.3.1",
"react/socket": "^1.14",
"symfony/http-foundation": "^6.3"
Expand Down
4 changes: 2 additions & 2 deletions src/Channels/Channel.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,11 @@ public function broadcast(array $payload, Connection $except = null): void
foreach ($chunks as $connections) {
foreach ($connections as $connection) {
if ($except && $except->id() === $connection->connection()->id()) {
break;
continue;
}

if (isset($payload['except']) && $payload['except'] === $connection->connection()->id()) {
break;
continue;
}

try {
Expand Down
22 changes: 19 additions & 3 deletions src/Contracts/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ abstract class Connection

/**
* Stores the ping state of the connection.
*
* @var \Carbon\Carbon
*/
protected $hasBeenPinged = false;

Expand Down Expand Up @@ -71,12 +69,30 @@ public function ping(): void
$this->hasBeenPinged = true;
}

/**
* Get the last time the connection was seen.
*/
public function lastSeenAt(): ?int
{
return $this->lastSeenAt;
}

/**
* Set the connection last seen at timestamp.
*/
public function setLastSeenAt(int $time): Connection
{
$this->lastSeenAt = $time;

return $this;
}

/**
* Touch the connection last seen at timestamp.
*/
public function touch(): Connection
{
$this->lastSeenAt = time();
$this->setLastSeenAt(time());

return $this;
}
Expand Down
4 changes: 4 additions & 0 deletions src/Jobs/PruneStaleConnections.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ public function handle(ChannelManager $channels): void
]),
]));

$channels
->for($connection->app())
->unsubscribeFromAll($connection->connection());

$connection->disconnect();
}
});
Expand Down
2 changes: 1 addition & 1 deletion src/Pusher/Http/Controllers/ChannelsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class ChannelsController extends Controller
*/
public function handle(RequestInterface $request, Connection $connection, ...$args): Response
{
$channels = $this->channels->all();
$channels = collect($this->channels->all());
$info = explode(',', $this->query['info'] ?? '');

if (isset($this->query['filter_by_prefix'])) {
Expand Down
2 changes: 0 additions & 2 deletions src/Servers/Reverb/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ class Connection extends ConnectionContract

/**
* Stores the ping state of the connection.
*
* @var \Carbon\Carbon
*/
protected $hasBeenPinged = false;

Expand Down
7 changes: 5 additions & 2 deletions tests/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,14 @@ public function origin(): string
return 'http://localhost';
}

public function setLastSeenAt(Carbon $lastSeenAt): void
public function setLastSeenAt(int $time): Connection
{
$this->lastSeenAt = $lastSeenAt;
$this->lastSeenAt = $time;

return $this;
}


public function setHasBeenPinged(): void
{
$this->hasBeenPinged = true;
Expand Down
7 changes: 4 additions & 3 deletions tests/Feature/Reverb/ServerTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use Illuminate\Support\Arr;
use Illuminate\Support\Carbon;
use Illuminate\Support\Str;
use Laravel\Reverb\Contracts\ChannelManager;
Expand Down Expand Up @@ -141,7 +142,7 @@
$this->subscribe('test-channel', connection: $connection);
$promise = $this->messagePromise($connection);

Carbon::setTestNow(now()->addMinutes(10));
Arr::first(channelManager()->connections())->setLastSeenAt(time() - 60 * 10);

(new PingInactiveConnections)->handle(channelManager());

Expand All @@ -155,7 +156,7 @@

expect(channelManager()->find('test-channel')->connections())->toHaveCount(1);

Carbon::setTestNow(now()->addMinutes(10));
Arr::first(channelManager()->connections())->setLastSeenAt(time() - 60 * 10);

$promiseTwo = $this->messagePromise($connection);
(new PingInactiveConnections)->handle(
Expand Down Expand Up @@ -204,7 +205,7 @@
$this->subscribe('presence-test-channel-4', connection: $connection, data: ['user_id' => 1, 'user_info' => ['name' => 'Test User 1']]);

expect(channelManager()->all())->toHaveCount(4);
channelManager()->all()->each(function ($channel) use ($connection) {
collect(channelManager()->all())->each(function ($channel) use ($connection) {
expect($channel->connections())->toHaveCount(1);
expect(collect($channel->connections())->map(fn ($connection) => $connection->id()))->toContain($this->connectionId);
});
Expand Down
8 changes: 4 additions & 4 deletions tests/Feature/Reverb/UsersTerminateControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@
$this->subscribe('test-channel-one', connection: $connection);
$this->subscribe('test-channel-two', connection: $connection);

expect(channelManager()->all()->get('test-channel-one')->connections())->toHaveCount(2);
expect(channelManager()->all()->get('test-channel-two')->connections())->toHaveCount(2);
expect(collect(channelManager()->all())->get('test-channel-one')->connections())->toHaveCount(2);
expect(collect(channelManager()->all())->get('test-channel-two')->connections())->toHaveCount(2);

$response = await($this->signedPostRequest("users/{$this->connectionId}/terminate_connections"));

$this->assertSame(200, $response->getStatusCode());
$this->assertSame('{}', $response->getBody()->getContents());
expect(channelManager()->all()->get('test-channel-one')->connections())->toHaveCount(1);
expect(channelManager()->all()->get('test-channel-two')->connections())->toHaveCount(1);
expect(collect(channelManager()->all())->get('test-channel-one')->connections())->toHaveCount(1);
expect(collect(channelManager()->all())->get('test-channel-two')->connections())->toHaveCount(1);
});
18 changes: 11 additions & 7 deletions tests/Feature/ServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@
$channelManager->shouldReceive('for')
->andReturn($channelManager);
$this->app->singleton(ChannelManager::class, fn () => $channelManager);
$server = $this->app->make(Server::class);

$this->server->close(new Connection);
$server->close(new Connection);

$channelManager->shouldHaveReceived('unsubscribeFromAll');
});
Expand Down Expand Up @@ -160,8 +161,9 @@
$channelManager->shouldReceive('for')
->andReturn($channelManager);
$this->app->singleton(ChannelManager::class, fn () => $channelManager);
$server = $this->app->make(Server::class);

$this->server->message(
$server->message(
$connection = new Connection,
json_encode([
'event' => 'pusher:subscribe',
Expand All @@ -171,7 +173,7 @@
],
]));

$this->server->close($connection);
$server->close($connection);

$channelManager->shouldHaveReceived('unsubscribeFromAll')
->once()
Expand All @@ -183,8 +185,9 @@
$channelManager->shouldReceive('for')
->andReturn($channelManager);
$this->app->singleton(ChannelManager::class, fn () => $channelManager);
$server = $this->app->make(Server::class);

$this->server->message(
$server->message(
$connection = new Connection,
json_encode([
'event' => 'pusher:subscribe',
Expand All @@ -194,7 +197,7 @@
],
]));

$this->server->close($connection);
$server->close($connection);

$channelManager->shouldHaveReceived('unsubscribeFromAll')
->once()
Expand All @@ -206,8 +209,9 @@
$channelManager->shouldReceive('for')
->andReturn($channelManager);
$this->app->singleton(ChannelManager::class, fn () => $channelManager);
$server = $this->app->make(Server::class);

$this->server->message(
$server->message(
$connection = new Connection,
json_encode([
'event' => 'pusher:subscribe',
Expand All @@ -217,7 +221,7 @@
],
]));

$this->server->close($connection);
$server->close($connection);

$channelManager->shouldHaveReceived('unsubscribeFromAll')
->once()
Expand Down
5 changes: 2 additions & 3 deletions tests/Unit/Channels/ChannelTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php

use Laravel\Reverb\Channels\Channel;
use Laravel\Reverb\Contracts\ApplicationProvider;
use Laravel\Reverb\Contracts\ChannelConnectionManager;
use Laravel\Reverb\Tests\Connection;

Expand Down Expand Up @@ -40,7 +39,7 @@
->once()
->andReturn($connections = connections(3));

$channel->broadcast(app(ApplicationProvider::class)->findByKey('pusher-key'), ['foo' => 'bar']);
$channel->broadcast(['foo' => 'bar']);

collect($connections)->each(fn ($connection) => $connection->assertSent(['foo' => 'bar']));
});
Expand All @@ -54,7 +53,7 @@
->once()
->andReturn($connections = connections(3));

$channel->broadcast(app(ApplicationProvider::class)->findByKey('pusher-key'), ['foo' => 'bar'], $connections[0]->connection());
$channel->broadcast(['foo' => 'bar'], $connections[0]->connection());

$connections[0]->assertNothingSent();
collect(array_slice($connections, -2))->each(fn ($connection) => $connection->assertSent(['foo' => 'bar']));
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Channels/PresenceChannelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
->once()
->andReturn($connections = connections(3));

$channel->broadcast(app(ApplicationProvider::class)->findByKey('pusher-key'), ['foo' => 'bar']);
$channel->broadcast(['foo' => 'bar']);

collect($connections)->each(fn ($connection) => $connection->assertSent(['foo' => 'bar']));
});
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Channels/PrivateChannelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
->once()
->andReturn($connections = connections(3));

$channel->broadcast(app(ApplicationProvider::class)->findByKey('pusher-key'), ['foo' => 'bar']);
$channel->broadcast(['foo' => 'bar']);

collect($connections)->each(fn ($connection) => $connection->assertSent(['foo' => 'bar']));
});
Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/Jobs/PingInactiveConnectionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@

$this->channelManager->shouldReceive('connections')
->once()
->andReturn(collect($connections));
->andReturn($connections);

$connections = collect($connections)->each(function ($connection) use ($channel) {
$channel->subscribe($connection->connection());
$connection->setLastSeenAt(now()->subMinutes(10));
$connection->setLastSeenAt(time() - 60 * 10);
});

(new PingInactiveConnections)->handle($this->channelManager);
Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/Jobs/PruneStaleConnectionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@

$this->channelManager->shouldReceive('connections')
->once()
->andReturn(collect($connections));
->andReturn($connections);

collect($connections)->each(function ($connection) use ($channel) {
$channel->subscribe($connection->connection());
$connection->setLastSeenAt(now()->subMinutes(10));
$connection->setLastSeenAt(time() - 60 * 10);
$connection->setHasBeenPinged();

$this->channelManager->shouldReceive('unsubscribeFromAll')
Expand Down
7 changes: 4 additions & 3 deletions tests/Unit/Managers/ChannelManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@

$channels->each(fn ($channel) => $this->channelManager->find($channel)->subscribe($this->connection));

$this->channelManager->all()->values()->each(function ($channel, $index) {
expect($channel->name())->toBe('test-channel-'.($index));
});
foreach ($this->channelManager->all() as $index => $channel) {
expect($channel->name())->toBe($index);
}

expect($this->channelManager->all())->toHaveCount(4);
});

Expand Down
18 changes: 10 additions & 8 deletions tests/Unit/Pusher/EventTest.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
<?php

use Laravel\Reverb\Contracts\ChannelManager;
use Laravel\Reverb\Pusher\Event as PusherEvent;
use Laravel\Reverb\Tests\Connection;

beforeEach(function () {
$this->connection = new Connection;
$this->pusher = new PusherEvent(app(ChannelManager::class));
});

it('can send an acknowledgement', function () {
PusherEvent::handle(
$this->pusher->handle(
$this->connection,
'pusher:connection_established'
);
Expand All @@ -23,7 +25,7 @@
});

it('can subscribe to a channel', function () {
PusherEvent::handle(
$this->pusher->handle(
$this->connection,
'pusher:subscribe',
['channel' => 'test-channel']
Expand All @@ -36,7 +38,7 @@
});

it('can unsubscribe from a channel', function () {
PusherEvent::handle(
$this->pusher->handle(
$this->connection,
'pusher:unsubscribe',
['channel' => 'test-channel']
Expand All @@ -46,7 +48,7 @@
});

it('can respond to a ping', function () {
PusherEvent::handle(
$this->pusher->handle(
$this->connection,
'pusher:ping',
);
Expand All @@ -57,7 +59,7 @@
});

it('can correctly format a payload', function () {
$payload = PusherEvent::formatPayload(
$payload = $this->pusher->formatPayload(
'foo',
['bar' => 'baz'],
'test-channel',
Expand All @@ -70,15 +72,15 @@
'channel' => 'test-channel',
]));

$payload = PusherEvent::formatPayload('foo');
$payload = $this->pusher->formatPayload('foo');

expect($payload)->toBe(json_encode([
'event' => 'pusher:foo',
]));
});

it('can correctly format an internal payload', function () {
$payload = PusherEvent::formatInternalPayload(
$payload = $this->pusher->formatInternalPayload(
'foo',
['bar' => 'baz'],
'test-channel',
Expand All @@ -91,7 +93,7 @@
'channel' => 'test-channel',
]));

$payload = PusherEvent::formatInternalPayload('foo');
$payload = $this->pusher->formatInternalPayload('foo');

expect($payload)->toBe(json_encode([
'event' => 'pusher_internal:foo',
Expand Down

0 comments on commit 32a7a64

Please sign in to comment.