Skip to content

Commit

Permalink
Test
Browse files Browse the repository at this point in the history
  • Loading branch information
sirn-se committed Jul 4, 2024
1 parent e51f316 commit ffbef85
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 5 deletions.
4 changes: 3 additions & 1 deletion src/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,9 @@ public function disconnect(): void
$this->dispatch('disconnect', [$this, $connection]);
}
$this->connections = [];
$this->server->close();
if ($this->server) {
$this->server->close();
}
$this->server = $this->streams = null;
$this->logger->info('[server] Server disconnected');
}
Expand Down
74 changes: 70 additions & 4 deletions tests/suites/server/ServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -400,12 +400,15 @@ public function testShutdown(): void
$server->addMiddleware(new CloseHandler());

$server->onHandshake(function ($server, $connection, $request, $response) {
$server->shutdown();
if ($connection->getName() == 'fake-connection-2') {
$server->shutdown();
}
});

$this->expectWsServerSetup(scheme: 'tcp', port: 8000);

// Accept connection 1
$this->expectWsSelectConnections(['@server']);
// Accept connection
$this->expectSocketServerAccept();
$this->expectSocketStream();
$this->expectSocketStreamGetMetadata();
Expand All @@ -419,15 +422,43 @@ public function testShutdown(): void
$this->expectSocketStreamGetRemoteName();
$this->expectSocketStreamSetTimeout();
$this->expectWsServerPerformHandshake();
$this->expectSocketStreamIsConnected();

// Accept connection 2
$this->expectWsSelectConnections(['@server']);
$this->expectSocketServerAccept();
$this->expectSocketStream();
$this->expectSocketStreamGetMetadata();
$this->expectSocketStreamGetRemoteName()->setReturn(function () {
return 'fake-connection-2';
});
$this->expectStreamCollectionAttach();
$this->expectSocketStreamGetLocalName()->setReturn(function () {
return 'fake-connection-2';
});
$this->expectSocketStreamGetRemoteName();
$this->expectSocketStreamSetTimeout();
$this->expectWsServerPerformHandshake();

// Send close connection 1
$this->expectSocketStreamIsWritable();
$this->expectSocketStreamWrite();
$this->expectSocketStreamIsReadable();
$this->expectSocketStreamCloseWrite();
$this->expectSocketStreamGetMetadata();

// Send close connection 2
$this->expectSocketStreamIsWritable();
$this->expectSocketStreamWrite();
$this->expectSocketStreamIsReadable();
$this->expectSocketStreamCloseWrite();
$this->expectSocketStreamGetMetadata();

$this->expectSocketStreamIsConnected();
$this->expectWsSelectConnections(['fake-connection-1']);
$this->expectSocketStreamIsConnected();
$this->expectWsSelectConnections(['fake-connection-1', 'fake-connection-2']);

// Receive close ack connection 1
$this->expectSocketStreamRead()->addAssert(function (string $method, array $params) {
$this->assertEquals(2, $params[0]);
})->setReturn(function () {
Expand All @@ -440,15 +471,50 @@ public function testShutdown(): void
});
$this->expectSocketStreamIsWritable();
$this->expectSocketStreamClose();

// Receive close ack connection 2
$this->expectSocketStreamRead()->addAssert(function (string $method, array $params) {
$this->assertEquals(2, $params[0]);
})->setReturn(function () {
return base64_decode('iIA=');
});
$this->expectSocketStreamRead()->addAssert(function (string $method, array $params) {
$this->assertEquals(4, $params[0]);
})->setReturn(function () {
return base64_decode('RExLFw==');
});
$this->expectSocketStreamIsWritable();
$this->expectSocketStreamClose();

// Connection detacher
$this->expectSocketStreamIsConnected();
$this->expectStreamCollectionDetach();
$this->expectSocketStreamIsConnected();
$this->expectStreamCollectionDetach();

$this->expectSocketServerClose();

$server->start();

unset($server);
}

public function testShutdownEmpty(): void
{
$this->expectStreamFactory();
$server = new Server(8000);
$server->setStreamFactory(new StreamFactory());
$server->addMiddleware(new CloseHandler());

$server->onTick(function ($server) {
$server->shutdown();
});
$this->expectWsServerSetup(scheme: 'tcp', port: 8000);
$this->expectWsSelectConnections([]);
$this->expectSocketServerClose();

$server->start();
}

public function testAlreadyStarted(): void
{
$this->expectStreamFactory();
Expand Down

0 comments on commit ffbef85

Please sign in to comment.