|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +import { spawn } from 'child_process'; |
| 5 | +import * as assert from 'assert'; |
| 6 | +import * as path from 'path'; |
| 7 | + |
| 8 | +describe('Standalone Process Tests', () => { |
| 9 | + // Helper function to run test script in a separate process |
| 10 | + const runTest = async (args: string[] = []): Promise<{ code: number; stdout: string; stderr: string }> => |
| 11 | + new Promise((resolve, reject) => { |
| 12 | + // Use the compiled main.js file from the lib directory |
| 13 | + const testFile = path.join(__dirname, './main.js'); |
| 14 | + |
| 15 | + const child = spawn('node', [testFile, ...args], { stdio: 'pipe' }); |
| 16 | + |
| 17 | + let stdout = ''; |
| 18 | + let stderr = ''; |
| 19 | + |
| 20 | + child.stdout.on('data', (data) => (stdout += data.toString())); |
| 21 | + child.stderr.on('data', (data) => (stderr += data.toString())); |
| 22 | + |
| 23 | + child.on('close', (code) => { |
| 24 | + resolve({ code: code || 0, stdout, stderr }); |
| 25 | + }); |
| 26 | + |
| 27 | + child.on('error', reject); |
| 28 | + }); |
| 29 | + |
| 30 | + // Helper function to check basic success criteria |
| 31 | + const assertSuccess = (result: { code: number; stdout: string; stderr: string }) => { |
| 32 | + assert.strictEqual(result.code, 0); |
| 33 | + assert.ok(result.stdout.includes('SUCCESS: Inference completed')); |
| 34 | + assert.ok(!result.stderr.includes('mutex lock failed')); |
| 35 | + }; |
| 36 | + |
| 37 | + // Helper function to check that no mutex crashes occurred |
| 38 | + const assertNoMutexErrors = (stderr: string) => { |
| 39 | + assert.ok(!stderr.includes('mutex lock failed')); |
| 40 | + assert.ok(!stderr.includes('std::__1::system_error')); |
| 41 | + }; |
| 42 | + |
| 43 | + it('should handle normal process exit', async () => { |
| 44 | + const result = await runTest([]); |
| 45 | + assertSuccess(result); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should handle process.exit() call', async () => { |
| 49 | + const result = await runTest(['--process-exit']); |
| 50 | + assertSuccess(result); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should handle uncaught exceptions', async () => { |
| 54 | + const result = await runTest(['--throw-exception']); |
| 55 | + |
| 56 | + assert.notStrictEqual(result.code, 0); |
| 57 | + assert.ok(result.stdout.includes('SUCCESS: Inference completed')); |
| 58 | + assert.ok(result.stderr.includes('Test exception')); |
| 59 | + assertNoMutexErrors(result.stderr); |
| 60 | + }); |
| 61 | + |
| 62 | + it('should handle multiple process exits consistently', async () => { |
| 63 | + for (let i = 0; i < 3; i++) { |
| 64 | + const result = await runTest(['--process-exit']); |
| 65 | + assertSuccess(result); |
| 66 | + } |
| 67 | + }); |
| 68 | + |
| 69 | + it('should handle session.release() before normal exit', async () => { |
| 70 | + const result = await runTest(['--release']); |
| 71 | + assertSuccess(result); |
| 72 | + assert.ok(result.stdout.includes('Session released')); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should handle session.release() before process.exit()', async () => { |
| 76 | + const result = await runTest(['--release', '--process-exit']); |
| 77 | + assertSuccess(result); |
| 78 | + assert.ok(result.stdout.includes('Session released')); |
| 79 | + }); |
| 80 | + |
| 81 | + it('should handle no session.release() before process.exit()', async () => { |
| 82 | + const result = await runTest(['--process-exit']); |
| 83 | + assertSuccess(result); |
| 84 | + assert.ok(result.stdout.includes('Session NOT released')); |
| 85 | + }); |
| 86 | +}); |
0 commit comments