Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/Gateway/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,10 @@ public function __construct(

$payload = $this->mapper->map($parsedMessage, Payload::class);

$this->raw->emit((string) $payload->op, [$this, $payload, $this->logger]);
$this->loop->futureTick(function () use ($payload) {
// Moving this to future tick improves stability
$this->raw->emit((string) $payload->op, [$this, $payload, $this->logger]);
});
});

$this->websocket->on(WebsocketEvents::CLOSE, $this->handleClose(...));
Expand Down
46 changes: 29 additions & 17 deletions tests/Gateway/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class ConnectionTest extends MockeryTestCase
public function testGetDefaultUrl(): void
{
$connection = new Connection(
Mockery::mock(LoopInterface::class),
$this->getLoop(),
'::token::',
new Bitwise(),
new DataMapper(new NullLogger()),
Expand All @@ -51,7 +51,7 @@ public function testGetDefaultUrl(): void
public function testSequence(): void
{
$connection = new Connection(
Mockery::mock(LoopInterface::class),
$this->getLoop(),
'::token::',
new Bitwise(),
new DataMapper(new NullLogger()),
Expand All @@ -67,7 +67,7 @@ public function testSequence(): void
public function testConnect(): void
{
$connection = new Connection(
Mockery::mock(LoopInterface::class),
$this->getLoop(),
'::token::',
new Bitwise(),
new DataMapper(new NullLogger()),
Expand All @@ -90,7 +90,7 @@ public function testConnect(): void
public function testDisconnect(): void
{
$connection = new Connection(
Mockery::mock(LoopInterface::class),
$this->getLoop(),
'::token::',
new Bitwise(),
new DataMapper(new NullLogger()),
Expand All @@ -112,7 +112,7 @@ public function testDisconnect(): void
public function testSessionId(): void
{
$connection = new Connection(
Mockery::mock(LoopInterface::class),
$this->getLoop(),
'::token::',
new Bitwise(),
new DataMapper(new NullLogger()),
Expand All @@ -128,7 +128,7 @@ public function testSessionId(): void
public function testResumeUrl(): void
{
$connection = new Connection(
Mockery::mock(LoopInterface::class),
$this->getLoop(),
'::token::',
new Bitwise(),
new DataMapper(new NullLogger()),
Expand Down Expand Up @@ -307,7 +307,7 @@ public function testItCanSendHeartbeatsAutomatically(): void
public function testItReturnsEventHandlers(): void
{
$connection = new Connection(
Mockery::mock(LoopInterface::class),
$this->getLoop(),
'::token::',
new Bitwise(),
new DataMapper(new NullLogger()),
Expand All @@ -320,7 +320,7 @@ public function testItReturnsEventHandlers(): void
public function testItIdentifies(): void
{
$connection = new Connection(
Mockery::mock(LoopInterface::class),
$this->getLoop(),
'::token::',
new Bitwise(123),
new DataMapper(new NullLogger()),
Expand Down Expand Up @@ -348,7 +348,7 @@ public function testItIdentifies(): void
public function testItIdentifiesWithShards(): void
{
$connection = new Connection(
Mockery::mock(LoopInterface::class),
$this->getLoop(),
'::token::',
new Bitwise(123),
new DataMapper(new NullLogger()),
Expand Down Expand Up @@ -379,7 +379,7 @@ public function testItIdentifiesWithShards(): void
public function testItResumes(): void
{
$connection = new Connection(
Mockery::mock(LoopInterface::class),
$this->getLoop(),
'::token::',
new Bitwise(123),
new DataMapper(new NullLogger()),
Expand Down Expand Up @@ -426,7 +426,7 @@ public function testItResumes(): void
public function testOpen(): void
{
$connection = new Connection(
Mockery::mock(LoopInterface::class),
$this->getLoop(),
'::token::',
new Bitwise(),
new DataMapper(new NullLogger()),
Expand Down Expand Up @@ -463,7 +463,7 @@ public function testItEmitsGatewayMessagesAsEvents(): void
->withAnyArgs();

$connection = new Connection(
Mockery::mock(LoopInterface::class),
$this->getLoop(),
'::token::',
new Bitwise(),
new DataMapper(new NullLogger()),
Expand Down Expand Up @@ -496,7 +496,7 @@ public function testItEmitsGatewayMessagesAsEvents(): void
public function testItSendsPresenceUpdates()
{
$connection = new Connection(
Mockery::mock(LoopInterface::class),
$this->getLoop(),
'::token::',
new Bitwise(123),
new DataMapper(new NullLogger()),
Expand Down Expand Up @@ -545,7 +545,7 @@ public function testItReconnectsWhenWebsocketConnectionClosedWithCertainCodes(in
->twice();

new Connection(
Mockery::mock(LoopInterface::class),
$this->getLoop(),
'::token::',
new Bitwise(1),
DataMapperFake::get(),
Expand Down Expand Up @@ -592,7 +592,7 @@ public function testItResumesWhenWebsocketConnectionClosedWithCertainCodes(int $
->once();

$connection = new Connection(
Mockery::mock(LoopInterface::class),
$this->getLoop(),
'::token::',
new Bitwise(1),
DataMapperFake::get(),
Expand Down Expand Up @@ -628,7 +628,7 @@ public function testItReconnectsIfMissingResumeUrl(int $code)
->twice();

$connection = new Connection(
Mockery::mock(LoopInterface::class),
$this->getLoop(),
'::token::',
new Bitwise(1),
DataMapperFake::get(),
Expand Down Expand Up @@ -663,7 +663,7 @@ public function testItReconnectsIfMissingSessionId(int $code)
->twice();

$connection = new Connection(
Mockery::mock(LoopInterface::class),
$this->getLoop(),
'::token::',
new Bitwise(1),
DataMapperFake::get(),
Expand All @@ -679,6 +679,18 @@ public function testItReconnectsIfMissingSessionId(int $code)
$this->assertEquals([Connection::DEFAULT_WEBSOCKET_URL . '?v=' . Connection::DISCORD_VERSION], $websocket->openings);
}

private function getLoop(): LoopInterface
{
$loop = Mockery::mock(LoopInterface::class);

$loop->shouldReceive('futureTick')
->andReturnUsing(function (callable $callback) {
$callback();
});

return $loop;
}

public static function resumeCloseCodesProvider(): array
{
return [
Expand Down