Skip to content

Commit

Permalink
we dont need forceReconnect or socket.destory anymore
Browse files Browse the repository at this point in the history
  • Loading branch information
sonnyp committed Jan 15, 2025
1 parent ced2057 commit 8935336
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 33 deletions.
15 changes: 0 additions & 15 deletions packages/connection/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,21 +245,6 @@ class Connection extends EventEmitter {
await promise(this.socket, "close", "error", timeout);
}

/**
* Forcibly disconnects the socket
* https://xmpp.org/rfcs/rfc6120.html#streams-close
* https://tools.ietf.org/html/rfc7395#section-3.6
*/
async forceDisconnect(timeout = this.timeout) {
if (!this.socket) return;

this._status("disconnecting");
this.socket.destroy();

// The 'disconnect' status is set by the socket 'close' listener
await promise(this.socket, "close", "error", timeout);
}

/**
* Opens the stream
*/
Expand Down
2 changes: 1 addition & 1 deletion packages/stream-management/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ export default function streamManagement({
clearTimeout(timeoutTimeout);
if (sm.timeout) {
timeoutTimeout = setTimeout(
() => entity.forceDisconnect().catch(),
() => entity.disconnect().catch(() => {}),
sm.timeout,
);
}
Expand Down
4 changes: 0 additions & 4 deletions packages/tls/lib/Socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,6 @@ class Socket extends EventEmitter {
this.socket.end();
}

destroy() {
this.socket.destroy();
}

write(data, fn) {
this.socket.write(data, fn);
}
Expand Down
4 changes: 0 additions & 4 deletions packages/websocket/lib/Socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,6 @@ export default class Socket extends EventEmitter {
this.socket.close();
}

destroy() {
this.socket.close();
}

write(data, fn) {
function done(err) {
if (!fn) return;
Expand Down
22 changes: 13 additions & 9 deletions test/stream-management.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,12 @@ test("client fail stanzas", async () => {
await xmpp.start();
// Expect send but don't actually send to server, so it will fail
await xmpp.streamManagement.outbound_q.push({
stanza: <iq to={domain} id="ping">
<ping xmlns="urn:xmppp:ping" />
</iq>,
stamp: datetime()
stanza: (
<iq to={domain} id="ping">
<ping xmlns="urn:xmppp:ping" />
</iq>
),
stamp: datetime(),
});
await xmpp.stop();

Expand All @@ -68,18 +70,20 @@ test("client retry stanzas", async () => {
await xmpp.start();
// Add to queue but don't actually send so it can retry after disconnect
await xmpp.streamManagement.outbound_q.push({
stanza: <iq to={domain} id="ping">
<ping xmlns="urn:xmppp:ping" />
</iq>,
stamp: datetime()
stanza: (
<iq to={domain} id="ping">
<ping xmlns="urn:xmppp:ping" />
</iq>
),
stamp: datetime(),
});
await xmpp.disconnect();

const el = await elP;
expect(el.attrs.id).toEqual("ping");
});

test("client reconnect automatically", async () => {
test("client reconnects when server fails to ack", async () => {
await server.enableModules(["smacks"]);
await server.restart();

Expand Down
112 changes: 112 additions & 0 deletions test/stream-management.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
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 promise_ack = promise(xmpp.streamManagement, "ack");
await xmpp.start();
await xmpp.send(
<iq to={domain} id="ping" type="get">
<ping xmlns="urn:xmppp:ping" />
</iq>,
);

const el = await promise_ack;
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 promise_fail = 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: (
<iq to={domain} id="ping" type="get">
<ping xmlns="urn:xmppp:ping" />
</iq>
),
stamp: datetime(),
});
await xmpp.stop();

const el = await promise_fail;
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 promise_ack = 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: (
<iq to={domain} id="ping" type="get">
<ping xmlns="urn:xmppp:ping" />
</iq>
),
stamp: datetime(),
});
await xmpp.disconnect();

const el = await promise_ack;
expect(el.attrs.id).toEqual("ping");
});

test(
"client reconnects when server fails to ack stanza",
async () => {
await server.enableModules(["smacks"]);
await server.restart();

xmpp = client({ credentials, service: domain });
xmpp.streamManagement.timeout = 10;
xmpp.streamManagement.debounceAckRequest = 1;
debug(xmpp, true);

const promise_resumed = promise(xmpp.streamManagement, "resumed");
await xmpp.start();
xmpp.send(
<iq to={domain} id="ping" type="get">
<ping xmlns="urn:xmppp:ping" />
</iq>,
);
// Pretend we don't receive the ack by removing event listeners
// on the socket
xmpp._detachSocket();

await promise_resumed;
expect().pass();
},
1000 * 10,
);

0 comments on commit 8935336

Please sign in to comment.