trying to get jest to exit cleanly when running all tests.

Seems like maybe something in libp2p. Giving up for now.
This commit is contained in:
Ladd Hoffman 2024-12-31 15:26:56 -06:00
parent 52bada4ac8
commit 002c0a9fa9
3 changed files with 17 additions and 5 deletions

View File

@ -122,6 +122,7 @@ export class RhizomeNode {
} }
async stop() { async stop() {
this.peers.stop();
await this.pubSub.stop(); await this.pubSub.stop();
await this.requestReply.stop(); await this.requestReply.stop();
await this.httpServer.stop(); await this.httpServer.stop();

View File

@ -136,6 +136,7 @@ export class Peers {
} }
start() { start() {
// TODO: Move this somewhere that makes more sense
this.rhizomeNode.pubSub.subscribeTopic( this.rhizomeNode.pubSub.subscribeTopic(
this.rhizomeNode.config.pubSubTopic, this.rhizomeNode.config.pubSubTopic,
(sender, msg) => { (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 { addPeer(addr: PeerAddress): Peer {
const peer = new Peer(this.rhizomeNode, addr); const peer = new Peer(this.rhizomeNode, addr);
this.peers.push(peer); this.peers.push(peer);

View File

@ -11,7 +11,6 @@ export type PeerRequest = {
export type RequestHandler = (req: PeerRequest, res: ResponseSocket) => void; 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 { export class RequestSocket {
sock = new Request(); sock = new Request();
@ -33,13 +32,16 @@ export class RequestSocket {
const [res] = await this.sock.receive(); const [res] = await this.sock.receive();
return res; return res;
} }
close() {
this.sock.close();
debug(`[${this.requestReply.rhizomeNode.config.peerId}]`, 'Request socket closed');
}
} }
export class ResponseSocket { export class ResponseSocket {
sock: Reply; constructor(readonly sock: Reply) {}
constructor(sock: Reply) {
this.sock = sock;
}
async send(msg: object | string) { async send(msg: object | string) {
if (typeof msg === 'object') { if (typeof msg === 'object') {
msg = JSON.stringify(msg); msg = JSON.stringify(msg);
@ -101,5 +103,6 @@ export class RequestReply {
await this.replySock.unbind(this.requestBindAddrStr); await this.replySock.unbind(this.requestBindAddrStr);
this.replySock.close(); this.replySock.close();
this.replySock = new Reply(); this.replySock = new Reply();
debug(`[${this.rhizomeNode.config.peerId}]`, 'Reply socket closed');
} }
} }