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.js | changed |
scheduler.js | changed |
tests/index.js | changed |
inbox.js | ||
---|---|---|
@@ -1,6 +1,6 @@ | ||
1 | 1 | const binarySearchInsert = require('binary-search-insert') |
2 | -// const Buffer = require('safe-buffer').Buffer | |
2 | +const Buffer = require('safe-buffer').Buffer | |
3 | 3 | |
4 | 4 | module.exports = class Inbox { |
5 | 5 | /** |
6 | 6 | * The port manager manages the the ports. This inculdes creation, deletion |
@@ -28,9 +28,8 @@ | ||
28 | 28 | binarySearchInsert(this._queue, messageArbiter, message) |
29 | 29 | this._queueWaitingTags(message) |
30 | 30 | |
31 | 31 | const oldestMessage = this._getOldestMessage() |
32 | - | |
33 | 32 | if (oldestMessage === message) { |
34 | 33 | this._oldestMessageResolve(message) |
35 | 34 | this._oldestMessagePromise = new Promise((resolve, reject) => { |
36 | 35 | this._oldestMessageResolve = resolve |
@@ -107,18 +106,12 @@ | ||
107 | 106 | } |
108 | 107 | |
109 | 108 | // decides which message to go first |
110 | 109 | function messageArbiter (messageA, messageB) { |
111 | - if (!messageA) { | |
112 | - return messageB | |
113 | - } else if (!messageB) { | |
114 | - return messageA | |
115 | - } | |
116 | - | |
117 | 110 | // order by number of ticks if messages have different number of ticks |
118 | 111 | if (messageA._fromTicks !== messageB._fromTicks) { |
119 | - return messageA._fromTicks < messageB._fromTicks ? messageA : messageB | |
112 | + return messageA._fromTicks > messageB._fromTicks | |
120 | 113 | } else { |
121 | 114 | // sender id |
122 | - return Buffer.compare(messageA._fromId, messageB._fromId) ? messageA : messageB | |
115 | + return Buffer.compare(messageA._fromId, messageB._fromId) | |
123 | 116 | } |
124 | 117 | } |
scheduler.js | ||
---|---|---|
@@ -112,9 +112,8 @@ | ||
112 | 112 | // find the old container and see if to can resolve any of the waits |
113 | 113 | while (this._waits[0]) { |
114 | 114 | const wait = this._waits[0] |
115 | 115 | const least = this.leastNumberOfTicks(wait.id) |
116 | - // console.log(wait, least, this.instances) | |
117 | 116 | if (wait.ticks <= least) { |
118 | 117 | this._waits.shift() |
119 | 118 | wait.resolve() |
120 | 119 | this._running.add(wait.id) |
tests/index.js | ||
---|---|---|
@@ -348,4 +348,66 @@ | ||
348 | 348 | t.deepEquals(stateRoot, expectedState, 'expected root!') |
349 | 349 | t.equals(hypervisor.scheduler.leastNumberOfTicks(), 0) |
350 | 350 | }) |
351 | 351 | |
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