Skip to content

Commit

Permalink
Add tests for connect() and disconnect() (#10009)
Browse files Browse the repository at this point in the history
This PR adds tests for the newly added `connect()` and `disconnect()` functions (see #9439). Tests are added to `@fluidframework/test-end-to-end-tests`.

The tests first check that the connection state is changed when using `connect()`/`disconnect()`. Next, we ensure that op processing is paused after `disconnect()`, and resumes after `connect()`.

Closes #9746
  • Loading branch information
scottn12 authored and sonalivdeshpande committed May 15, 2022
1 parent 59e80e8 commit e9b8ba3
Showing 1 changed file with 111 additions and 0 deletions.
111 changes: 111 additions & 0 deletions packages/test/test-end-to-end-tests/src/test/container.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
TestContainerRuntimeFactory,
ITestObjectProvider,
TestFluidObjectFactory,
timeoutPromise,
} from "@fluidframework/test-utils";
import { ensureFluidResolvedUrl, IAnyDriverError } from "@fluidframework/driver-utils";
import { requestFluidObject } from "@fluidframework/runtime-utils";
Expand All @@ -46,6 +47,8 @@ import { IContainerRuntimeBase } from "@fluidframework/runtime-definitions";
const id = "fluid-test://localhost/containerTest";
const testRequest: IRequest = { url: id };
const codeDetails: IFluidCodeDetails = { package: "test" };
const timeoutMs = 500;

// REVIEW: enable compat testing?
describeNoCompat("Container", (getTestObjectProvider) => {
let provider: ITestObjectProvider;
Expand Down Expand Up @@ -292,4 +295,112 @@ describeNoCompat("Container", (getTestObjectProvider) => {
assert.strictEqual(container.closed, true);
assert.strictEqual(pendingLocalState.url, (container.resolvedUrl as IFluidResolvedUrl).url);
});

it("can call connect() and disconnect() on Container", async () => {
const innerRequestHandler = async (request: IRequest, runtime: IContainerRuntimeBase) =>
runtime.IFluidHandleContext.resolveHandle(request);
const runtimeFactory = (_?: unknown) => new TestContainerRuntimeFactory(
TestDataObjectType,
getDataStoreFactory(),
{},
[innerRequestHandler]);
const localTestObjectProvider = new TestObjectProvider(
Loader,
provider.driver,
runtimeFactory);

const container = await localTestObjectProvider.makeTestContainer() as Container;
await timeoutPromise(
(resolve) => container.once("connected", () => resolve()),
{ durationMs: timeoutMs, errorMsg: "container initial connection timeout" },
);
assert.strictEqual(
container.connectionState, ConnectionState.Connected,
"container is not connected when loaded",
);

let disconnectedEventFired = false;
container.once("disconnected", () => { disconnectedEventFired = true; });
container.disconnect();
assert(disconnectedEventFired, "disconnected event didn't fire when calling container.disconnect");
assert.strictEqual(container.connectionState, ConnectionState.Disconnected, "container can't disconnect()");

container.connect();
await timeoutPromise(
(resolve) => container.once("connected", () => resolve()),
{ durationMs: timeoutMs, errorMsg: "container connect() timeout" },
);
assert.strictEqual(container.connectionState, ConnectionState.Connected, "container can't connect()");
});

it("can control op processing with connect() and disconnect()", async () => {
const innerRequestHandler = async (request: IRequest, runtime: IContainerRuntimeBase) =>
runtime.IFluidHandleContext.resolveHandle(request);
const runtimeFactory = (_?: unknown) => new TestContainerRuntimeFactory(
TestDataObjectType,
getDataStoreFactory(),
{},
[innerRequestHandler]);

const localTestObjectProvider = new TestObjectProvider(
Loader,
provider.driver,
runtimeFactory);

const container1 = await localTestObjectProvider.makeTestContainer() as Container;
await timeoutPromise(
(resolve) => container1.once("connected", () => resolve()),
{ durationMs: timeoutMs, errorMsg: "container1 initial connect timeout" },
);
assert.strictEqual(
container1.connectionState, ConnectionState.Connected,
"container is not connected after connected event fires",
);

const dataObject = await requestFluidObject<ITestDataObject>(container1, "default");
const directory1 = dataObject._root;
directory1.set("key", "value");
let value1 = await directory1.get("key");
assert.strictEqual(value1, "value", "value1 is not set");

const container2 = await localTestObjectProvider.loadTestContainer() as Container;
await timeoutPromise(
(resolve) => container2.once("connected", () => resolve()),
{ durationMs: timeoutMs, errorMsg: "container2 initial connect timeout" },
);
const dataObjectTest = await requestFluidObject<ITestDataObject>(container2, "default");
const directory2 = dataObjectTest._root;
await localTestObjectProvider.ensureSynchronized();
let value2 = await directory2.get("key");
assert.strictEqual(value2, "value", "value2 is not set");

let disconnectedEventFired = false;
container2.once("disconnected", () => { disconnectedEventFired = true; });
container2.disconnect();
assert(disconnectedEventFired, "disconnected event didn't fire when calling container.disconnect");
assert.strictEqual(container2.connectionState, ConnectionState.Disconnected, "container can't disconnect()");

directory1.set("key", "new-value");
value1 = await directory1.get("key");
assert.strictEqual(value1, "new-value", "value1 is not changed");

const valueChangePromise = timeoutPromise(
(resolve) => directory2.once("valueChanged", () => resolve()),
{ durationMs: timeoutMs, errorMsg: "valueChanged timeout (expected error)" },
);
await assert.rejects(
valueChangePromise,
"valueChanged event fired while disconnected",
);
value2 = await directory2.get("key");
assert.notStrictEqual(value1, value2, "container2 processing ops after disconnect()");

container2.connect();
await timeoutPromise(
(resolve) => directory2.once("valueChanged", () => resolve()),
{ durationMs: timeoutMs, errorMsg: "valueChanged timeout after connect()" },
);
value2 = await directory2.get("key");
assert.strictEqual(value1, value2, "container2 not processing ops after connect()");
});
});

0 comments on commit e9b8ba3

Please sign in to comment.