Skip to content

Commit

Permalink
Added unit tests for DDS to test refereneced routes
Browse files Browse the repository at this point in the history
  • Loading branch information
agarwal-navin committed Oct 28, 2020
1 parent c8fbfc6 commit 365bb67
Show file tree
Hide file tree
Showing 9 changed files with 474 additions and 23 deletions.
63 changes: 61 additions & 2 deletions packages/dds/cell/src/test/cell.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ describe("Cell", () => {
cell.set("testValue");
assert.equal(cell.get(), "testValue", "Could not retrieve cell value");

const services = MockSharedObjectServices.createFromTree(cell.snapshot());
const services = MockSharedObjectServices.createFromTree(cell.snapshot().snapshot);
const cell2 = new SharedCell("cell2", dataStoreRuntime, CellFactory.Attributes);
await cell2.load("branchId", services);

Expand All @@ -72,7 +72,7 @@ describe("Cell", () => {
const containerRuntimeFactory = new MockContainerRuntimeFactory();
const dataStoreRuntime2 = new MockFluidDataStoreRuntime();
const containerRuntime2 = containerRuntimeFactory.createContainerRuntime(dataStoreRuntime2);
const services2 = MockSharedObjectServices.createFromTree(cell.snapshot());
const services2 = MockSharedObjectServices.createFromTree(cell.snapshot().snapshot);
services2.deltaConnection = containerRuntime2.createDeltaConnection();

const cell2 = new SharedCell("cell2", dataStoreRuntime2, CellFactory.Attributes);
Expand Down Expand Up @@ -156,6 +156,65 @@ describe("Cell", () => {
assert.equal(cell.get(), undefined, "Could not delete cell value");
assert.equal(cell2.get(), undefined, "Could not delete cell value from remote client");
});

it("can generate referenced routes for handles", () => {
const factory = new CellFactory();
const subCell = factory.create(dataStoreRuntime, "subCell");
cell.set(subCell.handle);

containerRuntimeFactory.processAllMessages();

// Verify the referenced routes returned by snapshot.
const routeDetails = cell2.snapshot().routeDetails;
assert.strictEqual(routeDetails.source, cell2.id, "Source of the referenced routes should be cell's id");
assert.deepStrictEqual(
routeDetails.routes, [subCell.handle.absolutePath], "Referenced routes is incorrect");
});

it("can generate referenced routes for nested handles", () => {
const factory = new CellFactory();
const subCell = factory.create(dataStoreRuntime, "subCell");
const subCell2 = factory.create(dataStoreRuntime, "subCell2");
const containingObject = {
subcellHandle: subCell.handle,
nestedObj: {
subcell2Handle: subCell2.handle,
},
};
cell.set(containingObject);

containerRuntimeFactory.processAllMessages();

// Verify the referenced routes returned by snapshot.
const routeDetails = cell2.snapshot().routeDetails;
assert.strictEqual(routeDetails.source, cell2.id, "Source of the referenced routes should be cell's id");
assert.deepStrictEqual(
routeDetails.routes,
[subCell.handle.absolutePath, subCell2.handle.absolutePath],
"Referenced routes is incorrect");
});

it("can generate referenced routes for removed handles", () => {
const factory = new CellFactory();
const subCell = factory.create(dataStoreRuntime, "subCell");
cell.set(subCell.handle);

containerRuntimeFactory.processAllMessages();

// Verify the referenced routes returned by snapshot.
let routeDetails = cell2.snapshot().routeDetails;
assert.strictEqual(routeDetails.source, cell2.id, "Source of the referenced routes should be cell's id");
assert.deepStrictEqual(
routeDetails.routes, [subCell.handle.absolutePath], "Referenced routes is incorrect");

// Verify that removed handle updates referenced routes correctly.
cell.delete();
containerRuntimeFactory.processAllMessages();

routeDetails = cell2.snapshot().routeDetails;
assert.strictEqual(routeDetails.source, cell2.id, "Source of the referenced routes should be cell's id");
assert.deepStrictEqual(routeDetails.routes, [], "Referenced routes is incorrect");
});
});

describe("SharedCell reconnection flow", () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/dds/counter/src/test/counter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ describe("SharedCounter", () => {
testCounter.increment(-10);

// Load a new SharedCounter from the snapshot of the first one.
const services = MockSharedObjectServices.createFromTree(testCounter.snapshot());
const services = MockSharedObjectServices.createFromTree(testCounter.snapshot().snapshot);
const testCounter2 = factory.create(dataStoreRuntime, "counter2") as SharedCounter;
await testCounter2.load("branchId", services);

Expand Down
4 changes: 2 additions & 2 deletions packages/dds/ink/src/test/ink.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ describe("Ink", () => {
ink.appendPointToStroke(inkPoint, strokeId);

// Load a new Ink from the snapshot of the first one.
const services = MockSharedObjectServices.createFromTree(ink.snapshot());
const services = MockSharedObjectServices.createFromTree(ink.snapshot().snapshot);
const ink2 = new Ink(dataStoreRuntime, "ink2", InkFactory.Attributes);
await ink2.load("branchId", services);

Expand All @@ -121,7 +121,7 @@ describe("Ink", () => {
const containerRuntimeFactory = new MockContainerRuntimeFactory();
const dataStoreRuntime2 = new MockFluidDataStoreRuntime();
const containerRuntime2 = containerRuntimeFactory.createContainerRuntime(dataStoreRuntime2);
const services2 = MockSharedObjectServices.createFromTree(ink.snapshot());
const services2 = MockSharedObjectServices.createFromTree(ink.snapshot().snapshot);
services2.deltaConnection = containerRuntime2.createDeltaConnection();

const ink2 = new Ink(dataStoreRuntime2, "ink2", InkFactory.Attributes);
Expand Down
88 changes: 84 additions & 4 deletions packages/dds/map/src/test/directory.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ async function populate(directory: SharedDirectory, content: object) {
}

function serialize(directory: SharedDirectory): string {
const tree = directory.snapshot();
const tree = directory.snapshot().snapshot;
assert(tree.entries.length === 1);
assert(tree.entries[0].path === "header");
assert(tree.entries[0].type === TreeEntry.Blob);
Expand Down Expand Up @@ -114,6 +114,15 @@ describe("Directory", () => {
// eslint-disable-next-line max-len
const expected = `{"storage":{"first":{"type":"Plain","value":"second"},"third":{"type":"Plain","value":"fourth"},"fifth":{"type":"Plain","value":"sixth"},"object":{"type":"Plain","value":{"type":"__fluid_handle__","url":"${subMapHandleUrl}"}}}}`;
assert.equal(serialized, expected);

// Verify the referenced routes returned by snapshot.
const routeDetails = directory.snapshot().routeDetails;
assert.strictEqual(
routeDetails.source, directory.id, "Source of the referenced routes should be directory's id");
assert.deepStrictEqual(
routeDetails.routes,
[subMap.handle.absolutePath],
"Referenced routes is incorrect");
});

it("Should serialize a directory with subdirectories as a JSON object", () => {
Expand Down Expand Up @@ -288,7 +297,7 @@ describe("Directory", () => {
nestedDirectory.set("deepKey1", "deepValue1");
nestedDirectory.set("long2", logWord2);

const tree = directory.snapshot();
const tree = directory.snapshot().snapshot;
assert(tree.entries.length === 3);
assert(tree.entries[0].path === "blob0");
assert(tree.entries[1].path === "blob1");
Expand Down Expand Up @@ -335,7 +344,7 @@ describe("Directory", () => {
const containerRuntimeFactory = new MockContainerRuntimeFactory();
const dataStoreRuntime2 = new MockFluidDataStoreRuntime();
const containerRuntime2 = containerRuntimeFactory.createContainerRuntime(dataStoreRuntime2);
const services2 = MockSharedObjectServices.createFromTree(directory.snapshot());
const services2 = MockSharedObjectServices.createFromTree(directory.snapshot().snapshot);
services2.deltaConnection = containerRuntime2.createDeltaConnection();

const directory2 = new SharedDirectory("directory2", dataStoreRuntime2, DirectoryFactory.Attributes);
Expand Down Expand Up @@ -378,7 +387,7 @@ describe("Directory", () => {
const containerRuntimeFactory = new MockContainerRuntimeFactory();
const dataStoreRuntime2 = new MockFluidDataStoreRuntime();
const containerRuntime2 = containerRuntimeFactory.createContainerRuntime(dataStoreRuntime2);
const services2 = MockSharedObjectServices.createFromTree(directory.snapshot());
const services2 = MockSharedObjectServices.createFromTree(directory.snapshot().snapshot);
services2.deltaConnection = containerRuntime2.createDeltaConnection();

const directory2 = new SharedDirectory("directory2", dataStoreRuntime2, DirectoryFactory.Attributes);
Expand Down Expand Up @@ -1035,6 +1044,77 @@ describe("Directory", () => {
}
assert.ok(expectedDirectories2.size === 0);
});

it("can generate referenced routes for handles in subdirectories", () => {
const subMap = mapFactory.create(dataStoreRuntime, "subMap");
directory.set("object", subMap.handle);

const fooDirectory = directory.createSubDirectory("foo");
const subMap2 = mapFactory.create(dataStoreRuntime, "subMap2");
fooDirectory.set("object", subMap2.handle);

const barDirectory = fooDirectory.createSubDirectory("bar");
const subMap3 = mapFactory.create(dataStoreRuntime, "subMap3");
barDirectory.set("object", subMap3.handle);

const bazDirectory = barDirectory.createSubDirectory("baz");
const subMap4 = mapFactory.create(dataStoreRuntime, "subMap4");
bazDirectory.set("object", subMap4.handle);

// Verify the referenced routes returned by snapshot.
const routeDetails = directory.snapshot().routeDetails;
assert.strictEqual(
routeDetails.source, directory.id, "Source of the referenced routes should be directory's id");
assert.deepStrictEqual(
routeDetails.routes,
[
subMap.handle.absolutePath,
subMap2.handle.absolutePath,
subMap3.handle.absolutePath,
subMap4.handle.absolutePath,
],
"Referenced routes is incorrect");
});

it("can generate referenced routes for removed handles in subdirectories", () => {
const subMap = mapFactory.create(dataStoreRuntime, "subMap");
directory.set("object", subMap.handle);

const fooDirectory = directory.createSubDirectory("foo");
const subMap2 = mapFactory.create(dataStoreRuntime, "subMap2");
fooDirectory.set("object", subMap2.handle);

const barDirectory = fooDirectory.createSubDirectory("bar");
const subMap3 = mapFactory.create(dataStoreRuntime, "subMap3");
barDirectory.set("object", subMap3.handle);

// Verify the referenced routes returned by snapshot.
let routeDetails = directory.snapshot().routeDetails;
assert.strictEqual(
routeDetails.source, directory.id, "Source of the referenced routes should be directory's id");
assert.deepStrictEqual(
routeDetails.routes,
[
subMap.handle.absolutePath,
subMap2.handle.absolutePath,
subMap3.handle.absolutePath,
],
"Referenced routes is incorrect");

// Verify that removed handle updates referenced routes correctly.
fooDirectory.delete("object");

routeDetails = directory.snapshot().routeDetails;
assert.strictEqual(
routeDetails.source, directory.id, "Source of the referenced routes should be directory's id");
assert.deepStrictEqual(
routeDetails.routes,
[
subMap.handle.absolutePath,
subMap3.handle.absolutePath,
],
"Referenced routes is incorrect");
});
});
});
});
69 changes: 66 additions & 3 deletions packages/dds/map/src/test/map.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ describe("Map", () => {
it("new serialization format for small maps", async () => {
map.set("key", "value");

const tree = map.snapshot();
const tree = map.snapshot().snapshot;
assert(tree.entries.length === 1);
const content = JSON.stringify({
blobs: [],
Expand Down Expand Up @@ -177,7 +177,7 @@ describe("Map", () => {
map.set("longValue", longString);
map.set("zzz", "the end");

const tree = map.snapshot();
const tree = map.snapshot().snapshot;
assert(tree.entries.length === 2);
const content1 = JSON.stringify({
blobs: ["blob0"],
Expand Down Expand Up @@ -245,7 +245,7 @@ describe("Map", () => {
const containerRuntimeFactory = new MockContainerRuntimeFactory();
const dataStoreRuntime2 = new MockFluidDataStoreRuntime();
const containerRuntime2 = containerRuntimeFactory.createContainerRuntime(dataStoreRuntime2);
const services2 = MockSharedObjectServices.createFromTree(map.snapshot());
const services2 = MockSharedObjectServices.createFromTree(map.snapshot().snapshot);
services2.deltaConnection = containerRuntime2.createDeltaConnection();

const map2 = new SharedMap("testMap2", dataStoreRuntime2, MapFactory.Attributes);
Expand Down Expand Up @@ -456,5 +456,68 @@ describe("Map", () => {
assert.equal(await waitP2, "resolved", "promise not resolved after key is available in remote map");
});
});

describe("Referenced routes", () => {
it("can generate referenced routes for handles", () => {
const subMap = factory.create(dataStoreRuntime, "subMap");
map.set("object", subMap.handle);

containerRuntimeFactory.processAllMessages();

// Verify the referenced routes returned by snapshot.
const routeDetails = map2.snapshot().routeDetails;
assert.strictEqual(routeDetails.source, map2.id, "Source of the referenced routes should be map's id");
assert.deepStrictEqual(
routeDetails.routes, [subMap.handle.absolutePath], "Referenced routes is incorrect");
});

it("can generate referenced routes for nested handles", () => {
const subMap = factory.create(dataStoreRuntime, "subMap");
const subMap2 = factory.create(dataStoreRuntime, "subMap2");
const containingObject = {
subMapHandle: subMap.handle,
nestedObj: {
subMap2Handle: subMap2.handle,
},
};
map.set("object", containingObject);

containerRuntimeFactory.processAllMessages();

// Verify the referenced routes returned by snapshot.
const routeDetails = map2.snapshot().routeDetails;
assert.strictEqual(
routeDetails.source, map2.id, "Source of the referenced routes should be map's id");
assert.deepStrictEqual(
routeDetails.routes,
[subMap.handle.absolutePath, subMap2.handle.absolutePath],
"Referenced routes is incorrect");
});

it("can generate referenced routes for removed handles", () => {
const subMap = factory.create(dataStoreRuntime, "subMap");
const subMap2 = factory.create(dataStoreRuntime, "subMap2");
map.set("object", subMap.handle);
map.set("object2", subMap2.handle);

containerRuntimeFactory.processAllMessages();

// Verify the referenced routes returned by snapshot.
let routeDetails = map2.snapshot().routeDetails;
assert.strictEqual(routeDetails.source, map2.id, "Source of the referenced routes should be map's id");
assert.deepStrictEqual(
routeDetails.routes,
[subMap.handle.absolutePath, subMap2.handle.absolutePath],
"Referenced routes is incorrect");

map.delete("object");
containerRuntimeFactory.processAllMessages();

routeDetails = map2.snapshot().routeDetails;
assert.strictEqual(routeDetails.source, map2.id, "Source of the referenced routes should be map's id");
assert.deepStrictEqual(
routeDetails.routes, [subMap2.handle.absolutePath], "Referenced routes is incorrect");
});
});
});
});
Loading

0 comments on commit 365bb67

Please sign in to comment.