Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: compare & merge bug GCounter & PNCounter #73

Merged
merged 6 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions packages/crdt/src/builtins/GCounter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,20 @@ export class GCounter {
}
return true;
}

d-roak marked this conversation as resolved.
Show resolved Hide resolved
merge(peerCounter: GCounter): void {
let temp: { [nodeKey: string]: number } = Object.assign(
{},
this.counts,
peerCounter.counts,
);

Object.keys(temp).forEach((key) => {
this.counts[key] = Math.max(this.counts[key], peerCounter.counts[key]);

d-roak marked this conversation as resolved.
Show resolved Hide resolved
});

this.globalCounter = Object.values(this.counts).reduce((a, b) => a + b, 0);

d-roak marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
69 changes: 69 additions & 0 deletions packages/crdt/tests/GCounter.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { describe, test, expect, beforeEach } from "vitest";
import { GCounter } from "../src/builtins/GCounter";

describe("G-Counter Tests", () => {

d-roak marked this conversation as resolved.
Show resolved Hide resolved
let set1: GCounter;
let set2: GCounter;

beforeEach(() => {
set1 = new GCounter({ "node1": 5, "node2": 10});
set2 = new GCounter({ "node1": 5, "node2": 10});
});

test("Test Initial Values", () => {
expect(set1.value()).toBe(15);
expect(set2.value()).toBe(15);
});

test("Test Increment", () => {

d-roak marked this conversation as resolved.
Show resolved Hide resolved
set1.increment("node1", 10);
set1.increment("node2", 5);

expect(set1.value()).toBe(30);
});

test("Test Compare", () => {

d-roak marked this conversation as resolved.
Show resolved Hide resolved
expect(set1.compare(set2)).toBe(true);

set2.increment("node1", 5);

expect(set1.compare(set2)).toBe(false);

Check failure on line 33 in packages/crdt/tests/GCounter.test.ts

View workflow job for this annotation

GitHub Actions / tests

packages/crdt/tests/GCounter.test.ts > G-Counter Tests > Test Compare

AssertionError: expected true to be false // Object.is equality - Expected + Received - false + true ❯ packages/crdt/tests/GCounter.test.ts:33:36

let set3 = new GCounter({ "node1": 5, "node2": 10, "node3": 15 });

expect(set1.compare(set3)).toBe(false);
});

test("Test Merge", () => {

d-roak marked this conversation as resolved.
Show resolved Hide resolved
const counter1 = new GCounter({ "node1": 5 });
const counter2 = new GCounter({ "node2": 10 });

counter1.merge(counter2);

expect(counter1.counts).toEqual({ "node1": 5, "node2": 10 });

Check failure on line 47 in packages/crdt/tests/GCounter.test.ts

View workflow job for this annotation

GitHub Actions / tests

packages/crdt/tests/GCounter.test.ts > G-Counter Tests > Test Merge

AssertionError: expected { node1: NaN, node2: NaN } to deeply equal { node1: 5, node2: 10 } - Expected + Received Object { - "node1": 5, - "node2": 10, + "node1": NaN, + "node2": NaN, } ❯ packages/crdt/tests/GCounter.test.ts:47:33
expect(counter1.value()).toBe(15);

set1.increment("node1", 5);
set2.increment("node2", 10);
expect(set1.value()).toBe(35);
expect(set2.value()).toBe(40);
set1.merge(set2);
expect(set1.value()).toBe(45);
expect(set1.counts[0]).toBe(10);
expect(set1.counts[1]).toBe(20);
expect(set1.counts[2]).toBe(15);

set2.merge(set1);
expect(set2.value()).toBe(45);
expect(set2.counts[0]).toBe(10);
expect(set2.counts[1]).toBe(20);
expect(set2.counts[2]).toBe(15);

expect(set2.compare(set1)).toBe(true);
});

d-roak marked this conversation as resolved.
Show resolved Hide resolved
});
60 changes: 60 additions & 0 deletions packages/crdt/tests/PNCounter.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { describe, test, expect, beforeEach, afterEach } from "vitest";
import { PNCounter } from "../src/builtins/PNCounter";
import { GCounter } from "../src/builtins/GCounter";

describe("PN-Counter Tests", () => {

d-roak marked this conversation as resolved.
Show resolved Hide resolved
let set1: PNCounter;
let set2: PNCounter;

beforeEach(() => {
set1 = new PNCounter(new GCounter({ "node1": 5, "node2": 10, "node3": 15 }), new GCounter({ "node1": 3, "node2": 4, "node3": 3 }));
set2 = new PNCounter(new GCounter({ "node1": 5, "node2": 10, "node3": 15 }), new GCounter({ "node1": 3, "node2": 4, "node3": 3 }));
});

test("Test Initial Value", () => {
expect(set1.value()).toBe(20);
expect(set2.value()).toBe(20);
});

test("Test Increment", () => {
set1.increment("node1",10);
set2.increment("node1",20);
expect(set1.value()).toBe(30);
expect(set2.value()).toBe(40);
});

test("Test Decrement", () => {
set1.decrement("node1",10);
set2.decrement("node1",20);
expect(set1.value()).toBe(10);
expect(set2.value()).toBe(0);
});

test("Test Compare", () => {
expect(set1.compare(set2)).toBe(true);

set1.decrement("node1",10);

expect(set1.compare(set2)).toBe(false);

Check failure on line 39 in packages/crdt/tests/PNCounter.test.ts

View workflow job for this annotation

GitHub Actions / tests

packages/crdt/tests/PNCounter.test.ts > PN-Counter Tests > Test Compare

AssertionError: expected true to be false // Object.is equality - Expected + Received - false + true ❯ packages/crdt/tests/PNCounter.test.ts:39:36

set2.decrement("node1",10);

expect(set1.compare(set2)).toBe(true);
});

test("Test Merge", () => {
set1.increment("node1",10);
set2.decrement("node2",5);

expect(set1.compare(set2)).toBe(false);

Check failure on line 50 in packages/crdt/tests/PNCounter.test.ts

View workflow job for this annotation

GitHub Actions / tests

packages/crdt/tests/PNCounter.test.ts > PN-Counter Tests > Test Merge

AssertionError: expected true to be false // Object.is equality - Expected + Received - false + true ❯ packages/crdt/tests/PNCounter.test.ts:50:36
expect(set2.compare(set1)).toBe(false);

set1.merge(set2);
set2.merge(set1);

expect(set1.compare(set2)).toBe(true);
expect(set2.compare(set1)).toBe(true);
});

d-roak marked this conversation as resolved.
Show resolved Hide resolved
});
Loading