Skip to content

Commit

Permalink
Add tests for session error and close (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
ramya-rao-a authored Jul 28, 2020
1 parent 6819ad2 commit 86cad30
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 3 deletions.
13 changes: 10 additions & 3 deletions lib/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,9 @@ export class Session extends Entity {
* - **Rejects** the promise with an AmqpError when rhea emits the "session_error" event while trying
* to close an amqp session.
*/
close(): Promise<void> {
this.removeAllListeners();
return new Promise<void>((resolve, reject) => {
async close(): Promise<void> {

const closePromise = new Promise<void>((resolve, reject) => {
log.error("[%s] The amqp session '%s' is open ? -> %s", this.connection.id, this.id, this.isOpen());
if (this.isOpen()) {
let onError: Func<RheaEventContext, void>;
Expand Down Expand Up @@ -203,6 +203,13 @@ export class Session extends Entity {
return resolve();
}
});

try {
await closePromise;
} finally {
this.removeAllListeners();
}

}

/**
Expand Down
56 changes: 56 additions & 0 deletions test/session.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,61 @@ describe("Session", () => {
// Open the session.
session.begin();
});

it("sessionClose", (done: Function) => {
const session = new Session(
connection,
connection["_connection"].create_session()
);

session.on(SessionEvents.sessionOpen, async () => {
await session.close();
assert.strictEqual(session.listeners(SessionEvents.sessionOpen).length, 0);
assert.strictEqual(session.listeners(SessionEvents.sessionClose).length, 0);
});

session.on(SessionEvents.sessionClose, (event) => {
assert.exists(event, "Expected an AMQP event.");

done();
});

// Open the session.
session.begin();

assert.strictEqual(session.listeners(SessionEvents.sessionOpen).length, 1);
assert.strictEqual(session.listeners(SessionEvents.sessionClose).length, 1);
});

it("sessionError", (done: Function) => {
const errorCondition = "amqp:connection:forced";
const errorDescription = "testing error on close";
mockService.on(
rhea.SessionEvents.sessionOpen,
(context: rhea.EventContext) => {
context.session?.close({
condition: errorCondition,
description: errorDescription,
});
}
);

const session = new Session(
connection,
connection["_connection"].create_session()
);

session.on(SessionEvents.sessionError, async (event) => {
assert.exists(event, "Expected an AMQP event.");
const error = event.session?.error as rhea.ConnectionError;
assert.exists(error, "Expected an AMQP error.");
assert.strictEqual(error.condition, errorCondition);
assert.strictEqual(error.description, errorDescription);
await session.close();
done();
});

session.begin();
});
});
});

0 comments on commit 86cad30

Please sign in to comment.