git ssb

0+

wanderer🌟 / js-primea-hypervisor



Commit 9a16576184581f6734875fc6fa632bb9bd5fdad8

fixes for the arbiter

Signed-off-by: wanderer <mjbecze@gmail.com>
wanderer committed on 11/17/2017, 1:39:43 AM
Parent: a3ab8ad379b7b0a8f04de14210c76517f1cf4306

Files changed

inbox.jschanged
scheduler.jschanged
tests/index.jschanged
inbox.jsView
@@ -1,6 +1,6 @@
11 const binarySearchInsert = require('binary-search-insert')
2-// const Buffer = require('safe-buffer').Buffer
2+const Buffer = require('safe-buffer').Buffer
33
44 module.exports = class Inbox {
55 /**
66 * The port manager manages the the ports. This inculdes creation, deletion
@@ -28,9 +28,8 @@
2828 binarySearchInsert(this._queue, messageArbiter, message)
2929 this._queueWaitingTags(message)
3030
3131 const oldestMessage = this._getOldestMessage()
32-
3332 if (oldestMessage === message) {
3433 this._oldestMessageResolve(message)
3534 this._oldestMessagePromise = new Promise((resolve, reject) => {
3635 this._oldestMessageResolve = resolve
@@ -107,18 +106,12 @@
107106 }
108107
109108 // decides which message to go first
110109 function messageArbiter (messageA, messageB) {
111- if (!messageA) {
112- return messageB
113- } else if (!messageB) {
114- return messageA
115- }
116-
117110 // order by number of ticks if messages have different number of ticks
118111 if (messageA._fromTicks !== messageB._fromTicks) {
119- return messageA._fromTicks < messageB._fromTicks ? messageA : messageB
112+ return messageA._fromTicks > messageB._fromTicks
120113 } else {
121114 // sender id
122- return Buffer.compare(messageA._fromId, messageB._fromId) ? messageA : messageB
115+ return Buffer.compare(messageA._fromId, messageB._fromId)
123116 }
124117 }
scheduler.jsView
@@ -112,9 +112,8 @@
112112 // find the old container and see if to can resolve any of the waits
113113 while (this._waits[0]) {
114114 const wait = this._waits[0]
115115 const least = this.leastNumberOfTicks(wait.id)
116- // console.log(wait, least, this.instances)
117116 if (wait.ticks <= least) {
118117 this._waits.shift()
119118 wait.resolve()
120119 this._running.add(wait.id)
tests/index.jsView
@@ -348,4 +348,66 @@
348348 t.deepEquals(stateRoot, expectedState, 'expected root!')
349349 t.equals(hypervisor.scheduler.leastNumberOfTicks(), 0)
350350 })
351351
352+tape('simple message arbiter test', async t => {
353+ t.plan(5)
354+ const expectedState = {
355+ '/': Buffer.from('fc935489953ed357f06171dd23439d83190b3a1b', 'hex')
356+ }
357+
358+ const tree = new RadixTree({
359+ db: db
360+ })
361+
362+ class testVMContainerA extends BaseContainer {
363+ onCreation (m) {
364+ const message1 = new Message({
365+ data: 'first'
366+ })
367+ const message2 = new Message({
368+ data: 'second'
369+ })
370+ const message3 = new Message({
371+ data: 'third'
372+ })
373+ this.kernel.send(m.caps[0], message1)
374+ this.kernel.incrementTicks(1)
375+ this.kernel.send(m.caps[0], message2)
376+ this.kernel.incrementTicks(1)
377+ return this.kernel.send(m.caps[0], message3)
378+ }
379+ }
380+
381+ let recMsg = 0
382+
383+ class testVMContainerB extends BaseContainer {
384+ onMessage (m) {
385+ if (recMsg === 0) {
386+ t.equal(m.data, 'first', 'should recive fist message')
387+ } else if (recMsg === 1) {
388+ t.equal(m.data, 'second', 'should recive second message')
389+ } else if (recMsg === 2) {
390+ t.equal(m.data, 'third', 'should recive third message')
391+ }
392+ recMsg++
393+ }
394+
395+ static get typeId () {
396+ return 8
397+ }
398+ }
399+
400+ const hypervisor = new Hypervisor(tree)
401+ hypervisor.registerContainer(testVMContainerA)
402+ hypervisor.registerContainer(testVMContainerB)
403+
404+ let capB = await hypervisor.createInstance(testVMContainerB.typeId, new Message())
405+ await hypervisor.createInstance(testVMContainerA.typeId, new Message({
406+ caps: [capB]
407+ }))
408+
409+ const stateRoot = await hypervisor.createStateRoot(Infinity)
410+
411+ t.deepEquals(stateRoot, expectedState, 'expected root!')
412+ t.equals(hypervisor.scheduler.leastNumberOfTicks(), 0)
413+})

Built with git-ssb-web