Skip to content

Commit b79996f

Browse files
committed
test(desktop): synchronize the presence and Stop tests on real signals
The overlapping-stream test waits for both presence entries, then for the leaving one's removal, instead of sleeping; the Stop race releases its held transaction in a finally, so a failed poll cannot leave the run row locked.
1 parent 4c3615c commit b79996f

1 file changed

Lines changed: 18 additions & 15 deletions

File tree

‎apps/sim/lib/desktop/application/executor.integration.ts‎

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,9 @@ import {
3030
} from '@sim/db/schema'
3131
import { createDeferred } from '@sim/testing/helpers/deferred'
3232
import { featureFlagsMock, featureFlagsMockFns } from '@sim/testing/mocks/feature-flags.mock'
33-
import { sleep } from '@sim/utils/helpers'
3433
import { generateId } from '@sim/utils/id'
3534
import { eq, inArray, sql } from 'drizzle-orm'
36-
import { closeRedisConnection } from '@/lib/core/config/redis'
35+
import { closeRedisConnection, getRedisClient } from '@/lib/core/config/redis'
3736
import {
3837
claimDesktopTool,
3938
completeDesktopTool,
@@ -569,17 +568,20 @@ describe.runIf(Boolean(redisUrl))('desktop background executor protocol', () =>
569568
principal: desktop.principal,
570569
input: { deviceId: desktop.deviceId, toolCallId, executionToken, status: 'success' },
571570
})
572-
/** Release Stop only once the result is queued behind its lock on the run row. */
573-
await expect
574-
.poll(async () => {
575-
const [waiter] = await db.execute<{ pid: number }>(sql`
576-
SELECT pid FROM pg_stat_activity WHERE wait_event_type = 'Lock'
577-
AND ${stopPid}::int = ANY(pg_blocking_pids(pid)) LIMIT 1`)
578-
return Boolean(waiter)
579-
})
580-
.toBe(true)
581-
releaseStop.resolve()
582-
await stopping
571+
try {
572+
/** Release Stop only once the result is queued behind its lock on the run row. */
573+
await expect
574+
.poll(async () => {
575+
const [waiter] = await db.execute<{ pid: number }>(sql`
576+
SELECT pid FROM pg_stat_activity WHERE wait_event_type = 'Lock'
577+
AND ${stopPid}::int = ANY(pg_blocking_pids(pid)) LIMIT 1`)
578+
return Boolean(waiter)
579+
})
580+
.toBe(true)
581+
} finally {
582+
releaseStop.resolve()
583+
await stopping
584+
}
583585

584586
await expect(completing).resolves.toEqual({ outcome: 'superseded', status: 'cancelled' })
585587
expect((await row(toolCallId)).status).toBe('cancelled')
@@ -737,10 +739,11 @@ describe.runIf(Boolean(redisUrl))('desktop background executor protocol', () =>
737739
const closeStaying = (await open()).subscribe(() => {})
738740
await expect.poll(() => isDesktopPresent(desktop.deviceId)).toBe(true)
739741
const closeLeaving = (await open()).subscribe(() => {})
742+
const streams = () => getRedisClient()?.zcard(`desktop:presence:${desktop.deviceId}`)
740743
try {
741-
await sleep(200)
744+
await expect.poll(streams).toBe(2)
742745
closeLeaving()
743-
await sleep(200)
746+
await expect.poll(streams).toBe(1)
744747
expect(await isDesktopPresent(desktop.deviceId)).toBe(true)
745748
} finally {
746749
closeStaying()

0 commit comments

Comments
 (0)