From b4e894184a23b8f173931693b02a5940d29264c5 Mon Sep 17 00:00:00 2001 From: Claudear <262350598+claudear@users.noreply.github.com> Date: Thu, 16 Jul 2026 09:07:06 +0000 Subject: [PATCH 1/2] fix: harden flaky RealtimeCustomClientTest::testChannelExecutions DISCORD-15272384 The test subscribed to the project-scoped `executions` channel and then made two bare `receive()` calls, assuming the next two frames were exactly the execution `create` and `update` events, each bounded by a single 10s read timeout. The async execution's terminal `update` only arrives after worker pickup, runtime cold start, and the function's execution timeout (the `timeout` fixture sleeps 60s against a 10s function timeout), which regularly exceeds one read window and surfaces as `WebSocket\TimeoutException: Client read timeout`. The bare receives were also brittle to any intermediate or unrelated frames on the shared channel. Add a reusable `receiveUntilEvent()` helper to `RealtimeBase` (mirroring the presence tests' `receiveUntil`) that polls, skips unrelated frames, and waits generously for a matching frame. Use it in `testChannelExecutions` to wait for the specific `create` and `update` events. Test-only change. Co-Authored-By: Claude Opus 4.8 --- tests/e2e/Services/Realtime/RealtimeBase.php | 43 +++++++++++++++++++ .../Realtime/RealtimeCustomClientTest.php | 19 ++++++-- 2 files changed, 59 insertions(+), 3 deletions(-) diff --git a/tests/e2e/Services/Realtime/RealtimeBase.php b/tests/e2e/Services/Realtime/RealtimeBase.php index b2d17c1e4a..7f06e31e3f 100644 --- a/tests/e2e/Services/Realtime/RealtimeBase.php +++ b/tests/e2e/Services/Realtime/RealtimeBase.php @@ -4,9 +4,52 @@ namespace Tests\E2E\Services\Realtime; use WebSocket\Client as WebSocketClient; use WebSocket\ConnectionException; +use WebSocket\TimeoutException; trait RealtimeBase { + /** + * Receive websocket frames until one matches, skipping unrelated frames. + * + * Realtime channels such as `executions` are project-scoped, so a subscriber + * may see intermediate or unrelated frames before the one under test. Some + * events (e.g. an async execution's terminal `update`) also arrive only after + * substantial latency (worker pickup + runtime cold start + execution timeout), + * well beyond a single read timeout. This polls, tolerating both, until $match + * returns true or the overall deadline elapses. + * + * @param callable(array): bool $match + */ + private function receiveUntilEvent( + WebSocketClient $client, + callable $match, + int $timeoutMs = 60000, + int $pollMs = 50 + ): array { + $deadline = \microtime(true) + ($timeoutMs / 1000); + $lastMessage = []; + + while (\microtime(true) < $deadline) { + try { + $message = \json_decode($client->receive(), true); + } catch (TimeoutException) { + \usleep($pollMs * 1000); + continue; + } + + if (!\is_array($message)) { + continue; + } + + $lastMessage = $message; + if ($match($message)) { + return $message; + } + } + + $this->fail('Timed out waiting for expected websocket frame. Last frame: ' . \json_encode($lastMessage)); + } + private function getWebsocket( array $channels = [], array $headers = [], diff --git a/tests/e2e/Services/Realtime/RealtimeCustomClientTest.php b/tests/e2e/Services/Realtime/RealtimeCustomClientTest.php index ce241ab91c..3c61fa5aa3 100644 --- a/tests/e2e/Services/Realtime/RealtimeCustomClientTest.php +++ b/tests/e2e/Services/Realtime/RealtimeCustomClientTest.php @@ -2457,11 +2457,24 @@ final class RealtimeCustomClientTest extends Scope $this->assertEquals(202, $execution['headers']['status-code']); $this->assertNotEmpty($execution['body']['$id']); - $response = json_decode($client->receive(), true); - $responseUpdate = json_decode($client->receive(), true); - $executionId = $execution['body']['$id']; + // The async execution first emits a `create` event, then a terminal `update` + // once the runtime finishes. That `update` only arrives after worker pickup, + // runtime cold start, and the function's execution timeout, so it can easily + // exceed a single read timeout. Skip unrelated frames and wait generously for + // each expected event instead of assuming they are the next two frames. + $response = $this->receiveUntilEvent( + $client, + fn (array $message): bool => ($message['type'] ?? null) === 'event' + && \in_array("functions.{$functionId}.executions.{$executionId}.create", $message['data']['events'] ?? [], true) + ); + $responseUpdate = $this->receiveUntilEvent( + $client, + fn (array $message): bool => ($message['type'] ?? null) === 'event' + && \in_array("functions.{$functionId}.executions.{$executionId}.update", $message['data']['events'] ?? [], true) + ); + $this->assertArrayHasKey('type', $response); $this->assertArrayHasKey('data', $response); $this->assertEquals('event', $response['type']); From 31ba20203bd1e098e00cb51e422cf18492594c29 Mon Sep 17 00:00:00 2001 From: Claudear <262350598+claudear@users.noreply.github.com> Date: Thu, 16 Jul 2026 10:05:09 +0000 Subject: [PATCH 2/2] test: use assertEventually and handle ConnectionException in receiveUntilEvent Address PR review: replace manual usleep polling loop with assertEventually, and catch ConnectionException inside the probe so an idle-timeout eviction or server-side socket reset reports a descriptive failure instead of a raw stack trace. Co-Authored-By: Claude Opus 4.8 --- tests/e2e/Services/Realtime/RealtimeBase.php | 46 +++++++++++++------- 1 file changed, 30 insertions(+), 16 deletions(-) diff --git a/tests/e2e/Services/Realtime/RealtimeBase.php b/tests/e2e/Services/Realtime/RealtimeBase.php index 7f06e31e3f..30a0e78dd9 100644 --- a/tests/e2e/Services/Realtime/RealtimeBase.php +++ b/tests/e2e/Services/Realtime/RealtimeBase.php @@ -2,6 +2,7 @@ namespace Tests\E2E\Services\Realtime; +use Appwrite\Tests\Async\Exceptions\Critical; use WebSocket\Client as WebSocketClient; use WebSocket\ConnectionException; use WebSocket\TimeoutException; @@ -26,28 +27,41 @@ trait RealtimeBase int $timeoutMs = 60000, int $pollMs = 50 ): array { - $deadline = \microtime(true) + ($timeoutMs / 1000); + $matched = []; $lastMessage = []; - while (\microtime(true) < $deadline) { - try { - $message = \json_decode($client->receive(), true); - } catch (TimeoutException) { - \usleep($pollMs * 1000); - continue; - } + try { + $this->assertEventually(function () use ($client, $match, &$matched, &$lastMessage): void { + try { + $frame = $client->receive(); + } catch (TimeoutException) { + // No frame arrived within the read timeout yet; keep polling. + throw new \Exception('No websocket frame received within read timeout.'); + } catch (ConnectionException $e) { + // Server closed or reset the socket (e.g. idle-timeout eviction). + // Retrying can never recover, so fail fast with context instead of + // letting a raw stack trace bubble up. + throw new Critical('WebSocket connection closed while waiting for expected frame: ' . $e->getMessage()); + } - if (!\is_array($message)) { - continue; - } + $message = \json_decode($frame, true); + if (\is_array($message)) { + $lastMessage = $message; + } - $lastMessage = $message; - if ($match($message)) { - return $message; - } + if (!\is_array($message) || !$match($message)) { + throw new \Exception('Websocket frame did not match expected event.'); + } + + $matched = $message; + }, $timeoutMs, $pollMs); + } catch (Critical $e) { + $this->fail($e->getMessage() . ' Last frame: ' . \json_encode($lastMessage)); + } catch (\Exception) { + $this->fail('Timed out waiting for expected websocket frame. Last frame: ' . \json_encode($lastMessage)); } - $this->fail('Timed out waiting for expected websocket frame. Last frame: ' . \json_encode($lastMessage)); + return $matched; } private function getWebsocket(