修复测试阻塞问题
diff --git a/jest.config.js b/jest.config.js index 3f15118..7c61c56 100644 --- a/jest.config.js +++ b/jest.config.js
@@ -13,7 +13,7 @@ verbose: true, // detectOpenHandles: true, // Enable this flag to debug hanging tests // Force exit after all tests complete to avoid hanging on unclosed resources - forceExit: true, + // forceExit: true, // Run tests sequentially to avoid database conflicts // Multiple tests share the same database names (root.test for tree model, test for table model) maxWorkers: 1, // Run tests one at a time
diff --git a/package.json b/package.json index d696a84..5c36a09 100644 --- a/package.json +++ b/package.json
@@ -14,7 +14,7 @@ "test:unit": "jest --testPathPatterns=tests/unit", "test:e2e": "jest --testPathPatterns=tests/e2e", "test:e2e:debug": "node --inspect-brk node_modules/.bin/jest --runInBand --testPathPatterns=tests/e2e", - "test:e2e:open-handles": "jest --testPathPatterns=tests/e2e --detectOpenHandles --forceExit=false", + "test:e2e:check-handles": "jest --testPathPatterns=tests/e2e --detectOpenHandles --forceExit", "test:debug": "node --inspect-brk node_modules/.bin/jest --runInBand", "lint": "eslint .", "lint:fix": "eslint . --fix",
diff --git a/src/client/BaseSessionPool.ts b/src/client/BaseSessionPool.ts index acc7184..691fc76 100644 --- a/src/client/BaseSessionPool.ts +++ b/src/client/BaseSessionPool.ts
@@ -173,6 +173,11 @@ } reject(new Error("Timeout waiting for available session")); }, waitTimeout); + + // Use unref() so timeout doesn't prevent process exit + if (typeof timeoutId === 'object' && 'unref' in timeoutId) { + timeoutId.unref(); + } this.waitQueue.push((session: Session) => { clearTimeout(timeoutId);
diff --git a/src/connection/Connection.ts b/src/connection/Connection.ts index 1153f69..a18f0e2 100644 --- a/src/connection/Connection.ts +++ b/src/connection/Connection.ts
@@ -85,6 +85,16 @@ this.isConnected = false; }); + // Unref the underlying socket to allow process exit + // This prevents the connection from keeping the process alive + if (this.connection && (this.connection as any).connection) { + const socket = (this.connection as any).connection; + if (socket && typeof socket.unref === 'function') { + socket.unref(); + logger.debug("Socket unref'd to allow process exit"); + } + } + this.client = thrift.createClient(IClientRPCService, this.connection); await this.openSession(); @@ -182,6 +192,10 @@ timeoutHandle = setTimeout(() => { reject(new Error("Close session timeout")); }, 5000); + // Use unref() so timeout doesn't prevent process exit + if (timeoutHandle && typeof timeoutHandle === 'object' && 'unref' in timeoutHandle) { + timeoutHandle.unref(); + } }), ]).catch((error) => { // Clear timeout if it's still active