-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathset.test.js
260 lines (243 loc) · 8.91 KB
/
set.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
// @flow
import * as matchers from 'jest-extended';
import { v4 as uuidv4 } from 'uuid';
import expect from 'expect';
import { getSwarm, closeAllNodes } from './lib/ipfs';
import { IpfsObservedRemoveSet } from '../src';
import { generateValue } from './lib/values';
import waitForHashing from './lib/wait-for-hashing';
expect.extend(matchers);
jest.setTimeout(30000);
let nodes = [];
describe('IPFS Set', () => {
beforeAll(async () => {
nodes = await getSwarm(2);
});
afterAll(async () => {
await closeAllNodes();
});
test('Load from a hash', async () => {
const topicA = uuidv4();
const topicB = uuidv4();
const A = generateValue();
const B = generateValue();
const C = generateValue();
const alice = new IpfsObservedRemoveSet(nodes[0], topicA, [A, B, C], { bufferPublishing: 0 });
await alice.readyPromise;
const hash = await alice.getIpfsHash();
const bob = new IpfsObservedRemoveSet(nodes[0], topicB, undefined, { bufferPublishing: 0 });
await bob.readyPromise;
await bob.loadIpfsHash(hash);
expect(bob.has(A)).toEqual(true);
expect(bob.has(B)).toEqual(true);
expect(bob.has(C)).toEqual(true);
await alice.shutdown();
await bob.shutdown();
});
test('Synchronize sets', async () => {
const topic = uuidv4();
const X = generateValue();
const Y = generateValue();
const Z = generateValue();
const alice: IpfsObservedRemoveSet<string> = new IpfsObservedRemoveSet(nodes[0], topic, undefined, { bufferPublishing: 0 });
const bob: IpfsObservedRemoveSet<string> = new IpfsObservedRemoveSet(nodes[1], topic, undefined, { bufferPublishing: 0 });
await Promise.all([alice.readyPromise, bob.readyPromise]);
await new Promise((resolve) => setTimeout(resolve, 500));
let aliceAddCount = 0;
let bobAddCount = 0;
let aliceDeleteCount = 0;
let bobDeleteCount = 0;
alice.on('add', () => (aliceAddCount += 1));
bob.on('add', () => (bobAddCount += 1));
alice.on('delete', () => (aliceDeleteCount += 1));
bob.on('delete', () => (bobDeleteCount += 1));
alice.add(X);
alice.add(Y);
alice.add(Z);
while (aliceAddCount !== 3 || bobAddCount !== 3) {
await new Promise((resolve) => setTimeout(resolve, 20));
}
expect([...alice]).toIncludeSameMembers([X, Y, Z]);
expect([...bob]).toIncludeSameMembers([X, Y, Z]);
bob.delete(X);
bob.delete(Y);
bob.delete(Z);
while (aliceDeleteCount !== 3 || bobDeleteCount !== 3) {
await new Promise((resolve) => setTimeout(resolve, 100));
}
expect([...alice]).toIncludeSameMembers([]);
expect([...bob]).toIncludeSameMembers([]);
await alice.shutdown();
await bob.shutdown();
});
test('Synchronize add and delete events', async () => {
const topic = uuidv4();
const X = generateValue();
const Y = generateValue();
const alice = new IpfsObservedRemoveSet(nodes[0], topic, undefined, { bufferPublishing: 0 });
const bob = new IpfsObservedRemoveSet(nodes[1], topic, undefined, { bufferPublishing: 0 });
await Promise.all([alice.readyPromise, bob.readyPromise]);
await new Promise((resolve) => setTimeout(resolve, 500));
const aliceAddXPromise = new Promise((resolve) => {
alice.once('add', (value) => {
expect(value).toEqual(X);
resolve();
});
});
const aliceDeleteXPromise = new Promise((resolve) => {
alice.once('delete', (value) => {
expect(value).toEqual(X);
resolve();
});
});
bob.add(X);
await aliceAddXPromise;
bob.delete(X);
await aliceDeleteXPromise;
const bobAddYPromise = new Promise((resolve) => {
bob.once('add', (value) => {
expect(value).toEqual(Y);
resolve();
});
});
const bobDeleteYPromise = new Promise((resolve) => {
bob.once('delete', (value) => {
expect(value).toEqual(Y);
resolve();
});
});
alice.add(Y);
await bobAddYPromise;
alice.delete(Y);
await bobDeleteYPromise;
await alice.shutdown();
await bob.shutdown();
});
test('Automatically synchronize mixed sets', async () => {
const topic = uuidv4();
const A = generateValue();
const B = generateValue();
const C = generateValue();
const X = generateValue();
const Y = generateValue();
const Z = generateValue();
const alice = new IpfsObservedRemoveSet(nodes[0], topic, [A, B, C], { bufferPublishing: 0 });
const bob = new IpfsObservedRemoveSet(nodes[1], topic, [X, Y, Z], { bufferPublishing: 0 });
await Promise.all([bob.readyPromise, alice.readyPromise]);
await waitForHashing([alice, bob]);
await alice.shutdown();
await bob.shutdown();
});
test('Load from a hash (chunked)', async () => {
const topicA = uuidv4();
const topicB = uuidv4();
const A = generateValue();
const B = generateValue();
const C = generateValue();
const alice = new IpfsObservedRemoveSet(nodes[0], topicA, [A, B, C], { bufferPublishing: 0, chunkPubSub: true });
await alice.readyPromise;
const hash = await alice.getIpfsHash();
const bob = new IpfsObservedRemoveSet(nodes[0], topicB, undefined, { bufferPublishing: 0, chunkPubSub: true });
await bob.readyPromise;
await bob.loadIpfsHash(hash);
expect(bob.has(A)).toEqual(true);
expect(bob.has(B)).toEqual(true);
expect(bob.has(C)).toEqual(true);
await alice.shutdown();
await bob.shutdown();
});
test('Synchronize sets (chunked)', async () => {
const topic = uuidv4();
const X = generateValue();
const Y = generateValue();
const Z = generateValue();
const alice: IpfsObservedRemoveSet<string> = new IpfsObservedRemoveSet(nodes[0], topic, undefined, { bufferPublishing: 0, chunkPubSub: true });
const bob: IpfsObservedRemoveSet<string> = new IpfsObservedRemoveSet(nodes[1], topic, undefined, { bufferPublishing: 0, chunkPubSub: true });
await Promise.all([alice.readyPromise, bob.readyPromise]);
await new Promise((resolve) => setTimeout(resolve, 500));
let aliceAddCount = 0;
let bobAddCount = 0;
let aliceDeleteCount = 0;
let bobDeleteCount = 0;
alice.on('add', () => (aliceAddCount += 1));
bob.on('add', () => (bobAddCount += 1));
alice.on('delete', () => (aliceDeleteCount += 1));
bob.on('delete', () => (bobDeleteCount += 1));
alice.add(X);
alice.add(Y);
alice.add(Z);
while (aliceAddCount !== 3 || bobAddCount !== 3) {
await new Promise((resolve) => setTimeout(resolve, 20));
}
expect([...alice]).toIncludeSameMembers([X, Y, Z]);
expect([...bob]).toIncludeSameMembers([X, Y, Z]);
bob.delete(X);
bob.delete(Y);
bob.delete(Z);
while (aliceDeleteCount !== 3 || bobDeleteCount !== 3) {
await new Promise((resolve) => setTimeout(resolve, 100));
}
expect([...alice]).toIncludeSameMembers([]);
expect([...bob]).toIncludeSameMembers([]);
await alice.shutdown();
await bob.shutdown();
});
test('Synchronize add and delete events (chunked)', async () => {
const topic = uuidv4();
const X = generateValue();
const Y = generateValue();
const alice = new IpfsObservedRemoveSet(nodes[0], topic, undefined, { bufferPublishing: 0, chunkPubSub: true });
const bob = new IpfsObservedRemoveSet(nodes[1], topic, undefined, { bufferPublishing: 0, chunkPubSub: true });
await Promise.all([alice.readyPromise, bob.readyPromise]);
await new Promise((resolve) => setTimeout(resolve, 500));
const aliceAddXPromise = new Promise((resolve) => {
alice.once('add', (value) => {
expect(value).toEqual(X);
resolve();
});
});
const aliceDeleteXPromise = new Promise((resolve) => {
alice.once('delete', (value) => {
expect(value).toEqual(X);
resolve();
});
});
bob.add(X);
await aliceAddXPromise;
bob.delete(X);
await aliceDeleteXPromise;
const bobAddYPromise = new Promise((resolve) => {
bob.once('add', (value) => {
expect(value).toEqual(Y);
resolve();
});
});
const bobDeleteYPromise = new Promise((resolve) => {
bob.once('delete', (value) => {
expect(value).toEqual(Y);
resolve();
});
});
alice.add(Y);
await bobAddYPromise;
alice.delete(Y);
await bobDeleteYPromise;
await alice.shutdown();
await bob.shutdown();
});
test('Automatically synchronize mixed sets (chunked)', async () => {
const topic = uuidv4();
const A = generateValue();
const B = generateValue();
const C = generateValue();
const X = generateValue();
const Y = generateValue();
const Z = generateValue();
const alice = new IpfsObservedRemoveSet(nodes[0], topic, [A, B, C], { bufferPublishing: 0, chunkPubSub: true });
const bob = new IpfsObservedRemoveSet(nodes[1], topic, [X, Y, Z], { bufferPublishing: 0, chunkPubSub: true });
await Promise.all([bob.readyPromise, alice.readyPromise]);
await waitForHashing([alice, bob]);
await alice.shutdown();
await bob.shutdown();
});
});