-
Notifications
You must be signed in to change notification settings - Fork 536
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove
existing
property - Part 4, IRuntimeFactory + RuntimeFactory…
…Helper (#6604) See #3429 optional argument for IRuntimeFactory.instantiateRuntime to replace the existing state RuntimeFactoryHelper is to be used by current clients of IRuntimeFactory (see RFC - remove use of the existing property across (some) layers #6486 for examples) in a future PR, after the version bump in container definitions. This class now holds the wrapper logic about how the runtime is actually instantiated. * Support existing in IRuntimeFactory, add abstract helper class for IRuntimeFactory * Add test infra * Add small test * Simplify mocks * Use partials to mock IContainerContext, add more tests * Rename tests, make precedence test more explicit * Remove comment * Add some param comments * Update api.md * remove marker property * Remove redundant check * Some PR feedback, make abstract class generic * Type refactoring * Fix test * Fix test typing * Fix cast
- Loading branch information
Showing
5 changed files
with
132 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
packages/runtime/runtime-utils/src/runtimeFactoryHelper.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/*! | ||
* Copyright (c) Microsoft Corporation and contributors. All rights reserved. | ||
* Licensed under the MIT License. | ||
*/ | ||
|
||
import { | ||
IContainerContext, | ||
IRuntime, | ||
IRuntimeFactory, | ||
} from "@fluidframework/container-definitions"; | ||
import { IContainerRuntime } from "@fluidframework/container-runtime-definitions"; | ||
|
||
export abstract class RuntimeFactoryHelper<T = IContainerRuntime> implements IRuntimeFactory { | ||
public get IRuntimeFactory() { return this; } | ||
|
||
public async instantiateRuntime( | ||
context: IContainerContext, | ||
existing?: boolean, | ||
): Promise<IRuntime> { | ||
const fromExisting = existing === undefined | ||
? context.existing === true | ||
: existing; | ||
const runtime = await this.preInitialize(context, fromExisting); | ||
|
||
if (fromExisting) { | ||
await this.instantiateFromExisting(runtime); | ||
} else { | ||
await this.instantiateFirstTime(runtime); | ||
} | ||
|
||
await this.hasInitialized(runtime); | ||
return runtime; | ||
} | ||
|
||
public abstract preInitialize(context: IContainerContext, existing: boolean): Promise<IRuntime & T>; | ||
public async instantiateFirstTime(_runtime: T): Promise<void> {} | ||
public async instantiateFromExisting(_runtime: T): Promise<void> {} | ||
public async hasInitialized(_runtime: T): Promise<void> {} | ||
} |
85 changes: 85 additions & 0 deletions
85
packages/runtime/runtime-utils/src/test/runtimeFactoryHelper.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/*! | ||
* Copyright (c) Microsoft Corporation and contributors. All rights reserved. | ||
* Licensed under the MIT License. | ||
*/ | ||
|
||
import { IContainerContext, IRuntime } from "@fluidframework/container-definitions"; | ||
import { IContainerRuntime } from "@fluidframework/container-runtime-definitions"; | ||
import Sinon from "sinon"; | ||
import { RuntimeFactoryHelper } from "../runtimeFactoryHelper"; | ||
|
||
class TestRuntimeFactoryHelper extends RuntimeFactoryHelper { | ||
constructor( | ||
private readonly runtime: IRuntime & IContainerRuntime, | ||
) { | ||
super(); | ||
} | ||
|
||
public async preInitialize( | ||
_context: IContainerContext, | ||
_existing: boolean, | ||
): Promise<IRuntime & IContainerRuntime> { | ||
return this.runtime; | ||
} | ||
} | ||
|
||
describe("RuntimeFactoryHelper", () => { | ||
const sandbox: Sinon.SinonSandbox = Sinon.createSandbox(); | ||
const context: Partial<IContainerContext> = {}; | ||
const runtime: Partial<IRuntime & IContainerRuntime> = {}; | ||
let helper: TestRuntimeFactoryHelper; | ||
let unit: Sinon.SinonMock; | ||
|
||
beforeEach(() => { | ||
helper = new TestRuntimeFactoryHelper(runtime as IRuntime & IContainerRuntime); | ||
unit = sandbox.mock(helper); | ||
unit.expects("preInitialize").once(); | ||
unit.expects("hasInitialized").once(); | ||
}); | ||
|
||
afterEach(() => { | ||
sandbox.restore(); | ||
}); | ||
|
||
it("Instantiate when existing flag is `true`", async () => { | ||
unit.expects("instantiateFirstTime").never(); | ||
unit.expects("instantiateFromExisting").once(); | ||
await helper.instantiateRuntime(context as IContainerContext, /* existing */ true); | ||
|
||
unit.verify(); | ||
}); | ||
|
||
it("Instantiate when existing flag is `false`", async () => { | ||
unit.expects("instantiateFirstTime").once(); | ||
unit.expects("instantiateFromExisting").never(); | ||
await helper.instantiateRuntime(context as IContainerContext, /* existing */ false); | ||
|
||
unit.verify(); | ||
}); | ||
|
||
it("Instantiate when existing flag is unset", async () => { | ||
unit.expects("instantiateFirstTime").once(); | ||
unit.expects("instantiateFromExisting").never(); | ||
await helper.instantiateRuntime(context as IContainerContext); | ||
|
||
unit.verify(); | ||
}); | ||
|
||
it("Instantiate when existing flag is unset and context is existing", async () => { | ||
const existingContext: Partial<IContainerContext> = { existing: true }; | ||
unit.expects("instantiateFirstTime").never(); | ||
unit.expects("instantiateFromExisting").once(); | ||
await helper.instantiateRuntime(existingContext as IContainerContext); | ||
|
||
unit.verify(); | ||
}); | ||
|
||
it("Instantiate when existing flag takes precedence over context", async () => { | ||
const existingContext: Partial<IContainerContext> = { existing: true }; | ||
unit.expects("instantiateFirstTime").once(); | ||
unit.expects("instantiateFromExisting").never(); | ||
await helper.instantiateRuntime(existingContext as IContainerContext, /* existing */ false); | ||
|
||
unit.verify(); | ||
}); | ||
}); |