Skip to content

Commit 188d06f

Browse files
authored
fix(executor): keep the condition-join loop test off the Redis-backed isolate lease (#10275)
* fix(executor): keep the condition-join loop test off the Redis-backed isolate lease * improvement(testing): share one isolated-vm mock across executor tests
1 parent 85ea094 commit 188d06f

7 files changed

Lines changed: 83 additions & 27 deletions

File tree

‎apps/sim/executor/execution/condition-join-loop.test.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
createSerializedBlock,
44
createSerializedWorkflow,
55
} from '@sim/testing/factories/serialized-block.factory'
6+
import { isolatedVmMock } from '@sim/testing/mocks/isolated-vm.mock'
67
import { toolsMock, toolsMockFns } from '@sim/testing/mocks/tools.mock'
78
import { beforeEach, describe, expect, it, vi } from 'vitest'
89
import { BlockType } from '@/executor/constants'
@@ -12,6 +13,8 @@ import type { SerializedWorkflow } from '@/serializer/types'
1213

1314
vi.mock('@/tools', () => toolsMock)
1415

16+
vi.mock('@/lib/execution/isolated-vm', () => isolatedVmMock)
17+
1518
/** Real condition handler; every other block answers with its own name. */
1619
vi.mock('@/executor/handlers/registry', () => ({
1720
createBlockHandlers: () => [

‎apps/sim/executor/execution/condition-loop-ceiling.test.ts‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ import {
33
createSerializedBlock,
44
createSerializedWorkflow,
55
} from '@sim/testing/factories/serialized-block.factory'
6+
import {
7+
evaluateLoopIndexCheck,
8+
isolatedVmMock,
9+
isolatedVmMockFns,
10+
} from '@sim/testing/mocks/isolated-vm.mock'
611
import { beforeEach, describe, expect, it, vi } from 'vitest'
712
import { BlockType, DEFAULTS } from '@/executor/constants'
813
import { DAGExecutor } from '@/executor/execution/executor'
@@ -17,14 +22,7 @@ const { bodyRuns, conditionResult, runaway } = vi.hoisted(() => ({
1722
runaway: { controller: new AbortController(), limit: 30_000 },
1823
}))
1924

20-
/** Count conditions resolve to `return Boolean(<index> < <count>)`; user conditions stay opaque. */
21-
vi.mock('@/lib/execution/isolated-vm', () => ({
22-
executeInIsolatedVM: vi.fn(async ({ code }: { code: string }) => {
23-
const indexCheck = /^return Boolean\((\d+) < (\d+)\)$/.exec(code)
24-
if (indexCheck) return { result: Number(indexCheck[1]) < Number(indexCheck[2]) }
25-
return { result: bodyRuns.count < conditionResult.stopAfter }
26-
}),
27-
}))
25+
vi.mock('@/lib/execution/isolated-vm', () => isolatedVmMock)
2826

2927
vi.mock('@/executor/handlers/registry', () => ({
3028
createBlockHandlers: () => [
@@ -80,6 +78,10 @@ describe('condition-driven loop iteration ceiling', () => {
8078
bodyRuns.count = 0
8179
conditionResult.stopAfter = Number.POSITIVE_INFINITY
8280
runaway.controller = new AbortController()
81+
/** Count conditions evaluate; user conditions stay opaque and hold until `stopAfter`. */
82+
isolatedVmMockFns.mockExecuteInIsolatedVM.mockImplementation(async ({ code }) => ({
83+
result: evaluateLoopIndexCheck(code) ?? bodyRuns.count < conditionResult.stopAfter,
84+
}))
8385
})
8486

8587
it.each([

‎apps/sim/executor/execution/test-hooks.test.ts‎

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { SessionPrincipal } from '@sim/auth/principal'
22
import { createSerializedBlock, createSerializedWorkflow } from '@sim/testing'
3+
import { isolatedVmMock } from '@sim/testing/mocks/isolated-vm.mock'
34
import { beforeEach, describe, expect, it, vi } from 'vitest'
45
import {
56
createMockChannel,
@@ -17,14 +18,7 @@ const { executed, inputsSeen } = vi.hoisted(() => ({
1718
inputsSeen: [] as Array<{ blockId: string; inputs: Record<string, unknown> }>,
1819
}))
1920

20-
/** A For loop's continuation check is `return Boolean(<index> < <count>)`; nothing here needs a real isolate. */
21-
vi.mock('@/lib/execution/isolated-vm', () => ({
22-
executeInIsolatedVM: vi.fn(async ({ code }: { code: string }) => {
23-
const indexCheck = /^return Boolean\((\d+) < (\d+)\)$/.exec(code)
24-
if (!indexCheck) throw new Error(`Unexpected isolated-vm code in test: ${code}`)
25-
return { result: Number(indexCheck[1]) < Number(indexCheck[2]) }
26-
}),
27-
}))
21+
vi.mock('@/lib/execution/isolated-vm', () => isolatedVmMock)
2822

2923
vi.mock('@/executor/handlers/registry', () => ({
3024
createBlockHandlers: () => [

‎apps/sim/executor/orchestrators/loop.test.ts‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { createLogger } from '@sim/logger'
2+
import { isolatedVmMock, isolatedVmMockFns } from '@sim/testing/mocks/isolated-vm.mock'
23
import { storageServiceMockFns } from '@sim/testing/mocks/storage-service.mock'
34
import { uploadsMock } from '@sim/testing/mocks/uploads.mock'
45
import { beforeEach, describe, expect, it, vi } from 'vitest'
@@ -13,17 +14,13 @@ import { ResolvedSecretTraceRegistry } from '@/executor/utils/resolved-secret-tr
1314

1415
const mockUploadFile = storageServiceMockFns.mockUploadFile
1516

16-
const { mockExecuteInIsolatedVM } = vi.hoisted(() => ({
17-
mockExecuteInIsolatedVM: vi.fn(),
18-
}))
17+
const mockExecuteInIsolatedVM = isolatedVmMockFns.mockExecuteInIsolatedVM
1918
const mockLogger =
2019
vi.mocked(createLogger).mock.results[
2120
vi.mocked(createLogger).mock.calls.findIndex(([name]) => name === 'LoopOrchestrator')
2221
].value
2322

24-
vi.mock('@/lib/execution/isolated-vm', () => ({
25-
executeInIsolatedVM: mockExecuteInIsolatedVM,
26-
}))
23+
vi.mock('@/lib/execution/isolated-vm', () => isolatedVmMock)
2724

2825
vi.mock('@/lib/uploads', () => uploadsMock)
2926

‎apps/sim/lib/function-execution/execute-request.test.ts‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
workflowsUtilsMock,
1212
} from '@sim/testing'
1313
import { encryptionMock, encryptionMockFns } from '@sim/testing/mocks/encryption.mock'
14+
import { isolatedVmMock, isolatedVmMockFns } from '@sim/testing/mocks/isolated-vm.mock'
1415
import { remoteSandboxMock, remoteSandboxMockFns } from '@sim/testing/mocks/remote-sandbox.mock'
1516
import { storageServiceMockFns } from '@sim/testing/mocks/storage-service.mock'
1617
import { uploadsMock } from '@sim/testing/mocks/uploads.mock'
@@ -42,15 +43,13 @@ import {
4243
import { SandboxExecutionTimeoutError } from '@/lib/execution/remote-sandbox/timeout'
4344

4445
const {
45-
mockExecuteInIsolatedVM,
4646
mockValidateWorkspaceFileWriteTarget,
4747
mockWriteWorkspaceFileByPath,
4848
mockUploadExecutionFile,
4949
mockMountContributors,
5050
mockUnprovenancedMountCount,
5151
mockRenderedMountContributors,
5252
} = vi.hoisted(() => ({
53-
mockExecuteInIsolatedVM: vi.fn(),
5453
mockValidateWorkspaceFileWriteTarget: vi.fn(),
5554
mockWriteWorkspaceFileByPath: vi.fn(),
5655
mockUploadExecutionFile: vi.fn(),
@@ -61,9 +60,9 @@ const {
6160

6261
vi.mock('@/lib/core/security/encryption', () => encryptionMock)
6362

64-
vi.mock('@/lib/execution/isolated-vm', () => ({
65-
executeInIsolatedVM: mockExecuteInIsolatedVM,
66-
}))
63+
const mockExecuteInIsolatedVM = isolatedVmMockFns.mockExecuteInIsolatedVM
64+
65+
vi.mock('@/lib/execution/isolated-vm', () => isolatedVmMock)
6766

6867
vi.mock('@/lib/execution/remote-sandbox', () => remoteSandboxMock)
6968

‎packages/testing/src/mocks/index.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,13 @@ export {
330330
featureFlagsMock,
331331
featureFlagsMockFns,
332332
} from './feature-flags.mock'
333+
// Isolated VM mocks (for @/lib/execution/isolated-vm)
334+
export {
335+
evaluateLoopIndexCheck,
336+
isolatedVmMock,
337+
isolatedVmMockFns,
338+
resetIsolatedVmMock,
339+
} from './isolated-vm.mock'
333340
// Executor mocks - use side-effect import: import '@sim/testing/mocks/executor'
334341

335342
export {
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { vi } from 'vitest'
2+
3+
/** The continuation check the loop orchestrator compiles for count-driven loops. */
4+
const LOOP_INDEX_CHECK = /^return Boolean\((\d+) < (\d+)\)$/
5+
6+
/**
7+
* Evaluates a loop's `return Boolean(<index> < <count>)` continuation check, or returns `null`
8+
* for any other code so a test can decide what that code resolves to.
9+
*/
10+
export function evaluateLoopIndexCheck(code: string): boolean | null {
11+
const match = LOOP_INDEX_CHECK.exec(code)
12+
return match ? Number(match[1]) < Number(match[2]) : null
13+
}
14+
15+
async function executeLoopIndexCheck({ code }: { code: string }) {
16+
const result = evaluateLoopIndexCheck(code)
17+
if (result === null) throw new Error(`Unexpected isolated-vm code in test: ${code}`)
18+
return { result }
19+
}
20+
21+
/**
22+
* Controllable mock functions for `@/lib/execution/isolated-vm`. Executor tests mock it because
23+
* the real sandbox acquires a Redis-backed lease, which the shared Redis mock answers as
24+
* over-limit. By default only loop continuation checks evaluate; any other code throws, so a
25+
* test that needs more must say what it resolves to.
26+
*
27+
* @example
28+
* ```ts
29+
* isolatedVmMockFns.mockExecuteInIsolatedVM.mockImplementation(async ({ code }) => ({
30+
* result: evaluateLoopIndexCheck(code) ?? userConditionResult,
31+
* }))
32+
* ```
33+
*/
34+
export const isolatedVmMockFns = {
35+
/** Untyped like a bare `vi.fn()`, so each test resolves the fields its code path reads. */
36+
mockExecuteInIsolatedVM: vi.fn().mockImplementation(executeLoopIndexCheck),
37+
}
38+
39+
/**
40+
* Static mock module for `@/lib/execution/isolated-vm`.
41+
*
42+
* @example
43+
* ```ts
44+
* vi.mock('@/lib/execution/isolated-vm', () => isolatedVmMock)
45+
* ```
46+
*/
47+
export const isolatedVmMock = {
48+
executeInIsolatedVM: isolatedVmMockFns.mockExecuteInIsolatedVM,
49+
}
50+
51+
/** Restores the default loop-check-only implementation. */
52+
export function resetIsolatedVmMock(): void {
53+
isolatedVmMockFns.mockExecuteInIsolatedVM.mockReset().mockImplementation(executeLoopIndexCheck)
54+
}

0 commit comments

Comments
 (0)