tests/index.jsView |
---|
45 | 45 | const hypervisor = new Hypervisor({dag: node.dag}) |
46 | 46 | hypervisor.registerContainer('test', testVMContainer) |
47 | 47 | |
48 | 48 | const rootContainer = await hypervisor.createInstance('test') |
49 | | - const port = await rootContainer.ports.create('test', 'first') |
| 49 | + const port = rootContainer.ports.create('test') |
| 50 | + rootContainer.ports.bind(port, 'first') |
50 | 51 | |
51 | 52 | await rootContainer.send(port, message) |
52 | 53 | |
53 | 54 | const stateRoot = await hypervisor.createStateRoot(rootContainer, Infinity) |
76 | 77 | } |
77 | 78 | |
78 | 79 | class testVMContainer extends BaseContainer { |
79 | 80 | async run (m) { |
80 | | - const port = await this.kernel.ports.create('test2', 'child') |
| 81 | + const port = this.kernel.ports.create('test2') |
| 82 | + this.kernel.ports.bind(port, 'child') |
81 | 83 | await this.kernel.send(port, m) |
82 | 84 | this.kernel.incrementTicks(1) |
83 | 85 | } |
84 | 86 | } |
87 | 89 | hypervisor.registerContainer('test', testVMContainer) |
88 | 90 | hypervisor.registerContainer('test2', testVMContainer2) |
89 | 91 | |
90 | 92 | let root = await hypervisor.createInstance('test') |
91 | | - let port = await root.ports.create('test', 'first') |
| 93 | + let port = root.ports.create('test') |
92 | 94 | |
| 95 | + root.ports.bind(port, 'first') |
| 96 | + |
93 | 97 | await root.send(port, message) |
94 | 98 | const stateRoot = await hypervisor.createStateRoot(root, Infinity) |
95 | 99 | t.true(hasResolved, 'should resolve before generating the state root') |
96 | 100 | t.deepEquals(stateRoot, expectedState, 'expected state') |
105 | 109 | } |
106 | 110 | |
107 | 111 | hypervisor.registerContainer('test', testVMContainer3) |
108 | 112 | root = await hypervisor.createInstance('test', stateRoot) |
109 | | - port = await root.ports.get('first') |
| 113 | + port = root.ports.get('first') |
110 | 114 | |
111 | | - await root.send(port, message) |
| 115 | + root.send(port, message) |
112 | 116 | }) |
113 | 117 | |
114 | 118 | tape('ping pong', async t => { |
115 | 119 | class Ping extends BaseContainer { |
116 | 120 | async run (m) { |
117 | 121 | let port = this.kernel.ports.get('child') |
118 | 122 | if (!port) { |
119 | | - port = await this.kernel.ports.create('pong', 'child') |
| 123 | + port = this.kernel.ports.create('pong') |
| 124 | + this.kernel.ports.bind(port, 'child') |
120 | 125 | } |
121 | 126 | |
122 | 127 | if (this.kernel.ticks < 100) { |
123 | 128 | this.kernel.incrementTicks(1) |
140 | 145 | |
141 | 146 | hypervisor.registerContainer('ping', Ping) |
142 | 147 | hypervisor.registerContainer('pong', Pong) |
143 | 148 | const root = await hypervisor.createInstance('pong') |
144 | | - const port = await root.ports.create('ping', 'child') |
| 149 | + const port = root.ports.create('ping') |
| 150 | + root.ports.bind(port, 'child') |
145 | 151 | |
146 | 152 | await root.send(port, new Message()) |
147 | 153 | await hypervisor.createStateRoot(root, Infinity) |
148 | 154 | |
154 | 160 | let runs = 0 |
155 | 161 | |
156 | 162 | class Root extends BaseContainer { |
157 | 163 | async run (m) { |
158 | | - const one = this.kernel.ports.create('child', 'one') |
159 | | - const two = this.kernel.ports.create('child', 'two') |
160 | | - const three = this.kernel.ports.create('child', 'three') |
| 164 | + const one = this.kernel.ports.create('child') |
| 165 | + const two = this.kernel.ports.create('child') |
| 166 | + const three = this.kernel.ports.create('child') |
161 | 167 | |
| 168 | + this.kernel.ports.bind(one, 'one') |
| 169 | + this.kernel.ports.bind(two, 'two') |
| 170 | + this.kernel.ports.bind(three, 'three') |
| 171 | + |
162 | 172 | await Promise.all([ |
163 | 173 | this.kernel.send(one, new Message()), |
164 | 174 | this.kernel.send(two, new Message()), |
165 | 175 | this.kernel.send(three, new Message()) |
188 | 198 | hypervisor.registerContainer('root', Root) |
189 | 199 | hypervisor.registerContainer('child', Child) |
190 | 200 | |
191 | 201 | const root = await hypervisor.createInstance('root') |
192 | | - const port = await root.ports.create('root', 'first') |
| 202 | + const port = root.ports.create('root') |
| 203 | + root.ports.bind(port, 'first') |
| 204 | + |
193 | 205 | await root.send(port, new Message()) |
194 | 206 | await root.wait(Infinity) |
195 | 207 | |
196 | 208 | t.equals(runs, 3, 'the number of run should be 3') |
201 | 213 | tape('traps', async t => { |
202 | 214 | t.plan(1) |
203 | 215 | class Root extends BaseContainer { |
204 | 216 | async run (m) { |
205 | | - this.kernel.ports.create('root', 'one') |
206 | | - this.kernel.ports.create('root', 'two') |
207 | | - this.kernel.ports.create('root', 'three') |
| 217 | + const one = this.kernel.ports.create('child') |
| 218 | + const two = this.kernel.ports.create('child') |
| 219 | + const three = this.kernel.ports.create('child') |
208 | 220 | |
| 221 | + this.kernel.ports.bind(one, 'one') |
| 222 | + this.kernel.ports.bind(two, 'two') |
| 223 | + this.kernel.ports.bind(three, 'three') |
| 224 | + |
209 | 225 | throw new Error('it is a trap!!!') |
210 | 226 | } |
211 | 227 | } |
212 | 228 | |
229 | 245 | tape('invalid port referances', async t => { |
230 | 246 | t.plan(2) |
231 | 247 | class Root extends BaseContainer { |
232 | 248 | async run (m) { |
233 | | - const ports = this.kernel.ports.create('root', 'three') |
| 249 | + const port = this.kernel.ports.create('root') |
| 250 | + this.kernel.ports.bind(port, 'three') |
234 | 251 | this.kernel.ports.delete('three') |
235 | 252 | try { |
236 | | - await this.kernel.send(ports, new Message()) |
| 253 | + await this.kernel.send(port, new Message()) |
237 | 254 | } catch (e) { |
238 | 255 | t.pass() |
239 | 256 | } |
240 | 257 | } |
262 | 279 | class Root extends BaseContainer { |
263 | 280 | async run (m) { |
264 | 281 | if (!this.runs) { |
265 | 282 | this.runs = 1 |
266 | | - const one = this.kernel.ports.create('first', 'one') |
267 | | - const two = this.kernel.ports.create('second', 'two') |
| 283 | + const one = this.kernel.ports.create('first') |
| 284 | + const two = this.kernel.ports.create('second') |
268 | 285 | |
| 286 | + this.kernel.ports.bind(one, 'one') |
| 287 | + this.kernel.ports.bind(two, 'two') |
| 288 | + |
269 | 289 | await Promise.all([ |
270 | 290 | this.kernel.send(one, new Message()), |
271 | 291 | this.kernel.send(two, new Message()) |
272 | 292 | ]) |
303 | 323 | hypervisor.registerContainer('first', First) |
304 | 324 | hypervisor.registerContainer('second', Second) |
305 | 325 | |
306 | 326 | const root = await hypervisor.createInstance('root') |
307 | | - const port = await root.ports.create('root', 'first') |
308 | | - await root.send(port, new Message()) |
| 327 | + const port = root.ports.create('root') |
| 328 | + root.ports.bind(port, 'first') |
| 329 | + |
| 330 | + root.send(port, new Message()) |
309 | 331 | }) |
310 | 332 | |
311 | 333 | tape('message should arrive in the correct order, even if sent out of order', async t => { |
312 | 334 | t.plan(2) |
313 | 335 | |
314 | 336 | class Root extends BaseContainer { |
315 | | - async run (m) { |
| 337 | + run (m) { |
316 | 338 | if (!this.runs) { |
317 | 339 | this.runs = 1 |
318 | | - const one = this.kernel.ports.create('first', 'one') |
319 | | - const two = this.kernel.ports.create('second', 'two') |
| 340 | + const one = this.kernel.ports.create('first') |
| 341 | + const two = this.kernel.ports.create('second') |
320 | 342 | |
321 | | - await Promise.all([ |
| 343 | + this.kernel.ports.bind(one, 'one') |
| 344 | + this.kernel.ports.bind(two, 'two') |
| 345 | + |
| 346 | + return Promise.all([ |
322 | 347 | this.kernel.send(one, new Message()), |
323 | 348 | this.kernel.send(two, new Message()) |
324 | 349 | ]) |
325 | | - |
326 | | - this.kernel.incrementTicks(6) |
327 | 350 | } else if (this.runs === 1) { |
328 | 351 | this.runs++ |
329 | 352 | t.equals(m.data, 'second', 'should recive the first message') |
330 | 353 | } else if (this.runs === 2) { |
333 | 356 | } |
334 | 357 | } |
335 | 358 | |
336 | 359 | class First extends BaseContainer { |
337 | | - async run (m) { |
| 360 | + run (m) { |
338 | 361 | this.kernel.incrementTicks(2) |
339 | | - await this.kernel.send(m.fromPort, new Message({data: 'first'})) |
| 362 | + return this.kernel.send(m.fromPort, new Message({data: 'first'})) |
340 | 363 | } |
341 | 364 | } |
342 | 365 | |
343 | 366 | class Second extends BaseContainer { |
344 | | - async run (m) { |
| 367 | + run (m) { |
345 | 368 | this.kernel.incrementTicks(1) |
346 | | - await this.kernel.send(m.fromPort, new Message({data: 'second'})) |
| 369 | + this.kernel.send(m.fromPort, new Message({data: 'second'})) |
347 | 370 | } |
348 | 371 | } |
349 | 372 | |
350 | 373 | const hypervisor = new Hypervisor({ |
355 | 378 | hypervisor.registerContainer('first', First) |
356 | 379 | hypervisor.registerContainer('second', Second) |
357 | 380 | |
358 | 381 | const root = await hypervisor.createInstance('root') |
359 | | - const port = await root.ports.create('root', 'first') |
360 | | - await root.send(port, new Message()) |
| 382 | + const port = root.ports.create('root') |
| 383 | + root.ports.bind(port, 'first') |
| 384 | + |
| 385 | + root.send(port, new Message()) |
361 | 386 | }) |
362 | 387 | |
363 | 388 | tape('message should arrive in the correct order, even in a tie of ticks', async t => { |
364 | 389 | t.plan(2) |
366 | 391 | class Root extends BaseContainer { |
367 | 392 | async run (m) { |
368 | 393 | if (!this.runs) { |
369 | 394 | this.runs = 1 |
370 | | - const one = this.kernel.ports.create('first', 'one') |
371 | | - const two = this.kernel.ports.create('second', 'two') |
| 395 | + const one = this.kernel.ports.create('first') |
| 396 | + const two = this.kernel.ports.create('second') |
372 | 397 | |
| 398 | + this.kernel.ports.bind(one, 'one') |
| 399 | + this.kernel.ports.bind(two, 'two') |
| 400 | + |
373 | 401 | await Promise.all([ |
374 | 402 | this.kernel.send(one, new Message()), |
375 | 403 | this.kernel.send(two, new Message()) |
376 | 404 | ]) |
385 | 413 | } |
386 | 414 | } |
387 | 415 | |
388 | 416 | class First extends BaseContainer { |
389 | | - async run (m) { |
| 417 | + run (m) { |
390 | 418 | this.kernel.incrementTicks(2) |
391 | | - await this.kernel.send(m.fromPort, new Message({data: 'first'})) |
| 419 | + return this.kernel.send(m.fromPort, new Message({ |
| 420 | + data: 'first' |
| 421 | + })) |
392 | 422 | } |
393 | 423 | } |
394 | 424 | |
395 | 425 | class Second extends BaseContainer { |
396 | | - async run (m) { |
| 426 | + run (m) { |
397 | 427 | this.kernel.incrementTicks(2) |
398 | | - await this.kernel.send(m.fromPort, new Message({data: 'second'})) |
| 428 | + return this.kernel.send(m.fromPort, new Message({ |
| 429 | + data: 'second' |
| 430 | + })) |
399 | 431 | } |
400 | 432 | } |
401 | 433 | |
402 | 434 | const hypervisor = new Hypervisor({ |
407 | 439 | hypervisor.registerContainer('first', First) |
408 | 440 | hypervisor.registerContainer('second', Second) |
409 | 441 | |
410 | 442 | const root = await hypervisor.createInstance('root') |
411 | | - const port = await root.ports.create('root', 'first') |
412 | | - await root.send(port, new Message()) |
| 443 | + const port = await root.ports.create('root') |
| 444 | + root.ports.bind(port, 'first') |
| 445 | + root.send(port, new Message()) |
413 | 446 | }) |
414 | 447 | |
415 | 448 | tape('message should arrive in the correct order, even in a tie of ticks', async t => { |
416 | 449 | t.plan(2) |
417 | 450 | |
418 | 451 | class Root extends BaseContainer { |
419 | | - async run (m) { |
| 452 | + run (m) { |
420 | 453 | if (!this.runs) { |
421 | 454 | this.runs = 1 |
422 | | - const two = this.kernel.ports.create('second', 'two') |
423 | | - const one = this.kernel.ports.create('first', 'one') |
| 455 | + const two = this.kernel.ports.create('second') |
| 456 | + const one = this.kernel.ports.create('first') |
424 | 457 | |
425 | | - await Promise.all([ |
| 458 | + this.kernel.ports.bind(two, 'two') |
| 459 | + this.kernel.ports.bind(one, 'one') |
| 460 | + |
| 461 | + return Promise.all([ |
426 | 462 | this.kernel.send(two, new Message()), |
427 | 463 | this.kernel.send(one, new Message()) |
428 | 464 | ]) |
429 | | - |
430 | | - this.kernel.incrementTicks(6) |
431 | 465 | } else if (this.runs === 1) { |
432 | 466 | this.runs++ |
433 | 467 | t.equals(m.data, 'first', 'should recived the second message') |
434 | 468 | } else if (this.runs === 2) { |
437 | 471 | } |
438 | 472 | } |
439 | 473 | |
440 | 474 | class First extends BaseContainer { |
441 | | - async run (m) { |
| 475 | + run (m) { |
442 | 476 | this.kernel.incrementTicks(2) |
443 | | - await this.kernel.send(m.fromPort, new Message({data: 'first'})) |
| 477 | + return this.kernel.send(m.fromPort, new Message({ |
| 478 | + data: 'first' |
| 479 | + })) |
444 | 480 | } |
445 | 481 | } |
446 | 482 | |
447 | 483 | class Second extends BaseContainer { |
448 | | - async run (m) { |
| 484 | + run (m) { |
449 | 485 | this.kernel.incrementTicks(2) |
450 | | - await this.kernel.send(m.fromPort, new Message({data: 'second'})) |
| 486 | + return this.kernel.send(m.fromPort, new Message({ |
| 487 | + data: 'second' |
| 488 | + })) |
451 | 489 | } |
452 | 490 | } |
453 | 491 | |
454 | 492 | const hypervisor = new Hypervisor({ |
459 | 497 | hypervisor.registerContainer('first', First) |
460 | 498 | hypervisor.registerContainer('second', Second) |
461 | 499 | |
462 | 500 | const root = await hypervisor.createInstance('root') |
463 | | - const port = await root.ports.create('root', 'first') |
464 | | - await root.send(port, new Message()) |
| 501 | + |
| 502 | + const port = root.ports.create('root') |
| 503 | + root.ports.bind(port, 'first') |
| 504 | + |
| 505 | + root.send(port, new Message()) |
465 | 506 | }) |
466 | 507 | |
467 | 508 | tape('message should arrive in the correct order, with a tie in ticks but with differnt proity', async t => { |
468 | 509 | t.plan(2) |
469 | 510 | |
470 | 511 | class Root extends BaseContainer { |
471 | | - async run (m) { |
| 512 | + run (m) { |
472 | 513 | if (!this.runs) { |
473 | 514 | this.runs = 1 |
474 | | - const one = this.kernel.ports.create('first', 'one') |
475 | | - const two = this.kernel.ports.create('second', 'two') |
| 515 | + const one = this.kernel.ports.create('first') |
| 516 | + const two = this.kernel.ports.create('second') |
476 | 517 | |
477 | | - await Promise.all([ |
| 518 | + this.kernel.ports.bind(one, 'one') |
| 519 | + this.kernel.ports.bind(two, 'two') |
| 520 | + |
| 521 | + return Promise.all([ |
478 | 522 | this.kernel.send(two, new Message()), |
479 | 523 | this.kernel.send(one, new Message()) |
480 | 524 | ]) |
481 | | - |
482 | | - this.kernel.incrementTicks(6) |
483 | 525 | } else if (this.runs === 1) { |
484 | 526 | this.runs++ |
485 | 527 | t.equals(m.data, 'first', 'should recive the first message') |
486 | 528 | } else if (this.runs === 2) { |
489 | 531 | } |
490 | 532 | } |
491 | 533 | |
492 | 534 | class First extends BaseContainer { |
493 | | - async run (m) { |
| 535 | + run (m) { |
494 | 536 | this.kernel.incrementTicks(2) |
495 | | - await this.kernel.send(m.fromPort, new Message({ |
| 537 | + return this.kernel.send(m.fromPort, new Message({ |
496 | 538 | resources: { |
497 | 539 | priority: 100 |
498 | 540 | }, |
499 | 541 | data: 'first' |
501 | 543 | } |
502 | 544 | } |
503 | 545 | |
504 | 546 | class Second extends BaseContainer { |
505 | | - async run (m) { |
| 547 | + run (m) { |
506 | 548 | this.kernel.incrementTicks(2) |
507 | | - await this.kernel.send(m.fromPort, new Message({data: 'second'})) |
| 549 | + return this.kernel.send(m.fromPort, new Message({ |
| 550 | + data: 'second' |
| 551 | + })) |
508 | 552 | } |
509 | 553 | } |
510 | 554 | |
511 | 555 | const hypervisor = new Hypervisor({ |
516 | 560 | hypervisor.registerContainer('first', First) |
517 | 561 | hypervisor.registerContainer('second', Second) |
518 | 562 | |
519 | 563 | const root = await hypervisor.createInstance('root') |
520 | | - const port = await root.ports.create('root', 'first') |
521 | | - await root.send(port, new Message()) |
| 564 | + const port = root.ports.create('root') |
| 565 | + root.ports.bind(port, 'first') |
| 566 | + root.send(port, new Message()) |
522 | 567 | }) |
523 | 568 | |
524 | 569 | tape('message should arrive in the correct order, with a tie in ticks but with differnt proity', async t => { |
525 | 570 | t.plan(2) |
526 | 571 | |
527 | 572 | class Root extends BaseContainer { |
528 | | - async run (m) { |
| 573 | + run (m) { |
529 | 574 | if (!this.runs) { |
530 | 575 | this.runs = 1 |
531 | | - const one = this.kernel.ports.create('first', 'one') |
532 | | - const two = this.kernel.ports.create('second', 'two') |
533 | 576 | |
534 | | - await Promise.all([ |
| 577 | + const one = this.kernel.ports.create('first') |
| 578 | + const two = this.kernel.ports.create('second') |
| 579 | + |
| 580 | + this.kernel.ports.bind(one, 'one') |
| 581 | + this.kernel.ports.bind(two, 'two') |
| 582 | + |
| 583 | + return Promise.all([ |
535 | 584 | this.kernel.send(two, new Message()), |
536 | 585 | this.kernel.send(one, new Message()) |
537 | 586 | ]) |
538 | | - |
539 | | - this.kernel.incrementTicks(6) |
540 | 587 | } else if (this.runs === 1) { |
541 | 588 | this.runs++ |
542 | 589 | t.equals(m.data, 'second', 'should recive the first message') |
543 | 590 | } else if (this.runs === 2) { |
546 | 593 | } |
547 | 594 | } |
548 | 595 | |
549 | 596 | class First extends BaseContainer { |
550 | | - async run (m) { |
| 597 | + run (m) { |
551 | 598 | this.kernel.incrementTicks(2) |
552 | | - await this.kernel.send(m.fromPort, new Message({ |
| 599 | + return this.kernel.send(m.fromPort, new Message({ |
553 | 600 | data: 'first' |
554 | 601 | })) |
555 | 602 | } |
556 | 603 | } |
557 | 604 | |
558 | 605 | class Second extends BaseContainer { |
559 | | - async run (m) { |
| 606 | + run (m) { |
560 | 607 | this.kernel.incrementTicks(2) |
561 | | - await this.kernel.send(m.fromPort, new Message({ |
| 608 | + return this.kernel.send(m.fromPort, new Message({ |
562 | 609 | resources: { |
563 | 610 | priority: 100 |
564 | 611 | }, |
565 | 612 | data: 'second' |
575 | 622 | hypervisor.registerContainer('first', First) |
576 | 623 | hypervisor.registerContainer('second', Second) |
577 | 624 | |
578 | 625 | const root = await hypervisor.createInstance('root') |
579 | | - const port = await root.ports.create('root', 'first') |
580 | | - await root.send(port, new Message()) |
| 626 | + const port = root.ports.create('root') |
| 627 | + root.ports.bind(port, 'first') |
| 628 | + root.send(port, new Message()) |
581 | 629 | }) |
582 | 630 | |
583 | 631 | tape('should order parent messages correctly', async t => { |
584 | 632 | t.plan(1) |
585 | 633 | class Middle extends BaseContainer { |
586 | | - async run (m) { |
| 634 | + run (m) { |
587 | 635 | if (!this.runs) { |
588 | 636 | this.runs = 1 |
589 | 637 | this.kernel.incrementTicks(1) |
590 | | - const leaf = this.kernel.ports.create('leaf', 'leaf') |
591 | | - await this.kernel.send(leaf, new Message()) |
| 638 | + |
| 639 | + const leaf = this.kernel.ports.create('leaf') |
| 640 | + this.kernel.ports.bind(leaf, 'leaf') |
| 641 | + |
| 642 | + return this.kernel.send(leaf, new Message()) |
592 | 643 | } else { |
593 | 644 | ++this.runs |
594 | 645 | if (this.runs === 3) { |
595 | 646 | t.equals(m.data, 'first') |
598 | 649 | } |
599 | 650 | } |
600 | 651 | |
601 | 652 | class Leaf extends BaseContainer { |
602 | | - async run (m) { |
| 653 | + run (m) { |
603 | 654 | this.kernel.incrementTicks(2) |
604 | | - await this.kernel.send(m.fromPort, new Message({ |
| 655 | + return this.kernel.send(m.fromPort, new Message({ |
605 | 656 | data: 'first' |
606 | 657 | })) |
607 | 658 | } |
608 | 659 | } |
617 | 668 | |
618 | 669 | const root = await hypervisor.createInstance('root') |
619 | 670 | root.incrementTicks(2) |
620 | 671 | |
621 | | - const port = await root.ports.create('middle', 'first') |
| 672 | + const port = root.ports.create('middle') |
| 673 | + root.ports.bind(port, 'first') |
622 | 674 | |
623 | 675 | await root.send(port, new Message()) |
624 | | - await root.send(port, new Message()) |
| 676 | + root.send(port, new Message()) |
625 | 677 | }) |
626 | 678 | }) |