diff --git a/packages/transport/libp2p-test-utils/src/session.ts b/packages/transport/libp2p-test-utils/src/session.ts index 63e3edd34..7cde71ade 100644 --- a/packages/transport/libp2p-test-utils/src/session.ts +++ b/packages/transport/libp2p-test-utils/src/session.ts @@ -88,11 +88,22 @@ export class TestSession { const result = async () => { const definedOptions: Libp2pOptions | undefined = (options as any)?.[i] || options; + + const services: any = { + identify: identify(), + ...definedOptions?.services, + }; + if (definedOptions?.services?.relay !== null) { + services.relay = relay(); + } else { + delete services.relay; + } + const node = await createLibp2p({ addresses: { listen: listen(), }, - connectionManager: definedOptions?.connectionManager ?? {}, + connectionManager: definedOptions?.connectionManager, privateKey: definedOptions?.privateKey, datastore: definedOptions?.datastore, transports: definedOptions?.transports ?? transports(), @@ -100,11 +111,7 @@ export class TestSession { enabled: false, }, - services: { - relay: relay(), - identify: identify(), - ...definedOptions?.services, - } as any, + services, connectionEncrypters: [noise()], streamMuxers: definedOptions?.streamMuxers || [yamux()], start: definedOptions?.start, diff --git a/packages/transport/stream/test/stream.spec.ts b/packages/transport/stream/test/stream.spec.ts index 46cb6632d..ae40b525a 100644 --- a/packages/transport/stream/test/stream.spec.ts +++ b/packages/transport/stream/test/stream.spec.ts @@ -3239,6 +3239,25 @@ describe("start/stop", () => { await session.peers[0].stop(); await session.peers[0].start(); }); + + it("streams are pruned on disconnect", async () => { + // https://github.com/libp2p/js-libp2p/issues/2794 + session = await disconnected(2, { + services: { + relay: null, + directstream: (c: any) => new TestDirectStream(c), + }, + } as any); + await session.connect([[session.peers[0], session.peers[1]]]); + await waitForResolved(() => + expect(session.peers[0].services.directstream.peers.size).to.equal(1), + ); + + await session.peers[0].hangUp(session.peers[1].peerId); + await waitForResolved(() => + expect(session.peers[0].services.directstream.peers.size).to.equal(0), + ); + }); }); describe("multistream", () => {