Skip to content

Commit

Permalink
Merge pull request #300 from boostcampwm2023/BE-feature/crdt-test
Browse files Browse the repository at this point in the history
CRDT Tree ํ…Œ์ŠคํŠธ ์ฝ”๋“œ ์ž‘์„ฑ
  • Loading branch information
tnpfldyd authored Dec 13, 2023
2 parents 8270104 + 6510215 commit c51c54c
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 0 deletions.
55 changes: 55 additions & 0 deletions nestjs-BE/server/src/crdt/clock.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { COMPARE, Clock } from './clock';

it('clock ์—ญ์ง๋ ฌํ™”', () => {
const clockCount = 10;

const clock = new Clock('123', clockCount);
const parsedClock = Clock.parse(JSON.stringify(clock));

expect(JSON.stringify(clock)).toEqual(JSON.stringify(parsedClock));
});

it('copy', () => {
const clockCount = 10;

const clock = new Clock('123', clockCount);
const clockCopy = clock.copy();

expect(clock === clockCopy).toBeFalsy();
expect(JSON.stringify(clock)).toEqual(JSON.stringify(clockCopy));
});

it('increment', () => {
const clockCount = 10;
const incrementCount = 5;
const expectedNumber = 15;

const clock = new Clock('123', clockCount);

for (let i = 0; i < incrementCount; i++) clock.increment();

expect(clock.counter).toEqual(expectedNumber);
});

it('merge', () => {
const clockCount10 = 10;
const clockCount15 = 15;

const clock1 = new Clock('123', clockCount10);
const clock2 = new Clock('124', clockCount15);

clock1.merge(clock2);
clock2.merge(clock1);
});

it('compare', () => {
const clockCount10 = 10;
const clockCount25 = 25;

const clock1 = new Clock('123', clockCount10);
const clock2 = new Clock('124', clockCount25);
const clock3 = new Clock('126', clockCount25);

expect(clock1.compare(clock2)).toEqual(COMPARE.LESS);
expect(clock2.compare(clock3)).toEqual(COMPARE.LESS);
});
61 changes: 61 additions & 0 deletions nestjs-BE/server/src/crdt/crdt-tree.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { CrdtTree } from './crdt-tree';

it('crdt tree ๋™๊ธฐํ™”', () => {
const tree1 = new CrdtTree<string>('1');
const tree2 = new CrdtTree<string>('2');

const op_1_1 = tree1.generateOperationAdd('a', 'root', 'hello');
const op_1_2 = tree1.generateOperationAdd('b', 'root', 'hi');

const op_2_1 = tree2.generateOperationAdd('c', 'root', 'good');
const op_2_2 = tree2.generateOperationAdd('d', 'c', 'bad');

tree1.applyOperations([op_1_1, op_1_2]);
tree2.applyOperations([op_2_1, op_2_2]);

tree2.applyOperations([op_1_1, op_1_2]);
tree1.applyOperations([op_2_1, op_2_2]);

const op_1_3 = tree1.generateOperationUpdate('a', 'updatedByTree1');
const op_1_4 = tree1.generateOperationMove('d', 'b');

const op_2_3 = tree2.generateOperationUpdate('a', 'updatedByTree2');
const op_2_4 = tree2.generateOperationMove('a', 'c');

tree1.applyOperations([op_1_3, op_1_4]);
tree2.applyOperations([op_2_3, op_2_4]);

tree2.applyOperations([op_1_3, op_1_4]);
tree1.applyOperations([op_2_3, op_2_4]);

const op_1_5 = tree1.generateOperationDelete('b');

const op_2_5 = tree2.generateOperationUpdate('b', 'updatedByTree2');

tree1.applyOperations([op_1_5]);
tree2.applyOperations([op_2_5]);

tree2.applyOperations([op_1_5]);
tree1.applyOperations([op_2_5]);

tree1.clock.id = tree2.clock.id;

expect(tree1).toMatchObject(tree2);
});

it('crdt tree ์—ญ์ง๋ ฌํ™”', () => {
const tree = new CrdtTree<string>('1');

const op1 = tree.generateOperationAdd('a', 'root', 'hello');
const op2 = tree.generateOperationAdd('b', 'root', 'hi');
const op3 = tree.generateOperationAdd('c', 'root', 'good');
const op4 = tree.generateOperationAdd('d', 'c', 'bad');

tree.applyOperations([op1, op2, op3, op4]);

expect(JSON.stringify(tree));

const parsedTree = CrdtTree.parse<string>(JSON.stringify(tree));

expect(JSON.stringify(tree)).toEqual(JSON.stringify(parsedTree));
});
8 changes: 8 additions & 0 deletions nestjs-BE/server/src/crdt/node.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Node } from './node';

it('node ์—ญ์ง๋ ฌํ™”', () => {
const node = new Node<string>('1', 'root', 'hello');
const parsedNode = Node.parse<string>(JSON.stringify(node));

expect(JSON.stringify(node)).toEqual(JSON.stringify(parsedNode));
});

0 comments on commit c51c54c

Please sign in to comment.