fix(rustproxy): Use cooperative cancellation for background tasks, prune stale caches and metric entries, and switch tests to dynamic port allocation to avoid port conflicts

This commit is contained in:
2026-02-24 20:56:37 +00:00
parent 755c81c042
commit 33cd5330c4
24 changed files with 535 additions and 560 deletions
+15 -10
View File
@@ -2,15 +2,19 @@ import { expect, tap } from '@git.zone/tstest/tapbundle';
import * as net from 'net';
import { SmartProxy } from '../ts/index.js';
import type { IRouteConfig } from '../ts/index.js';
import { findFreePorts, assertPortsFree } from './helpers/port-allocator.js';
let proxy: SmartProxy;
let PORT: number;
tap.test('setup socket handler test', async () => {
[PORT] = await findFreePorts(1);
// Create a simple socket handler route
const routes: IRouteConfig[] = [{
name: 'echo-handler',
match: {
ports: 47780
match: {
ports: PORT
// No domains restriction - matches all connections
},
action: {
@@ -43,11 +47,11 @@ tap.test('should handle socket with custom function', async () => {
let response = '';
await new Promise<void>((resolve, reject) => {
client.connect(47780, 'localhost', () => {
client.connect(PORT, 'localhost', () => {
console.log('Client connected to proxy');
resolve();
});
client.on('error', reject);
});
@@ -78,7 +82,7 @@ tap.test('should handle async socket handler', async () => {
// Update route with async handler
await proxy.updateRoutes([{
name: 'async-handler',
match: { ports: 47780 },
match: { ports: PORT },
action: {
type: 'socket-handler',
socketHandler: async (socket, context) => {
@@ -108,12 +112,12 @@ tap.test('should handle async socket handler', async () => {
});
await new Promise<void>((resolve, reject) => {
client.connect(47780, 'localhost', () => {
client.connect(PORT, 'localhost', () => {
// Send initial data to trigger the handler
client.write('test data\n');
resolve();
});
client.on('error', reject);
});
@@ -131,7 +135,7 @@ tap.test('should handle errors in socket handler', async () => {
// Update route with error-throwing handler
await proxy.updateRoutes([{
name: 'error-handler',
match: { ports: 47780 },
match: { ports: PORT },
action: {
type: 'socket-handler',
socketHandler: (socket, context) => {
@@ -148,12 +152,12 @@ tap.test('should handle errors in socket handler', async () => {
});
await new Promise<void>((resolve, reject) => {
client.connect(47780, 'localhost', () => {
client.connect(PORT, 'localhost', () => {
// Connection established - send data to trigger handler
client.write('trigger\n');
resolve();
});
client.on('error', () => {
// Ignore client errors - we expect the connection to be closed
});
@@ -168,6 +172,7 @@ tap.test('should handle errors in socket handler', async () => {
tap.test('cleanup', async () => {
await proxy.stop();
await assertPortsFree([PORT]);
});
export default tap.start();