|
| 1 | +// SPDX-FileCopyrightText: 2026 LiveKit, Inc. |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: Apache-2.0 |
| 4 | +import { describe, expect, it, vi } from 'vitest'; |
| 5 | +import type { RunningJobInfo } from '../job.js'; |
| 6 | +import { type JobExecutor, JobStatus } from './job_executor.js'; |
| 7 | +import { ProcPool } from './proc_pool.js'; |
| 8 | + |
| 9 | +function createMockExecutor() { |
| 10 | + const executor: JobExecutor = { |
| 11 | + started: true, |
| 12 | + userArguments: {}, |
| 13 | + runningJob: undefined, |
| 14 | + status: JobStatus.RUNNING, |
| 15 | + start: vi.fn(async () => {}), |
| 16 | + join: vi.fn(async () => {}), |
| 17 | + initialize: vi.fn(async () => {}), |
| 18 | + close: vi.fn(async () => {}), |
| 19 | + launchJob: vi.fn(async () => {}), |
| 20 | + }; |
| 21 | + return executor; |
| 22 | +} |
| 23 | + |
| 24 | +describe('ProcPool warmed process lock handling', () => { |
| 25 | + it('releases lock token from the dequeued warmed process entry', async () => { |
| 26 | + const pool = new ProcPool('agent', 1, 1000, 1000, undefined, 0, 0); |
| 27 | + const unlock = vi.fn(); |
| 28 | + const executor = createMockExecutor(); |
| 29 | + const jobInfo = { |
| 30 | + acceptArguments: { name: 'n', identity: 'i', metadata: '' }, |
| 31 | + job: { id: 'job-id' }, |
| 32 | + url: 'wss://example.com', |
| 33 | + token: 'token', |
| 34 | + workerId: 'worker-id', |
| 35 | + } as unknown as RunningJobInfo; |
| 36 | + |
| 37 | + await pool.warmedProcQueue.put({ proc: executor, unlock }); |
| 38 | + await pool.launchJob(jobInfo); |
| 39 | + |
| 40 | + expect(unlock).toHaveBeenCalledTimes(1); |
| 41 | + expect(executor.launchJob).toHaveBeenCalledWith(jobInfo); |
| 42 | + }); |
| 43 | + |
| 44 | + it('releases queued lock tokens during close', async () => { |
| 45 | + const pool = new ProcPool('agent', 1, 1000, 1000, undefined, 0, 0); |
| 46 | + const unlock = vi.fn(); |
| 47 | + const executor = createMockExecutor(); |
| 48 | + |
| 49 | + await pool.warmedProcQueue.put({ proc: executor, unlock }); |
| 50 | + pool.started = true; |
| 51 | + await pool.close(); |
| 52 | + |
| 53 | + expect(unlock).toHaveBeenCalledTimes(1); |
| 54 | + expect(executor.close).toHaveBeenCalledTimes(1); |
| 55 | + }); |
| 56 | + |
| 57 | + it('releases both init and proc locks when closed before proc starts', async () => { |
| 58 | + const pool = new ProcPool('agent', 1, 1000, 1000, undefined, 0, 0); |
| 59 | + const initUnlock = vi.fn(); |
| 60 | + const procUnlock = vi.fn(); |
| 61 | + pool.closed = true; |
| 62 | + pool.initMutex.lock = vi.fn(async () => initUnlock); |
| 63 | + |
| 64 | + await pool.procWatchTask(procUnlock); |
| 65 | + |
| 66 | + expect(initUnlock).toHaveBeenCalledTimes(1); |
| 67 | + expect(procUnlock).toHaveBeenCalledTimes(1); |
| 68 | + }); |
| 69 | +}); |
0 commit comments