From 002c0a9fa9261b62160337870a17cebf5fac86c9 Mon Sep 17 00:00:00 2001 From: Ladd Date: Tue, 31 Dec 2024 15:26:56 -0600 Subject: [PATCH] trying to get jest to exit cleanly when running all tests. Seems like maybe something in libp2p. Giving up for now. --- src/node.ts | 1 + src/peers.ts | 8 ++++++++ src/request-reply.ts | 13 ++++++++----- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/node.ts b/src/node.ts index 775fd8f..546b9cb 100644 --- a/src/node.ts +++ b/src/node.ts @@ -122,6 +122,7 @@ export class RhizomeNode { } async stop() { + this.peers.stop(); await this.pubSub.stop(); await this.requestReply.stop(); await this.httpServer.stop(); diff --git a/src/peers.ts b/src/peers.ts index 99b7fe1..db5f32e 100644 --- a/src/peers.ts +++ b/src/peers.ts @@ -136,6 +136,7 @@ export class Peers { } start() { + // TODO: Move this somewhere that makes more sense this.rhizomeNode.pubSub.subscribeTopic( this.rhizomeNode.config.pubSubTopic, (sender, msg) => { @@ -147,6 +148,13 @@ export class Peers { ); } + stop() { + debug(`[${this.rhizomeNode.config.peerId}]`, 'Closing all peer request sockets'); + for (const peer of this.peers) { + peer.reqSock?.close(); + } + } + addPeer(addr: PeerAddress): Peer { const peer = new Peer(this.rhizomeNode, addr); this.peers.push(peer); diff --git a/src/request-reply.ts b/src/request-reply.ts index 73a8814..940776c 100644 --- a/src/request-reply.ts +++ b/src/request-reply.ts @@ -11,7 +11,6 @@ export type PeerRequest = { export type RequestHandler = (req: PeerRequest, res: ResponseSocket) => void; -// TODO: Retain handle to request socket for each peer, so we only need to open once export class RequestSocket { sock = new Request(); @@ -33,13 +32,16 @@ export class RequestSocket { const [res] = await this.sock.receive(); return res; } + + close() { + this.sock.close(); + debug(`[${this.requestReply.rhizomeNode.config.peerId}]`, 'Request socket closed'); + } } export class ResponseSocket { - sock: Reply; - constructor(sock: Reply) { - this.sock = sock; - } + constructor(readonly sock: Reply) {} + async send(msg: object | string) { if (typeof msg === 'object') { msg = JSON.stringify(msg); @@ -101,5 +103,6 @@ export class RequestReply { await this.replySock.unbind(this.requestBindAddrStr); this.replySock.close(); this.replySock = new Reply(); + debug(`[${this.rhizomeNode.config.peerId}]`, 'Reply socket closed'); } }