fix: address cleanup review lifecycle and connection settings

This commit is contained in:
Boki 2026-10-26 16:53:53 -04:00
parent 512f2f1c32
commit 9d3e1a7bb9
11 changed files with 264 additions and 17 deletions

View file

@ -94,3 +94,54 @@ test('successful initialization clears timeout handles immediately', async () =>
clearTimeout(timer);
}
});
test('reusing a manager after shutdown closes each new connection lifetime once', async () => {
const connect = mock(async () => {}),
disconnect = mock(async () => {});
const c = container({ cache: { connect, disconnect } });
const manager = new ServiceLifecycleManager();
await manager.initializeServices(c);
await manager.shutdownServices(c);
await manager.shutdownServices(c);
await manager.initializeServices(c);
await manager.shutdownServices(c);
expect(connect).toHaveBeenCalledTimes(2);
expect(disconnect).toHaveBeenCalledTimes(2);
});
test('hung rollback does not hide or indefinitely delay the original startup failure', async () => {
const pending = deferred();
const failure = new Error('startup failed');
const disconnect = mock(() => pending.promise);
const c = container({
cache: { connect: async () => {}, disconnect },
mongoClient: {
connect: async () => {
throw failure;
},
},
});
const manager = new ServiceLifecycleManager();
try {
await expect(manager.initializeServices(c, 5)).rejects.toBe(failure);
expect(disconnect).toHaveBeenCalledTimes(1);
} finally {
pending.resolve();
}
await manager.shutdownServices(c);
expect(disconnect).toHaveBeenCalledTimes(1);
});
test('a retry cannot overlap an earlier unresolved initializer after a timeout', async () => {
const pending = deferred();
const connect = mock(() => pending.promise),
disconnect = mock(async () => {});
const c = container({ cache: { connect, disconnect } });
const manager = new ServiceLifecycleManager();
await expect(manager.initializeServices(c, 5)).rejects.toThrow('timed out');
await expect(manager.initializeServices(c, 5)).rejects.toThrow('already in progress');
expect(connect).toHaveBeenCalledTimes(1);
pending.resolve();
await Bun.sleep(0);
expect(disconnect).toHaveBeenCalledTimes(1);
});

View file

@ -88,6 +88,14 @@ test('PostgreSQL nondefault fields reach wire configuration and Mongo retains UR
connectionTimeoutMillis: 4321,
idleTimeoutMillis: 8765,
});
for (const key of [
'query_timeout',
'statement_timeout',
'lock_timeout',
'idle_in_transaction_session_timeout',
] as const) {
expect(wire[key]).toBeUndefined();
}
const mongo = c.resolve('mongoClient') as unknown as {
config: { uri: string; poolSettings: { maxPoolSize: number } };
};
@ -148,3 +156,53 @@ test('all-disabled default composition needs neither store settings nor connecti
await c.dispose();
}
});
test('Redis ACL username reaches cache and both queue connection settings without opening sockets', async () => {
// Isolate the transport substitution: Bun module mocks otherwise leak between suites.
const child = Bun.spawn(
[
process.execPath,
'-e',
String.raw`
import {mock} from 'bun:test';
const captured=[];
class Redis {
constructor(options){captured.push(options);}
on(){return this;}
disconnect(){}
async quit(){}
}
mock.module('ioredis',()=>({default:Redis,Redis,Cluster:Redis}));
const {ServiceContainerBuilder}=await import('./libs/core/di/src/container/builder.ts');
const c=await new ServiceContainerBuilder().withConfig({
name:'redis-auth-test',version:'1.0.0',service:{name:'redis-auth-test',port:3000},log:{level:'fatal'},
database:{dragonfly:{enabled:true,host:'redis.fixture.test',username:'research-reader',password:'fixture-only'},
mongodb:{enabled:false,uri:'mongodb://localhost/test',database:'test'},
postgres:{enabled:false,database:'test',user:'test',password:'fixture-only'},questdb:{enabled:false}},
}).withOptions({enableBrowser:false,enableProxy:false}).skipInitialization().build();
c.resolve('cache');c.resolve('globalCache');
const queue=c.resolve('queueManager');
process.stdout.write('__redis_capture__'+JSON.stringify({captured,queue:queue.redisConnection})+'\n');
await c.dispose();
`,
],
{ stdout: 'pipe', stderr: 'pipe' }
);
const [stdout, stderr, code] = await Promise.all([
new Response(child.stdout).text(),
new Response(child.stderr).text(),
child.exited,
]);
if (code !== 0) throw new Error(`Redis transport probe failed: ${stderr}`);
const line = stdout.split('\n').find(line => line.startsWith('__redis_capture__'));
expect(line).toBeDefined();
const result = JSON.parse(line!.slice('__redis_capture__'.length));
expect(result.captured).toHaveLength(2);
for (const config of [...result.captured, result.queue]) {
expect(config).toMatchObject({
host: 'redis.fixture.test',
username: 'research-reader',
password: 'fixture-only',
});
}
});