|
| 1 | +import path from 'node:path'; |
| 2 | +import { Readable, Writable } from 'node:stream'; |
| 3 | +import { Client } from '../client/index.js'; |
| 4 | +import { StdioClientTransport } from '../client/stdio.js'; |
| 5 | +import { Server } from '../server/index.js'; |
| 6 | +import { StdioServerTransport } from '../server/stdio.js'; |
| 7 | +import { LoggingMessageNotificationSchema } from '../types.js'; |
| 8 | + |
| 9 | +const FIXTURES_DIR = path.resolve(__dirname, '../__fixtures__'); |
| 10 | + |
| 11 | +describe('Process cleanup', () => { |
| 12 | + vi.setConfig({ testTimeout: 5000 }); // 5 second timeout |
| 13 | + |
| 14 | + it('server should exit cleanly after closing transport', async () => { |
| 15 | + const server = new Server( |
| 16 | + { |
| 17 | + name: 'test-server', |
| 18 | + version: '1.0.0' |
| 19 | + }, |
| 20 | + { |
| 21 | + capabilities: {} |
| 22 | + } |
| 23 | + ); |
| 24 | + |
| 25 | + const mockReadable = new Readable({ |
| 26 | + read() { |
| 27 | + this.push(null); // signal EOF |
| 28 | + } |
| 29 | + }), |
| 30 | + mockWritable = new Writable({ |
| 31 | + write(chunk, encoding, callback) { |
| 32 | + callback(); |
| 33 | + } |
| 34 | + }); |
| 35 | + |
| 36 | + // Attach mock streams to process for the server transport |
| 37 | + const transport = new StdioServerTransport(mockReadable, mockWritable); |
| 38 | + await server.connect(transport); |
| 39 | + |
| 40 | + // Close the transport |
| 41 | + await transport.close(); |
| 42 | + |
| 43 | + // ensure a proper disposal mock streams |
| 44 | + mockReadable.destroy(); |
| 45 | + mockWritable.destroy(); |
| 46 | + |
| 47 | + // If we reach here without hanging, the test passes |
| 48 | + // The test runner will fail if the process hangs |
| 49 | + expect(true).toBe(true); |
| 50 | + }); |
| 51 | + |
| 52 | + it('onclose should be called exactly once', async () => { |
| 53 | + const client = new Client({ |
| 54 | + name: 'test-client', |
| 55 | + version: '1.0.0' |
| 56 | + }); |
| 57 | + |
| 58 | + const transport = new StdioClientTransport({ |
| 59 | + command: 'node', |
| 60 | + args: ['--import', 'tsx', 'testServer.ts'], |
| 61 | + cwd: FIXTURES_DIR |
| 62 | + }); |
| 63 | + |
| 64 | + await client.connect(transport); |
| 65 | + |
| 66 | + let onCloseWasCalled = 0; |
| 67 | + client.onclose = () => { |
| 68 | + onCloseWasCalled++; |
| 69 | + }; |
| 70 | + |
| 71 | + await client.close(); |
| 72 | + |
| 73 | + // A short delay to allow the close event to propagate |
| 74 | + await new Promise(resolve => setTimeout(resolve, 50)); |
| 75 | + |
| 76 | + expect(onCloseWasCalled).toBe(1); |
| 77 | + }); |
| 78 | + |
| 79 | + it('should exit cleanly for a server that hangs', async () => { |
| 80 | + const client = new Client({ |
| 81 | + name: 'test-client', |
| 82 | + version: '1.0.0' |
| 83 | + }); |
| 84 | + |
| 85 | + const transport = new StdioClientTransport({ |
| 86 | + command: 'node', |
| 87 | + args: ['--import', 'tsx', 'serverThatHangs.ts'], |
| 88 | + cwd: FIXTURES_DIR |
| 89 | + }); |
| 90 | + |
| 91 | + await client.connect(transport); |
| 92 | + await client.setLoggingLevel('debug'); |
| 93 | + client.setNotificationHandler(LoggingMessageNotificationSchema, notification => { |
| 94 | + console.debug('server log: ' + notification.params.data); |
| 95 | + }); |
| 96 | + const serverPid = transport.pid!; |
| 97 | + |
| 98 | + await client.close(); |
| 99 | + |
| 100 | + // A short delay to allow the close event to propagate |
| 101 | + await new Promise(resolve => setTimeout(resolve, 50)); |
| 102 | + |
| 103 | + try { |
| 104 | + process.kill(serverPid, 9); |
| 105 | + throw new Error('Expected server to be dead but it is alive'); |
| 106 | + } catch (err: unknown) { |
| 107 | + // 'ESRCH' the process doesn't exist |
| 108 | + if (err && typeof err === 'object' && 'code' in err && err.code === 'ESRCH') { |
| 109 | + // success |
| 110 | + } else throw err; |
| 111 | + } |
| 112 | + }); |
| 113 | +}); |
0 commit comments