|
| 1 | +import CRDT from './index'; |
| 2 | +import LinkedList, { RemoteInsertOperation } from './linked-list'; |
| 3 | +import { Node } from './node'; |
| 4 | + |
| 5 | +/** |
| 6 | + * utilities |
| 7 | + * 바로 전달하면 같은 인스턴스를 가리키게 되어 remote operation 의미가 사라짐 |
| 8 | + */ |
| 9 | +const deepCopyRemoteInsertion = (op: RemoteInsertOperation) => { |
| 10 | + const { prevId, node } = op; |
| 11 | + |
| 12 | + const copy = { ...node }; |
| 13 | + Object.setPrototypeOf(copy, Node.prototype); |
| 14 | + |
| 15 | + return { prevId, node: copy as Node }; |
| 16 | +}; |
| 17 | + |
| 18 | +const remoteInsertThroughSocket = (crdt: CRDT, op: RemoteInsertOperation) => { |
| 19 | + const copy = deepCopyRemoteInsertion(op); |
| 20 | + crdt.remoteInsert(copy); |
| 21 | +}; |
| 22 | + |
| 23 | +/** |
| 24 | + * 모든 remote site 문자열이 일치하는지 확인 |
| 25 | + */ |
| 26 | +const convergenceCheck = () => { |
| 27 | + expect(원희.read()).toEqual(주영.read()); |
| 28 | + expect(주영.read()).toEqual(도훈.read()); |
| 29 | + expect(도훈.read()).toEqual(세영.read()); |
| 30 | +}; |
| 31 | + |
| 32 | +/** |
| 33 | + * 테스트용 site들 |
| 34 | + */ |
| 35 | +const 원희 = new CRDT(1, 1, new LinkedList()); |
| 36 | +const 주영 = new CRDT(1, 2, new LinkedList()); |
| 37 | +const 도훈 = new CRDT(1, 3, new LinkedList()); |
| 38 | +const 세영 = new CRDT(1, 4, new LinkedList()); |
| 39 | + |
| 40 | +describe('Convergence', () => { |
| 41 | + it('하나의 site에서 삽입', () => { |
| 42 | + const 원희remotes = [ |
| 43 | + 원희.localInsert(-1, '녕'), |
| 44 | + 원희.localInsert(-1, '안'), |
| 45 | + ]; |
| 46 | + |
| 47 | + [주영, 도훈, 세영].forEach((나) => { |
| 48 | + 원희remotes.forEach((op) => remoteInsertThroughSocket(나, op)); |
| 49 | + }); |
| 50 | + |
| 51 | + convergenceCheck(); |
| 52 | + }); |
| 53 | + |
| 54 | + it('여러 site에서 같은 위치에 삽입', () => { |
| 55 | + const 도훈remote = 도훈.localInsert(0, '왭'); |
| 56 | + const 세영remote = 세영.localInsert(0, '?'); |
| 57 | + |
| 58 | + remoteInsertThroughSocket(도훈, 세영remote); |
| 59 | + remoteInsertThroughSocket(세영, 도훈remote); |
| 60 | + |
| 61 | + [원희, 주영].forEach((나) => { |
| 62 | + remoteInsertThroughSocket(나, 도훈remote); |
| 63 | + remoteInsertThroughSocket(나, 세영remote); |
| 64 | + }); |
| 65 | + |
| 66 | + convergenceCheck(); |
| 67 | + }); |
| 68 | + |
| 69 | + it('여러 site에서 다른 위치에 삽입', () => { |
| 70 | + const 원희remote = 원희.localInsert(0, '네'); |
| 71 | + const 주영remote = 주영.localInsert(1, '!'); |
| 72 | + |
| 73 | + remoteInsertThroughSocket(원희, 주영remote); |
| 74 | + remoteInsertThroughSocket(주영, 원희remote); |
| 75 | + |
| 76 | + [도훈, 세영].forEach((나) => { |
| 77 | + remoteInsertThroughSocket(나, 원희remote); |
| 78 | + remoteInsertThroughSocket(나, 주영remote); |
| 79 | + }); |
| 80 | + |
| 81 | + convergenceCheck(); |
| 82 | + }); |
| 83 | +}); |
0 commit comments