Real-Time APIs · Real-Time in Practice
Testing Real-Time Systems
Unit, integration, load and chaos tests for persistent connections.
Test protocol handlers as units (message in → messages out), run integration tests with real WebSocket clients against a test server, and load test connections: concurrent connections, message fan-out rates, reconnect storms and slow consumers (k6, Artillery, Gatling support WebSockets). Inject network faults (latency, drops) to verify resync logic, and test deploy draining.
Real-time test plan
| Test | Verifies |
|---|---|
| Protocol unit tests | Message handling logic |
| Integration with real clients | Auth, subscriptions, fan-out |
| Connection load test | Memory/FD limits, fan-out latency |
| Reconnect storm test | Admission control, auth capacity |
| Fault injection | Gap detection and resync |
import ws from "k6/ws";
import { check } from "k6";
export const options = { vus: 5000, duration: "10m" };
export default function () {
const res = ws.connect(`${__ENV.WS_URL}?ticket=${getTicket()}`, {}, (socket) => {
socket.on("open", () => socket.send(JSON.stringify({ type: "subscribe", channel: `room:${__VU % 100}` })));
socket.on("message", () => {});
socket.setInterval(() => socket.send(JSON.stringify({ type: "message", text: "hi" })), 10_000);
socket.setTimeout(() => socket.close(), 60_000);
});
check(res, { "status 101": (r) => r && r.status === 101 });
}
it("delivers room messages to other members", async () => {
const [a, b] = await Promise.all([connect("alice"), connect("bob")]);
await Promise.all([a.subscribe("room:1"), b.subscribe("room:1")]);
a.send({ type: "message", channel: "room:1", text: "hello" });
expect(await b.next("message")).toMatchObject({ channel: "room:1", text: "hello" });
});
Common pitfalls
- Load testing only HTTP RPS.
- Never testing reconnect storms.
- Mocking the socket layer in every test.
Key points
- Real clients in integration tests
- Load test connections and fan-out
- Inject faults to test resync
Further reading: k6: WebSockets
Practice questions
What should a load test for a chat service measure besides latency?
Answer
Concurrent connections per node, memory per connection, fan-out delivery time, behavior during reconnect storms and slow consumers.
How can you test resync logic?
Answer
Inject message drops/latency or kill gateway instances during tests and assert clients detect gaps and recover the full message sequence.
Open in the app to mark lessons complete, take notes and track your progress.