diff --git a/packages/core/src/__tests__/unit/application-lifecycle.unit.ts b/packages/core/src/__tests__/unit/application-lifecycle.unit.ts index 5e8b48053fd1..b9e627c9dd2e 100644 --- a/packages/core/src/__tests__/unit/application-lifecycle.unit.ts +++ b/packages/core/src/__tests__/unit/application-lifecycle.unit.ts @@ -140,6 +140,14 @@ describe('Application life cycle', () => { expect(app.state).to.equal('created'); }); + it('allows application.stop when it is initialized', async () => { + const app = new Application(); + await app.init(); + expect(app.state).to.equal('initialized'); + await app.stop(); + expect(app.state).to.equal('stopped'); + }); + it('await application.stop when it is stopping', async () => { const app = new Application(); await app.start(); diff --git a/packages/core/src/application.ts b/packages/core/src/application.ts index 6e42c1b4ec5d..8d26547fb055 100644 --- a/packages/core/src/application.ts +++ b/packages/core/src/application.ts @@ -88,8 +88,8 @@ export class Application extends Context implements LifeCycleObserver { * - !started -> starting -> started * - started -> started (no-op) * 2. stop - * - started -> stopping -> stopped - * - !started -> stopped (no-op) + * - (started | initialized) -> stopping -> stopped + * - ! (started || initialized) -> stopped (no-op) * * Two types of states are expected: * - stable, such as `started` and `stopped` @@ -399,7 +399,7 @@ export class Application extends Context implements LifeCycleObserver { if (this._state === 'stopping') return this.awaitState('stopped'); this.assertNotInProcess('stop'); // No-op if it's created or stopped - if (this._state !== 'started') return; + if (this._state !== 'started' && this._state !== 'initialized') return; this.setState('stopping'); if (!this._isShuttingDown) { // Explicit stop is called, let's remove signal listeners to avoid