Skip to content

Commit

Permalink
Fix IModelHost onAfterStartup event and automatic shutdown. (#1043) (#…
Browse files Browse the repository at this point in the history
…1048)

* Fix IModelHost onAfterStartup event and automatic shutdown.

* Add a comment about use of this in IModelHost.shutdown

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
(cherry picked from commit df26983)

Co-authored-by: Bill Goehrig <33036725+wgoehrig@users.noreply.github.com>
  • Loading branch information
mergify[bot] and wgoehrig committed Mar 26, 2021
1 parent 518127d commit 3f224a3
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"changes": [
{
"packageName": "@bentley/imodeljs-backend",
"comment": "",
"type": "none"
}
],
"packageName": "@bentley/imodeljs-backend",
"email": "33036725+wgoehrig@users.noreply.github.com"
}
6 changes: 4 additions & 2 deletions core/backend/src/IModelHost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,7 @@ export class IModelHost {

UsageLoggingUtilities.configure({ hostApplicationId: IModelHost.applicationId, hostApplicationVersion: IModelHost.applicationVersion, clientAuthManager: this._clientAuthIntrospectionManager });
process.once("beforeExit", IModelHost.shutdown);
IModelHost.onAfterStartup.raiseEvent();
}

private static _briefcaseCacheDir: string;
Expand Down Expand Up @@ -495,10 +496,11 @@ export class IModelHost {

/** This method must be called when an iModel.js services is shut down. Raises [[onBeforeShutdown]] */
public static async shutdown(): Promise<void> {
if (!this._isValid)
// NB: This method is set as a node listener where `this` is unbound
if (!IModelHost._isValid)
return;

this._isValid = false;
IModelHost._isValid = false;
IModelHost.onBeforeShutdown.raiseEvent();
IModelHost.platform.shutdown();
IModelHost.configuration = undefined;
Expand Down
30 changes: 30 additions & 0 deletions core/backend/src/test/standalone/IModelHost.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ import { BriefcaseManager } from "../../BriefcaseManager";
import { RpcRegistry } from "@bentley/imodeljs-common";
import { IModelTestUtils } from "../IModelTestUtils";
import { Schemas } from "../../Schema";
import sinon = require("sinon");

describe("IModelHost", () => {

afterEach(async () => {
sinon.restore();
// Restore the backend to the initial state.
await IModelTestUtils.shutdownBackend();
await IModelTestUtils.startBackend();
Expand All @@ -37,6 +39,34 @@ describe("IModelHost", () => {
expect(Schemas.getRegisteredSchema("Functional")).to.exist;
});

it("should raise onAfterStartup events", async () => {
await IModelTestUtils.shutdownBackend();

const eventHandler = sinon.spy();
IModelHost.onAfterStartup.addOnce(eventHandler);
const promise = IModelHost.startup();
expect(eventHandler.called).to.be.false;
await promise;
expect(eventHandler.calledOnce).to.be.true;
});

it("should raise onBeforeShutdown events", async () => {
const eventHandler = sinon.spy();
IModelHost.onBeforeShutdown.addOnce(eventHandler);
await IModelTestUtils.shutdownBackend();
expect(eventHandler.calledOnce).to.be.true;
});

it("should auto-shutdown on process beforeExit event", async () => {
expect(IModelHost.isValid).to.be.true;
const eventHandler = sinon.spy();
IModelHost.onBeforeShutdown.addOnce(eventHandler);
process.emit("beforeExit", 0);
await new Promise((resolve) => setImmediate(resolve));
expect(eventHandler.calledOnce).to.be.true;
expect(IModelHost.isValid).to.be.false;
});

it("should set the briefcase cache directory to expected locations", async () => {
// Shutdown IModelHost to allow this test to use it.
await IModelTestUtils.shutdownBackend();
Expand Down

0 comments on commit 3f224a3

Please sign in to comment.