Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for session error and close #61

Merged
merged 1 commit into from
Jul 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
});
});
});