git ssb

0+

wanderer🌟 / js-primea-hypervisor



Commit bc308a00381563261f0f7ecf1d231d19e2dfbc52

added nextMessage check

wanderer committed on 12/8/2017, 7:52:53 PM
Parent: 0e2eb764d9e711043dde259c1e74c27d2b613c09

Files changed

actor.jschanged
inbox.jschanged
index.jschanged
tests/index.jschanged
actor.jsView
@@ -34,10 +34,10 @@
3434 * Mints a new capabilitly with a given tag
3535 * @param {*} tag - a tag which can be used to identify caps
3636 * @return {Object}
3737 */
38- mintCap (tag = 0) {
39- return new Cap(this.id, tag)
38+ mintCap (tag = 0, funcIndex = 0) {
39+ return new Cap(this.id, tag, funcIndex)
4040 }
4141
4242 /**
4343 * adds a message to this actor's message queue
@@ -94,19 +94,18 @@
9494
9595 static serializeMetaData (type, transparent = 0, nonce = 0) {
9696 const p = new Pipe()
9797 leb128.write(type, p)
98- p.write(Buffer.from([transparent]))
98+ p.write(Buffer.from([0]))
9999 leb128.write(nonce, p)
100100 return p.buffer
101101 }
102102
103103 static deserializeMetaData (buffer) {
104104 const pipe = new Pipe(buffer)
105105 return {
106106 type: leb128.read(pipe),
107- nonce: leb128.read(pipe),
108- transparent: pipe.read(1)[0]
107+ nonce: leb128.read(pipe)
109108 }
110109 }
111110
112111 /**
inbox.jsView
@@ -52,8 +52,11 @@
5252 * @param {Integer} timeout
5353 * @returns {Promise}
5454 */
5555 async nextTaggedMessage (tags, timeout) {
56+ if (this._waitingTags) {
57+ throw new Error('already waiting on tags')
58+ }
5659 this._waitingTags = new Set(tags)
5760 this._queue = this._queue.filter(message => !this._queueTaggedMessage(message))
5861
5962 // todo: add saturation test
@@ -69,15 +72,16 @@
6972 * Waits for the the next message if any
7073 * @param {Integer} timeout
7174 * @returns {Promise}
7275 */
73- async nextMessage (timeout = 0) {
74- if (this._gettingNextMessage) {
75- throw new Error('already getting next message')
76- } else {
77- this._gettingNextMessage = true
76+ nextMessage (timeout) {
77+ if (!this._gettingNextMessage) {
78+ this._gettingNextMessage = this._nextMessage(timeout)
7879 }
80+ return this._gettingNextMessage
81+ }
7982
83+ async _nextMessage (timeout = 0) {
8084 await Promise.all([...this.actor._sending.values()])
8185 let message = this._getOldestMessage()
8286 if (message === undefined && timeout === 0) {
8387 return
index.jsView
@@ -28,9 +28,9 @@
2828
2929 // loads an instance of a container from the state
3030 async _loadActor (id) {
3131 const state = await this.tree.getSubTree(id)
32- const {type, nonce, transparent} = Actor.deserializeMetaData(state.root['/'][3])
32+ const {type, nonce} = Actor.deserializeMetaData(state.root['/'][3])
3333 const container = this._containerTypes[type]
3434
3535 // create a new actor instance
3636 const actor = new Actor({
@@ -38,9 +38,8 @@
3838 state,
3939 container,
4040 id,
4141 nonce,
42- transparent,
4342 type
4443 })
4544
4645 // save the newly created instance
tests/index.jsView
@@ -485,8 +485,56 @@
485485 const stateRoot = await hypervisor.createStateRoot()
486486 t.deepEquals(stateRoot, expectedState, 'expected root!')
487487 })
488488
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+
489537 tape('trying to listen for caps more then once', async t => {
490538 t.plan(4)
491539 const expectedState = {
492540 '/': Buffer.from('d4291da4536544bf90aa473a1148cb29f913d078', 'hex')

Built with git-ssb-web