Skip to content

Commit

Permalink
connection: Rename close() to _closeStream()
Browse files Browse the repository at this point in the history
  • Loading branch information
sonnyp committed Jan 8, 2025
1 parent 2877a7b commit 31bb31c
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 30 deletions.
4 changes: 2 additions & 2 deletions packages/connection/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ class Connection extends EventEmitter {
async _end() {
let el;
try {
el = await this.close();
el = await this._closeStream();
} catch {}

try {
Expand Down Expand Up @@ -311,7 +311,7 @@ class Connection extends EventEmitter {
* https://xmpp.org/rfcs/rfc6120.html#streams-close
* https://tools.ietf.org/html/rfc7395#section-3.6
*/
async close(timeout = this.timeout) {
async _closeStream(timeout = this.timeout) {
const fragment = this.footer(this.footerElement());

const p = Promise.all([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ test("timeout", async () => {
expect(el).toBe("<hello/>");
});

await expect(conn.close()).rejects.toThrow(new TimeoutError());
await expect(conn._closeStream()).rejects.toThrow(new TimeoutError());
});

test("error on status closing", async () => {
Expand All @@ -47,7 +47,7 @@ test("error on status closing", async () => {

conn.parser.emit("end");

await expect(conn.close()).rejects.toThrow(
await expect(conn._closeStream()).rejects.toThrow(
new Error("Connection is closing"),
);
});
Expand All @@ -69,7 +69,7 @@ test("resolves", async () => {
expect(el).toBe("<hello/>");
});

const promiseClose = conn.close();
const promiseClose = conn._closeStream();
conn.parser.emit("end", xml("goodbye"));

const el = await promiseClose;
Expand All @@ -91,7 +91,7 @@ test("emits closing status", () => {

const p = Promise.all([
promise(conn, "status").then((status) => expect(status).toBe("closing")),
conn.close(),
conn._closeStream(),
]);

conn.parser.emit("end");
Expand All @@ -115,6 +115,6 @@ test("do not emit closing status if parser property is missing", async () => {
expect(timeout(promise(conn, "status"), 500)).rejects.toThrow(
new TimeoutError(),
),
expect(conn.close()).rejects.toThrow(),
expect(conn._closeStream()).rejects.toThrow(),
]);
});
28 changes: 16 additions & 12 deletions packages/connection/test/end.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,35 @@ import Connection from "../index.js";
test("#_end", async () => {
const conn = new Connection();

const spy_close = jest.spyOn(conn, "close");
const spy_closeStream = jest.spyOn(conn, "_closeStream");
const spy_closeSocket = jest.spyOn(conn, "_closeSocket");

await conn._end();

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

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

const spy_close = jest.spyOn(conn, "close").mockImplementation(() => {
return Promise.reject();
});
const spy_closeStream = jest
.spyOn(conn, "_closeStream")
.mockImplementation(() => {
return Promise.reject();
});
const spy_closeSocket = jest.spyOn(conn, "_closeSocket");

await conn._end();

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

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

const spy_close = jest.spyOn(conn, "close");
const spy_closeStream = jest.spyOn(conn, "_closeStream");
const spy_closeSocket = jest
.spyOn(conn, "_closeSocket")
.mockImplementation(() => {
Expand All @@ -38,16 +40,18 @@ test("#_end with disconnect rejection", async () => {

await conn._end();

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

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

const spy_close = jest.spyOn(conn, "close").mockImplementation(() => {
return Promise.reject();
});
const spy_closeStream = jest
.spyOn(conn, "_closeStream")
.mockImplementation(() => {
return Promise.reject();
});
const spy_closeSocket = jest
.spyOn(conn, "_closeSocket")
.mockImplementation(() => {
Expand All @@ -56,6 +60,6 @@ test("#_end with close and disconnect rejection", async () => {

await conn._end();

expect(spy_close).toHaveBeenCalledTimes(1);
expect(spy_closeStream).toHaveBeenCalledTimes(1);
expect(spy_closeSocket).toHaveBeenCalledTimes(1);
});
16 changes: 8 additions & 8 deletions packages/connection/test/stop.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ test("resolves if socket property is undefined", async () => {
expect().pass();
});

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

test("resolves if disconnect rejects", async () => {
test("resolves if _closeSocket rejects", async () => {
const conn = new Connection();
conn.disconnect = () => Promise.reject();
conn.close = () => Promise.resolve();
conn._closeStream = () => Promise.resolve();
conn._closeSocket = () => Promise.reject();
await conn.stop();
expect().pass();
});
Expand All @@ -29,8 +29,8 @@ test("resolves with the result of close", async () => {
const conn = new Connection();
conn.socket = {};
const el = {};
conn.close = () => Promise.resolve(el);
conn.disconnect = () => Promise.resolve();
conn._closeStream = () => Promise.resolve(el);
conn._closeSocket = () => Promise.resolve();
expect(await conn.stop()).toBe(el);
});

Expand Down
6 changes: 3 additions & 3 deletions packages/tls/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ NebQHyTBqa5P7vjSioiWiSRCNOIL4HywMWtN/nZVk0cl8zwlLtMaGt9Tz7ty2OgL
const error = await promise(conn, "error");
expect(error.message).toBe("certificate has expired");

await conn.close().catch(() => {});
await conn._closeSocket().catch(() => {});
server.close();
});

Expand All @@ -111,7 +111,7 @@ test("rejects self signed certificates", async () => {
const error = await promise(conn, "error");
expect(error.code).toBe("DEPTH_ZERO_SELF_SIGNED_CERT");

await conn.close().catch(() => {});
await conn._closeSocket().catch(() => {});
server.close();
});

Expand Down Expand Up @@ -213,6 +213,6 @@ yA==
// which is what we want as it delays the sending of the stream header (conn.open)
expect(connect_emitted_on_conn_socket).toBe(true);

await conn.close().catch(() => {});
await conn._closeSocket().catch(() => {});
server.close();
});

0 comments on commit 31bb31c

Please sign in to comment.