-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #300 from boostcampwm2023/BE-feature/crdt-test
CRDT Tree ํ ์คํธ ์ฝ๋ ์์ฑ
- Loading branch information
Showing
3 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
}); |