tests/index.jsView |
---|
124 | 124 | |
125 | 125 | class Pong extends BaseContainer { |
126 | 126 | run (m) { |
127 | 127 | const port = m.fromPort |
| 128 | + this.kernel.incrementTicks(2) |
128 | 129 | return this.kernel.send(port, new Message()) |
129 | 130 | } |
130 | 131 | } |
131 | 132 | |
160 | 161 | ]) |
161 | 162 | |
162 | 163 | return new Promise((resolve, reject) => { |
163 | 164 | setTimeout(() => { |
164 | | - this.kernel.incrementTicks(2) |
| 165 | + this.kernel.incrementTicks(6) |
165 | 166 | resolve() |
166 | 167 | }, 200) |
167 | 168 | }) |
168 | 169 | } |
222 | 223 | } |
223 | 224 | }, 'should revert the state') |
224 | 225 | |
225 | 226 | t.end() |
| 227 | + }) |
226 | 228 | |
| 229 | + tape('message should arrive in the correct oder if sent in order', async t => { |
| 230 | + t.plan(2) |
| 231 | + |
| 232 | + class Root extends BaseContainer { |
| 233 | + async run (m) { |
| 234 | + if (!this.runs) { |
| 235 | + this.runs = 1 |
| 236 | + const one = this.kernel.createPort('first', 'one') |
| 237 | + const two = this.kernel.createPort('second', 'two') |
| 238 | + |
| 239 | + await Promise.all([ |
| 240 | + this.kernel.send(one, new Message()), |
| 241 | + this.kernel.send(two, new Message()) |
| 242 | + ]) |
| 243 | + |
| 244 | + this.kernel.incrementTicks(6) |
| 245 | + } else if (this.runs === 1) { |
| 246 | + this.runs++ |
| 247 | + t.equals(m.data, 'first', 'should recive the first message') |
| 248 | + } else if (this.runs === 2) { |
| 249 | + t.equals(m.data, 'second', 'should recived the second message') |
| 250 | + } |
| 251 | + } |
| 252 | + } |
| 253 | + |
| 254 | + class First extends BaseContainer { |
| 255 | + async run (m) { |
| 256 | + this.kernel.incrementTicks(1) |
| 257 | + await this.kernel.send(m.fromPort, new Message({data: 'first'})) |
| 258 | + } |
| 259 | + } |
| 260 | + |
| 261 | + class Second extends BaseContainer { |
| 262 | + async run (m) { |
| 263 | + this.kernel.incrementTicks(2) |
| 264 | + await this.kernel.send(m.fromPort, new Message({data: 'second'})) |
| 265 | + } |
| 266 | + } |
| 267 | + |
| 268 | + const hypervisor = new Hypervisor({ |
| 269 | + dag: node.dag |
| 270 | + }) |
| 271 | + |
| 272 | + hypervisor.registerContainer('root', Root) |
| 273 | + hypervisor.registerContainer('first', First) |
| 274 | + hypervisor.registerContainer('second', Second) |
| 275 | + |
| 276 | + const root = await hypervisor.createInstance('root') |
| 277 | + const port = await root.createPort('root', 'first') |
| 278 | + await root.send(port, new Message()) |
| 279 | + }) |
| 280 | + |
| 281 | + tape('message should arrive in the correct order, even if sent out of order', async t => { |
| 282 | + t.plan(2) |
| 283 | + |
| 284 | + class Root extends BaseContainer { |
| 285 | + async run (m) { |
| 286 | + if (!this.runs) { |
| 287 | + this.runs = 1 |
| 288 | + const one = this.kernel.createPort('first', 'one') |
| 289 | + const two = this.kernel.createPort('second', 'two') |
| 290 | + |
| 291 | + await Promise.all([ |
| 292 | + this.kernel.send(one, new Message()), |
| 293 | + this.kernel.send(two, new Message()) |
| 294 | + ]) |
| 295 | + |
| 296 | + this.kernel.incrementTicks(6) |
| 297 | + } else if (this.runs === 1) { |
| 298 | + this.runs++ |
| 299 | + t.equals(m.data, 'second', 'should recive the first message') |
| 300 | + } else if (this.runs === 2) { |
| 301 | + t.equals(m.data, 'first', 'should recived the second message') |
| 302 | + } |
| 303 | + } |
| 304 | + } |
| 305 | + |
| 306 | + class First extends BaseContainer { |
| 307 | + async run (m) { |
| 308 | + this.kernel.incrementTicks(2) |
| 309 | + await this.kernel.send(m.fromPort, new Message({data: 'first'})) |
| 310 | + } |
| 311 | + } |
| 312 | + |
| 313 | + class Second extends BaseContainer { |
| 314 | + async run (m) { |
| 315 | + this.kernel.incrementTicks(1) |
| 316 | + await this.kernel.send(m.fromPort, new Message({data: 'second'})) |
| 317 | + } |
| 318 | + } |
| 319 | + |
| 320 | + const hypervisor = new Hypervisor({ |
| 321 | + dag: node.dag |
| 322 | + }) |
| 323 | + |
| 324 | + hypervisor.registerContainer('root', Root) |
| 325 | + hypervisor.registerContainer('first', First) |
| 326 | + hypervisor.registerContainer('second', Second) |
| 327 | + |
| 328 | + const root = await hypervisor.createInstance('root') |
| 329 | + const port = await root.createPort('root', 'first') |
| 330 | + await root.send(port, new Message()) |
| 331 | + }) |
| 332 | + |
| 333 | + tape('message should arrive in the correct order, even in a tie of ticks', async t => { |
| 334 | + t.plan(2) |
| 335 | + |
| 336 | + class Root extends BaseContainer { |
| 337 | + async run (m) { |
| 338 | + if (!this.runs) { |
| 339 | + this.runs = 1 |
| 340 | + const one = this.kernel.createPort('first', 'one') |
| 341 | + const two = this.kernel.createPort('second', 'two') |
| 342 | + |
| 343 | + await Promise.all([ |
| 344 | + this.kernel.send(one, new Message()), |
| 345 | + this.kernel.send(two, new Message()) |
| 346 | + ]) |
| 347 | + |
| 348 | + this.kernel.incrementTicks(6) |
| 349 | + } else if (this.runs === 1) { |
| 350 | + this.runs++ |
| 351 | + t.equals(m.data, 'first', 'should recived the second message') |
| 352 | + } else if (this.runs === 2) { |
| 353 | + t.equals(m.data, 'second', 'should recive the first message') |
| 354 | + } |
| 355 | + } |
| 356 | + } |
| 357 | + |
| 358 | + class First extends BaseContainer { |
| 359 | + async run (m) { |
| 360 | + this.kernel.incrementTicks(2) |
| 361 | + await this.kernel.send(m.fromPort, new Message({data: 'first'})) |
| 362 | + } |
| 363 | + } |
| 364 | + |
| 365 | + class Second extends BaseContainer { |
| 366 | + async run (m) { |
| 367 | + this.kernel.incrementTicks(2) |
| 368 | + await this.kernel.send(m.fromPort, new Message({data: 'second'})) |
| 369 | + } |
| 370 | + } |
| 371 | + |
| 372 | + const hypervisor = new Hypervisor({ |
| 373 | + dag: node.dag |
| 374 | + }) |
| 375 | + |
| 376 | + hypervisor.registerContainer('root', Root) |
| 377 | + hypervisor.registerContainer('first', First) |
| 378 | + hypervisor.registerContainer('second', Second) |
| 379 | + |
| 380 | + const root = await hypervisor.createInstance('root') |
| 381 | + const port = await root.createPort('root', 'first') |
| 382 | + await root.send(port, new Message()) |
| 383 | + }) |
| 384 | + |
| 385 | + tape('message should arrive in the correct order, even in a tie of ticks', async t => { |
| 386 | + t.plan(2) |
| 387 | + |
| 388 | + class Root extends BaseContainer { |
| 389 | + async run (m) { |
| 390 | + if (!this.runs) { |
| 391 | + this.runs = 1 |
| 392 | + const two = this.kernel.createPort('second', 'two') |
| 393 | + const one = this.kernel.createPort('first', 'one') |
| 394 | + |
| 395 | + await Promise.all([ |
| 396 | + this.kernel.send(two, new Message()), |
| 397 | + this.kernel.send(one, new Message()) |
| 398 | + ]) |
| 399 | + |
| 400 | + this.kernel.incrementTicks(6) |
| 401 | + } else if (this.runs === 1) { |
| 402 | + this.runs++ |
| 403 | + t.equals(m.data, 'first', 'should recived the second message') |
| 404 | + } else if (this.runs === 2) { |
| 405 | + t.equals(m.data, 'second', 'should recive the first message') |
| 406 | + } |
| 407 | + } |
| 408 | + } |
| 409 | + |
| 410 | + class First extends BaseContainer { |
| 411 | + async run (m) { |
| 412 | + this.kernel.incrementTicks(2) |
| 413 | + await this.kernel.send(m.fromPort, new Message({data: 'first'})) |
| 414 | + } |
| 415 | + } |
| 416 | + |
| 417 | + class Second extends BaseContainer { |
| 418 | + async run (m) { |
| 419 | + this.kernel.incrementTicks(2) |
| 420 | + await this.kernel.send(m.fromPort, new Message({data: 'second'})) |
| 421 | + } |
| 422 | + } |
| 423 | + |
| 424 | + const hypervisor = new Hypervisor({ |
| 425 | + dag: node.dag |
| 426 | + }) |
| 427 | + |
| 428 | + hypervisor.registerContainer('root', Root) |
| 429 | + hypervisor.registerContainer('first', First) |
| 430 | + hypervisor.registerContainer('second', Second) |
| 431 | + |
| 432 | + const root = await hypervisor.createInstance('root') |
| 433 | + const port = await root.createPort('root', 'first') |
| 434 | + await root.send(port, new Message()) |
| 435 | + }) |
| 436 | + |
| 437 | + tape('message should arrive in the correct order, with a tie in ticks but with differnt proity', async t => { |
| 438 | + t.plan(2) |
| 439 | + |
| 440 | + class Root extends BaseContainer { |
| 441 | + async run (m) { |
| 442 | + if (!this.runs) { |
| 443 | + this.runs = 1 |
| 444 | + const one = this.kernel.createPort('first', 'one') |
| 445 | + const two = this.kernel.createPort('second', 'two') |
| 446 | + |
| 447 | + await Promise.all([ |
| 448 | + this.kernel.send(two, new Message()), |
| 449 | + this.kernel.send(one, new Message()) |
| 450 | + ]) |
| 451 | + |
| 452 | + this.kernel.incrementTicks(6) |
| 453 | + } else if (this.runs === 1) { |
| 454 | + this.runs++ |
| 455 | + t.equals(m.data, 'first', 'should recive the first message') |
| 456 | + } else if (this.runs === 2) { |
| 457 | + t.equals(m.data, 'second', 'should recived the second message') |
| 458 | + } |
| 459 | + } |
| 460 | + } |
| 461 | + |
| 462 | + class First extends BaseContainer { |
| 463 | + async run (m) { |
| 464 | + this.kernel.incrementTicks(2) |
| 465 | + await this.kernel.send(m.fromPort, new Message({ |
| 466 | + resources: { |
| 467 | + priority: 100 |
| 468 | + }, |
| 469 | + data: 'first' |
| 470 | + })) |
| 471 | + } |
| 472 | + } |
| 473 | + |
| 474 | + class Second extends BaseContainer { |
| 475 | + async run (m) { |
| 476 | + this.kernel.incrementTicks(2) |
| 477 | + await this.kernel.send(m.fromPort, new Message({data: 'second'})) |
| 478 | + } |
| 479 | + } |
| 480 | + |
| 481 | + const hypervisor = new Hypervisor({ |
| 482 | + dag: node.dag |
| 483 | + }) |
| 484 | + |
| 485 | + hypervisor.registerContainer('root', Root) |
| 486 | + hypervisor.registerContainer('first', First) |
| 487 | + hypervisor.registerContainer('second', Second) |
| 488 | + |
| 489 | + const root = await hypervisor.createInstance('root') |
| 490 | + const port = await root.createPort('root', 'first') |
| 491 | + await root.send(port, new Message()) |
| 492 | + }) |
| 493 | + |
| 494 | + tape('message should arrive in the correct order, with a tie in ticks but with differnt proity', async t => { |
| 495 | + t.plan(2) |
| 496 | + |
| 497 | + class Root extends BaseContainer { |
| 498 | + async run (m) { |
| 499 | + if (!this.runs) { |
| 500 | + this.runs = 1 |
| 501 | + const one = this.kernel.createPort('first', 'one') |
| 502 | + const two = this.kernel.createPort('second', 'two') |
| 503 | + |
| 504 | + await Promise.all([ |
| 505 | + this.kernel.send(two, new Message()), |
| 506 | + this.kernel.send(one, new Message()) |
| 507 | + ]) |
| 508 | + |
| 509 | + this.kernel.incrementTicks(6) |
| 510 | + } else if (this.runs === 1) { |
| 511 | + this.runs++ |
| 512 | + t.equals(m.data, 'second', 'should recive the first message') |
| 513 | + } else if (this.runs === 2) { |
| 514 | + t.equals(m.data, 'first', 'should recived the second message') |
| 515 | + } |
| 516 | + } |
| 517 | + } |
| 518 | + |
| 519 | + class First extends BaseContainer { |
| 520 | + async run (m) { |
| 521 | + this.kernel.incrementTicks(2) |
| 522 | + await this.kernel.send(m.fromPort, new Message({ |
| 523 | + data: 'first' |
| 524 | + })) |
| 525 | + } |
| 526 | + } |
| 527 | + |
| 528 | + class Second extends BaseContainer { |
| 529 | + async run (m) { |
| 530 | + this.kernel.incrementTicks(2) |
| 531 | + await this.kernel.send(m.fromPort, new Message({ |
| 532 | + resources: { |
| 533 | + priority: 100 |
| 534 | + }, |
| 535 | + data: 'second' |
| 536 | + })) |
| 537 | + } |
| 538 | + } |
| 539 | + |
| 540 | + const hypervisor = new Hypervisor({ |
| 541 | + dag: node.dag |
| 542 | + }) |
| 543 | + |
| 544 | + hypervisor.registerContainer('root', Root) |
| 545 | + hypervisor.registerContainer('first', First) |
| 546 | + hypervisor.registerContainer('second', Second) |
| 547 | + |
| 548 | + const root = await hypervisor.createInstance('root') |
| 549 | + const port = await root.createPort('root', 'first') |
| 550 | + await root.send(port, new Message()) |
| 551 | + }) |
| 552 | + |
| 553 | + tape('should order parent messages correctly', async t => { |
| 554 | + class Middle extends BaseContainer { |
| 555 | + async run (m) { |
| 556 | + if (!this.runs) { |
| 557 | + this.runs = 1 |
| 558 | + this.kernel.incrementTicks(1) |
| 559 | + const leaf = this.kernel.createPort('leaf', 'leaf') |
| 560 | + await this.kernel.send(leaf, new Message()) |
| 561 | + } else { |
| 562 | + ++this.runs |
| 563 | + if (this.runs === 3) { |
| 564 | + t.equals(m.data, 'first') |
| 565 | + } |
| 566 | + } |
| 567 | + } |
| 568 | + } |
| 569 | + |
| 570 | + class Leaf extends BaseContainer { |
| 571 | + async run (m) { |
| 572 | + this.kernel.incrementTicks(2) |
| 573 | + await this.kernel.send(m.fromPort, new Message({ |
| 574 | + data: 'first' |
| 575 | + })) |
| 576 | + } |
| 577 | + } |
| 578 | + |
| 579 | + const hypervisor = new Hypervisor({ |
| 580 | + dag: node.dag |
| 581 | + }) |
| 582 | + |
| 583 | + hypervisor.registerContainer('root', BaseContainer) |
| 584 | + hypervisor.registerContainer('middle', Middle) |
| 585 | + hypervisor.registerContainer('leaf', Leaf) |
| 586 | + |
| 587 | + const root = await hypervisor.createInstance('root') |
| 588 | + root.incrementTicks(2) |
| 589 | + const port = await root.createPort('middle', 'first') |
| 590 | + |
| 591 | + await root.send(port, new Message()) |
| 592 | + await root.send(port, new Message()) |
| 593 | + |
| 594 | + await root.wait(Infinity) |
| 595 | + |
| 596 | + t.end() |
227 | 597 | node.stop(() => { |
228 | 598 | process.exit() |
229 | 599 | }) |
230 | 600 | }) |