tests/index.jsView |
---|
9 | 9 | start: false |
10 | 10 | }) |
11 | 11 | |
12 | 12 | class BaseContainer extends AbstractContainer { |
13 | | - onCreation (message) { |
| 13 | + onCreation(message) { |
14 | 14 | const port = message.ports[0] |
15 | 15 | if (port) { |
16 | 16 | return this.kernel.ports.bind('root', port) |
17 | 17 | } |
18 | 18 | } |
19 | | - static get typeId () { |
| 19 | + static get typeId() { |
20 | 20 | return 9 |
21 | 21 | } |
22 | 22 | } |
23 | 23 | |
29 | 29 | '/': 'zdpuAqbcQhgu2T2MBgHbYu1MtHXyZzNsCaQjTPTR6NN9s5hbk' |
30 | 30 | } |
31 | 31 | |
32 | 32 | class testVMContainer extends BaseContainer { |
33 | | - onMessage (m) { |
| 33 | + onMessage(m) { |
34 | 34 | t.true(m === message, 'should recive a message') |
35 | 35 | } |
36 | 36 | } |
37 | 37 | |
76 | 76 | '/': 'zdpuAop4nt8pqzg7duciSYbZmWfDaBiz87RCtGCbb35ewUrbW' |
77 | 77 | } |
78 | 78 | |
79 | 79 | class testVMContainer extends BaseContainer { |
80 | | - onCreation () {} |
| 80 | + onCreation() {} |
81 | 81 | } |
82 | 82 | |
83 | 83 | try { |
84 | 84 | const hypervisor = new Hypervisor(node.dag) |
117 | 117 | } |
118 | 118 | let hasResolved = false |
119 | 119 | |
120 | 120 | class testVMContainer2 extends BaseContainer { |
121 | | - onMessage (m) { |
| 121 | + onMessage(m) { |
122 | 122 | t.true(m === message, 'should recive a message') |
123 | 123 | return new Promise((resolve, reject) => { |
124 | 124 | setTimeout(() => { |
125 | 125 | this.kernel.incrementTicks(1) |
128 | 128 | }, 200) |
129 | 129 | }) |
130 | 130 | } |
131 | 131 | |
132 | | - static get typeId () { |
| 132 | + static get typeId() { |
133 | 133 | return 99 |
134 | 134 | } |
135 | 135 | } |
136 | 136 | |
137 | 137 | class testVMContainer extends BaseContainer { |
138 | | - async onMessage (m) { |
| 138 | + async onMessage(m) { |
139 | 139 | const [portRef1, portRef2] = this.kernel.ports.createChannel() |
140 | 140 | await this.kernel.createInstance(this.kernel.createMessage({ |
141 | 141 | data: { |
142 | 142 | type: testVMContainer2.typeId |
179 | 179 | t.deepEquals(stateRoot, expectedState, 'expected state') |
180 | 180 | |
181 | 181 | |
182 | 182 | class testVMContainer3 extends BaseContainer { |
183 | | - onMessage (m) { |
| 183 | + onMessage(m) { |
184 | 184 | const port = this.kernel.ports.get('child') |
185 | 185 | this.kernel.send(port, m) |
186 | 186 | this.kernel.incrementTicks(1) |
187 | 187 | } |
195 | 195 | |
196 | 196 | tape('traps', async t => { |
197 | 197 | t.plan(1) |
198 | 198 | class Root extends BaseContainer { |
199 | | - async onMessage (m) { |
| 199 | + async onMessage(m) { |
200 | 200 | const [portRef1, portRef2] = this.kernel.ports.createChannel() |
201 | 201 | const [portRef3, portRef4] = this.kernel.ports.createChannel() |
202 | 202 | const [portRef5, portRef6] = this.kernel.ports.createChannel() |
203 | 203 | |
256 | 256 | t.plan(2) |
257 | 257 | let runs = 0 |
258 | 258 | |
259 | 259 | class Root extends BaseContainer { |
260 | | - async onMessage (m) { |
| 260 | + async onMessage(m) { |
261 | 261 | if (!runs) { |
262 | 262 | runs++ |
263 | 263 | const [portRef1, portRef2] = this.kernel.ports.createChannel() |
264 | 264 | const [portRef3, portRef4] = this.kernel.ports.createChannel() |
294 | 294 | runs++ |
295 | 295 | |
296 | 296 | } |
297 | 297 | } |
298 | | - static get typeId () { |
| 298 | + static get typeId() { |
299 | 299 | return 299 |
300 | 300 | } |
301 | 301 | } |
302 | 302 | |
303 | 303 | class First extends BaseContainer { |
304 | | - onMessage (m) { |
| 304 | + onMessage(m) { |
305 | 305 | this.kernel.incrementTicks(2) |
306 | 306 | return this.kernel.send(m.fromPort, this.kernel.createMessage({ |
307 | 307 | data: 'second' |
308 | 308 | })) |
309 | 309 | } |
310 | | - static get typeId () { |
| 310 | + static get typeId() { |
311 | 311 | return 29 |
312 | 312 | } |
313 | 313 | } |
314 | 314 | |
315 | 315 | class Waiter extends BaseContainer { |
316 | | - onMessage (m) { |
| 316 | + onMessage(m) { |
317 | 317 | return new Promise((resolve, reject) => { |
318 | 318 | setTimeout(() => { |
319 | 319 | this.kernel.send(m.fromPort, this.kernel.createMessage({ |
320 | 320 | data: 'first' |
358 | 358 | t.plan(3) |
359 | 359 | let runs = 0 |
360 | 360 | |
361 | 361 | class Root extends BaseContainer { |
362 | | - onIdle () {} |
363 | | - async onMessage (m) { |
| 362 | + onIdle() {} |
| 363 | + async onMessage(m) { |
364 | 364 | if (!runs) { |
365 | 365 | runs++ |
366 | 366 | const [portRef1, portRef2] = this.kernel.ports.createChannel() |
367 | 367 | const [portRef3, portRef4] = this.kernel.ports.createChannel() |
398 | 398 | runs++ |
399 | 399 | t.equals(m.data, 'third', 'should recived the third message') |
400 | 400 | } |
401 | 401 | } |
402 | | - static get typeId () { |
| 402 | + static get typeId() { |
403 | 403 | return 299 |
404 | 404 | } |
405 | 405 | } |
406 | 406 | |
407 | 407 | class First extends BaseContainer { |
408 | | - onMessage (m) { |
| 408 | + onMessage(m) { |
409 | 409 | this.kernel.incrementTicks(2) |
410 | 410 | return this.kernel.send(m.fromPort, this.kernel.createMessage({ |
411 | 411 | data: 'second' |
412 | 412 | })) |
413 | 413 | } |
414 | | - static get typeId () { |
| 414 | + static get typeId() { |
415 | 415 | return 29 |
416 | 416 | } |
417 | 417 | } |
418 | 418 | |
419 | 419 | class Second extends BaseContainer { |
420 | | - onMessage (m) { |
| 420 | + onMessage(m) { |
421 | 421 | this.kernel.incrementTicks(3) |
422 | 422 | return this.kernel.send(m.fromPort, this.kernel.createMessage({ |
423 | 423 | data: 'third' |
424 | 424 | })) |
425 | 425 | } |
426 | | - static get typeId () { |
| 426 | + static get typeId() { |
427 | 427 | return 2 |
428 | 428 | } |
429 | 429 | } |
430 | 430 | |
431 | 431 | class Waiter extends BaseContainer { |
432 | | - onCreation (m) { |
| 432 | + onCreation(m) { |
433 | 433 | return new Promise((resolve, reject) => { |
434 | 434 | setTimeout(() => { |
435 | 435 | this.kernel.send(m.ports[0], this.kernel.createMessage({ |
436 | 436 | data: 'first' |
492 | 492 | let runs = 0 |
493 | 493 | let instance |
494 | 494 | |
495 | 495 | class Root extends BaseContainer { |
496 | | - async onMessage (m) { |
| 496 | + async onMessage(m) { |
497 | 497 | let one = this.kernel.ports.get('one') |
498 | 498 | if (!one) { |
499 | 499 | const [portRef1, portRef2] = this.kernel.ports.createChannel() |
500 | 500 | const message1 = this.kernel.createMessage({ |
511 | 511 | this.kernel.send(one, this.kernel.createMessage()) |
512 | 512 | ]) |
513 | 513 | } |
514 | 514 | } |
515 | | - static get typeId () { |
| 515 | + static get typeId() { |
516 | 516 | return 299 |
517 | 517 | } |
518 | 518 | } |
519 | 519 | |
520 | 520 | class First extends BaseContainer { |
521 | | - onMessage (m) { |
| 521 | + onMessage(m) { |
522 | 522 | ++runs |
523 | 523 | if (runs === 2) { |
524 | 524 | t.equals(instance, this, 'should have same instances') |
525 | 525 | } else { |
613 | 613 | const expectedSr = { |
614 | 614 | '/': 'zdpuAxKfu5nMTfpz6uHPqXdHZFQDZdRUer8zcQ6nvC4pTQsop' |
615 | 615 | } |
616 | 616 | class Root extends BaseContainer { |
617 | | - async onMessage (m) { |
| 617 | + async onMessage(m) { |
618 | 618 | const [portRef1, portRef2] = this.kernel.ports.createChannel() |
619 | 619 | const message1 = this.kernel.createMessage({ |
620 | 620 | data: { |
621 | 621 | type: First.typeId |
630 | 630 | } |
631 | 631 | } |
632 | 632 | |
633 | 633 | class First extends BaseContainer { |
634 | | - onMessage (m) { |
| 634 | + onMessage(m) { |
635 | 635 | this.kernel.incrementTicks(2) |
636 | 636 | return this.kernel.ports.delete('root') |
637 | 637 | } |
638 | | - static get typeId () { |
| 638 | + static get typeId() { |
639 | 639 | return 299 |
640 | 640 | } |
641 | 641 | } |
642 | 642 | |
674 | 674 | '/': 'zdpuAxKfu5nMTfpz6uHPqXdHZFQDZdRUer8zcQ6nvC4pTQsop' |
675 | 675 | } |
676 | 676 | |
677 | 677 | class Root extends BaseContainer { |
678 | | - onMessage (m) { |
| 678 | + onMessage(m) { |
679 | 679 | return this.kernel.createInstance(new Message({ |
680 | 680 | data: { |
681 | 681 | type: Root.typeId |
682 | 682 | } |
713 | 713 | const expectedSr = { |
714 | 714 | '/': 'zdpuAxKfu5nMTfpz6uHPqXdHZFQDZdRUer8zcQ6nvC4pTQsop' |
715 | 715 | } |
716 | 716 | class Root extends BaseContainer { |
717 | | - onMessage (m) { |
| 717 | + onMessage(m) { |
718 | 718 | const [, portRef2] = this.kernel.ports.createChannel() |
719 | 719 | return this.kernel.createInstance(this.kernel.createMessage({ |
720 | 720 | data: { |
721 | 721 | type: Sub.typeId |
725 | 725 | } |
726 | 726 | } |
727 | 727 | |
728 | 728 | class Sub extends BaseContainer { |
729 | | - async onInitailize (message) { |
| 729 | + async onInitailize(message) { |
730 | 730 | await this.kernel.ports.bind('root', message.ports[0]) |
731 | 731 | const [portRef1, portRef2] = this.kernel.ports.createChannel() |
732 | 732 | await this.kernel.ports.bind('child', portRef1) |
733 | 733 | await this.kernel.createInstance(this.kernel.createMessage({ |
736 | 736 | }, |
737 | 737 | ports: [portRef2] |
738 | 738 | })) |
739 | 739 | } |
740 | | - static get typeId () { |
| 740 | + static get typeId() { |
741 | 741 | return 299 |
742 | 742 | } |
743 | 743 | } |
744 | 744 | |
776 | 776 | const expectedSr = { |
777 | 777 | '/': 'zdpuAr4A3i1t6B7BkLT9C7DoxwvFnNg74gEzyqhpFj7nqVBy6' |
778 | 778 | } |
779 | 779 | class Root extends BaseContainer { |
780 | | - async onMessage (m) { |
| 780 | + async onMessage(m) { |
781 | 781 | if (m.ports.length) { |
782 | 782 | const port = this.kernel.ports.get('test1') |
783 | 783 | await this.kernel.send(port, m) |
784 | 784 | return this.kernel.ports.unbind('test1') |
807 | 807 | } |
808 | 808 | } |
809 | 809 | |
810 | 810 | class Sub extends BaseContainer { |
811 | | - async onMessage (message) { |
| 811 | + async onMessage(message) { |
812 | 812 | if (message.data === 'getChannel') { |
813 | 813 | const ports = this.kernel.ports.createChannel() |
814 | 814 | await this.kernel.send(message.fromPort, this.kernel.createMessage({ |
815 | 815 | data: 'bindPort', |
819 | 819 | } else if (message.data === 'bindPort') { |
820 | 820 | return this.kernel.ports.bind('channel', message.ports[0]) |
821 | 821 | } |
822 | 822 | } |
823 | | - static get typeId () { |
| 823 | + static get typeId() { |
824 | 824 | return 299 |
825 | 825 | } |
826 | 826 | } |
827 | 827 | |
851 | 851 | |
852 | 852 | t.end() |
853 | 853 | }) |
854 | 854 | |
855 | | - tape.only('should remove multiple subgraphs', async t => { |
| 855 | + tape('should remove multiple subgraphs', async t => { |
856 | 856 | const expectedSr = { |
857 | 857 | '/': 'zdpuAzYGmZeZsi5Zer7LXCTm1AsmqpUMJAXZnEeFW2UVDZj2P' |
858 | 858 | } |
859 | 859 | class Root extends BaseContainer { |
860 | | - onMessage (m) { |
| 860 | + onMessage(m) { |
861 | 861 | if (m.ports.length) { |
862 | 862 | const port = this.kernel.ports.get('test1') |
863 | 863 | return Promise.all([ |
864 | 864 | this.kernel.send(port, m), |
891 | 891 | } |
892 | 892 | } |
893 | 893 | |
894 | 894 | class Sub extends BaseContainer { |
895 | | - async onMessage (message) { |
| 895 | + async onMessage(message) { |
896 | 896 | if (message.data === 'getChannel') { |
897 | 897 | const ports = this.kernel.ports.createChannel() |
898 | 898 | await this.kernel.send(message.fromPort, this.kernel.createMessage({ |
899 | 899 | data: 'bindPort', |
903 | 903 | } else if (message.data === 'bindPort') { |
904 | 904 | return this.kernel.ports.bind('channel', message.ports[0]) |
905 | 905 | } |
906 | 906 | } |
907 | | - static get typeId () { |
| 907 | + static get typeId() { |
908 | 908 | return 299 |
909 | 909 | } |
910 | 910 | } |
911 | 911 | |
947 | 947 | let runs = 0 |
948 | 948 | const returnValue = 'this is a test' |
949 | 949 | |
950 | 950 | class testVMContainer extends BaseContainer { |
951 | | - onMessage (m) { |
| 951 | + onMessage(m) { |
952 | 952 | runs++ |
953 | 953 | if (runs === 1) { |
954 | 954 | return returnValue |
955 | 955 | } else { |
991 | 991 | |
992 | 992 | tape('start up', async t => { |
993 | 993 | t.plan(1) |
994 | 994 | class testVMContainer extends BaseContainer { |
995 | | - onMessage () {} |
996 | | - onStartup () { |
| 995 | + onMessage() {} |
| 996 | + onStartup() { |
997 | 997 | t.true(true, 'should start up') |
998 | 998 | } |
999 | 999 | } |
1000 | 1000 | |
1011 | 1011 | tape('large code size', async t => { |
1012 | 1012 | t.plan(1) |
1013 | 1013 | const content = Buffer.from(new ArrayBuffer(1000000)) |
1014 | 1014 | class testVMContainer extends BaseContainer { |
1015 | | - onMessage () {} |
| 1015 | + onMessage() {} |
1016 | 1016 | } |
1017 | 1017 | |
1018 | 1018 | const hypervisor = new Hypervisor(node.dag) |
1019 | 1019 | hypervisor.registerContainer(testVMContainer) |
1026 | 1026 | const instance = await hypervisor.getInstance(hypervisor.ROOT_ID) |
1027 | 1027 | t.equals(content.length, instance.code.length) |
1028 | 1028 | }) |
1029 | 1029 | |
1030 | | - tape.skip('creation service messaging', async t => { |
| 1030 | + tape('creation service messaging', async t => { |
1031 | 1031 | t.plan(1) |
1032 | 1032 | class TestVMContainer extends BaseContainer { |
1033 | 1033 | async onCreation (m) { |
1034 | 1034 | const creationPort = m.ports[0] |
1070 | 1070 | }, |
1071 | 1071 | ports: [port] |
1072 | 1072 | })) |
1073 | 1073 | |
1074 | | - await hypervisor.createStateRoot() |
1075 | | - await hypervisor.graph.tree(hypervisor.state, Infinity, true) |
1076 | | - console.log(JSON.stringify(hypervisor.state, null, 2)) |
| 1074 | + const stateRoot = await hypervisor.createStateRoot() |
| 1075 | + |
| 1076 | + console.log(stateRoot) |
| 1077 | + const expectedSR = { |
| 1078 | + '/': 'zdpuAonuhk7ZhdghJh4saaUCskY5mXZ6M9BcV9iAhCanAQx9i' |
| 1079 | + } |
| 1080 | + t.deepEquals(stateRoot, expectedSR) |
1077 | 1081 | }) |
1078 | 1082 | }) |