tests/index.jsView |
---|
11 | 11 | constructor (exInterface) { |
12 | 12 | this.exInterface = exInterface |
13 | 13 | } |
14 | 14 | |
15 | | - initailize (message) { |
| 15 | + initialize (message) { |
16 | 16 | const port = message.ports[0] |
17 | 17 | if (port) { |
18 | 18 | this.exInterface.ports.bind('root', port) |
19 | 19 | } |
24 | 24 | tape('basic', async t => { |
25 | 25 | t.plan(2) |
26 | 26 | let message |
27 | 27 | const expectedState = { |
28 | | - '/': 'zdpuAyGKaZ3nbBQdgESbEgVYr81TcAFB6LE2MQQPWLZaYxuF3' |
| 28 | + '/': 'zdpuB1wc9Pb6jUzfNt4nAxAEUxB7kNhg4vbq7YLcEyBUb6iAB' |
29 | 29 | } |
30 | 30 | |
31 | 31 | class testVMContainer extends BaseContainer { |
32 | 32 | run (m) { |
37 | 37 | const hypervisor = new Hypervisor(node.dag) |
38 | 38 | hypervisor.registerContainer('test', testVMContainer) |
39 | 39 | |
40 | 40 | const rootContainer = await hypervisor.createInstance('test') |
41 | | - const port = rootContainer.ports.create('test') |
| 41 | + |
| 42 | + const [portRef1, portRef2] = rootContainer.ports.createChannel() |
| 43 | + const initMessage = rootContainer.createMessage({ |
| 44 | + data: Buffer.from('test code'), |
| 45 | + ports: [portRef2] |
| 46 | + }) |
| 47 | + |
| 48 | + rootContainer.ports.create('test', initMessage) |
| 49 | + |
| 50 | + rootContainer.ports.bind('first', portRef1) |
42 | 51 | message = rootContainer.createMessage() |
43 | | - rootContainer.ports.bind('first', port) |
44 | | - rootContainer.send(port, message) |
| 52 | + rootContainer.send(portRef1, message) |
45 | 53 | |
46 | 54 | const stateRoot = await hypervisor.createStateRoot(Infinity) |
47 | 55 | t.deepEquals(stateRoot, expectedState, 'expected root!') |
48 | 56 | }) |
53 | 61 | '/': 'zdpuAx5LRRwTgzPipKEPgh7MHUKu4Pd1BYjDqBcf9whgzvrqf' |
54 | 62 | } |
55 | 63 | |
56 | 64 | class testVMContainer extends BaseContainer { |
57 | | - initailize () {} |
| 65 | + initialize () {} |
58 | 66 | } |
59 | 67 | |
60 | 68 | const hypervisor = new Hypervisor(node.dag) |
61 | 69 | hypervisor.registerContainer('test', testVMContainer) |
62 | 70 | |
63 | 71 | const root = await hypervisor.createInstance('test') |
64 | | - const port = root.ports.create('test') |
65 | | - root.ports.bind('one', port) |
| 72 | + const [portRef1, portRef2] = root.ports.createChannel() |
66 | 73 | |
| 74 | + root.ports.bind('one', portRef1) |
| 75 | + root.ports.create('test', root.createMessage({ |
| 76 | + ports: [portRef2] |
| 77 | + })) |
| 78 | + |
67 | 79 | const stateRoot = await hypervisor.createStateRoot(Infinity) |
68 | 80 | t.deepEquals(stateRoot, expectedState, 'expected root!') |
69 | 81 | }) |
70 | 82 | |
82 | 94 | } |
83 | 95 | |
84 | 96 | class testVMContainer extends BaseContainer { |
85 | 97 | run (m) { |
86 | | - const port = this.exInterface.ports.create('test2') |
87 | | - this.exInterface.ports.bind('child', port) |
| 98 | + const [portRef1, portRef2] = this.exInterface.ports.createChannel() |
| 99 | + this.exInterface.ports.create('test2', this.exInterface.createMessage({ |
| 100 | + ports: [portRef2] |
| 101 | + })) |
| 102 | + this.exInterface.ports.bind('child', portRef1) |
88 | 103 | this.exInterface.incrementTicks(2) |
89 | | - this.exInterface.send(port, m) |
| 104 | + this.exInterface.send(portRef1, m) |
90 | 105 | } |
91 | 106 | } |
92 | 107 | |
93 | 108 | const hypervisor = new Hypervisor(node.dag) |
94 | 109 | hypervisor.registerContainer('test', testVMContainer) |
95 | 110 | hypervisor.registerContainer('test2', testVMContainer2) |
96 | 111 | |
97 | | - let root = await hypervisor.createInstance('test') |
98 | | - let port = root.ports.create('test') |
| 112 | + const root = await hypervisor.createInstance('test') |
| 113 | + const [portRef1, portRef2] = root.ports.createChannel() |
| 114 | + root.ports.create('test', root.createMessage({ |
| 115 | + ports: [portRef2] |
| 116 | + })) |
99 | 117 | |
100 | | - root.ports.bind('first', port) |
101 | | - message = root.createMessage() |
| 118 | + root.ports.bind('first', portRef1) |
| 119 | + message = root.createMessage({ |
| 120 | + data: 'test' |
| 121 | + }) |
102 | 122 | |
103 | | - root.send(port, message) |
| 123 | + root.send(portRef1, message) |
104 | 124 | const stateRoot = await hypervisor.createStateRoot(Infinity) |
105 | 125 | t.deepEquals(stateRoot, expectedState, 'expected state') |
106 | 126 | }) |
107 | 127 | |
127 | 147 | } |
128 | 148 | |
129 | 149 | class testVMContainer extends BaseContainer { |
130 | 150 | run (m) { |
131 | | - const port = this.exInterface.ports.create('test2') |
132 | | - this.exInterface.ports.bind('child', port) |
133 | | - this.exInterface.send(port, m) |
| 151 | + const [portRef1, portRef2] = this.exInterface.ports.createChannel() |
| 152 | + this.exInterface.ports.create('test2', this.exInterface.createMessage({ |
| 153 | + ports: [portRef2] |
| 154 | + })) |
| 155 | + this.exInterface.ports.bind('child', portRef1) |
| 156 | + this.exInterface.send(portRef1, m) |
134 | 157 | this.exInterface.incrementTicks(1) |
135 | 158 | } |
136 | 159 | } |
137 | 160 | |
140 | 163 | hypervisor.registerContainer('test2', testVMContainer2) |
141 | 164 | |
142 | 165 | let root = await hypervisor.createInstance('test') |
143 | 166 | const rootId = root.id |
144 | | - let port = root.ports.create('test') |
| 167 | + const [portRef1, portRef2] = root.ports.createChannel() |
| 168 | + root.ports.create('test', root.createMessage({ |
| 169 | + ports: [portRef2] |
| 170 | + })) |
145 | 171 | |
146 | | - root.ports.bind('first', port) |
| 172 | + root.ports.bind('first', portRef1) |
147 | 173 | message = root.createMessage() |
148 | 174 | |
149 | | - root.send(port, message) |
| 175 | + root.send(portRef1, message) |
150 | 176 | const stateRoot = await hypervisor.createStateRoot(Infinity) |
151 | 177 | t.true(hasResolved, 'should resolve before generating the state root') |
152 | 178 | t.deepEquals(stateRoot, expectedState, 'expected state') |
| 179 | + |
153 | 180 | |
154 | 181 | class testVMContainer3 extends BaseContainer { |
155 | 182 | run (m) { |
156 | 183 | const port = this.exInterface.ports.get('child') |
160 | 187 | } |
161 | 188 | |
162 | 189 | hypervisor.registerContainer('test', testVMContainer3) |
163 | 190 | root = await hypervisor.getInstance(rootId) |
164 | | - port = root.ports.get('first') |
| 191 | + const port = root.ports.get('first') |
165 | 192 | root.send(port, message) |
166 | 193 | }) |
167 | 194 | |
168 | 195 | tape('traps', async t => { |
169 | 196 | t.plan(1) |
170 | 197 | class Root extends BaseContainer { |
171 | 198 | async run (m) { |
172 | | - const one = this.exInterface.ports.create('root') |
173 | | - const two = this.exInterface.ports.create('root') |
174 | | - const three = this.exInterface.ports.create('root') |
| 199 | + const [portRef1, portRef2] = this.exInterface.ports.createChannel() |
| 200 | + const [portRef3, portRef4] = this.exInterface.ports.createChannel() |
| 201 | + const [portRef5, portRef6] = this.exInterface.ports.createChannel() |
175 | 202 | |
176 | | - this.exInterface.ports.bind('one', one) |
177 | | - this.exInterface.ports.bind('two', two) |
178 | | - this.exInterface.ports.bind('three', three) |
| 203 | + this.exInterface.ports.bind('one', portRef1) |
| 204 | + this.exInterface.ports.bind('two', portRef3) |
| 205 | + this.exInterface.ports.bind('three', portRef5) |
179 | 206 | |
| 207 | + const message1 = this.exInterface.createMessage({ |
| 208 | + ports: [portRef2] |
| 209 | + }) |
| 210 | + const message2 = this.exInterface.createMessage({ |
| 211 | + ports: [portRef4] |
| 212 | + }) |
| 213 | + const message3 = this.exInterface.createMessage({ |
| 214 | + ports: [portRef6] |
| 215 | + }) |
| 216 | + |
| 217 | + this.exInterface.ports.create('root', message1) |
| 218 | + this.exInterface.ports.create('root', message2) |
| 219 | + this.exInterface.ports.create('root', message3) |
| 220 | + |
180 | 221 | throw new Error('it is a trap!!!') |
181 | 222 | } |
182 | 223 | } |
183 | 224 | |
200 | 241 | class Root extends BaseContainer { |
201 | 242 | run (m) { |
202 | 243 | if (!runs) { |
203 | 244 | runs++ |
204 | | - const one = this.exInterface.ports.create('first') |
205 | | - const two = this.exInterface.ports.create('second') |
206 | 245 | |
207 | | - this.exInterface.ports.bind('two', two) |
208 | | - this.exInterface.ports.bind('one', one) |
| 246 | + const [portRef1, portRef2] = this.exInterface.ports.createChannel() |
| 247 | + const [portRef3, portRef4] = this.exInterface.ports.createChannel() |
209 | 248 | |
210 | | - this.exInterface.send(one, this.exInterface.createMessage()) |
211 | | - this.exInterface.send(two, this.exInterface.createMessage()) |
| 249 | + this.exInterface.ports.bind('two', portRef3) |
| 250 | + this.exInterface.ports.bind('one', portRef1) |
| 251 | + |
| 252 | + const message1 = this.exInterface.createMessage({ |
| 253 | + ports: [portRef2] |
| 254 | + }) |
| 255 | + const message2 = this.exInterface.createMessage({ |
| 256 | + ports: [portRef4] |
| 257 | + }) |
| 258 | + |
| 259 | + this.exInterface.ports.create('first', message1) |
| 260 | + this.exInterface.ports.create('second', message2) |
| 261 | + |
| 262 | + this.exInterface.send(portRef1, this.exInterface.createMessage()) |
| 263 | + this.exInterface.send(portRef3, this.exInterface.createMessage()) |
212 | 264 | } else if (runs === 1) { |
213 | 265 | runs++ |
214 | 266 | t.equals(m.data, 'first', 'should recive the first message') |
215 | 267 | } else if (runs === 2) { |
242 | 294 | hypervisor.registerContainer('first', First) |
243 | 295 | hypervisor.registerContainer('second', Second) |
244 | 296 | |
245 | 297 | const root = await hypervisor.createInstance('root') |
246 | | - const port = root.ports.create('root') |
247 | | - root.ports.bind('first', port) |
248 | 298 | |
249 | | - root.send(port, root.createMessage()) |
| 299 | + const [portRef1, portRef2] = root.ports.createChannel() |
| 300 | + root.ports.create('root', root.createMessage({ |
| 301 | + ports: [portRef2] |
| 302 | + })) |
| 303 | + |
| 304 | + root.ports.bind('first', portRef1) |
| 305 | + const message = root.createMessage() |
| 306 | + root.send(portRef1, message) |
250 | 307 | }) |
251 | 308 | |
252 | 309 | tape('message should arrive in the correct oder if sent in order', async t => { |
253 | 310 | t.plan(2) |
256 | 313 | class Root extends BaseContainer { |
257 | 314 | run (m) { |
258 | 315 | if (!runs) { |
259 | 316 | runs++ |
260 | | - const one = this.exInterface.ports.create('first') |
261 | | - const two = this.exInterface.ports.create('second') |
262 | 317 | |
263 | | - this.exInterface.ports.bind('one', one) |
264 | | - this.exInterface.ports.bind('two', two) |
| 318 | + const [portRef1, portRef2] = this.exInterface.ports.createChannel() |
| 319 | + const [portRef3, portRef4] = this.exInterface.ports.createChannel() |
265 | 320 | |
266 | | - Promise.all([ |
267 | | - this.exInterface.send(one, this.exInterface.createMessage()), |
268 | | - this.exInterface.send(two, this.exInterface.createMessage()) |
269 | | - ]) |
| 321 | + this.exInterface.ports.bind('one', portRef1) |
| 322 | + this.exInterface.ports.bind('two', portRef3) |
| 323 | + |
| 324 | + const message1 = this.exInterface.createMessage({ |
| 325 | + ports: [portRef2] |
| 326 | + }) |
| 327 | + const message2 = this.exInterface.createMessage({ |
| 328 | + ports: [portRef4] |
| 329 | + }) |
| 330 | + |
| 331 | + this.exInterface.ports.create('first', message1) |
| 332 | + this.exInterface.ports.create('second', message2) |
| 333 | + |
| 334 | + this.exInterface.send(portRef1, this.exInterface.createMessage()) |
| 335 | + this.exInterface.send(portRef3, this.exInterface.createMessage()) |
270 | 336 | } else if (runs === 1) { |
271 | 337 | runs++ |
272 | 338 | t.equals(m.data, 'second', 'should recived the second message') |
273 | 339 | } else if (runs === 2) { |
300 | 366 | hypervisor.registerContainer('first', First) |
301 | 367 | hypervisor.registerContainer('second', Second) |
302 | 368 | |
303 | 369 | const root = await hypervisor.createInstance('root') |
304 | | - const port = root.ports.create('root') |
305 | | - root.ports.bind('first', port) |
306 | 370 | |
307 | | - root.send(port, root.createMessage()) |
| 371 | + const [portRef1, portRef2] = root.ports.createChannel() |
| 372 | + root.ports.create('root', root.createMessage({ |
| 373 | + ports: [portRef2] |
| 374 | + })) |
| 375 | + |
| 376 | + root.ports.bind('first', portRef1) |
| 377 | + const message = root.createMessage() |
| 378 | + root.send(portRef1, message) |
308 | 379 | }) |
309 | 380 | |
310 | 381 | tape('message should arrive in the correct oder if sent in order', async t => { |
311 | 382 | t.plan(2) |
314 | 385 | class Root extends BaseContainer { |
315 | 386 | run (m) { |
316 | 387 | if (!runs) { |
317 | 388 | runs++ |
318 | | - const one = this.exInterface.ports.create('first') |
319 | | - const two = this.exInterface.ports.create('second') |
| 389 | + const [portRef1, portRef2] = this.exInterface.ports.createChannel() |
| 390 | + const [portRef3, portRef4] = this.exInterface.ports.createChannel() |
320 | 391 | |
321 | | - this.exInterface.ports.bind('one', one) |
322 | | - this.exInterface.ports.bind('two', two) |
| 392 | + this.exInterface.ports.bind('one', portRef1) |
| 393 | + this.exInterface.ports.bind('two', portRef3) |
323 | 394 | |
324 | | - this.exInterface.send(one, this.exInterface.createMessage()) |
325 | | - this.exInterface.send(two, this.exInterface.createMessage()) |
| 395 | + const message1 = this.exInterface.createMessage({ |
| 396 | + ports: [portRef2] |
| 397 | + }) |
| 398 | + const message2 = this.exInterface.createMessage({ |
| 399 | + ports: [portRef4] |
| 400 | + }) |
326 | 401 | |
| 402 | + this.exInterface.ports.create('first', message1) |
| 403 | + this.exInterface.ports.create('second', message2) |
| 404 | + |
| 405 | + this.exInterface.send(portRef1, this.exInterface.createMessage()) |
| 406 | + this.exInterface.send(portRef3, this.exInterface.createMessage()) |
| 407 | + |
327 | 408 | this.exInterface.incrementTicks(6) |
328 | 409 | } else if (runs === 1) { |
329 | 410 | runs++ |
330 | 411 | t.equals(m.data, 'first', 'should recive the first message') |
336 | 417 | |
337 | 418 | class First extends BaseContainer { |
338 | 419 | run (m) { |
339 | 420 | this.exInterface.incrementTicks(1) |
340 | | - this.exInterface.send(m.fromPort, this.exInterface.createMessage({data: 'first'})) |
| 421 | + this.exInterface.send(m.fromPort, this.exInterface.createMessage({ |
| 422 | + data: 'first' |
| 423 | + })) |
341 | 424 | } |
342 | 425 | } |
343 | 426 | |
344 | 427 | class Second extends BaseContainer { |
345 | 428 | run (m) { |
346 | 429 | this.exInterface.incrementTicks(2) |
347 | | - this.exInterface.send(m.fromPort, this.exInterface.createMessage({data: 'second'})) |
| 430 | + this.exInterface.send(m.fromPort, this.exInterface.createMessage({ |
| 431 | + data: 'second' |
| 432 | + })) |
348 | 433 | } |
349 | 434 | } |
350 | 435 | |
351 | 436 | const hypervisor = new Hypervisor(node.dag) |
354 | 439 | hypervisor.registerContainer('first', First) |
355 | 440 | hypervisor.registerContainer('second', Second) |
356 | 441 | |
357 | 442 | const root = await hypervisor.createInstance('root') |
358 | | - const port = root.ports.create('root') |
359 | | - root.ports.bind('first', port) |
| 443 | + const [portRef1, portRef2] = root.ports.createChannel() |
| 444 | + root.ports.create('root', root.createMessage({ |
| 445 | + ports: [portRef2] |
| 446 | + })) |
360 | 447 | |
361 | | - root.send(port, root.createMessage()) |
| 448 | + root.ports.bind('first', portRef1) |
| 449 | + const message = root.createMessage() |
| 450 | + root.send(portRef1, message) |
362 | 451 | }) |
363 | 452 | |
364 | 453 | tape('saturation', async t => { |
365 | 454 | t.plan(2) |
368 | 457 | class Root extends BaseContainer { |
369 | 458 | run (m) { |
370 | 459 | if (!runs) { |
371 | 460 | runs++ |
372 | | - const one = this.exInterface.ports.create('first') |
373 | | - const two = this.exInterface.ports.create('second') |
| 461 | + const [portRef1, portRef2] = this.exInterface.ports.createChannel() |
| 462 | + const [portRef3, portRef4] = this.exInterface.ports.createChannel() |
374 | 463 | |
375 | | - this.exInterface.ports.bind('two', two) |
376 | | - this.exInterface.ports.bind('one', one) |
| 464 | + this.exInterface.ports.bind('one', portRef1) |
| 465 | + this.exInterface.ports.bind('two', portRef3) |
377 | 466 | |
378 | | - Promise.all([ |
379 | | - this.exInterface.send(one, this.exInterface.createMessage()), |
380 | | - this.exInterface.send(two, this.exInterface.createMessage()) |
381 | | - ]) |
| 467 | + const message1 = this.exInterface.createMessage({ |
| 468 | + ports: [portRef2] |
| 469 | + }) |
| 470 | + const message2 = this.exInterface.createMessage({ |
| 471 | + ports: [portRef4] |
| 472 | + }) |
| 473 | + |
| 474 | + this.exInterface.ports.create('first', message1) |
| 475 | + this.exInterface.ports.create('second', message2) |
| 476 | + |
| 477 | + this.exInterface.send(portRef1, this.exInterface.createMessage()) |
| 478 | + this.exInterface.send(portRef3, this.exInterface.createMessage()) |
| 479 | + |
382 | 480 | this.exInterface.incrementTicks(6) |
383 | 481 | } else if (runs === 1) { |
384 | 482 | runs++ |
385 | 483 | t.equals(m.data, 'first', 'should recive the first message') |
386 | 484 | } else if (runs === 2) { |
| 485 | + runs++ |
387 | 486 | t.equals(m.data, 'second', 'should recived the second message') |
388 | 487 | } |
389 | 488 | } |
390 | 489 | } |
399 | 498 | } |
400 | 499 | |
401 | 500 | class Second extends BaseContainer { |
402 | 501 | run (m) { |
403 | | - |
404 | 502 | this.exInterface.incrementTicks(3) |
| 503 | + console.log('sending second', this.exInterface.ticks) |
405 | 504 | this.exInterface.send(m.fromPort, this.exInterface.createMessage({ |
406 | 505 | data: 'second' |
407 | 506 | })) |
408 | 507 | } |
409 | 508 | } |
410 | 509 | |
411 | 510 | class Waiter extends BaseContainer { |
412 | | - initailize () { |
| 511 | + initialize () { |
413 | 512 | return new Promise((resolve, reject) => { |
414 | 513 | setTimeout(() => { |
415 | 514 | resolve() |
416 | 515 | }, 200) |
425 | 524 | hypervisor.registerContainer('second', Second) |
426 | 525 | hypervisor.registerContainer('waiter', Waiter) |
427 | 526 | |
428 | 527 | const root = await hypervisor.createInstance('root') |
429 | | - const port = root.ports.create('root') |
430 | | - root.ports.bind('first', port) |
431 | | - const port1 = root.ports.create('waiter') |
432 | | - root.ports.bind('sencond', port1) |
| 528 | + const [portRef1, portRef2] = root.ports.createChannel() |
433 | 529 | |
434 | | - await root.send(port, root.createMessage()) |
435 | | - root.incrementTicks(7) |
| 530 | + const message = root.createMessage() |
| 531 | + root.send(portRef1, message) |
| 532 | + root.ports.bind('first', portRef1) |
| 533 | + root.ports.create('root', root.createMessage({ |
| 534 | + ports: [portRef2] |
| 535 | + })) |
436 | 536 | |
437 | | - root.send(port, root.createMessage()) |
| 537 | + const [portRef3, portRef4] = root.ports.createChannel() |
| 538 | + root.ports.bind('sencond', portRef3) |
| 539 | + root.ports.create('waiter', root.createMessage({ |
| 540 | + ports: [portRef4] |
| 541 | + })) |
| 542 | + |
| 543 | + root.incrementTicks(100) |
| 544 | + root.send(portRef1, root.createMessage({data: 'testss'})) |
| 545 | + hypervisor.scheduler.done(root.id) |
438 | 546 | }) |
439 | 547 | |
440 | 548 | tape('message should arrive in the correct order, even in a tie of ticks', async t => { |
441 | 549 | t.plan(2) |
445 | 553 | class Root extends BaseContainer { |
446 | 554 | run (m) { |
447 | 555 | if (!runs) { |
448 | 556 | runs++ |
449 | | - const one = this.exInterface.ports.create('first') |
450 | | - const two = this.exInterface.ports.create('second') |
| 557 | + const [portRef1, portRef2] = this.exInterface.ports.createChannel() |
| 558 | + const [portRef3, portRef4] = this.exInterface.ports.createChannel() |
451 | 559 | |
452 | | - this.exInterface.ports.bind('two', two) |
453 | | - this.exInterface.ports.bind('one', one) |
| 560 | + this.exInterface.ports.bind('two', portRef3) |
| 561 | + this.exInterface.ports.bind('one', portRef1) |
454 | 562 | |
455 | | - this.exInterface.send(one, this.exInterface.createMessage()) |
456 | | - this.exInterface.send(two, this.exInterface.createMessage()) |
| 563 | + const message1 = this.exInterface.createMessage({ |
| 564 | + ports: [portRef2] |
| 565 | + }) |
| 566 | + const message2 = this.exInterface.createMessage({ |
| 567 | + ports: [portRef4] |
| 568 | + }) |
457 | 569 | |
| 570 | + this.exInterface.ports.create('first', message1) |
| 571 | + this.exInterface.ports.create('second', message2) |
| 572 | + |
| 573 | + this.exInterface.send(portRef1, this.exInterface.createMessage()) |
| 574 | + this.exInterface.send(portRef3, this.exInterface.createMessage()) |
| 575 | + |
458 | 576 | this.exInterface.incrementTicks(6) |
459 | 577 | } else if (runs === 1) { |
460 | 578 | runs++ |
461 | 579 | t.equals(m.data, 'second', 'should recived the second message') |
489 | 607 | hypervisor.registerContainer('first', First) |
490 | 608 | hypervisor.registerContainer('second', Second) |
491 | 609 | |
492 | 610 | const root = await hypervisor.createInstance('root') |
| 611 | + const [portRef1, portRef2] = root.ports.createChannel() |
| 612 | + const message = root.createMessage() |
493 | 613 | |
494 | | - const port = root.ports.create('root') |
495 | | - root.ports.bind('first', port) |
496 | | - |
497 | | - root.send(port, root.createMessage()) |
| 614 | + root.send(portRef1, message) |
| 615 | + root.ports.bind('first', portRef1) |
| 616 | + root.ports.create('root', root.createMessage({ |
| 617 | + ports: [portRef2] |
| 618 | + })) |
498 | 619 | }) |
499 | 620 | |
500 | 621 | tape('message should arrive in the correct order, with a tie in ticks but with differnt proity', async t => { |
501 | 622 | t.plan(2) |
505 | 626 | class Root extends BaseContainer { |
506 | 627 | run (m) { |
507 | 628 | if (!runs) { |
508 | 629 | runs++ |
509 | | - const one = this.exInterface.ports.create('first') |
510 | | - const two = this.exInterface.ports.create('second') |
| 630 | + const [portRef1, portRef2] = this.exInterface.ports.createChannel() |
| 631 | + const [portRef3, portRef4] = this.exInterface.ports.createChannel() |
511 | 632 | |
512 | | - this.exInterface.ports.bind('one', one) |
513 | | - this.exInterface.ports.bind('two', two) |
| 633 | + this.exInterface.ports.bind('one', portRef1) |
| 634 | + this.exInterface.ports.bind('two', portRef3) |
514 | 635 | |
515 | | - this.exInterface.send(two, this.exInterface.createMessage()) |
516 | | - this.exInterface.send(one, this.exInterface.createMessage()) |
| 636 | + const message1 = this.exInterface.createMessage({ |
| 637 | + ports: [portRef2] |
| 638 | + }) |
| 639 | + const message2 = this.exInterface.createMessage({ |
| 640 | + ports: [portRef4] |
| 641 | + }) |
517 | 642 | |
| 643 | + this.exInterface.ports.create('first', message1) |
| 644 | + this.exInterface.ports.create('second', message2) |
| 645 | + |
| 646 | + this.exInterface.send(portRef1, this.exInterface.createMessage()) |
| 647 | + this.exInterface.send(portRef3, this.exInterface.createMessage()) |
| 648 | + |
518 | 649 | this.exInterface.incrementTicks(6) |
519 | 650 | } else if (runs === 1) { |
520 | 651 | runs++ |
521 | 652 | t.equals(m.data, 'first', 'should recive the first message') |
549 | 680 | hypervisor.registerContainer('first', First) |
550 | 681 | hypervisor.registerContainer('second', Second) |
551 | 682 | |
552 | 683 | const root = await hypervisor.createInstance('root') |
553 | | - const port = root.ports.create('root') |
554 | | - root.ports.bind('first', port) |
555 | | - root.send(port, root.createMessage()) |
| 684 | + const [portRef1, portRef2] = root.ports.createChannel() |
| 685 | + const message = root.createMessage() |
| 686 | + |
| 687 | + root.send(portRef1, message) |
| 688 | + root.ports.bind('first', portRef1) |
| 689 | + root.ports.create('root', root.createMessage({ |
| 690 | + ports: [portRef2] |
| 691 | + })) |
556 | 692 | }) |
557 | 693 | |
558 | 694 | tape('send to the same container at the same time', async t => { |
559 | 695 | t.plan(2) |
564 | 700 | class Root extends BaseContainer { |
565 | 701 | run (m) { |
566 | 702 | let one = this.exInterface.ports.get('one') |
567 | 703 | if (!one) { |
568 | | - one = this.exInterface.ports.create('first') |
569 | | - this.exInterface.ports.bind('one', one) |
| 704 | + const [portRef1, portRef2] = this.exInterface.ports.createChannel() |
| 705 | + this.exInterface.ports.bind('one', portRef1) |
| 706 | + const message1 = this.exInterface.createMessage({ |
| 707 | + ports: [portRef2] |
| 708 | + }) |
| 709 | + this.exInterface.ports.create('first', message1) |
570 | 710 | } else { |
571 | 711 | this.exInterface.send(one, this.exInterface.createMessage()) |
572 | 712 | this.exInterface.send(one, this.exInterface.createMessage()) |
573 | 713 | } |
590 | 730 | hypervisor.registerContainer('root', Root) |
591 | 731 | hypervisor.registerContainer('first', First) |
592 | 732 | |
593 | 733 | const root = await hypervisor.createInstance('root') |
594 | | - const port = root.ports.create('root') |
595 | | - root.ports.bind('first', port) |
596 | | - root.send(port, root.createMessage()) |
| 734 | + const [portRef1, portRef2] = root.ports.createChannel() |
| 735 | + root.ports.bind('first', portRef1) |
| 736 | + root.ports.create('root', root.createMessage({ |
| 737 | + ports: [portRef2] |
| 738 | + })) |
| 739 | + |
| 740 | + const message = root.createMessage() |
| 741 | + root.send(portRef1, message) |
597 | 742 | await hypervisor.createStateRoot() |
598 | | - root.send(port, root.createMessage()) |
| 743 | + root.send(portRef1, root.createMessage()) |
599 | 744 | await hypervisor.createStateRoot() |
600 | 745 | t.equals(runs, 2) |
601 | 746 | }) |
| 747 | + |
602 | 748 | tape('checking ports', async t => { |
603 | 749 | t.plan(4) |
604 | 750 | const hypervisor = new Hypervisor(node.dag) |
605 | 751 | hypervisor.registerContainer('base', BaseContainer) |
606 | 752 | |
607 | 753 | const root = await hypervisor.createInstance('base') |
608 | | - let port = root.ports.create('base') |
609 | | - await root.ports.bind('test', port) |
610 | 754 | |
| 755 | + const [portRef1, portRef2] = root.ports.createChannel() |
| 756 | + root.ports.create('base', root.createMessage({ |
| 757 | + ports: [portRef2] |
| 758 | + })) |
| 759 | + root.ports.bind('test', portRef1) |
| 760 | + |
611 | 761 | try { |
612 | 762 | root.createMessage({ |
613 | | - ports: [port] |
| 763 | + ports: [portRef1] |
614 | 764 | }) |
615 | 765 | } catch (e) { |
616 | 766 | t.pass('should thow if sending a port that is bound') |
617 | 767 | } |
618 | 768 | |
619 | 769 | try { |
620 | | - await root.ports.bind('test', port) |
| 770 | + await root.ports.bind('test', portRef1) |
621 | 771 | } catch (e) { |
622 | 772 | t.pass('should thow if binding an already bound port') |
623 | 773 | } |
624 | 774 | |
625 | 775 | try { |
626 | | - let port2 = root.ports.create('base') |
627 | | - await root.ports.bind('test', port2) |
| 776 | + const [portRef3] = root.ports.createChannel() |
| 777 | + await root.ports.bind('test', portRef3) |
628 | 778 | } catch (e) { |
629 | 779 | t.pass('should thow if binding an already bound name') |
630 | 780 | } |
631 | 781 | |
632 | 782 | await root.ports.unbind('test') |
633 | | - const message = root.createMessage({ports: [port]}) |
634 | | - t.equals(message.ports[0], port, 'should create a message if the port is unbound') |
| 783 | + const message = root.createMessage({ |
| 784 | + ports: [portRef1] |
| 785 | + }) |
| 786 | + t.equals(message.ports[0], portRef1, 'should create a message if the port is unbound') |
635 | 787 | }) |
636 | 788 | |
637 | 789 | tape('port deletion', async t => { |
638 | 790 | const expectedSr = { |
639 | 791 | '/': 'zdpuB2QXxn1KQtLFfBqaritTRoe5BuKP5sNFSrPtRT6sxkY7Z' |
640 | 792 | } |
641 | 793 | class Root extends BaseContainer { |
642 | 794 | run (m) { |
643 | | - const one = this.exInterface.ports.create('first') |
644 | | - this.exInterface.ports.bind('one', one) |
645 | | - this.exInterface.send(one, this.exInterface.createMessage()) |
| 795 | + const [portRef1, portRef2] = this.exInterface.ports.createChannel() |
| 796 | + this.exInterface.ports.bind('one', portRef1) |
| 797 | + const message1 = this.exInterface.createMessage({ |
| 798 | + ports: [portRef2] |
| 799 | + }) |
| 800 | + |
| 801 | + this.exInterface.ports.create('first', message1) |
| 802 | + this.exInterface.send(portRef1, this.exInterface.createMessage()) |
646 | 803 | this.exInterface.incrementTicks(6) |
647 | 804 | } |
648 | 805 | } |
649 | 806 | |
659 | 816 | hypervisor.registerContainer('root', Root) |
660 | 817 | hypervisor.registerContainer('first', First) |
661 | 818 | |
662 | 819 | const root = await hypervisor.createInstance('root') |
663 | | - const port = root.ports.create('root') |
664 | | - root.ports.bind('first', port) |
665 | | - root.send(port, root.createMessage()) |
| 820 | + const [portRef1, portRef2] = root.ports.createChannel() |
| 821 | + root.ports.bind('first', portRef1) |
| 822 | + root.ports.create('root', root.createMessage({ |
| 823 | + ports: [portRef2] |
| 824 | + })) |
| 825 | + |
| 826 | + const message = root.createMessage() |
| 827 | + root.send(portRef1, message) |
| 828 | + |
666 | 829 | const sr = await hypervisor.createStateRoot() |
667 | 830 | t.deepEquals(sr, expectedSr, 'should produce the corret state root') |
668 | 831 | |
669 | 832 | t.end() |
683 | 846 | |
684 | 847 | hypervisor.registerContainer('root', Root) |
685 | 848 | |
686 | 849 | const root = await hypervisor.createInstance('root') |
687 | | - const port = root.ports.create('root') |
688 | | - root.ports.bind('first', port) |
689 | | - root.send(port, root.createMessage()) |
| 850 | + const [portRef1, portRef2] = root.ports.createChannel() |
| 851 | + root.ports.bind('first', portRef1) |
| 852 | + root.ports.create('root', root.createMessage({ |
| 853 | + ports: [portRef2] |
| 854 | + })) |
| 855 | + |
| 856 | + const message = root.createMessage() |
| 857 | + root.send(portRef1, message) |
690 | 858 | const sr = await hypervisor.createStateRoot() |
691 | 859 | t.deepEquals(sr, expectedSr, 'should produce the corret state root') |
692 | 860 | |
693 | 861 | t.end() |
698 | 866 | '/': 'zdpuB2QXxn1KQtLFfBqaritTRoe5BuKP5sNFSrPtRT6sxkY7Z' |
699 | 867 | } |
700 | 868 | class Root extends BaseContainer { |
701 | 869 | run (m) { |
702 | | - this.exInterface.ports.create('sub') |
| 870 | + const [, portRef2] = this.exInterface.ports.createChannel() |
| 871 | + this.exInterface.ports.create('sub', this.exInterface.createMessage({ |
| 872 | + ports: [portRef2] |
| 873 | + })) |
703 | 874 | } |
704 | 875 | } |
705 | 876 | |
706 | 877 | class Sub extends BaseContainer { |
707 | 878 | initailize (message) { |
708 | 879 | this.exInterface.ports.bind('root', message.ports[0]) |
709 | | - const port = this.exInterface.ports.create('root') |
710 | | - this.exInterface.ports.bind('child', port) |
| 880 | + const [portRef1, portRef2] = root.ports.createChannel() |
| 881 | + root.ports.bind('child', portRef1) |
| 882 | + root.ports.create('root', root.createMessage({ |
| 883 | + ports: [portRef2] |
| 884 | + })) |
711 | 885 | } |
712 | 886 | } |
713 | 887 | |
714 | 888 | const hypervisor = new Hypervisor(node.dag) |
716 | 890 | hypervisor.registerContainer('root', Root) |
717 | 891 | hypervisor.registerContainer('sub', Sub) |
718 | 892 | |
719 | 893 | const root = await hypervisor.createInstance('root') |
720 | | - const port = root.ports.create('root') |
721 | | - root.ports.bind('first', port) |
722 | | - root.send(port, root.createMessage()) |
| 894 | + const [portRef1, portRef2] = root.ports.createChannel() |
| 895 | + root.ports.bind('first', portRef1) |
| 896 | + root.ports.create('root', root.createMessage({ |
| 897 | + ports: [portRef2] |
| 898 | + })) |
| 899 | + |
| 900 | + root.send(portRef1, root.createMessage()) |
723 | 901 | const sr = await hypervisor.createStateRoot() |
| 902 | + |
724 | 903 | t.deepEquals(sr, expectedSr, 'should produce the corret state root') |
725 | | - |
726 | 904 | t.end() |
727 | 905 | }) |
728 | 906 | |
729 | 907 | tape('should not remove connected nodes', async t => { |
735 | 913 | if (m.ports.length) { |
736 | 914 | const port = this.exInterface.ports.get('test1') |
737 | 915 | this.exInterface.send(port, m) |
738 | 916 | this.exInterface.ports.unbind('test1') |
739 | | - |
740 | 917 | } else { |
741 | | - const port1 = this.exInterface.ports.create('sub') |
742 | | - this.exInterface.ports.bind('test1', port1) |
743 | | - const port2 = this.exInterface.ports.create('sub') |
744 | | - this.exInterface.ports.bind('test2', port2) |
745 | | - this.exInterface.send(port2, this.exInterface.createMessage({data: 'getChannel'})) |
| 918 | + const [portRef1, portRef2] = this.exInterface.ports.createChannel() |
| 919 | + this.exInterface.ports.create('sub', this.exInterface.createMessage({ |
| 920 | + ports: [portRef2] |
| 921 | + })) |
| 922 | + this.exInterface.ports.bind('test1', portRef1) |
| 923 | + |
| 924 | + const [portRef3, portRef4] = this.exInterface.ports.createChannel() |
| 925 | + this.exInterface.ports.create('sub', this.exInterface.createMessage({ |
| 926 | + ports: [portRef4] |
| 927 | + })) |
| 928 | + this.exInterface.ports.bind('test2', portRef3) |
| 929 | + this.exInterface.send(portRef3, this.exInterface.createMessage({ |
| 930 | + data: 'getChannel' |
| 931 | + })) |
746 | 932 | } |
747 | 933 | } |
748 | 934 | } |
749 | 935 | |
767 | 953 | hypervisor.registerContainer('root', Root) |
768 | 954 | hypervisor.registerContainer('sub', Sub) |
769 | 955 | |
770 | 956 | const root = await hypervisor.createInstance('root') |
771 | | - const port = root.ports.create('root') |
772 | | - root.ports.bind('first', port) |
773 | | - root.send(port, root.createMessage()) |
| 957 | + const [portRef1, portRef2] = root.ports.createChannel() |
| 958 | + root.ports.bind('first', portRef1) |
| 959 | + root.ports.create('root', root.createMessage({ |
| 960 | + ports: [portRef2] |
| 961 | + })) |
| 962 | + |
| 963 | + root.send(portRef1, root.createMessage()) |
774 | 964 | const sr = await hypervisor.createStateRoot() |
775 | 965 | t.deepEquals(sr, expectedSr, 'should produce the corret state root') |
776 | | - |
| 966 | + |
777 | 967 | |
778 | 968 | t.end() |
779 | 969 | }) |
780 | 970 | |
789 | 979 | this.exInterface.send(port, m) |
790 | 980 | this.exInterface.ports.unbind('test1') |
791 | 981 | this.exInterface.ports.unbind('test2') |
792 | 982 | } else { |
793 | | - const port1 = this.exInterface.ports.create('sub') |
794 | | - this.exInterface.ports.bind('test1', port1) |
795 | | - const port2 = this.exInterface.ports.create('sub') |
796 | | - this.exInterface.ports.bind('test2', port2) |
797 | | - this.exInterface.send(port2, this.exInterface.createMessage({data: 'getChannel'})) |
| 983 | + const [portRef1, portRef2] = this.exInterface.ports.createChannel() |
| 984 | + this.exInterface.ports.create('sub', this.exInterface.createMessage({ |
| 985 | + ports: [portRef2] |
| 986 | + })) |
| 987 | + this.exInterface.ports.bind('test1', portRef1) |
| 988 | + |
| 989 | + const [portRef3, portRef4] = this.exInterface.ports.createChannel() |
| 990 | + this.exInterface.ports.create('sub', this.exInterface.createMessage({ |
| 991 | + ports: [portRef4] |
| 992 | + })) |
| 993 | + this.exInterface.ports.bind('test2', portRef3) |
| 994 | + this.exInterface.send(portRef3, this.exInterface.createMessage({ |
| 995 | + data: 'getChannel' |
| 996 | + })) |
798 | 997 | } |
799 | 998 | } |
800 | 999 | } |
801 | 1000 | |
819 | 1018 | hypervisor.registerContainer('root', Root) |
820 | 1019 | hypervisor.registerContainer('sub', Sub) |
821 | 1020 | |
822 | 1021 | const root = await hypervisor.createInstance('root') |
823 | | - const port = root.ports.create('root') |
824 | | - root.ports.bind('first', port) |
825 | | - root.send(port, root.createMessage()) |
| 1022 | + |
| 1023 | + const [portRef1, portRef2] = root.ports.createChannel() |
| 1024 | + root.ports.bind('first', portRef1) |
| 1025 | + root.ports.create('root', root.createMessage({ |
| 1026 | + ports: [portRef2] |
| 1027 | + })) |
| 1028 | + |
| 1029 | + root.send(portRef1, root.createMessage()) |
| 1030 | + |
826 | 1031 | const sr = await hypervisor.createStateRoot() |
827 | 1032 | t.deepEquals(sr, expectedSr, 'should produce the corret state root') |
828 | 1033 | |
829 | 1034 | t.end() |