Files: 9a207a1ce69e564a67b6833876ce76b4f3258efa / tests / index.js
22157 bytesRaw
1 | const tape = require('tape') |
2 | const AbstractContainer = require('primea-abstract-container') |
3 | const Message = require('primea-message') |
4 | const Hypervisor = require('../') |
5 | |
6 | const level = require('level-browserify') |
7 | const RadixTree = require('dfinity-radix-tree') |
8 | const db = level('./testdb') |
9 | |
10 | class BaseContainer extends AbstractContainer { |
11 | onCreation () {} |
12 | static get typeId () { |
13 | return 9 |
14 | } |
15 | } |
16 | |
17 | tape('basic', async t => { |
18 | t.plan(2) |
19 | let message |
20 | const expectedState = { |
21 | '/': Buffer.from('a364c55f9993e0bc63f7155d5eb661ae9ad769d9', 'hex') |
22 | } |
23 | |
24 | const tree = new RadixTree({ |
25 | db: db |
26 | }) |
27 | |
28 | class testVMContainer extends BaseContainer { |
29 | onMessage (m, tag) { |
30 | t.true(m === message, 'should recive a message') |
31 | } |
32 | } |
33 | |
34 | const hypervisor = new Hypervisor(tree) |
35 | hypervisor.registerContainer(testVMContainer) |
36 | |
37 | let rootCap = await hypervisor.createActor(testVMContainer.typeId, new Message()) |
38 | |
39 | message = new Message() |
40 | hypervisor.send(rootCap, message) |
41 | |
42 | const stateRoot = await hypervisor.createStateRoot() |
43 | t.deepEquals(stateRoot, expectedState, 'expected root!') |
44 | }) |
45 | |
46 | tape('two communicating actors', async t => { |
47 | t.plan(2) |
48 | let message |
49 | const expectedState = { |
50 | '/': Buffer.from('9e8d5671e2c7d167e03784e5d9ec36e15747ad95', 'hex') |
51 | } |
52 | |
53 | const tree = new RadixTree({ |
54 | db: db |
55 | }) |
56 | |
57 | class testVMContainerA extends BaseContainer { |
58 | onCreation (m) { |
59 | message = new Message() |
60 | this.actor.send(m.caps[0], message) |
61 | } |
62 | } |
63 | |
64 | class testVMContainerB extends BaseContainer { |
65 | onMessage (m) { |
66 | t.true(m === message, 'should recive a message') |
67 | } |
68 | |
69 | static get typeId () { |
70 | return 8 |
71 | } |
72 | } |
73 | |
74 | const hypervisor = new Hypervisor(tree) |
75 | hypervisor.registerContainer(testVMContainerA) |
76 | hypervisor.registerContainer(testVMContainerB) |
77 | |
78 | let capB = await hypervisor.createActor(testVMContainerB.typeId, new Message()) |
79 | await hypervisor.createActor(testVMContainerA.typeId, new Message({ |
80 | caps: [capB] |
81 | })) |
82 | |
83 | const stateRoot = await hypervisor.createStateRoot() |
84 | t.deepEquals(stateRoot, expectedState, 'expected root!') |
85 | }) |
86 | |
87 | tape('three communicating actors', async t => { |
88 | t.plan(3) |
89 | let message |
90 | const expectedState = { |
91 | '/': Buffer.from('840607eafe779858648d3311039f986e68f4752e', 'hex') |
92 | } |
93 | |
94 | const tree = new RadixTree({ |
95 | db: db |
96 | }) |
97 | |
98 | class testVMContainerA extends BaseContainer { |
99 | onCreation (m) { |
100 | message = new Message() |
101 | this.actor.send(m.caps[0], message) |
102 | } |
103 | } |
104 | |
105 | class testVMContainerB extends BaseContainer { |
106 | onMessage (m) { |
107 | t.true(m === message, 'should recive a message') |
108 | } |
109 | |
110 | static get typeId () { |
111 | return 8 |
112 | } |
113 | } |
114 | |
115 | const hypervisor = new Hypervisor(tree) |
116 | hypervisor.registerContainer(testVMContainerA) |
117 | hypervisor.registerContainer(testVMContainerB) |
118 | |
119 | let capB = await hypervisor.createActor(testVMContainerB.typeId, new Message()) |
120 | await hypervisor.createActor(testVMContainerA.typeId, new Message({ |
121 | caps: [capB] |
122 | })) |
123 | |
124 | await hypervisor.createActor(testVMContainerA.typeId, new Message({ |
125 | caps: [capB] |
126 | })) |
127 | |
128 | const stateRoot = await hypervisor.createStateRoot() |
129 | t.deepEquals(stateRoot, expectedState, 'expected root!') |
130 | }) |
131 | |
132 | tape('three communicating actors, with tick counting', async t => { |
133 | t.plan(3) |
134 | let message |
135 | const expectedState = { |
136 | '/': Buffer.from('840607eafe779858648d3311039f986e68f4752e', 'hex') |
137 | } |
138 | |
139 | const tree = new RadixTree({ |
140 | db: db |
141 | }) |
142 | |
143 | let ticks = 1 |
144 | |
145 | class testVMContainerA extends BaseContainer { |
146 | async onCreation (m) { |
147 | this.actor.incrementTicks(ticks) |
148 | ticks++ |
149 | message = new Message() |
150 | this.actor.send(m.caps[0], message) |
151 | } |
152 | } |
153 | |
154 | class testVMContainerB extends BaseContainer { |
155 | onMessage (m) { |
156 | t.true(m, 'should recive a message') |
157 | } |
158 | |
159 | static get typeId () { |
160 | return 8 |
161 | } |
162 | } |
163 | |
164 | const hypervisor = new Hypervisor(tree) |
165 | hypervisor.registerContainer(testVMContainerA) |
166 | hypervisor.registerContainer(testVMContainerB) |
167 | |
168 | let capB = await hypervisor.createActor(testVMContainerB.typeId, new Message()) |
169 | await hypervisor.createActor(testVMContainerA.typeId, new Message({ |
170 | caps: [capB] |
171 | })) |
172 | |
173 | await hypervisor.createActor(testVMContainerA.typeId, new Message({ |
174 | caps: [capB] |
175 | })) |
176 | |
177 | const stateRoot = await hypervisor.createStateRoot() |
178 | |
179 | t.deepEquals(stateRoot, expectedState, 'expected root!') |
180 | }) |
181 | |
182 | tape('response caps', async t => { |
183 | t.plan(3) |
184 | let message |
185 | const expectedState = { |
186 | '/': Buffer.from('9e8d5671e2c7d167e03784e5d9ec36e15747ad95', 'hex') |
187 | } |
188 | |
189 | const tree = new RadixTree({ |
190 | db: db |
191 | }) |
192 | |
193 | class testVMContainerA extends BaseContainer { |
194 | onCreation (m) { |
195 | message = new Message() |
196 | message.responseCap = this.actor.mintCap() |
197 | this.actor.send(m.caps[0], message) |
198 | } |
199 | |
200 | onMessage (m) { |
201 | t.true(m, 'should recive a response message') |
202 | } |
203 | } |
204 | |
205 | class testVMContainerB extends BaseContainer { |
206 | onMessage (m) { |
207 | t.true(m === message, 'should recive a message') |
208 | } |
209 | |
210 | static get typeId () { |
211 | return 8 |
212 | } |
213 | } |
214 | |
215 | const hypervisor = new Hypervisor(tree) |
216 | hypervisor.registerContainer(testVMContainerA) |
217 | hypervisor.registerContainer(testVMContainerB) |
218 | |
219 | let capB = await hypervisor.createActor(testVMContainerB.typeId, new Message()) |
220 | await hypervisor.createActor(testVMContainerA.typeId, new Message({ |
221 | caps: [capB] |
222 | })) |
223 | |
224 | const stateRoot = await hypervisor.createStateRoot() |
225 | |
226 | t.deepEquals(stateRoot, expectedState, 'expected root!') |
227 | }) |
228 | |
229 | tape('response caps for errors', async t => { |
230 | t.plan(3) |
231 | let message |
232 | const expectedState = { |
233 | '/': Buffer.from('9e8d5671e2c7d167e03784e5d9ec36e15747ad95', 'hex') |
234 | } |
235 | |
236 | const tree = new RadixTree({ |
237 | db: db |
238 | }) |
239 | |
240 | class testVMContainerA extends BaseContainer { |
241 | onCreation (m) { |
242 | message = new Message() |
243 | message.responseCap = this.actor.mintCap() |
244 | this.actor.send(m.caps[0], message) |
245 | } |
246 | |
247 | onMessage (m) { |
248 | t.true(m.data.exceptionError instanceof Error, 'should recive a response message') |
249 | } |
250 | } |
251 | |
252 | class testVMContainerB extends BaseContainer { |
253 | onMessage (m) { |
254 | t.true(m === message, 'should recive a message') |
255 | throw new Error('test error') |
256 | } |
257 | |
258 | static get typeId () { |
259 | return 8 |
260 | } |
261 | } |
262 | |
263 | const hypervisor = new Hypervisor(tree) |
264 | hypervisor.registerContainer(testVMContainerA) |
265 | hypervisor.registerContainer(testVMContainerB) |
266 | |
267 | let capB = await hypervisor.createActor(testVMContainerB.typeId, new Message()) |
268 | await hypervisor.createActor(testVMContainerA.typeId, new Message({ |
269 | caps: [capB] |
270 | })) |
271 | |
272 | const stateRoot = await hypervisor.createStateRoot() |
273 | t.deepEquals(stateRoot, expectedState, 'expected root!') |
274 | }) |
275 | |
276 | tape('actor creation', async t => { |
277 | t.plan(2) |
278 | let message |
279 | const expectedState = { |
280 | '/': Buffer.from('b19c67aea0ff97e96df6e2aacbd45b1bd260af30', 'hex') |
281 | } |
282 | |
283 | const tree = new RadixTree({ |
284 | db: db |
285 | }) |
286 | |
287 | class testVMContainerA extends BaseContainer { |
288 | onCreation (m) { |
289 | message = new Message() |
290 | const cap = this.actor.mintCap() |
291 | message.caps.push(cap) |
292 | return this.actor.createActor(testVMContainerB.typeId, message) |
293 | } |
294 | |
295 | onMessage (m) { |
296 | t.equals(m.data, 'test', 'should recive a response message') |
297 | } |
298 | } |
299 | |
300 | class testVMContainerB extends BaseContainer { |
301 | onCreation (m) { |
302 | const cap = m.caps[0] |
303 | return this.actor.send(cap, new Message({data: 'test'})) |
304 | } |
305 | |
306 | static get typeId () { |
307 | return 8 |
308 | } |
309 | } |
310 | |
311 | const hypervisor = new Hypervisor(tree) |
312 | hypervisor.registerContainer(testVMContainerA) |
313 | hypervisor.registerContainer(testVMContainerB) |
314 | |
315 | await hypervisor.createActor(testVMContainerA.typeId, new Message()) |
316 | |
317 | const stateRoot = await hypervisor.createStateRoot() |
318 | t.deepEquals(stateRoot, expectedState, 'expected root!') |
319 | }) |
320 | |
321 | tape('simple message arbiter test', async t => { |
322 | t.plan(4) |
323 | const expectedState = { |
324 | '/': Buffer.from('9e8d5671e2c7d167e03784e5d9ec36e15747ad95', 'hex') |
325 | } |
326 | |
327 | const tree = new RadixTree({ |
328 | db: db |
329 | }) |
330 | |
331 | class testVMContainerA extends BaseContainer { |
332 | onCreation (m) { |
333 | const message1 = new Message({ |
334 | data: 'first' |
335 | }) |
336 | const message2 = new Message({ |
337 | data: 'second' |
338 | }) |
339 | const message3 = new Message({ |
340 | data: 'third' |
341 | }) |
342 | this.actor.send(m.caps[0], message1) |
343 | this.actor.incrementTicks(1) |
344 | this.actor.send(m.caps[0], message2) |
345 | this.actor.incrementTicks(1) |
346 | this.actor.send(m.caps[0], message3) |
347 | } |
348 | } |
349 | |
350 | let recMsg = 0 |
351 | |
352 | class testVMContainerB extends BaseContainer { |
353 | onMessage (m) { |
354 | if (recMsg === 0) { |
355 | t.equal(m.data, 'first', 'should recive fist message') |
356 | } else if (recMsg === 1) { |
357 | t.equal(m.data, 'second', 'should recive second message') |
358 | } else { |
359 | t.equal(m.data, 'third', 'should recive third message') |
360 | } |
361 | recMsg++ |
362 | } |
363 | |
364 | static get typeId () { |
365 | return 8 |
366 | } |
367 | } |
368 | |
369 | const hypervisor = new Hypervisor(tree) |
370 | hypervisor.registerContainer(testVMContainerA) |
371 | hypervisor.registerContainer(testVMContainerB) |
372 | |
373 | let capB = await hypervisor.createActor(testVMContainerB.typeId, new Message()) |
374 | await hypervisor.createActor(testVMContainerA.typeId, new Message({ |
375 | caps: [capB] |
376 | })) |
377 | |
378 | const stateRoot = await hypervisor.createStateRoot() |
379 | t.deepEquals(stateRoot, expectedState, 'expected root!') |
380 | }) |
381 | |
382 | tape('arbiter test for id comparision', async t => { |
383 | t.plan(4) |
384 | let message |
385 | const expectedState = { |
386 | '/': Buffer.from('840607eafe779858648d3311039f986e68f4752e', 'hex') |
387 | } |
388 | |
389 | const tree = new RadixTree({ |
390 | db: db |
391 | }) |
392 | |
393 | class testVMContainerA extends BaseContainer { |
394 | onCreation (m) { |
395 | message = new Message({ |
396 | data: m.data |
397 | }) |
398 | this.actor.send(m.caps[0], message) |
399 | } |
400 | } |
401 | |
402 | let recMsg = 0 |
403 | |
404 | class testVMContainerB extends BaseContainer { |
405 | onMessage (m) { |
406 | if (recMsg === 0) { |
407 | t.equal(m.data, 'first', 'should recive fist message') |
408 | } else if (recMsg === 1) { |
409 | t.equal(m.data, 'second', 'should recive second message') |
410 | } else { |
411 | t.equal(m.data, 'third', 'should recive third message') |
412 | } |
413 | recMsg++ |
414 | } |
415 | |
416 | static get typeId () { |
417 | return 8 |
418 | } |
419 | } |
420 | |
421 | const hypervisor = new Hypervisor(tree) |
422 | hypervisor.registerContainer(testVMContainerA) |
423 | hypervisor.registerContainer(testVMContainerB) |
424 | |
425 | let capB = await hypervisor.createActor(testVMContainerB.typeId, new Message()) |
426 | await hypervisor.send(capB, new Message({ |
427 | data: 'first' |
428 | })) |
429 | |
430 | await hypervisor.createActor(testVMContainerA.typeId, new Message({ |
431 | caps: [capB], |
432 | data: 'second' |
433 | })) |
434 | |
435 | await hypervisor.createActor(testVMContainerA.typeId, new Message({ |
436 | caps: [capB], |
437 | data: 'third' |
438 | })) |
439 | |
440 | const stateRoot = await hypervisor.createStateRoot() |
441 | t.deepEquals(stateRoot, expectedState, 'expected root!') |
442 | }) |
443 | |
444 | tape('basic tagged caps', async t => { |
445 | t.plan(4) |
446 | const expectedState = { |
447 | '/': Buffer.from('d4291da4536544bf90aa473a1148cb29f913d078', 'hex') |
448 | } |
449 | |
450 | const tree = new RadixTree({ |
451 | db: db |
452 | }) |
453 | |
454 | class testVMContainerA extends BaseContainer { |
455 | async onMessage (m) { |
456 | t.true(m, 'should recive first message') |
457 | const rCap = this.actor.mintCap(1) |
458 | const message = new Message() |
459 | message.responseCap = rCap |
460 | this.actor.send(m.caps[0], message) |
461 | const rMessage = await this.actor.inbox.nextTaggedMessage([1], 44) |
462 | t.true(rMessage, 'should recive a response message') |
463 | } |
464 | } |
465 | |
466 | class testVMContainerB extends BaseContainer { |
467 | onMessage (m) { |
468 | t.true(m, 'should recive a message') |
469 | } |
470 | |
471 | static get typeId () { |
472 | return 8 |
473 | } |
474 | } |
475 | |
476 | const hypervisor = new Hypervisor(tree) |
477 | hypervisor.registerContainer(testVMContainerA) |
478 | hypervisor.registerContainer(testVMContainerB) |
479 | |
480 | let capA = await hypervisor.createActor(testVMContainerA.typeId, new Message()) |
481 | let capB = await hypervisor.createActor(testVMContainerB.typeId, new Message()) |
482 | |
483 | await hypervisor.send(capA, new Message({caps: [capB]})) |
484 | |
485 | const stateRoot = await hypervisor.createStateRoot() |
486 | t.deepEquals(stateRoot, expectedState, 'expected root!') |
487 | }) |
488 | |
489 | tape('return while waiting for tag', async t => { |
490 | t.plan(4) |
491 | const expectedState = { |
492 | '/': Buffer.from('d4291da4536544bf90aa473a1148cb29f913d078', 'hex') |
493 | } |
494 | |
495 | const tree = new RadixTree({ |
496 | db: db |
497 | }) |
498 | |
499 | class testVMContainerA extends BaseContainer { |
500 | async onMessage (m) { |
501 | if (m.tag === 1) { |
502 | t.true(m, 'should recive second message') |
503 | } else { |
504 | t.true(m, 'should recive first message') |
505 | const rCap = this.actor.mintCap(1) |
506 | const message = new Message() |
507 | message.responseCap = rCap |
508 | this.actor.send(m.caps[0], message) |
509 | this.actor.inbox.nextTaggedMessage([1], 44) |
510 | } |
511 | } |
512 | } |
513 | |
514 | class testVMContainerB extends BaseContainer { |
515 | onMessage (m) { |
516 | t.true(m, 'should recive a message') |
517 | } |
518 | |
519 | static get typeId () { |
520 | return 8 |
521 | } |
522 | } |
523 | |
524 | const hypervisor = new Hypervisor(tree) |
525 | hypervisor.registerContainer(testVMContainerA) |
526 | hypervisor.registerContainer(testVMContainerB) |
527 | |
528 | let capA = await hypervisor.createActor(testVMContainerA.typeId, new Message()) |
529 | let capB = await hypervisor.createActor(testVMContainerB.typeId, new Message()) |
530 | |
531 | await hypervisor.send(capA, new Message({caps: [capB]})) |
532 | |
533 | const stateRoot = await hypervisor.createStateRoot() |
534 | t.deepEquals(stateRoot, expectedState, 'expected root!') |
535 | }) |
536 | |
537 | tape('trying to listen for caps more then once', async t => { |
538 | t.plan(4) |
539 | const expectedState = { |
540 | '/': Buffer.from('d4291da4536544bf90aa473a1148cb29f913d078', 'hex') |
541 | } |
542 | |
543 | const tree = new RadixTree({ |
544 | db: db |
545 | }) |
546 | |
547 | class testVMContainerA extends BaseContainer { |
548 | async onMessage (m) { |
549 | t.true(m, 'should recive first message') |
550 | const rCap = this.actor.mintCap(1) |
551 | const message = new Message({data: 'first'}) |
552 | message.responseCap = rCap |
553 | this.actor.send(m.caps[0], message) |
554 | const promise = this.actor.inbox.nextTaggedMessage([1], 44) |
555 | try { |
556 | await this.actor.inbox.nextTaggedMessage([1], 44) |
557 | } catch (e) { |
558 | t.true(e, 'should error if waiting twice') |
559 | } |
560 | return promise |
561 | } |
562 | } |
563 | |
564 | class testVMContainerB extends BaseContainer { |
565 | onMessage (m) { |
566 | t.true(m, 'should recive a message') |
567 | } |
568 | |
569 | static get typeId () { |
570 | return 8 |
571 | } |
572 | } |
573 | |
574 | const hypervisor = new Hypervisor(tree) |
575 | hypervisor.registerContainer(testVMContainerA) |
576 | hypervisor.registerContainer(testVMContainerB) |
577 | |
578 | let capA = await hypervisor.createActor(testVMContainerA.typeId, new Message()) |
579 | let capB = await hypervisor.createActor(testVMContainerB.typeId, new Message()) |
580 | |
581 | await hypervisor.send(capA, new Message({caps: [capB]})) |
582 | |
583 | const stateRoot = await hypervisor.createStateRoot() |
584 | t.deepEquals(stateRoot, expectedState, 'expected root!') |
585 | }) |
586 | |
587 | tape('multple messages to restore on waiting for tags', async t => { |
588 | t.plan(6) |
589 | const expectedState = { |
590 | '/': Buffer.from('b5c0822ccb21bbaa2ad8069c4dcd18add7d6e2d1', 'hex') |
591 | } |
592 | |
593 | const tree = new RadixTree({ |
594 | db: db |
595 | }) |
596 | |
597 | class testVMContainerA extends BaseContainer { |
598 | async onMessage (m) { |
599 | t.true(m, 'should recive first message') |
600 | if (m.caps.length) { |
601 | const cap1 = this.actor.mintCap(1) |
602 | const cap2 = this.actor.mintCap(2) |
603 | const message1 = new Message({ |
604 | data: 'first' |
605 | }) |
606 | const message2 = new Message({ |
607 | data: 'second' |
608 | }) |
609 | message1.caps.push(cap1) |
610 | message2.caps.push(cap2) |
611 | this.actor.send(m.caps[0], message1) |
612 | this.actor.send(m.caps[1], message2) |
613 | const rMessage = await this.actor.inbox.nextTaggedMessage([1, 2], 44) |
614 | t.true(rMessage, 'should recive a response message') |
615 | } |
616 | } |
617 | } |
618 | |
619 | class testVMContainerB extends BaseContainer { |
620 | onMessage (m) { |
621 | t.true(m, 'should recive a message') |
622 | const cap = m.caps[0] |
623 | this.actor.incrementTicks(1) |
624 | this.actor.send(cap, new Message({data: m.data})) |
625 | } |
626 | |
627 | static get typeId () { |
628 | return 8 |
629 | } |
630 | } |
631 | |
632 | const hypervisor = new Hypervisor(tree) |
633 | hypervisor.registerContainer(testVMContainerA) |
634 | hypervisor.registerContainer(testVMContainerB) |
635 | |
636 | let capA = await hypervisor.createActor(testVMContainerA.typeId, new Message()) |
637 | let capB1 = await hypervisor.createActor(testVMContainerB.typeId, new Message()) |
638 | let capB2 = await hypervisor.createActor(testVMContainerB.typeId, new Message()) |
639 | |
640 | await hypervisor.send(capA, new Message({caps: [capB1, capB2]})) |
641 | |
642 | const stateRoot = await hypervisor.createStateRoot() |
643 | |
644 | t.deepEquals(stateRoot, expectedState, 'expected root!') |
645 | }) |
646 | |
647 | tape('multple messages to backup on waiting for tags', async t => { |
648 | t.plan(6) |
649 | const expectedState = { |
650 | '/': Buffer.from('b5c0822ccb21bbaa2ad8069c4dcd18add7d6e2d1', 'hex') |
651 | } |
652 | |
653 | const tree = new RadixTree({ |
654 | db: db |
655 | }) |
656 | |
657 | class testVMContainerA extends BaseContainer { |
658 | async onMessage (m) { |
659 | t.true(m, 'should recive first message') |
660 | if (m.caps.length) { |
661 | const cap1 = this.actor.mintCap(1) |
662 | const cap2 = this.actor.mintCap(2) |
663 | const message1 = new Message({ |
664 | data: 'first' |
665 | }) |
666 | const message2 = new Message({ |
667 | data: 'second' |
668 | }) |
669 | message1.caps.push(cap1) |
670 | message2.caps.push(cap2) |
671 | this.actor.send(m.caps[0], message1) |
672 | this.actor.send(m.caps[1], message2) |
673 | const rMessage = await this.actor.inbox.nextTaggedMessage([1, 2], 44) |
674 | t.true(rMessage, 'should recive a response message') |
675 | } |
676 | } |
677 | } |
678 | |
679 | class testVMContainerB extends BaseContainer { |
680 | async onMessage (m) { |
681 | t.true(m, 'should recive a message') |
682 | const cap = m.caps[0] |
683 | this.actor.incrementTicks(1) |
684 | this.actor.send(cap, new Message({data: m.data})) |
685 | } |
686 | |
687 | static get typeId () { |
688 | return 8 |
689 | } |
690 | } |
691 | |
692 | const hypervisor = new Hypervisor(tree) |
693 | hypervisor.registerContainer(testVMContainerA) |
694 | hypervisor.registerContainer(testVMContainerB) |
695 | |
696 | let capA = await hypervisor.createActor(testVMContainerA.typeId, new Message()) |
697 | let capB1 = await hypervisor.createActor(testVMContainerB.typeId, new Message()) |
698 | let capB2 = await hypervisor.createActor(testVMContainerB.typeId, new Message()) |
699 | |
700 | await hypervisor.send(capA, new Message({caps: [capB1, capB2]})) |
701 | |
702 | const stateRoot = await hypervisor.createStateRoot() |
703 | t.deepEquals(stateRoot, expectedState, 'expected root!') |
704 | }) |
705 | |
706 | tape('multple messages, but single tag', async t => { |
707 | t.plan(6) |
708 | const expectedState = { |
709 | '/': Buffer.from('b5c0822ccb21bbaa2ad8069c4dcd18add7d6e2d1', 'hex') |
710 | } |
711 | |
712 | const tree = new RadixTree({ |
713 | db: db |
714 | }) |
715 | |
716 | class testVMContainerA extends BaseContainer { |
717 | async onMessage (m) { |
718 | t.true(m, 'should recive first message') |
719 | if (m.caps.length) { |
720 | const cap1 = this.actor.mintCap(1) |
721 | const cap2 = this.actor.mintCap(2) |
722 | const message1 = new Message({ |
723 | data: 'first' |
724 | }) |
725 | const message2 = new Message({ |
726 | data: 'second' |
727 | }) |
728 | message1.caps.push(cap1) |
729 | message2.caps.push(cap2) |
730 | await this.actor.send(m.caps[0], message1) |
731 | await this.actor.send(m.caps[1], message2) |
732 | const rMessage = await this.actor.inbox.nextTaggedMessage([2], 44) |
733 | t.true(rMessage, 'should recive a response message') |
734 | } |
735 | } |
736 | } |
737 | |
738 | class testVMContainerB extends BaseContainer { |
739 | async onMessage (m) { |
740 | t.true(m, 'should recive a message') |
741 | const cap = m.caps[0] |
742 | this.actor.incrementTicks(1) |
743 | this.actor.send(cap, new Message({data: m.data})) |
744 | } |
745 | |
746 | static get typeId () { |
747 | return 8 |
748 | } |
749 | } |
750 | |
751 | const hypervisor = new Hypervisor(tree) |
752 | hypervisor.registerContainer(testVMContainerA) |
753 | hypervisor.registerContainer(testVMContainerB) |
754 | |
755 | let capA = await hypervisor.createActor(testVMContainerA.typeId, new Message()) |
756 | let capB1 = await hypervisor.createActor(testVMContainerB.typeId, new Message()) |
757 | let capB2 = await hypervisor.createActor(testVMContainerB.typeId, new Message()) |
758 | |
759 | await hypervisor.send(capA, new Message({caps: [capB1, capB2]})) |
760 | |
761 | const stateRoot = await hypervisor.createStateRoot() |
762 | t.deepEquals(stateRoot, expectedState, 'expected root!') |
763 | }) |
764 | |
765 | tape('deadlock test', async t => { |
766 | t.plan(7) |
767 | const expectedState = { |
768 | '/': Buffer.from('f290945ad63dd06b9ada924fa5149df4a0a32f53', 'hex') |
769 | } |
770 | |
771 | const tree = new RadixTree({ |
772 | db: db |
773 | }) |
774 | |
775 | class testVMContainerA extends BaseContainer { |
776 | async onMessage (m) { |
777 | t.true(m, 'should recive first message 1') |
778 | const rMessage = await this.actor.inbox.nextTaggedMessage([1], 50) |
779 | t.equals(rMessage, undefined, 'should recive a response message 1') |
780 | } |
781 | } |
782 | |
783 | class testVMContainerB extends BaseContainer { |
784 | async onMessage (m) { |
785 | t.true(m, 'should recive first message 2') |
786 | this.actor.incrementTicks(47) |
787 | const rMessage = await this.actor.inbox.nextTaggedMessage([1], 1) |
788 | t.equals(rMessage, undefined, 'should recive a response message 2') |
789 | } |
790 | |
791 | static get typeId () { |
792 | return 8 |
793 | } |
794 | } |
795 | |
796 | class testVMContainerC extends BaseContainer { |
797 | async onMessage (m) { |
798 | t.true(m, 'should recive first message 3') |
799 | this.actor.incrementTicks(45) |
800 | const rMessage = await this.actor.inbox.nextTaggedMessage([1], 1) |
801 | t.equals(rMessage, undefined, 'should recive a response message 3') |
802 | } |
803 | |
804 | static get typeId () { |
805 | return 7 |
806 | } |
807 | } |
808 | |
809 | const hypervisor = new Hypervisor(tree) |
810 | hypervisor.registerContainer(testVMContainerA) |
811 | hypervisor.registerContainer(testVMContainerB) |
812 | hypervisor.registerContainer(testVMContainerC) |
813 | |
814 | let capA = await hypervisor.createActor(testVMContainerA.typeId, new Message()) |
815 | let capB = await hypervisor.createActor(testVMContainerB.typeId, new Message()) |
816 | let capC = await hypervisor.createActor(testVMContainerC.typeId, new Message()) |
817 | |
818 | await Promise.all([ |
819 | hypervisor.send(capA, new Message()), |
820 | hypervisor.send(capB, new Message()), |
821 | hypervisor.send(capC, new Message()) |
822 | ]) |
823 | const stateRoot = await hypervisor.createStateRoot() |
824 | t.deepEquals(stateRoot, expectedState, 'expected root!') |
825 | }) |
826 |
Built with git-ssb-web