-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(supaworker): add e2e test for worker spawning with message queue
Adds an end-to-end test for verifying worker spawning behavior when processing a large batch of messages, ensuring proper sequence incrementation and worker scaling
- Loading branch information
Showing
1 changed file
with
42 additions
and
0 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
pkgs/supaworker/tests/e2e/worker_spawns_next_worker.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { sql } from '../sql.ts'; | ||
import { assertGreaterOrEqual } from 'jsr:@std/assert'; | ||
import { delay } from 'jsr:@std/async'; | ||
|
||
async function seqLastValue(): Promise<number> { | ||
const seqResult = await sql`SELECT last_value::integer FROM test_seq`; | ||
return seqResult[0].last_value; | ||
} | ||
|
||
Deno.test('should spawn next worker when CPU clock limit hits', async () => { | ||
await sql`ALTER SEQUENCE test_seq RESTART WITH 1`; | ||
await sql`DELETE FROM supaworker.workers`; | ||
await sql`SELECT supaworker.spawn('increment-sequence')`; | ||
|
||
const MESSAGES_TO_SEND = 5000; | ||
|
||
try { | ||
await sql` | ||
SELECT pgmq.send_batch( | ||
'pgflow', | ||
ARRAY( | ||
SELECT '{}'::jsonb | ||
FROM generate_series(1, ${MESSAGES_TO_SEND}) | ||
) | ||
)`; | ||
|
||
let lastVal = 0; | ||
while (lastVal < MESSAGES_TO_SEND) { | ||
console.log('Polling... current value:', lastVal); | ||
await delay(1000); | ||
lastVal = await seqLastValue(); | ||
} | ||
|
||
assertGreaterOrEqual( | ||
lastVal, | ||
MESSAGES_TO_SEND, | ||
'Sequence value should be greater than or equal to the number of messages sent' | ||
); | ||
} finally { | ||
await sql.end(); | ||
} | ||
}); |