git ssb

0+

wanderer🌟 / js-primea-hypervisor



Commit e1300d130bf43108644745c15e32b3573369fc84

speed up tests, track sending

Signed-off-by: wanderer <mjbecze@gmail.com>
wanderer committed on 11/26/2017, 3:50:02 AM
Parent: 06883c17f234bddfdc0a57bba0e27a7b31b4d9a9

Files changed

actor.jschanged
inbox.jschanged
tests/index.jschanged
actor.jsView
@@ -1,5 +1,6 @@
11 const Message = require('primea-message')
2+const LockMap = require('lockmap')
23 const CapsManager = require('./capsManager.js')
34 const Inbox = require('./inbox.js')
45
56 module.exports = class Actor {
@@ -25,8 +26,9 @@
2526 })
2627
2728 this.ticks = 0
2829 this.running = false
30+ this._sending = new LockMap()
2931
3032 this.caps = new CapsManager(opts.state.caps)
3133 }
3234
@@ -165,11 +167,12 @@
165167 * @param {Object} portRef - the port
166168 * @param {Message} message - the message
167169 */
168170 send (cap, message) {
171+ const resolve = this._sending.lock(cap)
169172 message._fromTicks = this.ticks
170173 message._fromId = this.id
171174 message.tag = cap.tag
172175
173- return this.hypervisor.send(cap, message)
176+ return this.hypervisor.send(cap, message).then(() => resolve(cap))
174177 }
175178 }
inbox.jsView
@@ -76,8 +76,9 @@
7676 } else {
7777 this._gettingNextMessage = true
7878 }
7979
80+ await Promise.all([...this.actor._sending.values()])
8081 let message = this._getOldestMessage()
8182 if (message === undefined && timeout === 0) {
8283 return
8384 }
tests/index.jsView
@@ -69,9 +69,9 @@
6969
7070 class testVMContainerA extends BaseContainer {
7171 onCreation (m) {
7272 message = new Message()
73- return this.actor.send(m.caps[0], message)
73+ this.actor.send(m.caps[0], message)
7474 }
7575 }
7676
7777 class testVMContainerB extends BaseContainer {
@@ -111,9 +111,9 @@
111111
112112 class testVMContainerA extends BaseContainer {
113113 onCreation (m) {
114114 message = new Message()
115- return this.actor.send(m.caps[0], message)
115+ this.actor.send(m.caps[0], message)
116116 }
117117 }
118118
119119 class testVMContainerB extends BaseContainer {
@@ -161,18 +161,15 @@
161161 async onCreation (m) {
162162 this.actor.incrementTicks(ticks)
163163 ticks++
164164 message = new Message()
165- await this.actor.send(m.caps[0], message)
165+ this.actor.send(m.caps[0], message)
166166 }
167167 }
168168
169169 class testVMContainerB extends BaseContainer {
170170 onMessage (m) {
171171 t.true(m, 'should recive a message')
172- return new Promise((resolve, reject) => {
173- setTimeout(resolve, 200)
174- })
175172 }
176173
177174 static get typeId () {
178175 return 8
@@ -183,9 +180,9 @@
183180 hypervisor.registerContainer(testVMContainerA)
184181 hypervisor.registerContainer(testVMContainerB)
185182
186183 let capB = await hypervisor.createActor(testVMContainerB.typeId, new Message())
187- hypervisor.createActor(testVMContainerA.typeId, new Message({
184+ await hypervisor.createActor(testVMContainerA.typeId, new Message({
188185 caps: [capB]
189186 }))
190187
191188 await hypervisor.createActor(testVMContainerA.typeId, new Message({
@@ -211,9 +208,9 @@
211208 class testVMContainerA extends BaseContainer {
212209 onCreation (m) {
213210 message = new Message()
214211 message.responseCap = this.actor.mintCap()
215- return this.actor.send(m.caps[0], message)
212+ this.actor.send(m.caps[0], message)
216213 }
217214
218215 onMessage (m) {
219216 t.true(m, 'should recive a response message')
@@ -258,9 +255,9 @@
258255 class testVMContainerA extends BaseContainer {
259256 onCreation (m) {
260257 message = new Message()
261258 message.responseCap = this.actor.mintCap()
262- return this.actor.send(m.caps[0], message)
259+ this.actor.send(m.caps[0], message)
263260 }
264261
265262 onMessage (m) {
266263 t.true(m.data.exceptionError instanceof Error, 'should recive a response message')
@@ -318,9 +315,9 @@
318315
319316 class testVMContainerB extends BaseContainer {
320317 onCreation (m) {
321318 const cap = m.caps[0]
322- return this.actor.send(cap, new Message({data: 'test'}))
319+ this.actor.send(cap, new Message({data: 'test'}))
323320 }
324321
325322 static get typeId () {
326323 return 8
@@ -362,9 +359,9 @@
362359 this.actor.send(m.caps[0], message1)
363360 this.actor.incrementTicks(1)
364361 this.actor.send(m.caps[0], message2)
365362 this.actor.incrementTicks(1)
366- return this.actor.send(m.caps[0], message3)
363+ this.actor.send(m.caps[0], message3)
367364 }
368365 }
369366
370367 let recMsg = 0
@@ -431,12 +428,8 @@
431428 } else {
432429 t.equal(m.data, 'third', 'should recive third message')
433430 }
434431 recMsg++
435-
436- return new Promise((resolve, reject) => {
437- setTimeout(resolve, 200)
438- })
439432 }
440433
441434 static get typeId () {
442435 return 8
@@ -447,11 +440,12 @@
447440 hypervisor.registerContainer(testVMContainerA)
448441 hypervisor.registerContainer(testVMContainerB)
449442
450443 let capB = await hypervisor.createActor(testVMContainerB.typeId, new Message())
451- hypervisor.send(capB, new Message({
444+ await hypervisor.send(capB, new Message({
452445 data: 'first'
453446 }))
447+
454448 await hypervisor.createActor(testVMContainerA.typeId, new Message({
455449 caps: [capB],
456450 data: 'second'
457451 }))
@@ -481,9 +475,9 @@
481475 t.true(m, 'should recive first message')
482476 const rCap = this.actor.mintCap(1)
483477 const message = new Message()
484478 message.responseCap = rCap
485- await this.actor.send(m.caps[0], message)
479+ this.actor.send(m.caps[0], message)
486480 const rMessage = await this.actor.inbox.nextTaggedMessage([1], 44)
487481 t.true(rMessage, 'should recive a response message')
488482 }
489483 }
@@ -527,9 +521,9 @@
527521 t.true(m, 'should recive first message')
528522 const rCap = this.actor.mintCap(1)
529523 const message = new Message({data: 'first'})
530524 message.responseCap = rCap
531- await this.actor.send(m.caps[0], message)
525+ this.actor.send(m.caps[0], message)
532526 const promise = this.actor.inbox.nextTaggedMessage([1], 44)
533527 try {
534528 await this.actor.inbox.nextTaggedMessage([1], 44)
535529 } catch (e) {
@@ -586,28 +580,22 @@
586580 data: 'second'
587581 })
588582 message1.caps.push(cap1)
589583 message2.caps.push(cap2)
590- await Promise.all([
591- this.actor.send(m.caps[0], message1),
592- this.actor.send(m.caps[1], message2)
593- ])
584+ this.actor.send(m.caps[0], message1)
585+ this.actor.send(m.caps[1], message2)
594586 const rMessage = await this.actor.inbox.nextTaggedMessage([1, 2], 44)
595587 t.true(rMessage, 'should recive a response message')
596588 }
597589 }
598590 }
599591
600592 class testVMContainerB extends BaseContainer {
601- async onMessage (m) {
593+ onMessage (m) {
602594 t.true(m, 'should recive a message')
603595 const cap = m.caps[0]
604596 this.actor.incrementTicks(1)
605- await this.actor.send(cap, new Message({data: m.data}))
606-
607- return new Promise((resolve, reject) => {
608- setTimeout(resolve, 200)
609- })
597+ this.actor.send(cap, new Message({data: m.data}))
610598 }
611599
612600 static get typeId () {
613601 return 8
@@ -652,10 +640,10 @@
652640 data: 'second'
653641 })
654642 message1.caps.push(cap1)
655643 message2.caps.push(cap2)
656- await this.actor.send(m.caps[0], message1)
657- await this.actor.send(m.caps[1], message2)
644+ this.actor.send(m.caps[0], message1)
645+ this.actor.send(m.caps[1], message2)
658646 const rMessage = await this.actor.inbox.nextTaggedMessage([1, 2], 44)
659647 t.true(rMessage, 'should recive a response message')
660648 }
661649 }
@@ -665,13 +653,9 @@
665653 async onMessage (m) {
666654 t.true(m, 'should recive a message')
667655 const cap = m.caps[0]
668656 this.actor.incrementTicks(1)
669- await this.actor.send(cap, new Message({data: m.data}))
670-
671- return new Promise((resolve, reject) => {
672- setTimeout(resolve, 200)
673- })
657+ this.actor.send(cap, new Message({data: m.data}))
674658 }
675659
676660 static get typeId () {
677661 return 8
@@ -729,13 +713,9 @@
729713 async onMessage (m) {
730714 t.true(m, 'should recive a message')
731715 const cap = m.caps[0]
732716 this.actor.incrementTicks(1)
733- await this.actor.send(cap, new Message({data: m.data}))
734-
735- return new Promise((resolve, reject) => {
736- setTimeout(resolve, 200)
737- })
717+ this.actor.send(cap, new Message({data: m.data}))
738718 }
739719
740720 static get typeId () {
741721 return 8

Built with git-ssb-web