Skip to content

Commit

Permalink
test: add disconnect tests
Browse files Browse the repository at this point in the history
  • Loading branch information
marcus-pousette committed Nov 6, 2024
1 parent d6b4d16 commit 97ee354
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
19 changes: 13 additions & 6 deletions packages/transport/libp2p-test-utils/src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,23 +88,30 @@ export class TestSession<T> {
const result = async () => {
const definedOptions: Libp2pOptions<T> | 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<T>({
addresses: {
listen: listen(),
},
connectionManager: definedOptions?.connectionManager ?? {},
connectionManager: definedOptions?.connectionManager,
privateKey: definedOptions?.privateKey,
datastore: definedOptions?.datastore,
transports: definedOptions?.transports ?? transports(),
connectionMonitor: {
enabled: false,
},

services: {
relay: relay(),
identify: identify(),
...definedOptions?.services,
} as any,
services,
connectionEncrypters: [noise()],
streamMuxers: definedOptions?.streamMuxers || [yamux()],
start: definedOptions?.start,
Expand Down
19 changes: 19 additions & 0 deletions packages/transport/stream/test/stream.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down

0 comments on commit 97ee354

Please sign in to comment.