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']);