From 143116065c872072d9731c0d79399e0aa65f8581 Mon Sep 17 00:00:00 2001 From: Sonny Piers Date: Fri, 17 Jan 2025 00:00:31 +0100 Subject: [PATCH] f --- packages/stream-management/index.js | 16 ++--- test/stream-management.js | 106 ---------------------------- test/stream-management.test.js | 2 +- 3 files changed, 8 insertions(+), 116 deletions(-) delete mode 100644 test/stream-management.js diff --git a/packages/stream-management/index.js b/packages/stream-management/index.js index f7b25774..056d335f 100644 --- a/packages/stream-management/index.js +++ b/packages/stream-management/index.js @@ -66,7 +66,6 @@ export default function streamManagement({ entity.on("disconnect", () => { clearTimeout(timeoutTimeout); clearTimeout(requestAckTimeout); - sm.removeListener("stanza", scheduleRequestAck); }); async function resumed(resumed) { @@ -80,6 +79,12 @@ export default function streamManagement({ entity._ready(true); } + function failed() { + sm.enabled = false; + sm.id = ""; + failQueue(); + } + function ackQueue(n) { const oldOutbound = sm.outbound; for (let i = 0; i < +n - oldOutbound; i++) { @@ -97,18 +102,11 @@ export default function streamManagement({ sm.outbound = 0; } - function failed() { - sm.enabled = false; - sm.id = ""; - failQueue(); - } - function enabled({ id, max }) { sm.enabled = true; sm.id = id; sm.max = max; scheduleRequestAck(); - sm.on("stanza", scheduleRequestAck); } entity.on("online", () => { @@ -129,8 +127,8 @@ export default function streamManagement({ }); middleware.use((context, next) => { - clearTimeout(timeoutTimeout); const { stanza } = context; + clearTimeout(timeoutTimeout); if (["presence", "message", "iq"].includes(stanza.name)) { sm.inbound += 1; } else if (stanza.is("r", NS)) { diff --git a/test/stream-management.js b/test/stream-management.js deleted file mode 100644 index ef66faed..00000000 --- a/test/stream-management.js +++ /dev/null @@ -1,106 +0,0 @@ -import { client } from "../packages/client/index.js"; -import { promise } from "../packages/events/index.js"; -import { datetime } from "../packages/time/index.js"; -import debug from "../packages/debug/index.js"; -import server from "../server/index.js"; - -const username = "client"; -const password = "foobar"; -const credentials = { username, password }; -const domain = "localhost"; - -let xmpp; - -afterEach(async () => { - await xmpp?.stop(); - await server.reset(); -}); - -test("client ack stanzas", async () => { - await server.enableModules(["smacks"]); - await server.restart(); - - xmpp = client({ credentials, service: domain }); - debug(xmpp); - - const elP = promise(xmpp.streamManagement, "ack"); - await xmpp.start(); - await xmpp.send( - - - , - ); - - const el = await elP; - expect(el.attrs.id).toEqual("ping"); -}); - -test("client fail stanzas", async () => { - await server.enableModules(["smacks"]); - await server.restart(); - - xmpp = client({ credentials, service: domain }); - debug(xmpp); - - const elP = promise(xmpp.streamManagement, "fail"); - await xmpp.start(); - // Expect send but don't actually send to server, so it will fail - await xmpp.streamManagement.outbound_q.push({ - stanza: ( - - - - ), - stamp: datetime(), - }); - await xmpp.stop(); - - const el = await elP; - expect(el.attrs.id).toEqual("ping"); -}); - -test("client retry stanzas", async () => { - await server.enableModules(["smacks"]); - await server.restart(); - - xmpp = client({ credentials, service: domain }); - debug(xmpp); - - const elP = promise(xmpp.streamManagement, "ack"); - await xmpp.start(); - // Add to queue but don't actually send so it can retry after disconnect - await xmpp.streamManagement.outbound_q.push({ - stanza: ( - - - - ), - stamp: datetime(), - }); - await xmpp.disconnect(); - - const el = await elP; - expect(el.attrs.id).toEqual("ping"); -}); - -test("client reconnects when server fails to ack", async () => { - await server.enableModules(["smacks"]); - await server.restart(); - - xmpp = client({ credentials, service: domain }); - xmpp.streamManagement.timeout = 10; - xmpp.streamManagement.debounceAckRequest = 1; - debug(xmpp); - - const resumedP = promise(xmpp.streamManagement, "resumed"); - await xmpp.start(); - await xmpp.send( - - - , - ); - xmpp.socket.socket.pause(); - - await resumedP; - expect().pass(); -}); diff --git a/test/stream-management.test.js b/test/stream-management.test.js index 3033ded8..ce328a0b 100644 --- a/test/stream-management.test.js +++ b/test/stream-management.test.js @@ -94,7 +94,7 @@ test( xmpp = client({ credentials, service: domain }); xmpp.streamManagement.timeout = 10; xmpp.streamManagement.debounceAckRequest = 1; - debug(xmpp, true); + debug(xmpp); const promise_resumed = promise(xmpp.streamManagement, "resumed"); await xmpp.start();