tests/index.jsView |
---|
38 | 38 | try { |
39 | 39 | const hypervisor = new Hypervisor(node.dag) |
40 | 40 | hypervisor.registerContainer(testVMContainer) |
41 | 41 | |
42 | | - const rootContainer = await hypervisor.createInstance(new Message({ |
| 42 | + const port = hypervisor.creationService.getPort() |
| 43 | + |
| 44 | + let rootContainer = await hypervisor.send(port, new Message({ |
43 | 45 | data: { |
44 | 46 | type: testVMContainer.typeId |
45 | 47 | } |
46 | 48 | })) |
47 | 49 | |
| 50 | + rootContainer = await hypervisor.getInstance(rootContainer.id) |
| 51 | + |
48 | 52 | hypervisor.pin(rootContainer) |
49 | 53 | |
50 | 54 | const [portRef1, portRef2] = rootContainer.ports.createChannel() |
51 | 55 | const initMessage = rootContainer.createMessage({ |
55 | 59 | }, |
56 | 60 | ports: [portRef2] |
57 | 61 | }) |
58 | 62 | |
59 | | - await rootContainer.createInstance(initMessage) |
| 63 | + await rootContainer.send(port, initMessage) |
| 64 | + await rootContainer.ports.bind('first', portRef1) |
60 | 65 | |
61 | | - await rootContainer.ports.bind('first', portRef1) |
62 | 66 | message = rootContainer.createMessage() |
63 | 67 | await rootContainer.send(portRef1, message) |
| 68 | + rootContainer.shutdown() |
64 | 69 | |
65 | 70 | |
66 | 71 | const stateRoot = await hypervisor.createStateRoot(Infinity) |
67 | 72 | t.deepEquals(stateRoot, expectedState, 'expected root!') |
85 | 90 | try { |
86 | 91 | const hypervisor = new Hypervisor(node.dag) |
87 | 92 | hypervisor.registerContainer(testVMContainer) |
88 | 93 | |
89 | | - const root = await hypervisor.createInstance(new Message({ |
| 94 | + const creationPort = hypervisor.creationService.getPort() |
| 95 | + let root = await hypervisor.send(creationPort, new Message({ |
90 | 96 | data: { |
91 | 97 | type: testVMContainer.typeId |
92 | 98 | } |
93 | 99 | })) |
94 | 100 | |
95 | 101 | hypervisor.pin(root) |
| 102 | + |
| 103 | + root = await hypervisor.getInstance(root.id) |
| 104 | + |
96 | 105 | const [portRef1, portRef2] = root.ports.createChannel() |
97 | 106 | |
98 | | - await root.ports.bind('one', portRef1) |
99 | | - await root.createInstance(root.createMessage({ |
100 | | - data: { |
101 | | - type: testVMContainer.typeId |
102 | | - }, |
103 | | - ports: [portRef2] |
104 | | - })) |
| 107 | + await Promise.all([ |
| 108 | + root.ports.bind('one', portRef1), |
| 109 | + root.send(creationPort, root.createMessage({ |
| 110 | + data: { |
| 111 | + type: testVMContainer.typeId |
| 112 | + }, |
| 113 | + ports: [portRef2] |
| 114 | + })) |
| 115 | + ]) |
105 | 116 | |
| 117 | + root.shutdown() |
| 118 | + |
106 | 119 | const stateRoot = await hypervisor.createStateRoot(Infinity) |
107 | 120 | |
108 | 121 | |
109 | 122 | |
118 | 131 | let message |
119 | 132 | const expectedState = { |
120 | 133 | '/': 'zdpuB2Huo3ro3Fv9mpMhnUcL3jjd37T6MJ6jEd8GvA2cpvaYR' |
121 | 134 | } |
| 135 | + |
122 | 136 | let hasResolved = false |
123 | 137 | |
124 | 138 | class testVMContainer2 extends BaseContainer { |
125 | 139 | onMessage (m) { |
140 | 154 | |
141 | 155 | class testVMContainer extends BaseContainer { |
142 | 156 | async onMessage (m) { |
143 | 157 | const [portRef1, portRef2] = this.kernel.ports.createChannel() |
144 | | - await this.kernel.createInstance(this.kernel.createMessage({ |
145 | | - data: { |
146 | | - type: testVMContainer2.typeId |
147 | | - }, |
148 | | - ports: [portRef2] |
149 | | - })) |
150 | | - await this.kernel.send(portRef1, m) |
| 158 | + const port = this.kernel.hypervisor.creationService.getPort() |
| 159 | + |
| 160 | + await Promise.all([ |
| 161 | + this.kernel.send(port, this.kernel.createMessage({ |
| 162 | + data: { |
| 163 | + type: testVMContainer2.typeId |
| 164 | + }, |
| 165 | + ports: [portRef2] |
| 166 | + })), |
| 167 | + this.kernel.send(portRef1, m) |
| 168 | + ]) |
| 169 | + |
151 | 170 | this.kernel.incrementTicks(1) |
152 | 171 | return this.kernel.ports.bind('child', portRef1) |
153 | 172 | } |
154 | 173 | } |
156 | 175 | const hypervisor = new Hypervisor(node.dag) |
157 | 176 | hypervisor.registerContainer(testVMContainer) |
158 | 177 | hypervisor.registerContainer(testVMContainer2) |
159 | 178 | |
160 | | - let root = await hypervisor.createInstance(new Message({ |
| 179 | + let creationPort = hypervisor.creationService.getPort() |
| 180 | + let root = await hypervisor.send(creationPort, new Message({ |
161 | 181 | data: { |
162 | 182 | type: testVMContainer.typeId |
163 | 183 | } |
164 | 184 | })) |
165 | 185 | |
166 | 186 | hypervisor.pin(root) |
167 | 187 | |
168 | 188 | const rootId = root.id |
| 189 | + root = await hypervisor.getInstance(rootId) |
169 | 190 | const [portRef1, portRef2] = root.ports.createChannel() |
170 | | - await root.createInstance(root.createMessage({ |
171 | | - data: { |
172 | | - type: testVMContainer.typeId |
173 | | - }, |
174 | | - ports: [portRef2] |
175 | | - })) |
176 | 191 | |
177 | | - await root.ports.bind('first', portRef1) |
178 | 192 | message = root.createMessage() |
| 193 | + await Promise.all([ |
| 194 | + root.send(creationPort, root.createMessage({ |
| 195 | + data: { |
| 196 | + type: testVMContainer.typeId |
| 197 | + }, |
| 198 | + ports: [portRef2] |
| 199 | + })), |
| 200 | + root.ports.bind('first', portRef1), |
| 201 | + root.send(portRef1, message) |
| 202 | + ]) |
179 | 203 | |
180 | | - await root.send(portRef1, message) |
| 204 | + root.shutdown() |
| 205 | + |
181 | 206 | const stateRoot = await hypervisor.createStateRoot(Infinity) |
182 | 207 | t.true(hasResolved, 'should resolve before generating the state root') |
183 | 208 | |
184 | | - |
185 | | - |
186 | 209 | t.deepEquals(stateRoot, expectedState, 'expected state') |
187 | 210 | |
188 | 211 | |
189 | 212 | class testVMContainer3 extends BaseContainer { |
203 | 226 | tape('traps', async t => { |
204 | 227 | t.plan(1) |
205 | 228 | class Root extends BaseContainer { |
206 | 229 | async onMessage (m) { |
207 | | - const [portRef1, portRef2] = this.kernel.ports.createChannel() |
208 | | - const [portRef3, portRef4] = this.kernel.ports.createChannel() |
209 | | - const [portRef5, portRef6] = this.kernel.ports.createChannel() |
| 230 | + const [portRef1] = this.kernel.ports.createChannel() |
| 231 | + const [portRef3] = this.kernel.ports.createChannel() |
| 232 | + const [portRef5] = this.kernel.ports.createChannel() |
210 | 233 | |
211 | 234 | await Promise.all( |
212 | 235 | this.kernel.ports.bind('one', portRef1), |
213 | 236 | this.kernel.ports.bind('two', portRef3), |
214 | 237 | this.kernel.ports.bind('three', portRef5) |
215 | 238 | ) |
216 | 239 | |
217 | | - const message1 = this.kernel.createMessage({ |
218 | | - data: { |
219 | | - type: Root.typeId |
220 | | - }, |
221 | | - ports: [portRef2] |
222 | | - }) |
223 | | - const message2 = this.kernel.createMessage({ |
224 | | - data: { |
225 | | - type: Root.typeId |
226 | | - }, |
227 | | - ports: [portRef4] |
228 | | - }) |
229 | | - const message3 = this.kernel.createMessage({ |
230 | | - data: { |
231 | | - type: Root.typeId |
232 | | - }, |
233 | | - ports: [portRef6] |
234 | | - }) |
235 | | - |
236 | | - await Promise.all([ |
237 | | - this.kernel.createInstance(message1), |
238 | | - this.kernel.createInstance(message2), |
239 | | - this.kernel.createInstance(message3) |
240 | | - ]) |
241 | | - |
242 | 240 | throw new Error('it is a trap!!!') |
243 | 241 | } |
244 | 242 | } |
245 | 243 | |
246 | 244 | const hypervisor = new Hypervisor(node.dag) |
247 | 245 | |
248 | 246 | hypervisor.registerContainer(Root) |
249 | | - const root = await hypervisor.createInstance(new Message({ |
| 247 | + const creationPort = hypervisor.creationService.getPort() |
| 248 | + const root = await hypervisor.send(creationPort, new Message({ |
250 | 249 | data: { |
251 | 250 | type: Root.typeId |
252 | 251 | } |
253 | 252 | })) |
264 | 263 | |
265 | 264 | tape('recieving older messages', async t => { |
266 | 265 | t.plan(2) |
267 | 266 | let runs = 0 |
| 267 | + const hypervisor = new Hypervisor(node.dag) |
| 268 | + const creationPort = hypervisor.creationService.getPort() |
268 | 269 | |
269 | 270 | class Root extends BaseContainer { |
270 | 271 | async onMessage (m) { |
271 | 272 | if (!runs) { |
285 | 286 | }, |
286 | 287 | ports: [portRef4] |
287 | 288 | }) |
288 | 289 | |
289 | | - await Promise.all([ |
290 | | - this.kernel.createInstance(message1), |
| 290 | + return Promise.all([ |
| 291 | + this.kernel.send(creationPort, message1), |
291 | 292 | this.kernel.send(portRef1, this.kernel.createMessage()), |
292 | 293 | this.kernel.send(portRef3, this.kernel.createMessage()), |
293 | 294 | this.kernel.ports.bind('one', portRef1), |
294 | | - this.kernel.ports.bind('two', portRef3) |
| 295 | + this.kernel.ports.bind('two', portRef3), |
| 296 | + this.kernel.send(creationPort, message2) |
295 | 297 | ]) |
296 | | - return this.kernel.createInstance(message2) |
297 | 298 | } else if (runs === 1) { |
298 | 299 | runs++ |
299 | 300 | t.equals(m.data, 'first', 'should recive the first message') |
300 | 301 | } else if (runs === 2) { |
334 | 335 | } |
335 | 336 | } |
336 | 337 | |
337 | 338 | try { |
338 | | - const hypervisor = new Hypervisor(node.dag) |
339 | | - |
340 | 339 | hypervisor.registerContainer(Root) |
341 | 340 | hypervisor.registerContainer(First) |
342 | 341 | hypervisor.registerContainer(Waiter) |
343 | 342 | |
344 | | - const root = await hypervisor.createInstance(new Message({ |
| 343 | + let root = await hypervisor.send(creationPort, new Message({ |
345 | 344 | data: { |
346 | 345 | type: Root.typeId |
347 | 346 | } |
348 | 347 | })) |
349 | 348 | |
350 | 349 | hypervisor.pin(root) |
| 350 | + |
| 351 | + root = await hypervisor.getInstance(root.id) |
351 | 352 | const [portRef1, portRef2] = root.ports.createChannel() |
352 | 353 | |
353 | 354 | const message = root.createMessage() |
354 | 355 | await Promise.all([ |
355 | 356 | root.send(portRef1, message), |
356 | 357 | root.ports.bind('first', portRef1), |
357 | | - root.createInstance(root.createMessage({ |
| 358 | + root.send(creationPort, root.createMessage({ |
358 | 359 | data: { |
359 | 360 | type: Root.typeId |
360 | 361 | }, |
361 | 362 | ports: [portRef2] |
362 | 363 | })) |
363 | 364 | ]) |
| 365 | + root.shutdown() |
364 | 366 | } catch (e) { |
365 | 367 | console.log(e) |
366 | 368 | } |
367 | 369 | }) |
369 | 371 | tape('saturation', async t => { |
370 | 372 | t.plan(3) |
371 | 373 | let runs = 0 |
372 | 374 | |
| 375 | + const hypervisor = new Hypervisor(node.dag) |
| 376 | + const creationPort = hypervisor.creationService.getPort() |
| 377 | + |
373 | 378 | class Root extends BaseContainer { |
374 | 379 | onIdle () {} |
375 | 380 | async onMessage (m) { |
376 | 381 | if (!runs) { |
383 | 388 | type: First.typeId |
384 | 389 | }, |
385 | 390 | ports: [portRef2] |
386 | 391 | }) |
| 392 | + |
387 | 393 | const message2 = this.kernel.createMessage({ |
388 | 394 | data: { |
389 | 395 | type: Second.typeId |
390 | 396 | }, |
392 | 398 | }) |
393 | 399 | |
394 | 400 | this.kernel.incrementTicks(6) |
395 | 401 | return Promise.all([ |
396 | | - this.kernel.createInstance(message1), |
397 | | - this.kernel.createInstance(message2), |
| 402 | + this.kernel.send(creationPort, message1), |
| 403 | + this.kernel.send(creationPort, message2), |
398 | 404 | this.kernel.send(portRef1, this.kernel.createMessage()), |
399 | 405 | this.kernel.send(portRef3, this.kernel.createMessage()), |
400 | 406 | this.kernel.ports.bind('one', portRef1), |
401 | 407 | this.kernel.ports.bind('two', portRef3) |
452 | 458 | } |
453 | 459 | } |
454 | 460 | |
455 | 461 | try { |
456 | | - const hypervisor = new Hypervisor(node.dag) |
457 | | - |
458 | 462 | hypervisor.registerContainer(Root) |
459 | 463 | hypervisor.registerContainer(First) |
460 | 464 | hypervisor.registerContainer(Second) |
461 | 465 | hypervisor.registerContainer(Waiter) |
462 | 466 | |
463 | | - let root = await hypervisor.createInstance(new Message({ |
| 467 | + let root = await hypervisor.send(creationPort, new Message({ |
464 | 468 | data: { |
465 | 469 | type: Root.typeId |
466 | 470 | } |
467 | 471 | })) |
474 | 478 | const message = root.createMessage() |
475 | 479 | await Promise.all([ |
476 | 480 | root.send(portRef1, message), |
477 | 481 | root.ports.bind('first', portRef1), |
478 | | - root.createInstance(root.createMessage({ |
| 482 | + root.send(creationPort, root.createMessage({ |
479 | 483 | data: { |
480 | 484 | type: Root.typeId |
481 | 485 | }, |
482 | 486 | ports: [portRef2] |
483 | 487 | })), |
484 | 488 | root.ports.bind('sencond', portRef3), |
485 | | - root.createInstance(root.createMessage({ |
| 489 | + root.send(creationPort, root.createMessage({ |
486 | 490 | data: { |
487 | 491 | type: Waiter.typeId |
488 | 492 | }, |
489 | 493 | ports: [portRef4] |
490 | 494 | })) |
491 | 495 | ]) |
492 | 496 | |
493 | | - |
494 | 497 | root.incrementTicks(100) |
495 | 498 | await root.send(portRef1, root.createMessage({ |
496 | 499 | data: 'testss' |
497 | 500 | })) |
506 | 509 | |
507 | 510 | let runs = 0 |
508 | 511 | let instance |
509 | 512 | |
| 513 | + const hypervisor = new Hypervisor(node.dag) |
| 514 | + const creationPort = hypervisor.creationService.getPort() |
| 515 | + |
510 | 516 | class Root extends BaseContainer { |
511 | 517 | async onMessage (m) { |
512 | 518 | let one = this.kernel.ports.get('one') |
513 | 519 | if (!one) { |
517 | 523 | type: First.typeId |
518 | 524 | }, |
519 | 525 | ports: [portRef2] |
520 | 526 | }) |
521 | | - await this.kernel.createInstance(message1) |
| 527 | + await this.kernel.send(creationPort, message1) |
522 | 528 | return this.kernel.ports.bind('one', portRef1) |
523 | 529 | } else { |
524 | 530 | return Promise.all([ |
525 | 531 | this.kernel.send(one, this.kernel.createMessage()), |
543 | 549 | } |
544 | 550 | } |
545 | 551 | |
546 | 552 | try { |
547 | | - const hypervisor = new Hypervisor(node.dag) |
548 | | - |
549 | 553 | hypervisor.registerContainer(Root) |
550 | 554 | hypervisor.registerContainer(First) |
551 | 555 | |
552 | | - const root = await hypervisor.createInstance(new Message({ |
| 556 | + let root = await hypervisor.send(creationPort, new Message({ |
553 | 557 | data: { |
554 | 558 | type: Root.typeId |
555 | 559 | } |
556 | 560 | })) |
557 | 561 | |
558 | 562 | hypervisor.pin(root) |
| 563 | + root = await hypervisor.getInstance(root.id) |
559 | 564 | |
560 | 565 | const [portRef1, portRef2] = root.ports.createChannel() |
561 | 566 | await Promise.all([ |
562 | 567 | root.ports.bind('first', portRef1), |
563 | | - root.createInstance(root.createMessage({ |
| 568 | + root.send(creationPort, root.createMessage({ |
564 | 569 | data: { |
565 | 570 | type: Root.typeId |
566 | 571 | }, |
567 | 572 | ports: [portRef2] |
568 | 573 | })) |
569 | 574 | ]) |
570 | 575 | |
571 | 576 | const message = root.createMessage() |
| 577 | + |
572 | 578 | await root.send(portRef1, message) |
| 579 | + root.shutdown() |
573 | 580 | await hypervisor.createStateRoot() |
574 | 581 | await root.send(portRef1, root.createMessage()) |
575 | 582 | await hypervisor.createStateRoot() |
576 | 583 | t.equals(runs, 2) |
581 | 588 | |
582 | 589 | tape('checking ports', async t => { |
583 | 590 | t.plan(4) |
584 | 591 | const hypervisor = new Hypervisor(node.dag) |
| 592 | + const creationPort = hypervisor.creationService.getPort() |
585 | 593 | hypervisor.registerContainer(BaseContainer) |
586 | 594 | |
587 | | - const root = await hypervisor.createInstance(new Message({ |
| 595 | + let root = await hypervisor.send(creationPort, new Message({ |
588 | 596 | data: { |
589 | 597 | type: BaseContainer.typeId |
590 | 598 | } |
591 | 599 | })) |
592 | 600 | |
593 | 601 | hypervisor.pin(root) |
| 602 | + root = await hypervisor.getInstance(root.id) |
594 | 603 | |
595 | 604 | const [portRef1, portRef2] = root.ports.createChannel() |
596 | | - root.createInstance(root.createMessage({ |
| 605 | + root.send(creationPort, root.createMessage({ |
597 | 606 | data: { |
598 | 607 | type: BaseContainer.typeId |
599 | 608 | }, |
600 | 609 | ports: [portRef2] |
632 | 641 | tape('port deletion', async t => { |
633 | 642 | const expectedSr = { |
634 | 643 | '/': 'zdpuAxKfu5nMTfpz6uHPqXdHZFQDZdRUer8zcQ6nvC4pTQsop' |
635 | 644 | } |
| 645 | + |
| 646 | + const hypervisor = new Hypervisor(node.dag) |
| 647 | + const creationPort = hypervisor.creationService.getPort() |
| 648 | + |
636 | 649 | class Root extends BaseContainer { |
637 | 650 | async onMessage (m) { |
638 | 651 | const [portRef1, portRef2] = this.kernel.ports.createChannel() |
639 | 652 | const message1 = this.kernel.createMessage({ |
642 | 655 | }, |
643 | 656 | ports: [portRef2] |
644 | 657 | }) |
645 | 658 | |
646 | | - await this.kernel.createInstance(message1) |
647 | | - await this.kernel.send(portRef1, this.kernel.createMessage()) |
| 659 | + await Promise.all([ |
| 660 | + this.kernel.send(creationPort, message1), |
| 661 | + this.kernel.send(portRef1, this.kernel.createMessage()) |
| 662 | + ]) |
648 | 663 | this.kernel.incrementTicks(6) |
649 | 664 | return this.kernel.ports.bind('one', portRef1) |
650 | 665 | } |
651 | 666 | } |
659 | 674 | return 299 |
660 | 675 | } |
661 | 676 | } |
662 | 677 | |
663 | | - const hypervisor = new Hypervisor(node.dag) |
664 | | - |
665 | 678 | hypervisor.registerContainer(Root) |
666 | 679 | hypervisor.registerContainer(First) |
667 | 680 | |
668 | | - const root = await hypervisor.createInstance(new Message({ |
| 681 | + let root = await hypervisor.send(creationPort, new Message({ |
669 | 682 | data: { |
670 | 683 | type: Root.typeId |
671 | 684 | } |
672 | 685 | })) |
673 | 686 | |
674 | 687 | hypervisor.pin(root) |
| 688 | + root = await hypervisor.getInstance(root.id) |
675 | 689 | |
676 | 690 | const [portRef1, portRef2] = root.ports.createChannel() |
677 | 691 | await root.ports.bind('first', portRef1) |
678 | | - await root.createInstance(root.createMessage({ |
| 692 | + await root.send(creationPort, root.createMessage({ |
679 | 693 | data: { |
680 | 694 | type: Root.typeId |
681 | 695 | }, |
682 | 696 | ports: [portRef2] |
684 | 698 | |
685 | 699 | const message = root.createMessage() |
686 | 700 | await root.send(portRef1, message) |
687 | 701 | |
| 702 | + root.shutdown() |
| 703 | + |
688 | 704 | const sr = await hypervisor.createStateRoot() |
689 | 705 | t.deepEquals(sr, expectedSr, 'should produce the corret state root') |
690 | 706 | await hypervisor.graph.tree(sr, Infinity, true) |
691 | 707 | |
696 | 712 | const expectedSr = { |
697 | 713 | '/': 'zdpuAxKfu5nMTfpz6uHPqXdHZFQDZdRUer8zcQ6nvC4pTQsop' |
698 | 714 | } |
699 | 715 | |
| 716 | + const hypervisor = new Hypervisor(node.dag) |
| 717 | + const creationPort = hypervisor.creationService.getPort() |
| 718 | + |
700 | 719 | class Root extends BaseContainer { |
701 | 720 | onMessage (m) { |
702 | | - return this.kernel.createInstance(new Message({ |
| 721 | + return this.kernel.send(creationPort, new Message({ |
703 | 722 | data: { |
704 | 723 | type: Root.typeId |
705 | 724 | } |
706 | 725 | })) |
707 | 726 | } |
708 | 727 | } |
709 | 728 | |
710 | | - const hypervisor = new Hypervisor(node.dag) |
711 | 729 | hypervisor.registerContainer(Root) |
712 | 730 | |
713 | | - const root = await hypervisor.createInstance(new Message({ |
| 731 | + let root = await hypervisor.send(creationPort, new Message({ |
714 | 732 | data: { |
715 | 733 | type: Root.typeId |
716 | 734 | } |
717 | 735 | })) |
718 | 736 | |
| 737 | + root = await hypervisor.getInstance(root.id) |
719 | 738 | hypervisor.pin(root) |
720 | 739 | |
721 | 740 | const [portRef1, portRef2] = root.ports.createChannel() |
722 | 741 | await root.ports.bind('first', portRef1) |
723 | | - await root.createInstance(root.createMessage({ |
| 742 | + await root.send(creationPort, root.createMessage({ |
724 | 743 | data: { |
725 | 744 | type: Root.typeId |
726 | 745 | }, |
727 | 746 | ports: [portRef2] |
728 | 747 | })) |
729 | 748 | |
730 | 749 | const message = root.createMessage() |
731 | 750 | await root.send(portRef1, message) |
| 751 | + root.shutdown() |
732 | 752 | const sr = await hypervisor.createStateRoot() |
733 | 753 | t.deepEquals(sr, expectedSr, 'should produce the corret state root') |
734 | 754 | |
735 | 755 | t.end() |
738 | 758 | tape('should remove subgraphs', async t => { |
739 | 759 | const expectedSr = { |
740 | 760 | '/': 'zdpuAxKfu5nMTfpz6uHPqXdHZFQDZdRUer8zcQ6nvC4pTQsop' |
741 | 761 | } |
| 762 | + |
| 763 | + const hypervisor = new Hypervisor(node.dag) |
| 764 | + const creationPort = hypervisor.creationService.getPort() |
| 765 | + |
742 | 766 | class Root extends BaseContainer { |
743 | 767 | onMessage (m) { |
744 | 768 | const [, portRef2] = this.kernel.ports.createChannel() |
745 | | - return this.kernel.createInstance(this.kernel.createMessage({ |
| 769 | + return this.kernel.send(creationPort, this.kernel.createMessage({ |
746 | 770 | data: { |
747 | 771 | type: Sub.typeId |
748 | 772 | }, |
749 | 773 | ports: [portRef2] |
755 | 779 | async onInitailize (message) { |
756 | 780 | await this.kernel.ports.bind('root', message.ports[0]) |
757 | 781 | const [portRef1, portRef2] = this.kernel.ports.createChannel() |
758 | 782 | await this.kernel.ports.bind('child', portRef1) |
759 | | - await this.kernel.createInstance(this.kernel.createMessage({ |
| 783 | + await this.kernel.send(creationPort, this.kernel.createMessage({ |
760 | 784 | data: { |
761 | 785 | type: Root.typeId |
762 | 786 | }, |
763 | 787 | ports: [portRef2] |
768 | 792 | } |
769 | 793 | } |
770 | 794 | |
771 | 795 | try { |
772 | | - const hypervisor = new Hypervisor(node.dag) |
773 | | - |
774 | 796 | hypervisor.registerContainer(Root) |
775 | 797 | hypervisor.registerContainer(Sub) |
776 | 798 | |
777 | | - const root = await hypervisor.createInstance(new Message({ |
| 799 | + let root = await hypervisor.send(creationPort, new Message({ |
778 | 800 | data: { |
779 | 801 | type: Root.typeId |
780 | 802 | } |
781 | 803 | })) |
782 | 804 | |
| 805 | + root = await hypervisor.getInstance(root.id) |
| 806 | + |
783 | 807 | hypervisor.pin(root) |
784 | 808 | |
785 | 809 | const [portRef1, portRef2] = root.ports.createChannel() |
786 | 810 | await root.ports.bind('first', portRef1) |
787 | | - await root.createInstance(root.createMessage({ |
| 811 | + await root.send(creationPort, root.createMessage({ |
788 | 812 | data: { |
789 | 813 | type: Root.typeId |
790 | 814 | }, |
791 | 815 | ports: [portRef2] |
792 | 816 | })) |
793 | 817 | |
794 | 818 | await root.send(portRef1, root.createMessage()) |
| 819 | + root.shutdown() |
795 | 820 | const sr = await hypervisor.createStateRoot() |
796 | 821 | |
797 | 822 | t.deepEquals(sr, expectedSr, 'should produce the corret state root') |
798 | 823 | t.end() |
804 | 829 | tape('should not remove connected nodes', async t => { |
805 | 830 | const expectedSr = { |
806 | 831 | '/': 'zdpuAr4A3i1t6B7BkLT9C7DoxwvFnNg74gEzyqhpFj7nqVBy6' |
807 | 832 | } |
| 833 | + |
| 834 | + const hypervisor = new Hypervisor(node.dag) |
| 835 | + const creationPort = hypervisor.creationService.getPort() |
| 836 | + |
808 | 837 | class Root extends BaseContainer { |
809 | 838 | async onMessage (m) { |
810 | 839 | if (m.ports.length) { |
811 | 840 | const port = this.kernel.ports.get('test1') |
812 | 841 | await this.kernel.send(port, m) |
813 | 842 | return this.kernel.ports.unbind('test1') |
814 | 843 | } else { |
815 | 844 | const [portRef1, portRef2] = this.kernel.ports.createChannel() |
816 | | - await this.kernel.createInstance(this.kernel.createMessage({ |
| 845 | + await this.kernel.send(creationPort, this.kernel.createMessage({ |
817 | 846 | data: { |
818 | 847 | type: Sub.typeId |
819 | 848 | }, |
820 | 849 | ports: [portRef2] |
821 | 850 | })) |
822 | 851 | await this.kernel.ports.bind('test1', portRef1) |
823 | 852 | |
824 | 853 | const [portRef3, portRef4] = this.kernel.ports.createChannel() |
825 | | - await this.kernel.createInstance(this.kernel.createMessage({ |
| 854 | + await this.kernel.send(creationPort, this.kernel.createMessage({ |
826 | 855 | data: { |
827 | 856 | type: Sub.typeId |
828 | 857 | }, |
829 | 858 | ports: [portRef4] |
853 | 882 | return 299 |
854 | 883 | } |
855 | 884 | } |
856 | 885 | |
857 | | - const hypervisor = new Hypervisor(node.dag) |
858 | | - |
859 | 886 | hypervisor.registerContainer(Root) |
860 | 887 | hypervisor.registerContainer(Sub) |
861 | 888 | |
862 | | - const root = await hypervisor.createInstance(new Message({ |
| 889 | + let root = await hypervisor.send(creationPort, new Message({ |
863 | 890 | data: { |
864 | 891 | type: Root.typeId |
865 | 892 | } |
866 | 893 | })) |
867 | 894 | |
| 895 | + root = await hypervisor.getInstance(root.id) |
868 | 896 | hypervisor.pin(root) |
869 | 897 | |
870 | 898 | const [portRef1, portRef2] = root.ports.createChannel() |
871 | 899 | await root.ports.bind('first', portRef1) |
872 | | - await root.createInstance(root.createMessage({ |
| 900 | + await root.send(creationPort, root.createMessage({ |
873 | 901 | data: { |
874 | 902 | type: Root.typeId |
875 | 903 | }, |
876 | 904 | ports: [portRef2] |
877 | 905 | })) |
878 | 906 | |
879 | 907 | await root.send(portRef1, root.createMessage()) |
| 908 | + root.shutdown() |
880 | 909 | const sr = await hypervisor.createStateRoot() |
| 910 | + |
881 | 911 | t.deepEquals(sr, expectedSr, 'should produce the corret state root') |
882 | | - |
883 | | - |
884 | 912 | t.end() |
885 | 913 | }) |
886 | 914 | |
887 | 915 | tape('should remove multiple subgraphs', async t => { |
888 | 916 | const expectedSr = { |
889 | 917 | '/': 'zdpuAzYGmZeZsi5Zer7LXCTm1AsmqpUMJAXZnEeFW2UVDZj2P' |
890 | 918 | } |
| 919 | + |
| 920 | + const hypervisor = new Hypervisor(node.dag) |
| 921 | + const creationPort = hypervisor.creationService.getPort() |
| 922 | + |
891 | 923 | class Root extends BaseContainer { |
892 | 924 | onMessage (m) { |
893 | 925 | if (m.ports.length) { |
894 | 926 | const port = this.kernel.ports.get('test1') |
900 | 932 | } else { |
901 | 933 | const [portRef1, portRef2] = this.kernel.ports.createChannel() |
902 | 934 | const [portRef3, portRef4] = this.kernel.ports.createChannel() |
903 | 935 | return Promise.all([ |
904 | | - this.kernel.createInstance(this.kernel.createMessage({ |
| 936 | + this.kernel.send(creationPort, this.kernel.createMessage({ |
905 | 937 | data: { |
906 | 938 | type: Sub.typeId |
907 | 939 | }, |
908 | 940 | ports: [portRef2] |
909 | 941 | })), |
910 | 942 | this.kernel.ports.bind('test1', portRef1), |
911 | | - this.kernel.createInstance(this.kernel.createMessage({ |
| 943 | + this.kernel.send(creationPort, this.kernel.createMessage({ |
912 | 944 | data: { |
913 | 945 | type: Sub.typeId |
914 | 946 | }, |
915 | 947 | ports: [portRef4] |
941 | 973 | } |
942 | 974 | } |
943 | 975 | |
944 | 976 | try { |
945 | | - const hypervisor = new Hypervisor(node.dag) |
946 | | - |
947 | 977 | hypervisor.registerContainer(Root) |
948 | 978 | hypervisor.registerContainer(Sub) |
949 | 979 | |
950 | | - const root = await hypervisor.createInstance(new Message({ |
| 980 | + let root = await hypervisor.send(creationPort, new Message({ |
951 | 981 | data: { |
952 | 982 | type: Root.typeId |
953 | 983 | } |
954 | 984 | })) |
955 | 985 | |
| 986 | + root = await hypervisor.getInstance(root.id) |
956 | 987 | hypervisor.pin(root) |
957 | 988 | |
958 | 989 | const [portRef1, portRef2] = root.ports.createChannel() |
959 | 990 | await Promise.all([ |
960 | 991 | root.ports.bind('first', portRef1), |
961 | | - root.createInstance(root.createMessage({ |
| 992 | + root.send(creationPort, root.createMessage({ |
962 | 993 | data: { |
963 | 994 | type: Root.typeId |
964 | 995 | }, |
965 | 996 | ports: [portRef2] |
966 | 997 | })), |
967 | 998 | root.send(portRef1, root.createMessage()) |
968 | 999 | ]) |
969 | 1000 | |
| 1001 | + root.shutdown() |
| 1002 | + |
970 | 1003 | const sr = await hypervisor.createStateRoot() |
971 | 1004 | t.deepEquals(sr, expectedSr, 'should produce the corret state root') |
972 | 1005 | |
973 | 1006 | t.end() |
979 | 1012 | tape('response ports', async t => { |
980 | 1013 | t.plan(2) |
981 | 1014 | let runs = 0 |
982 | 1015 | const returnValue = 'this is a test' |
| 1016 | + const hypervisor = new Hypervisor(node.dag) |
| 1017 | + const creationPort = hypervisor.creationService.getPort() |
983 | 1018 | |
984 | 1019 | class testVMContainer extends BaseContainer { |
985 | 1020 | onMessage (m) { |
986 | 1021 | runs++ |
991 | 1026 | } |
992 | 1027 | } |
993 | 1028 | } |
994 | 1029 | |
995 | | - const hypervisor = new Hypervisor(node.dag) |
996 | 1030 | hypervisor.registerContainer(testVMContainer) |
997 | 1031 | |
998 | | - const rootContainer = await hypervisor.createInstance(new Message({ |
| 1032 | + let rootContainer = await hypervisor.send(creationPort, new Message({ |
999 | 1033 | data: { |
1000 | 1034 | type: testVMContainer.typeId |
1001 | 1035 | } |
1002 | 1036 | })) |
1003 | 1037 | |
| 1038 | + rootContainer = await hypervisor.getInstance(rootContainer.id) |
| 1039 | + |
1004 | 1040 | hypervisor.pin(rootContainer) |
1005 | 1041 | |
1006 | 1042 | const [portRef1, portRef2] = rootContainer.ports.createChannel() |
1007 | 1043 | const initMessage = rootContainer.createMessage({ |
1010 | 1046 | }, |
1011 | 1047 | ports: [portRef2] |
1012 | 1048 | }) |
1013 | 1049 | |
1014 | | - rootContainer.createInstance(initMessage) |
| 1050 | + rootContainer.send(creationPort, initMessage) |
1015 | 1051 | |
1016 | 1052 | await rootContainer.ports.bind('first', portRef1) |
1017 | 1053 | const message = rootContainer.createMessage() |
1018 | 1054 | const rPort = rootContainer.getResponsePort(message) |
1033 | 1069 | } |
1034 | 1070 | } |
1035 | 1071 | |
1036 | 1072 | const hypervisor = new Hypervisor(node.dag) |
| 1073 | + const creationPort = hypervisor.creationService.getPort() |
1037 | 1074 | hypervisor.registerContainer(testVMContainer) |
1038 | | - const instance = await hypervisor.createInstance(new Message({ |
| 1075 | + const instance = await hypervisor.send(creationPort, new Message({ |
1039 | 1076 | data: { |
1040 | 1077 | type: testVMContainer.typeId |
1041 | 1078 | } |
1042 | 1079 | })) |
1050 | 1087 | onMessage () {} |
1051 | 1088 | } |
1052 | 1089 | |
1053 | 1090 | const hypervisor = new Hypervisor(node.dag) |
| 1091 | + const creationPort = hypervisor.creationService.getPort() |
1054 | 1092 | hypervisor.registerContainer(testVMContainer) |
1055 | | - const oldInst = await hypervisor.createInstance(new Message({ |
| 1093 | + const oldInst = await hypervisor.send(creationPort, new Message({ |
1056 | 1094 | data: { |
1057 | 1095 | type: testVMContainer.typeId, |
1058 | 1096 | code: content |
1059 | 1097 | } |
1077 | 1115 | ports: [port2] |
1078 | 1116 | }) |
1079 | 1117 | return this.kernel.send(creationPort, message) |
1080 | 1118 | } |
1081 | | - onMessage () { |
1082 | | - |
1083 | | - } |
1084 | 1119 | } |
1085 | 1120 | |
1086 | 1121 | class TestVMContainer2 extends BaseContainer { |
1087 | | - onMessage () { |
1088 | | - |
1089 | | - } |
1090 | | - |
1091 | 1122 | static get typeId () { |
1092 | 1123 | return 66 |
1093 | 1124 | } |
1094 | 1125 | } |
1097 | 1128 | hypervisor.registerContainer(TestVMContainer) |
1098 | 1129 | hypervisor.registerContainer(TestVMContainer2) |
1099 | 1130 | |
1100 | 1131 | const port = hypervisor.creationService.getPort() |
| 1132 | + const port2 = hypervisor.creationService.getPort() |
1101 | 1133 | |
1102 | | - const root = await hypervisor.createInstance(new Message({ |
| 1134 | + const root = await hypervisor.send(port2, new Message({ |
1103 | 1135 | data: { |
1104 | 1136 | type: TestVMContainer.typeId |
1105 | 1137 | }, |
1106 | 1138 | ports: [port] |