Files: 60e1d083f94f993b772846d9d209f845081a9094 / tests / index.js
24835 bytesRaw
1 | const tape = require('tape') |
2 | const IPFS = require('ipfs') |
3 | const Hypervisor = require('../') |
4 | |
5 | // start ipfs |
6 | const node = new IPFS({ |
7 | start: false |
8 | }) |
9 | |
10 | class BaseContainer { |
11 | constructor (exInterface) { |
12 | this.exInterface = exInterface |
13 | } |
14 | |
15 | initailize (message) { |
16 | const port = message.ports[0] |
17 | if (port) { |
18 | this.exInterface.ports.bind('root', port) |
19 | } |
20 | } |
21 | } |
22 | |
23 | node.on('ready', () => { |
24 | tape('basic', async t => { |
25 | t.plan(2) |
26 | let message |
27 | const expectedState = { |
28 | '/': 'zdpuAyGKaZ3nbBQdgESbEgVYr81TcAFB6LE2MQQPWLZaYxuF3' |
29 | } |
30 | |
31 | class testVMContainer extends BaseContainer { |
32 | run (m) { |
33 | t.true(m === message, 'should recive a message') |
34 | } |
35 | } |
36 | |
37 | const hypervisor = new Hypervisor(node.dag) |
38 | hypervisor.registerContainer('test', testVMContainer) |
39 | |
40 | const rootContainer = await hypervisor.createInstance('test') |
41 | const port = rootContainer.ports.create('test') |
42 | message = rootContainer.createMessage() |
43 | rootContainer.ports.bind('first', port) |
44 | rootContainer.send(port, message) |
45 | |
46 | const stateRoot = await hypervisor.createStateRoot(Infinity) |
47 | t.deepEquals(stateRoot, expectedState, 'expected root!') |
48 | }) |
49 | |
50 | tape('basic - do not store containers with no ports bound', async t => { |
51 | t.plan(1) |
52 | const expectedState = { |
53 | '/': 'zdpuAx5LRRwTgzPipKEPgh7MHUKu4Pd1BYjDqBcf9whgzvrqf' |
54 | } |
55 | |
56 | class testVMContainer extends BaseContainer { |
57 | initailize () {} |
58 | } |
59 | |
60 | const hypervisor = new Hypervisor(node.dag) |
61 | hypervisor.registerContainer('test', testVMContainer) |
62 | |
63 | const root = await hypervisor.createInstance('test') |
64 | const port = root.ports.create('test') |
65 | root.ports.bind('one', port) |
66 | |
67 | const stateRoot = await hypervisor.createStateRoot(Infinity) |
68 | t.deepEquals(stateRoot, expectedState, 'expected root!') |
69 | }) |
70 | |
71 | tape('one child contract with saturated ports', async t => { |
72 | t.plan(2) |
73 | let message |
74 | const expectedState = { |
75 | '/': 'zdpuAtVcH6MUnvt2RXnLsDXyLB3CBSQ7aydfh2ogSKGCejJCQ' |
76 | } |
77 | |
78 | class testVMContainer2 extends BaseContainer { |
79 | run (m) { |
80 | t.true(m === message, 'should recive a message') |
81 | } |
82 | } |
83 | |
84 | class testVMContainer extends BaseContainer { |
85 | run (m) { |
86 | const port = this.exInterface.ports.create('test2') |
87 | this.exInterface.ports.bind('child', port) |
88 | this.exInterface.incrementTicks(2) |
89 | this.exInterface.send(port, m) |
90 | } |
91 | } |
92 | |
93 | const hypervisor = new Hypervisor(node.dag) |
94 | hypervisor.registerContainer('test', testVMContainer) |
95 | hypervisor.registerContainer('test2', testVMContainer2) |
96 | |
97 | let root = await hypervisor.createInstance('test') |
98 | let port = root.ports.create('test') |
99 | |
100 | root.ports.bind('first', port) |
101 | message = root.createMessage() |
102 | |
103 | root.send(port, message) |
104 | const stateRoot = await hypervisor.createStateRoot(Infinity) |
105 | t.deepEquals(stateRoot, expectedState, 'expected state') |
106 | }) |
107 | |
108 | tape('one child contract', async t => { |
109 | t.plan(4) |
110 | let message |
111 | const expectedState = { |
112 | '/': 'zdpuAtVcH6MUnvt2RXnLsDXyLB3CBSQ7aydfh2ogSKGCejJCQ' |
113 | } |
114 | let hasResolved = false |
115 | |
116 | class testVMContainer2 extends BaseContainer { |
117 | run (m) { |
118 | t.true(m === message, 'should recive a message') |
119 | return new Promise((resolve, reject) => { |
120 | setTimeout(() => { |
121 | this.exInterface.incrementTicks(1) |
122 | hasResolved = true |
123 | resolve() |
124 | }, 200) |
125 | }) |
126 | } |
127 | } |
128 | |
129 | class testVMContainer extends BaseContainer { |
130 | run (m) { |
131 | const port = this.exInterface.ports.create('test2') |
132 | this.exInterface.ports.bind('child', port) |
133 | this.exInterface.send(port, m) |
134 | this.exInterface.incrementTicks(1) |
135 | } |
136 | } |
137 | |
138 | const hypervisor = new Hypervisor(node.dag) |
139 | hypervisor.registerContainer('test', testVMContainer) |
140 | hypervisor.registerContainer('test2', testVMContainer2) |
141 | |
142 | let root = await hypervisor.createInstance('test') |
143 | const rootId = root.id |
144 | let port = root.ports.create('test') |
145 | |
146 | root.ports.bind('first', port) |
147 | message = root.createMessage() |
148 | |
149 | root.send(port, message) |
150 | const stateRoot = await hypervisor.createStateRoot(Infinity) |
151 | t.true(hasResolved, 'should resolve before generating the state root') |
152 | t.deepEquals(stateRoot, expectedState, 'expected state') |
153 | // test reviving the state |
154 | class testVMContainer3 extends BaseContainer { |
155 | run (m) { |
156 | const port = this.exInterface.ports.get('child') |
157 | this.exInterface.send(port, m) |
158 | this.exInterface.incrementTicks(1) |
159 | } |
160 | } |
161 | |
162 | hypervisor.registerContainer('test', testVMContainer3) |
163 | root = await hypervisor.getInstance(rootId) |
164 | port = root.ports.get('first') |
165 | root.send(port, message) |
166 | }) |
167 | |
168 | tape('traps', async t => { |
169 | t.plan(1) |
170 | class Root extends BaseContainer { |
171 | async run (m) { |
172 | const one = this.exInterface.ports.create('root') |
173 | const two = this.exInterface.ports.create('root') |
174 | const three = this.exInterface.ports.create('root') |
175 | |
176 | this.exInterface.ports.bind('one', one) |
177 | this.exInterface.ports.bind('two', two) |
178 | this.exInterface.ports.bind('three', three) |
179 | |
180 | throw new Error('it is a trap!!!') |
181 | } |
182 | } |
183 | |
184 | const hypervisor = new Hypervisor(node.dag) |
185 | |
186 | hypervisor.registerContainer('root', Root) |
187 | const root = await hypervisor.createInstance('root') |
188 | await root.run(root.createMessage()) |
189 | const stateRoot = await hypervisor.createStateRoot() |
190 | |
191 | t.deepEquals(stateRoot, { |
192 | '/': 'zdpuAwrMmQXqFusve7zcRYxVUuji4NVzZR5GyjwyStsjteCoW' |
193 | }, 'should revert the state') |
194 | }) |
195 | |
196 | tape('message should arrive in the correct oder if sent in order', async t => { |
197 | t.plan(2) |
198 | let runs = 0 |
199 | |
200 | class Root extends BaseContainer { |
201 | run (m) { |
202 | if (!runs) { |
203 | runs++ |
204 | const one = this.exInterface.ports.create('first') |
205 | const two = this.exInterface.ports.create('second') |
206 | |
207 | this.exInterface.ports.bind('two', two) |
208 | this.exInterface.ports.bind('one', one) |
209 | |
210 | this.exInterface.send(one, this.exInterface.createMessage()) |
211 | this.exInterface.send(two, this.exInterface.createMessage()) |
212 | } else if (runs === 1) { |
213 | runs++ |
214 | t.equals(m.data, 'first', 'should recive the first message') |
215 | } else if (runs === 2) { |
216 | t.equals(m.data, 'second', 'should recived the second message') |
217 | } |
218 | } |
219 | } |
220 | |
221 | class First extends BaseContainer { |
222 | run (m) { |
223 | this.exInterface.incrementTicks(2) |
224 | return this.exInterface.send(m.fromPort, this.exInterface.createMessage({ |
225 | data: 'first' |
226 | })) |
227 | } |
228 | } |
229 | |
230 | class Second extends BaseContainer { |
231 | run (m) { |
232 | this.exInterface.incrementTicks(3) |
233 | return this.exInterface.send(m.fromPort, this.exInterface.createMessage({ |
234 | data: 'second' |
235 | })) |
236 | } |
237 | } |
238 | |
239 | const hypervisor = new Hypervisor(node.dag) |
240 | |
241 | hypervisor.registerContainer('root', Root) |
242 | hypervisor.registerContainer('first', First) |
243 | hypervisor.registerContainer('second', Second) |
244 | |
245 | const root = await hypervisor.createInstance('root') |
246 | const port = root.ports.create('root') |
247 | root.ports.bind('first', port) |
248 | |
249 | root.send(port, root.createMessage()) |
250 | }) |
251 | |
252 | tape('message should arrive in the correct oder if sent in order', async t => { |
253 | t.plan(2) |
254 | let runs = 0 |
255 | |
256 | class Root extends BaseContainer { |
257 | run (m) { |
258 | if (!runs) { |
259 | runs++ |
260 | const one = this.exInterface.ports.create('first') |
261 | const two = this.exInterface.ports.create('second') |
262 | |
263 | this.exInterface.ports.bind('one', one) |
264 | this.exInterface.ports.bind('two', two) |
265 | |
266 | Promise.all([ |
267 | this.exInterface.send(one, this.exInterface.createMessage()), |
268 | this.exInterface.send(two, this.exInterface.createMessage()) |
269 | ]) |
270 | } else if (runs === 1) { |
271 | runs++ |
272 | t.equals(m.data, 'second', 'should recived the second message') |
273 | } else if (runs === 2) { |
274 | t.equals(m.data, 'first', 'should recive the first message') |
275 | } |
276 | } |
277 | } |
278 | |
279 | class First extends BaseContainer { |
280 | run (m) { |
281 | this.exInterface.incrementTicks(2) |
282 | return this.exInterface.send(m.fromPort, this.exInterface.createMessage({ |
283 | data: 'first' |
284 | })) |
285 | } |
286 | } |
287 | |
288 | class Second extends BaseContainer { |
289 | run (m) { |
290 | this.exInterface.incrementTicks(1) |
291 | return this.exInterface.send(m.fromPort, this.exInterface.createMessage({ |
292 | data: 'second' |
293 | })) |
294 | } |
295 | } |
296 | |
297 | const hypervisor = new Hypervisor(node.dag) |
298 | |
299 | hypervisor.registerContainer('root', Root) |
300 | hypervisor.registerContainer('first', First) |
301 | hypervisor.registerContainer('second', Second) |
302 | |
303 | const root = await hypervisor.createInstance('root') |
304 | const port = root.ports.create('root') |
305 | root.ports.bind('first', port) |
306 | |
307 | root.send(port, root.createMessage()) |
308 | }) |
309 | |
310 | tape('message should arrive in the correct oder if sent in order', async t => { |
311 | t.plan(2) |
312 | let runs = 0 |
313 | |
314 | class Root extends BaseContainer { |
315 | run (m) { |
316 | if (!runs) { |
317 | runs++ |
318 | const one = this.exInterface.ports.create('first') |
319 | const two = this.exInterface.ports.create('second') |
320 | |
321 | this.exInterface.ports.bind('one', one) |
322 | this.exInterface.ports.bind('two', two) |
323 | |
324 | this.exInterface.send(one, this.exInterface.createMessage()) |
325 | this.exInterface.send(two, this.exInterface.createMessage()) |
326 | |
327 | this.exInterface.incrementTicks(6) |
328 | } else if (runs === 1) { |
329 | runs++ |
330 | t.equals(m.data, 'first', 'should recive the first message') |
331 | } else if (runs === 2) { |
332 | t.equals(m.data, 'second', 'should recived the second message') |
333 | } |
334 | } |
335 | } |
336 | |
337 | class First extends BaseContainer { |
338 | run (m) { |
339 | this.exInterface.incrementTicks(1) |
340 | this.exInterface.send(m.fromPort, this.exInterface.createMessage({data: 'first'})) |
341 | } |
342 | } |
343 | |
344 | class Second extends BaseContainer { |
345 | run (m) { |
346 | this.exInterface.incrementTicks(2) |
347 | this.exInterface.send(m.fromPort, this.exInterface.createMessage({data: 'second'})) |
348 | } |
349 | } |
350 | |
351 | const hypervisor = new Hypervisor(node.dag) |
352 | |
353 | hypervisor.registerContainer('root', Root) |
354 | hypervisor.registerContainer('first', First) |
355 | hypervisor.registerContainer('second', Second) |
356 | |
357 | const root = await hypervisor.createInstance('root') |
358 | const port = root.ports.create('root') |
359 | root.ports.bind('first', port) |
360 | |
361 | root.send(port, root.createMessage()) |
362 | }) |
363 | |
364 | tape('saturation', async t => { |
365 | t.plan(2) |
366 | let runs = 0 |
367 | |
368 | class Root extends BaseContainer { |
369 | run (m) { |
370 | if (!runs) { |
371 | runs++ |
372 | const one = this.exInterface.ports.create('first') |
373 | const two = this.exInterface.ports.create('second') |
374 | |
375 | this.exInterface.ports.bind('two', two) |
376 | this.exInterface.ports.bind('one', one) |
377 | |
378 | Promise.all([ |
379 | this.exInterface.send(one, this.exInterface.createMessage()), |
380 | this.exInterface.send(two, this.exInterface.createMessage()) |
381 | ]) |
382 | this.exInterface.incrementTicks(6) |
383 | } else if (runs === 1) { |
384 | runs++ |
385 | t.equals(m.data, 'first', 'should recive the first message') |
386 | } else if (runs === 2) { |
387 | t.equals(m.data, 'second', 'should recived the second message') |
388 | } |
389 | } |
390 | } |
391 | |
392 | class First extends BaseContainer { |
393 | run (m) { |
394 | this.exInterface.incrementTicks(2) |
395 | this.exInterface.send(m.fromPort, this.exInterface.createMessage({ |
396 | data: 'first' |
397 | })) |
398 | } |
399 | } |
400 | |
401 | class Second extends BaseContainer { |
402 | run (m) { |
403 | // this.exInterface.incrementTicks(3) |
404 | this.exInterface.incrementTicks(3) |
405 | this.exInterface.send(m.fromPort, this.exInterface.createMessage({ |
406 | data: 'second' |
407 | })) |
408 | } |
409 | } |
410 | |
411 | class Waiter extends BaseContainer { |
412 | initailize () { |
413 | return new Promise((resolve, reject) => { |
414 | setTimeout(() => { |
415 | resolve() |
416 | }, 200) |
417 | }) |
418 | } |
419 | } |
420 | |
421 | const hypervisor = new Hypervisor(node.dag) |
422 | |
423 | hypervisor.registerContainer('root', Root) |
424 | hypervisor.registerContainer('first', First) |
425 | hypervisor.registerContainer('second', Second) |
426 | hypervisor.registerContainer('waiter', Waiter) |
427 | |
428 | const root = await hypervisor.createInstance('root') |
429 | const port = root.ports.create('root') |
430 | root.ports.bind('first', port) |
431 | const port1 = root.ports.create('waiter') |
432 | root.ports.bind('sencond', port1) |
433 | |
434 | await root.send(port, root.createMessage()) |
435 | root.incrementTicks(7) |
436 | |
437 | root.send(port, root.createMessage()) |
438 | }) |
439 | |
440 | tape('message should arrive in the correct order, even in a tie of ticks', async t => { |
441 | t.plan(2) |
442 | |
443 | let runs = 0 |
444 | |
445 | class Root extends BaseContainer { |
446 | run (m) { |
447 | if (!runs) { |
448 | runs++ |
449 | const one = this.exInterface.ports.create('first') |
450 | const two = this.exInterface.ports.create('second') |
451 | |
452 | this.exInterface.ports.bind('two', two) |
453 | this.exInterface.ports.bind('one', one) |
454 | |
455 | this.exInterface.send(one, this.exInterface.createMessage()) |
456 | this.exInterface.send(two, this.exInterface.createMessage()) |
457 | |
458 | this.exInterface.incrementTicks(6) |
459 | } else if (runs === 1) { |
460 | runs++ |
461 | t.equals(m.data, 'second', 'should recived the second message') |
462 | } else if (runs === 2) { |
463 | t.equals(m.data, 'first', 'should recive the first message') |
464 | } |
465 | } |
466 | } |
467 | |
468 | class First extends BaseContainer { |
469 | run (m) { |
470 | this.exInterface.incrementTicks(2) |
471 | return this.exInterface.send(m.fromPort, this.exInterface.createMessage({ |
472 | data: 'first' |
473 | })) |
474 | } |
475 | } |
476 | |
477 | class Second extends BaseContainer { |
478 | run (m) { |
479 | this.exInterface.incrementTicks(2) |
480 | return this.exInterface.send(m.fromPort, this.exInterface.createMessage({ |
481 | data: 'second' |
482 | })) |
483 | } |
484 | } |
485 | |
486 | const hypervisor = new Hypervisor(node.dag) |
487 | |
488 | hypervisor.registerContainer('root', Root) |
489 | hypervisor.registerContainer('first', First) |
490 | hypervisor.registerContainer('second', Second) |
491 | |
492 | const root = await hypervisor.createInstance('root') |
493 | |
494 | const port = root.ports.create('root') |
495 | root.ports.bind('first', port) |
496 | |
497 | root.send(port, root.createMessage()) |
498 | }) |
499 | |
500 | tape('message should arrive in the correct order, with a tie in ticks but with differnt proity', async t => { |
501 | t.plan(2) |
502 | |
503 | let runs = 0 |
504 | |
505 | class Root extends BaseContainer { |
506 | run (m) { |
507 | if (!runs) { |
508 | runs++ |
509 | const one = this.exInterface.ports.create('first') |
510 | const two = this.exInterface.ports.create('second') |
511 | |
512 | this.exInterface.ports.bind('one', one) |
513 | this.exInterface.ports.bind('two', two) |
514 | |
515 | this.exInterface.send(two, this.exInterface.createMessage()) |
516 | this.exInterface.send(one, this.exInterface.createMessage()) |
517 | |
518 | this.exInterface.incrementTicks(6) |
519 | } else if (runs === 1) { |
520 | runs++ |
521 | t.equals(m.data, 'first', 'should recive the first message') |
522 | } else if (runs === 2) { |
523 | t.equals(m.data, 'second', 'should recived the second message') |
524 | } |
525 | } |
526 | } |
527 | |
528 | class First extends BaseContainer { |
529 | run (m) { |
530 | this.exInterface.incrementTicks(2) |
531 | return this.exInterface.send(m.fromPort, this.exInterface.createMessage({ |
532 | data: 'first' |
533 | })) |
534 | } |
535 | } |
536 | |
537 | class Second extends BaseContainer { |
538 | run (m) { |
539 | this.exInterface.incrementTicks(2) |
540 | return this.exInterface.send(m.fromPort, this.exInterface.createMessage({ |
541 | data: 'second' |
542 | })) |
543 | } |
544 | } |
545 | |
546 | const hypervisor = new Hypervisor(node.dag) |
547 | |
548 | hypervisor.registerContainer('root', Root) |
549 | hypervisor.registerContainer('first', First) |
550 | hypervisor.registerContainer('second', Second) |
551 | |
552 | const root = await hypervisor.createInstance('root') |
553 | const port = root.ports.create('root') |
554 | root.ports.bind('first', port) |
555 | root.send(port, root.createMessage()) |
556 | }) |
557 | |
558 | tape('send to the same container at the same time', async t => { |
559 | t.plan(2) |
560 | |
561 | let runs = 0 |
562 | let instance |
563 | |
564 | class Root extends BaseContainer { |
565 | run (m) { |
566 | let one = this.exInterface.ports.get('one') |
567 | if (!one) { |
568 | one = this.exInterface.ports.create('first') |
569 | this.exInterface.ports.bind('one', one) |
570 | } else { |
571 | this.exInterface.send(one, this.exInterface.createMessage()) |
572 | this.exInterface.send(one, this.exInterface.createMessage()) |
573 | } |
574 | } |
575 | } |
576 | |
577 | class First extends BaseContainer { |
578 | run (m) { |
579 | ++runs |
580 | if (runs === 2) { |
581 | t.equals(instance, this, 'should have same instances') |
582 | } else { |
583 | instance = this |
584 | } |
585 | } |
586 | } |
587 | |
588 | const hypervisor = new Hypervisor(node.dag) |
589 | |
590 | hypervisor.registerContainer('root', Root) |
591 | hypervisor.registerContainer('first', First) |
592 | |
593 | const root = await hypervisor.createInstance('root') |
594 | const port = root.ports.create('root') |
595 | root.ports.bind('first', port) |
596 | root.send(port, root.createMessage()) |
597 | await hypervisor.createStateRoot() |
598 | root.send(port, root.createMessage()) |
599 | await hypervisor.createStateRoot() |
600 | t.equals(runs, 2) |
601 | }) |
602 | tape('checking ports', async t => { |
603 | t.plan(4) |
604 | const hypervisor = new Hypervisor(node.dag) |
605 | hypervisor.registerContainer('base', BaseContainer) |
606 | |
607 | const root = await hypervisor.createInstance('base') |
608 | let port = root.ports.create('base') |
609 | await root.ports.bind('test', port) |
610 | |
611 | try { |
612 | root.createMessage({ |
613 | ports: [port] |
614 | }) |
615 | } catch (e) { |
616 | t.pass('should thow if sending a port that is bound') |
617 | } |
618 | |
619 | try { |
620 | await root.ports.bind('test', port) |
621 | } catch (e) { |
622 | t.pass('should thow if binding an already bound port') |
623 | } |
624 | |
625 | try { |
626 | let port2 = root.ports.create('base') |
627 | await root.ports.bind('test', port2) |
628 | } catch (e) { |
629 | t.pass('should thow if binding an already bound name') |
630 | } |
631 | |
632 | await root.ports.unbind('test') |
633 | const message = root.createMessage({ports: [port]}) |
634 | t.equals(message.ports[0], port, 'should create a message if the port is unbound') |
635 | }) |
636 | |
637 | tape('port deletion', async t => { |
638 | const expectedSr = { |
639 | '/': 'zdpuB2QXxn1KQtLFfBqaritTRoe5BuKP5sNFSrPtRT6sxkY7Z' |
640 | } |
641 | class Root extends BaseContainer { |
642 | run (m) { |
643 | const one = this.exInterface.ports.create('first') |
644 | this.exInterface.ports.bind('one', one) |
645 | this.exInterface.send(one, this.exInterface.createMessage()) |
646 | this.exInterface.incrementTicks(6) |
647 | } |
648 | } |
649 | |
650 | class First extends BaseContainer { |
651 | run (m) { |
652 | this.exInterface.incrementTicks(2) |
653 | this.exInterface.ports.delete('root') |
654 | } |
655 | } |
656 | |
657 | const hypervisor = new Hypervisor(node.dag) |
658 | |
659 | hypervisor.registerContainer('root', Root) |
660 | hypervisor.registerContainer('first', First) |
661 | |
662 | const root = await hypervisor.createInstance('root') |
663 | const port = root.ports.create('root') |
664 | root.ports.bind('first', port) |
665 | root.send(port, root.createMessage()) |
666 | const sr = await hypervisor.createStateRoot() |
667 | t.deepEquals(sr, expectedSr, 'should produce the corret state root') |
668 | |
669 | t.end() |
670 | }) |
671 | |
672 | tape('clear unbounded ports', async t => { |
673 | const expectedSr = { |
674 | '/': 'zdpuB2QXxn1KQtLFfBqaritTRoe5BuKP5sNFSrPtRT6sxkY7Z' |
675 | } |
676 | class Root extends BaseContainer { |
677 | run (m) { |
678 | this.exInterface.ports.create('root') |
679 | } |
680 | } |
681 | |
682 | const hypervisor = new Hypervisor(node.dag) |
683 | |
684 | hypervisor.registerContainer('root', Root) |
685 | |
686 | const root = await hypervisor.createInstance('root') |
687 | const port = root.ports.create('root') |
688 | root.ports.bind('first', port) |
689 | root.send(port, root.createMessage()) |
690 | const sr = await hypervisor.createStateRoot() |
691 | t.deepEquals(sr, expectedSr, 'should produce the corret state root') |
692 | |
693 | t.end() |
694 | }) |
695 | |
696 | tape('should remove subgraphs', async t => { |
697 | const expectedSr = { |
698 | '/': 'zdpuB2QXxn1KQtLFfBqaritTRoe5BuKP5sNFSrPtRT6sxkY7Z' |
699 | } |
700 | class Root extends BaseContainer { |
701 | run (m) { |
702 | this.exInterface.ports.create('sub') |
703 | } |
704 | } |
705 | |
706 | class Sub extends BaseContainer { |
707 | initailize (message) { |
708 | this.exInterface.ports.bind('root', message.ports[0]) |
709 | const port = this.exInterface.ports.create('root') |
710 | this.exInterface.ports.bind('child', port) |
711 | } |
712 | } |
713 | |
714 | const hypervisor = new Hypervisor(node.dag) |
715 | |
716 | hypervisor.registerContainer('root', Root) |
717 | hypervisor.registerContainer('sub', Sub) |
718 | |
719 | const root = await hypervisor.createInstance('root') |
720 | const port = root.ports.create('root') |
721 | root.ports.bind('first', port) |
722 | root.send(port, root.createMessage()) |
723 | const sr = await hypervisor.createStateRoot() |
724 | t.deepEquals(sr, expectedSr, 'should produce the corret state root') |
725 | |
726 | t.end() |
727 | }) |
728 | |
729 | tape('should not remove connected nodes', async t => { |
730 | const expectedSr = { |
731 | '/': 'zdpuAwsZTd5mRZBCYA1FJSHrpYDPgSZSiaTQp9xkUeajaoMHM' |
732 | } |
733 | class Root extends BaseContainer { |
734 | run (m) { |
735 | if (m.ports.length) { |
736 | const port = this.exInterface.ports.get('test1') |
737 | this.exInterface.send(port, m) |
738 | this.exInterface.ports.unbind('test1') |
739 | // this.exInterface.ports.unbind('test2') |
740 | } else { |
741 | const port1 = this.exInterface.ports.create('sub') |
742 | this.exInterface.ports.bind('test1', port1) |
743 | const port2 = this.exInterface.ports.create('sub') |
744 | this.exInterface.ports.bind('test2', port2) |
745 | this.exInterface.send(port2, this.exInterface.createMessage({data: 'getChannel'})) |
746 | } |
747 | } |
748 | } |
749 | |
750 | class Sub extends BaseContainer { |
751 | run (message) { |
752 | if (message.data === 'getChannel') { |
753 | const ports = this.exInterface.ports.createChannel() |
754 | this.exInterface.ports.bind('channel', ports[0]) |
755 | this.exInterface.send(message.fromPort, this.exInterface.createMessage({ |
756 | data: 'bindPort', |
757 | ports: [ports[1]] |
758 | })) |
759 | } else if (message.data === 'bindPort') { |
760 | this.exInterface.ports.bind('channel', message.ports[0]) |
761 | } |
762 | } |
763 | } |
764 | |
765 | const hypervisor = new Hypervisor(node.dag) |
766 | |
767 | hypervisor.registerContainer('root', Root) |
768 | hypervisor.registerContainer('sub', Sub) |
769 | |
770 | const root = await hypervisor.createInstance('root') |
771 | const port = root.ports.create('root') |
772 | root.ports.bind('first', port) |
773 | root.send(port, root.createMessage()) |
774 | const sr = await hypervisor.createStateRoot() |
775 | t.deepEquals(sr, expectedSr, 'should produce the corret state root') |
776 | // await hypervisor.graph.tree(sr, Infinity) |
777 | |
778 | t.end() |
779 | }) |
780 | |
781 | tape('should remove multiple subgraphs', async t => { |
782 | const expectedSr = { |
783 | '/': 'zdpuAmi9tkYTpoVsZvqQgxpQFRhCgYFVv4W3fjjfVhf1j8swv' |
784 | } |
785 | class Root extends BaseContainer { |
786 | run (m) { |
787 | if (m.ports.length) { |
788 | const port = this.exInterface.ports.get('test1') |
789 | this.exInterface.send(port, m) |
790 | this.exInterface.ports.unbind('test1') |
791 | this.exInterface.ports.unbind('test2') |
792 | } else { |
793 | const port1 = this.exInterface.ports.create('sub') |
794 | this.exInterface.ports.bind('test1', port1) |
795 | const port2 = this.exInterface.ports.create('sub') |
796 | this.exInterface.ports.bind('test2', port2) |
797 | this.exInterface.send(port2, this.exInterface.createMessage({data: 'getChannel'})) |
798 | } |
799 | } |
800 | } |
801 | |
802 | class Sub extends BaseContainer { |
803 | run (message) { |
804 | if (message.data === 'getChannel') { |
805 | const ports = this.exInterface.ports.createChannel() |
806 | this.exInterface.ports.bind('channel', ports[0]) |
807 | this.exInterface.send(message.fromPort, this.exInterface.createMessage({ |
808 | data: 'bindPort', |
809 | ports: [ports[1]] |
810 | })) |
811 | } else if (message.data === 'bindPort') { |
812 | this.exInterface.ports.bind('channel', message.ports[0]) |
813 | } |
814 | } |
815 | } |
816 | |
817 | const hypervisor = new Hypervisor(node.dag) |
818 | |
819 | hypervisor.registerContainer('root', Root) |
820 | hypervisor.registerContainer('sub', Sub) |
821 | |
822 | const root = await hypervisor.createInstance('root') |
823 | const port = root.ports.create('root') |
824 | root.ports.bind('first', port) |
825 | root.send(port, root.createMessage()) |
826 | const sr = await hypervisor.createStateRoot() |
827 | t.deepEquals(sr, expectedSr, 'should produce the corret state root') |
828 | |
829 | t.end() |
830 | }) |
831 | }) |
832 |
Built with git-ssb-web