Skip to content

Commit

Permalink
connection: Make disconnect a public function
Browse files Browse the repository at this point in the history
  • Loading branch information
sonnyp committed Jan 8, 2025
1 parent 31bb31c commit 32206c0
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 52 deletions.
13 changes: 8 additions & 5 deletions packages/connection/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class Connection extends EventEmitter {
);
} catch {}

return this._end();
return this.disconnect();
}

_onData(data) {
Expand Down Expand Up @@ -107,7 +107,7 @@ class Connection extends EventEmitter {
if (isStreamError) {
// "Stream Errors Are Unrecoverable"
// "The entity that receives the stream error then SHALL close the stream"
this._end();
this.disconnect();
}
}

Expand Down Expand Up @@ -211,15 +211,18 @@ class Connection extends EventEmitter {
}
}

async _end() {
async disconnect() {
let el;
try {
el = await this._closeStream();
} catch {}
} catch (err) {
console.log(err);
}

try {
await this._closeSocket();
} catch (err) {
console.log(err);
this.#onSocketClosed(true, err);
}

Expand Down Expand Up @@ -301,7 +304,7 @@ class Connection extends EventEmitter {
* https://tools.ietf.org/html/rfc7395#section-3.6
*/
async stop() {
const el = await this._end();
const el = await this.disconnect();
this._status("offline", el);
return el;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import Connection from "../index.js";

test("#_end", async () => {
test("disconnect", async () => {
const conn = new Connection();

const spy_closeStream = jest.spyOn(conn, "_closeStream");
const el = {};
const spy_closeStream = jest
.spyOn(conn, "_closeStream")
.mockImplementation(async () => el);
const spy_closeSocket = jest.spyOn(conn, "_closeSocket");

await conn._end();
expect(await conn.disconnect()).toBe(el);

expect(spy_closeStream).toHaveBeenCalledTimes(1);
expect(spy_closeSocket).toHaveBeenCalledTimes(1);
});

test("#_end with close rejection", async () => {
test("disconnect with _closeStream rejection", async () => {
const conn = new Connection();

const spy_closeStream = jest
Expand All @@ -22,13 +25,13 @@ test("#_end with close rejection", async () => {
});
const spy_closeSocket = jest.spyOn(conn, "_closeSocket");

await conn._end();
await conn.disconnect();

expect(spy_closeStream).toHaveBeenCalledTimes(1);
expect(spy_closeSocket).toHaveBeenCalledTimes(1);
});

test("#_end with disconnect rejection", async () => {
test("disconnect with _closeSocket rejection", async () => {
const conn = new Connection();

const spy_closeStream = jest.spyOn(conn, "_closeStream");
Expand All @@ -38,13 +41,13 @@ test("#_end with disconnect rejection", async () => {
return Promise.reject();
});

await conn._end();
await conn.disconnect();

expect(spy_closeStream).toHaveBeenCalledTimes(1);
expect(spy_closeSocket).toHaveBeenCalledTimes(1);
});

test("#_end with close and disconnect rejection", async () => {
test("disconnect with _closeStream and _closeSocket rejections", async () => {
const conn = new Connection();

const spy_closeStream = jest
Expand All @@ -58,8 +61,22 @@ test("#_end with close and disconnect rejection", async () => {
return Promise.reject();
});

await conn._end();
await conn.disconnect();

expect(spy_closeStream).toHaveBeenCalledTimes(1);
expect(spy_closeSocket).toHaveBeenCalledTimes(1);
});

test("resolves if socket property is undefined", async () => {
const conn = new Connection();
conn.footerElement = () => <foo />;
conn.socket = undefined;
await conn.disconnect();
expect().pass();
});

test("does not reject if connection is not established", async () => {
const conn = new Connection();
await conn.disconnect();
expect().pass();
});
4 changes: 2 additions & 2 deletions packages/connection/test/onElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ test("#_onElement stream:error", (done) => {
application,
]);
const conn = new Connection();
conn._end = () => {
jest.spyOn(conn, "disconnect").mockImplementation(() => {
done();
return Promise.resolve();
};
});

conn.on("element", (el) => {
expect(el).toBe(foo);
Expand Down
48 changes: 15 additions & 33 deletions packages/connection/test/stop.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,25 @@
import Connection from "../index.js";
import { EventEmitter } from "@xmpp/events";
import Connection from "../index.js";

test("resolves if socket property is undefined", async () => {
test("stop", async () => {
const conn = new Connection();
conn.footerElement = () => <foo />;
conn.socket = undefined;
await conn.stop();
expect().pass();
});

test("resolves if _closeStream rejects", async () => {
const conn = new Connection();
conn._closeStream = () => Promise.reject();
conn._closeSocket = () => Promise.resolve();
await conn.stop();
expect().pass();
});
const close_el = {};
const spy_disconnect = jest
.spyOn(conn, "disconnect")
.mockImplementation(async () => {
return close_el;
});
const spy_status = jest.spyOn(conn, "_status");

test("resolves if _closeSocket rejects", async () => {
const conn = new Connection();
conn._closeStream = () => Promise.resolve();
conn._closeSocket = () => Promise.reject();
await conn.stop();
expect().pass();
});
conn.status = "online";

test("resolves with the result of close", async () => {
const conn = new Connection();
conn.socket = {};
const el = {};
conn._closeStream = () => Promise.resolve(el);
conn._closeSocket = () => Promise.resolve();
expect(await conn.stop()).toBe(el);
});

test("does not throw if connection is not established", async () => {
const conn = new Connection();
await conn.stop();
expect().pass();

expect(spy_disconnect).toHaveBeenCalledTimes(1);
expect(spy_status).toHaveBeenCalledTimes(1);
expect(spy_status).toHaveBeenCalledWith("offline", close_el);
expect(conn.status).toBe("offline");
});

// https://github.com/xmppjs/xmpp.js/issues/956
Expand Down
4 changes: 2 additions & 2 deletions packages/connection/test/streamError.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import xml from "@xmpp/xml";
test("#_streamError", async () => {
const conn = new Connection();

const spy_end = jest.spyOn(conn, "_end");
const spy_disconnect = jest.spyOn(conn, "disconnect");
const spy_send = jest.spyOn(conn, "send");

await conn._streamError("foo-bar");

expect(spy_end).toHaveBeenCalled();
expect(spy_disconnect).toHaveBeenCalled();

expect(spy_send).toHaveBeenCalledWith(
xml("stream:error", {}, [
Expand Down
2 changes: 1 addition & 1 deletion test/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ test("statuses", async () => {
]);

// trigger reconnect
await xmpp._closeSocket();
await xmpp.disconnect();

statuses = [xmpp.status];
await promise(xmpp, "open");
Expand Down

0 comments on commit 32206c0

Please sign in to comment.