Skip to content

Commit

Permalink
e2e test auto reconnect
Browse files Browse the repository at this point in the history
Needs to use force-disconnect especially if we emulate an issue using pause
  • Loading branch information
singpolyma committed Jan 13, 2025
1 parent bb3998a commit 4736120
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 1 deletion.
15 changes: 15 additions & 0 deletions packages/connection/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,21 @@ 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 @@ -170,7 +170,7 @@ export default function streamManagement({
clearTimeout(timeoutTimeout);
if (sm.timeout) {
timeoutTimeout = setTimeout(
() => entity.disconnect().catch(),
() => entity.forceDisconnect().catch(),

Check warning on line 173 in packages/stream-management/index.js

View workflow job for this annotation

GitHub Actions / test (20)

Promise.catch() requires 1 argument, but received 0

Check warning on line 173 in packages/stream-management/index.js

View workflow job for this annotation

GitHub Actions / test (22)

Promise.catch() requires 1 argument, but received 0

Check warning on line 173 in packages/stream-management/index.js

View workflow job for this annotation

GitHub Actions / test (23)

Promise.catch() requires 1 argument, but received 0
sm.timeout,
);
}
Expand Down
4 changes: 4 additions & 0 deletions packages/tls/lib/Socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ class Socket extends EventEmitter {
this.socket.end();
}

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

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

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

write(data, fn) {
if (WebSocket === WS) {
this.socket.send(data, fn);
Expand Down
22 changes: 22 additions & 0 deletions test/stream-management.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,25 @@ test("client retry stanzas", async () => {
const el = await elP;
expect(el.attrs.id).toEqual("ping");
});

test("client reconnect automatically", 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(
<iq to={domain} id="ping">
<ping xmlns="urn:xmppp:ping" />
</iq>,
);
xmpp.socket.socket.pause();

await resumedP;
expect().pass();
});

0 comments on commit 4736120

Please sign in to comment.