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