Skip to content

Commit 70fdd75

Browse files
committed
chore: update tests
1 parent fd525b0 commit 70fdd75

File tree

4 files changed

+62
-19
lines changed

4 files changed

+62
-19
lines changed

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,4 @@ jobs:
4646
sleep 10
4747
4848
- name: Run Tests
49-
run: docker compose exec tests vendor/bin/phpunit
49+
run: docker compose exec tests vendor/bin/phpunit --debug

src/WebSocket/Client.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,4 +225,46 @@ private function emit(string $event, mixed $data = null): void
225225
$handler($data);
226226
}
227227
}
228+
229+
public function receive(): ?string
230+
{
231+
if (!$this->connected) {
232+
throw new \RuntimeException('Not connected to WebSocket server');
233+
}
234+
235+
$frame = $this->client->recv($this->timeout);
236+
237+
if ($frame === false) {
238+
if ($this->client->errCode === SWOOLE_ERROR_CLIENT_NO_CONNECTION) {
239+
$this->handleClose();
240+
return null;
241+
}
242+
throw new \RuntimeException(
243+
"Failed to receive data: {$this->client->errCode} - {$this->client->errMsg}"
244+
);
245+
}
246+
247+
if ($frame === "") {
248+
return null;
249+
}
250+
251+
if ($frame instanceof Frame) {
252+
switch ($frame->opcode) {
253+
case WEBSOCKET_OPCODE_TEXT:
254+
return $frame->data;
255+
case WEBSOCKET_OPCODE_CLOSE:
256+
$this->handleClose();
257+
return null;
258+
case WEBSOCKET_OPCODE_PING:
259+
$this->emit('ping', $frame->data);
260+
$this->client->push('', WEBSOCKET_OPCODE_PONG);
261+
return null;
262+
case WEBSOCKET_OPCODE_PONG:
263+
$this->emit('pong', $frame->data);
264+
return null;
265+
}
266+
}
267+
268+
return null;
269+
}
228270
}

tests/e2e/AdapterTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
namespace Utopia\WebSocket\Tests;
44

55
use PHPUnit\Framework\TestCase;
6-
use WebSocket\Client as WebSocketClient;
6+
use Utopia\WebSocket\Client;
77

88
class AdapterTest extends TestCase
99
{
10-
private function getWebsocket(string $host, int $port): WebSocketClient
10+
private function getWebsocket(string $host, int $port): Client
1111
{
12-
return new WebSocketClient('ws://' . $host . ':' . $port, [
12+
return new Client('ws://' . $host . ':' . $port, [
1313
'timeout' => 10,
1414
]);
1515
}

tests/e2e/ClientTest.php

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,10 @@ private function getClient(string $host, int $port): Client
1414
]);
1515
}
1616

17-
public function testSwoole(): void
18-
{
19-
$this->testClient('swoole', 80);
20-
}
21-
22-
public function testWorkerman(): void
23-
{
24-
$this->testClient('workerman', 80);
25-
}
26-
27-
private function testClient(string $host, int $port): void
17+
public function testClient(): void
2818
{
19+
$host = 'swoole';
20+
$port = 80;
2921
$client = $this->getClient($host, $port);
3022

3123
$messageReceived = false;
@@ -37,8 +29,11 @@ private function testClient(string $host, int $port): void
3729
$client->connect();
3830
$client->send('ping');
3931

40-
// Wait for response
41-
\Swoole\Event::wait();
32+
$startTime = time();
33+
while (!$messageReceived && (time() - $startTime) < 10) {
34+
\Swoole\Event::wait();
35+
}
36+
4237
$this->assertTrue($messageReceived);
4338
$this->assertTrue($client->isConnected());
4439

@@ -81,13 +76,19 @@ private function testClient(string $host, int $port): void
8176
$clientA->onMessage($broadcastHandler);
8277
$clientB->onMessage($broadcastHandler);
8378

79+
$startTime = time();
8480
$clientA->send('broadcast');
85-
\Swoole\Event::wait();
81+
while ($broadcastCount < 3 && (time() - $startTime) < 10) {
82+
\Swoole\Event::wait();
83+
}
8684
$this->assertEquals(3, $broadcastCount);
8785

8886
$broadcastCount = 0;
87+
$startTime = time();
8988
$clientB->send('broadcast');
90-
\Swoole\Event::wait();
89+
while ($broadcastCount < 3 && (time() - $startTime) < 10) {
90+
\Swoole\Event::wait();
91+
}
9192
$this->assertEquals(3, $broadcastCount);
9293

9394
// Test disconnection

0 commit comments

Comments
 (0)