This repository has been archived by the owner on Sep 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
CRDT.test.js
144 lines (135 loc) · 4.34 KB
/
CRDT.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import assert from 'assert'
import { GCounter, PNCounter, GSet, TwoPSet, ORSet, LWWSet } from '../src/index.js'
import CmRDTSet from '../src/CmRDT-Set.js'
const crdts = [
{
type: 'G-Counter',
class: GCounter,
create: (id) => new GCounter(id),
update: (crdt, value) => crdt.increment(value),
merge: (crdt, other) => crdt.merge(other),
query: (crdt) => crdt.value,
getExpectedMergedValue: (values) => values.reduce((acc, val) => acc + val, 0),
},
{
type: 'PN-Counter',
class: PNCounter,
create: (id) => new PNCounter(id),
update: (crdt, value) => {
crdt.increment(value + 1)
crdt.decrement(value)
},
merge: (crdt, other) => crdt.merge(other),
query: (crdt) => crdt.value,
getExpectedMergedValue: (values) => -values.reduce((acc, val) => acc + val, 0) + values.reduce((acc, val) => (acc + 1) + val, 0),
},
{
type: 'G-Set',
class: GSet,
create: () => new GSet(),
update: (crdt, value) => crdt.add(value),
merge: (crdt, other) => crdt.merge(other),
query: (crdt) => new Set(crdt.values()),
getExpectedMergedValue: (values) => new Set(values),
},
{
type: '2P-Set',
class: TwoPSet,
create: () => new GSet(),
update: (crdt, value) => crdt.add(value),
merge: (crdt, other) => crdt.merge(other),
query: (crdt) => new Set(crdt.values()),
getExpectedMergedValue: (values) => new Set(values),
},
{
type: 'OR-Set',
class: ORSet,
create: () => new ORSet(),
update: (crdt, value) => crdt.add(value),
merge: (crdt, other) => crdt.merge(other),
query: (crdt) => new Set(crdt.values()),
getExpectedMergedValue: (values) => new Set(values),
},
{
type: 'LWW-Set',
class: LWWSet,
create: () => new LWWSet(),
update: (crdt, value) => crdt.add(value, 0),
merge: (crdt, other) => crdt.merge(other),
query: (crdt) => new Set(crdt.values()),
getExpectedMergedValue: (values) => new Set(values),
},
]
describe('CRDT', () => {
crdts.forEach(async (CRDT) => {
describe(CRDT.type, () => {
// Associativity:
// a + (b + c) == (a + b) + c
it('is associative', () => {
// a + (b + c)
const crdt1 = CRDT.create('A')
const crdt2 = CRDT.create('B')
const crdt3 = CRDT.create('C')
CRDT.update(crdt1, 42)
CRDT.update(crdt2, 2)
CRDT.update(crdt3, 1)
CRDT.merge(crdt2, crdt3)
CRDT.merge(crdt1, crdt2)
const expectedValue1 = CRDT.getExpectedMergedValue([2, 42, 1])
const res1 = CRDT.query(crdt1)
assert.deepEqual(res1, expectedValue1)
// (a + b) + c
const crdt4 = CRDT.create('A')
const crdt5 = CRDT.create('B')
const crdt6 = CRDT.create('C')
CRDT.update(crdt4, 42)
CRDT.update(crdt5, 2)
CRDT.update(crdt6, 1)
CRDT.merge(crdt4, crdt5)
CRDT.merge(crdt6, crdt4)
const expectedValue2 = CRDT.getExpectedMergedValue([1, 2, 42])
const res2 = CRDT.query(crdt6)
assert.deepEqual(res2, expectedValue2)
// a + (b + c) == (a + b) + c
assert.deepEqual(res1, res2)
})
// Commutativity:
// a + b == b + a
it('is commutative', () => {
// a + b
const crdt1 = CRDT.create('A')
const crdt2 = CRDT.create('B')
CRDT.update(crdt1, 12)
CRDT.update(crdt2, 43)
CRDT.merge(crdt1, crdt2)
const expectedValue1 = CRDT.getExpectedMergedValue([43, 12])
const res1 = CRDT.query(crdt1)
assert.deepEqual(res1, expectedValue1)
// b + a
const crdt3 = CRDT.create('A')
const crdt4 = CRDT.create('B')
CRDT.update(crdt3, 12)
CRDT.update(crdt4, 43)
CRDT.merge(crdt3, crdt4)
const expectedValue2 = CRDT.getExpectedMergedValue([12, 43])
const res2 = CRDT.query(crdt3)
assert.deepEqual(res2, expectedValue2)
// a + b == b + a
assert.deepEqual(res1, res2)
})
// Idempotence:
// a + a = a
it('is idempotent', () => {
const crdt = CRDT.create('A')
CRDT.update(crdt, 3)
CRDT.update(crdt, 42)
CRDT.update(crdt, 7)
CRDT.merge(crdt, crdt)
const res = CRDT.query(crdt)
const expectedValue = CRDT.getExpectedMergedValue([7, 3, 42])
// a + a = a
assert.deepEqual(res, expectedValue)
})
})
})
})