REST API
Collision Course (Parallel-Safe Isolation)
RequestValidateHard
What you will learn
- Reproduce the classic flaky-parallel-suite bug: shared mutable state losing updates under concurrency
- Isolate per-worker state (namespace token) so parallel tests never collide
- Account for serverless multi-instance state when reasoning about shared counters
Description
A shared counter corrupts under concurrency (lost updates); a per-worker workspace token isolates state so parallel suites stay correct.
Test Scenario
await request.delete('/api/v1/counter');
const N = 20;
await Promise.all(Array.from({ length: N }, () => request.post('/api/v1/counter/increment')));
const shared = await (await request.get('/api/v1/counter')).json();
expect(shared.data.value).toBeLessThan(N); // collisions lost updates
const { data } = await (await request.post('/api/v1/session', { data: {} })).json();
const inc = await request.post('/api/v1/counter/increment', { headers: { 'X-Workspace': data.workspaceToken } });
expect((await inc.json()).data.value).toBe(1); // isolated โ no contentionEndpoint
/api/v1/counter/incrementTesting Tips
- โขFire many concurrent POSTs to /api/v1/counter/increment with NO workspace โ the final value is LESS than the call count (lost updates)
- โขPOST /api/v1/session to get a workspaceToken, then send it as X-Workspace for an isolated counter
- โขNB: on a multi-instance deployment the shared counter is per-instance โ run the collision locally for the clearest signal
// Playwright API Testing - RESTimport { test, expect } from '@playwright/test';test('REST API request', async ({ request }) => {const response = await request.get('/api/v1/resource');expect(response.ok()).toBeTruthy();expect(response.status()).toBe(200);const data = await response.json();expect(data).toBeDefined();});
Challenge ID: rest-shared-state-isolation
2 frameworks available
API Playground
Send requests and inspect responses
Endpoint: /api/v1/counter/increment
Press Enter to send