Skip to content

Commit

Permalink
Add test inleaving broadcast and send
Browse files Browse the repository at this point in the history
  • Loading branch information
trowski committed Jun 11, 2022
1 parent df69b3b commit 054d94f
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions test/WebsocketTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Amp\ByteStream;
use Amp\DeferredFuture;
use Amp\Future;
use Amp\Http\Rfc7230;
use Amp\Http\Server\Driver\Client as HttpClient;
use Amp\Http\Server\ErrorHandler;
Expand All @@ -18,6 +19,7 @@
use Psr\Http\Message\UriInterface as PsrUri;
use Psr\Log\NullLogger;
use Revolt\EventLoop;
use function Amp\delay;

class WebsocketTest extends AsyncTestCase
{
Expand Down Expand Up @@ -271,4 +273,27 @@ public function testMulticast(): void
$gateway->multicastBinary('Binary', [$client->getId()])->await();
}, $client);
}

public function testBroadcastIntermixedWithSends(): void
{
$client = $this->createMock(WebsocketClient::class);
$client->method('getRemoteAddress')
->willReturn(new Socket\InternetAddress('127.0.0.1', 1));
$client->expects($this->exactly(5))
->method('send')
->withConsecutive(...\array_map(fn (int $index) => [(string) $index], \range(1, 5)))
->willReturnCallback(static fn () => delay(0.01));
$client->method('isClosed')
->willReturn(false);

$this->execute(function (WebsocketGateway $gateway, WebsocketClient $client) {
Future\await([
$gateway->broadcast('1'),
$gateway->send('2', $client->getId()),
$gateway->broadcast('3'),
$gateway->send('4', $client->getId()),
$gateway->broadcast('5'),
]);
}, $client);
}
}

0 comments on commit 054d94f

Please sign in to comment.