Skip to content

Instantly share code, notes, and snippets.

@djyde
Created March 24, 2021 13:43
Show Gist options
  • Save djyde/5c2dbdff57514dfe9f381606edf24a9a to your computer and use it in GitHub Desktop.
Save djyde/5c2dbdff57514dfe9f381606edf24a9a to your computer and use it in GitHub Desktop.
lww-test
test("merge - scene#1", async () => {
let cloud = new Graph();
// initial state on cloud
const v1 = cloud.addVertex("v1");
const v2 = cloud.addVertex("v2");
const v3 = cloud.addVertex("v3");
const v4 = cloud.addVertex("v4");
// sync from cloud
let userA = new Graph();
userA = userA.merge(cloud);
let userB = new Graph();
userB = userB.merge(cloud);
expect(userA.adjacencyList).toEqual(cloud.adjacencyList);
expect(userB.adjacencyList).toEqual(cloud.adjacencyList);
// Firstly, userA add a new vertex
const v5 = userA.addVertex("v5");
// In the **same time**, userB connect v1 to v2
userB.addEdge(v1, v2);
await delay();
// userA's network is better than userB now, she sync to the cloud first
cloud = cloud.merge(userA);
// now, cloud should have vertex v5
expect(cloud.isInGraph(v5)).toBe(true);
// userA has vertex v5 too. But she doesn't sync from cloud now
expect(userA.isInGraph(v5)).toBe(true);
await delay();
// userB successfully connected to cloud now, she start to sync
cloud = cloud.merge(userB);
// the data in cloud is the most fresh, it has both of the users changes
expect(cloud.isInGraph(v5)).toBe(true);
expect(cloud.isAdjacency(v1, v2));
// but userA hasn't sync from cloud, so she wouldn't have v1->v2 edge:
expect(userA.adjacencyList).not.toEqual(cloud.adjacencyList);
expect(userA.isAdjacency(v1, v2)).toBe(false);
// userB hasn't sync either, so she wouldn't have v5
expect(userB.isInGraph(v5)).toBe(false);
await delay();
// userA and userB both online now, they both syncing
userA = userA.merge(cloud);
userB = userB.merge(cloud);
// now, both of the users should have both updates:
expect(userA.adjacencyList).toEqual(userB.adjacencyList);
expect(userA.isAdjacency(v1, v2)).toBe(true);
expect(userB.isInGraph(v5)).toBe(true);
await delay();
// user be now can see the v5, she doesn't like this, so she remove it
userB.removeVertex(v5);
await delay();
// but userA doesn't know that, she makes an edge between v1 and v5
userA.addEdge(v1, v5);
await delay();
// userA sync to cloud now
cloud = cloud.merge(userA);
expect(cloud.isAdjacency(v1, v5)).toBe(true);
// userA go on connect v2 to v5
userA.addEdge(v2, v5);
await delay();
// userB now sync
cloud = cloud.merge(userB);
// now, because userB removed the v5, it shouldn't in cloud now
expect(cloud.isInGraph(v5)).toBe(false);
// userA doesn't sync from the cloud yet, so she still can see v5
expect(userA.isInGraph(v5)).toBe(true);
// until she gets online. She will notice that any edges that associated with v5, has gone
userA = userA.merge(cloud);
expect(userA.isInGraph(v5)).toBe(false);
// Now three places are in sync
expect(cloud.adjacencyList).toEqual(userA.adjacencyList)
expect(cloud.adjacencyList).toEqual(userB.adjacencyList);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment