From 0a840ef80f291eff6ef8e008c773924ed23601fd Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Wed, 29 Apr 2026 20:11:39 +1200 Subject: [PATCH] fix: remove destructive assertEventually wrapping around receive() WebSocket receive() consumes a fresh message on each call, so retrying inside assertEventually desynchronizes subsequent assertions instead of re-checking the same message. Keep the increased socket timeout to tolerate slow connections, but use direct receive() calls again to stay consistent with RealtimeConsoleClientTest::testManualAuthentication. Co-Authored-By: Claude Opus 4.7 (1M context) --- .../Realtime/RealtimeCustomClientTest.php | 107 ++++++++---------- 1 file changed, 50 insertions(+), 57 deletions(-) diff --git a/tests/e2e/Services/Realtime/RealtimeCustomClientTest.php b/tests/e2e/Services/Realtime/RealtimeCustomClientTest.php index f9af4e15ee..14a1222e79 100644 --- a/tests/e2e/Services/Realtime/RealtimeCustomClientTest.php +++ b/tests/e2e/Services/Realtime/RealtimeCustomClientTest.php @@ -181,21 +181,14 @@ class RealtimeCustomClientTest extends Scope $client = $this->getWebsocket(['account'], [ 'origin' => 'http://localhost' ], timeout: 10); - $assertResponseEventually = function (callable $assertion) use ($client): void { - $this->assertEventually(function () use ($client, $assertion) { - $response = \json_decode($client->receive(), true); - $this->assertIsArray($response); - $assertion($response); - }, 10000, 250); - }; - $assertResponseEventually(function (array $response): void { - $this->assertArrayHasKey('type', $response); - $this->assertArrayHasKey('data', $response); - $this->assertEquals('connected', $response['type']); - $this->assertNotEmpty($response['data']); - $this->assertCount(1, $response['data']['channels']); - $this->assertContains('account', $response['data']['channels']); - }); + $response = json_decode($client->receive(), true); + + $this->assertArrayHasKey('type', $response); + $this->assertArrayHasKey('data', $response); + $this->assertEquals('connected', $response['type']); + $this->assertNotEmpty($response['data']); + $this->assertCount(1, $response['data']['channels']); + $this->assertContains('account', $response['data']['channels']); $client->send(\json_encode([ 'type' => 'authentication', @@ -204,16 +197,16 @@ class RealtimeCustomClientTest extends Scope ] ])); - $assertResponseEventually(function (array $response) use ($userId): void { - $this->assertArrayHasKey('type', $response); - $this->assertArrayHasKey('data', $response); - $this->assertEquals('response', $response['type']); - $this->assertNotEmpty($response['data']); - $this->assertEquals('authentication', $response['data']['to']); - $this->assertTrue($response['data']['success']); - $this->assertNotEmpty($response['data']['user']); - $this->assertEquals($userId, $response['data']['user']['$id']); - }); + $response = json_decode($client->receive(), true); + + $this->assertArrayHasKey('type', $response); + $this->assertArrayHasKey('data', $response); + $this->assertEquals('response', $response['type']); + $this->assertNotEmpty($response['data']); + $this->assertEquals('authentication', $response['data']['to']); + $this->assertTrue($response['data']['success']); + $this->assertNotEmpty($response['data']['user']); + $this->assertEquals($userId, $response['data']['user']['$id']); /** * Test for FAILURE @@ -225,28 +218,28 @@ class RealtimeCustomClientTest extends Scope ] ])); - $assertResponseEventually(function (array $response): void { - $this->assertArrayHasKey('type', $response); - $this->assertArrayHasKey('data', $response); - $this->assertEquals('error', $response['type']); - $this->assertNotEmpty($response['data']); - $this->assertEquals(1003, $response['data']['code']); - $this->assertEquals('Session is not valid.', $response['data']['message']); - }); + $response = json_decode($client->receive(), true); + + $this->assertArrayHasKey('type', $response); + $this->assertArrayHasKey('data', $response); + $this->assertEquals('error', $response['type']); + $this->assertNotEmpty($response['data']); + $this->assertEquals(1003, $response['data']['code']); + $this->assertEquals('Session is not valid.', $response['data']['message']); $client->send(\json_encode([ 'type' => 'authentication', 'data' => [] ])); - $assertResponseEventually(function (array $response): void { - $this->assertArrayHasKey('type', $response); - $this->assertArrayHasKey('data', $response); - $this->assertEquals('error', $response['type']); - $this->assertNotEmpty($response['data']); - $this->assertEquals(1003, $response['data']['code']); - $this->assertEquals('Payload is not valid.', $response['data']['message']); - }); + $response = json_decode($client->receive(), true); + + $this->assertArrayHasKey('type', $response); + $this->assertArrayHasKey('data', $response); + $this->assertEquals('error', $response['type']); + $this->assertNotEmpty($response['data']); + $this->assertEquals(1003, $response['data']['code']); + $this->assertEquals('Payload is not valid.', $response['data']['message']); $client->send(\json_encode([ 'type' => 'unknown', @@ -255,27 +248,27 @@ class RealtimeCustomClientTest extends Scope ] ])); - $assertResponseEventually(function (array $response): void { - $this->assertArrayHasKey('type', $response); - $this->assertArrayHasKey('data', $response); - $this->assertEquals('error', $response['type']); - $this->assertNotEmpty($response['data']); - $this->assertEquals(1003, $response['data']['code']); - $this->assertEquals('Message type is not valid.', $response['data']['message']); - }); + $response = json_decode($client->receive(), true); + + $this->assertArrayHasKey('type', $response); + $this->assertArrayHasKey('data', $response); + $this->assertEquals('error', $response['type']); + $this->assertNotEmpty($response['data']); + $this->assertEquals(1003, $response['data']['code']); + $this->assertEquals('Message type is not valid.', $response['data']['message']); $client->send(\json_encode([ 'test' => '123', ])); - $assertResponseEventually(function (array $response): void { - $this->assertArrayHasKey('type', $response); - $this->assertArrayHasKey('data', $response); - $this->assertEquals('error', $response['type']); - $this->assertNotEmpty($response['data']); - $this->assertEquals(1003, $response['data']['code']); - $this->assertEquals('Message format is not valid.', $response['data']['message']); - }); + $response = json_decode($client->receive(), true); + + $this->assertArrayHasKey('type', $response); + $this->assertArrayHasKey('data', $response); + $this->assertEquals('error', $response['type']); + $this->assertNotEmpty($response['data']); + $this->assertEquals(1003, $response['data']['code']); + $this->assertEquals('Message format is not valid.', $response['data']['message']); $client->close();