git ssb

0+

wanderer🌟 / js-primea-hypervisor



Commit 6b20ce6345f5cf4e554a3d7b67876db977eb8070

add checking for tie messages

wanderer committed on 6/20/2017, 5:38:35 PM
Parent: 9fbf8df01199fda8b2e900caefc75b880c9e095e

Files changed

exoInterface.jschanged
index.jschanged
portManager.jschanged
tests/index.jschanged
exoInterface.jsView
@@ -49,13 +49,16 @@
4949 await this.hypervisor.scheduler.wait(this.ticks, this.id)
5050 }
5151
5252 if (this.ports.hasMessages()) {
53- const message = this.ports.nextMessage()
53+ let message = this.ports.peekNextMessage()
5454 if (this.ticks < message._fromTicks) {
5555 this.ticks = message._fromTicks
56+ // check for tie messages
5657 this.hypervisor.scheduler.update(this)
58+ await this.hypervisor.scheduler.wait(this.ticks, this.id)
5759 }
60+ message = this.ports.nextMessage()
5861 this.currentMessage = message
5962
6063 // run the next message
6164 this.run(message)
index.jsView
@@ -49,10 +49,10 @@
4949 id: id
5050 })
5151
5252 // save the newly created instance
53+ this.scheduler.update(exoInterface)
5354 this.scheduler.releaseLock(lock)
54- this.scheduler.update(exoInterface)
5555 return exoInterface
5656 }
5757
5858 async createInstance (type, code, entryPorts = [], id = {nonce: 0, parent: null}) {
portManager.jsView
@@ -13,11 +13,8 @@
1313
1414 // order by number of ticks if messages have different number of ticks
1515 if (a._fromTicks !== b._fromTicks) {
1616 return a._fromTicks < b._fromTicks ? nameA : nameB
17- } else if (a.priority !== b.priority) {
18- // decide by priority
19- return a.priority > b.priority ? nameA : nameB
2017 } else {
2118 // insertion order
2219 return nameA
2320 }
@@ -207,8 +204,17 @@
207204 message.fromName = portName
208205 return message
209206 }
210207
208+ peekNextMessage () {
209+ const portName = Object.keys(this.ports).reduce(messageArbiter.bind(this))
210+ const port = this.ports[portName]
211+ const message = port.messages[0]
212+ message._fromPort = port
213+ message.fromName = portName
214+ return message
215+ }
216+
211217 hasMessages () {
212218 return Object.keys(this.ports).some(name => this.ports[name].messages.length)
213219 }
214220
tests/index.jsView
@@ -12,9 +12,8 @@
1212 this.exInterface = exInterface
1313 }
1414
1515 initailize (message) {
16- // console.log('- init', this.exInterface.id)
1716 const port = message.ports[0]
1817 if (port) {
1918 this.exInterface.ports.bind('root', port)
2019 }
@@ -147,61 +146,8 @@
147146
148147 t.end()
149148 })
150149
151- // tape('queing multiple messages', async t => {
152- // t.plan(2)
153- // let runs = 0
154- // const stateRoot = {
155- // '/': 'zdpuAoNgKdoeLeGQz9AwBr9xfi6MZNAiHUW2WPGQF7JiBofzg'
156- // }
157-
158- // class Root extends BaseContainer {
159- // async run (m) {
160- // const one = this.exInterface.ports.create('child')
161- // const two = this.exInterface.ports.create('child')
162- // const three = this.exInterface.ports.create('child')
163-
164- // this.exInterface.ports.bind('one', one)
165- // this.exInterface.ports.bind('two', two)
166- // this.exInterface.ports.bind('three', three)
167- // this.exInterface.send(one, this.exInterface.createMessage())
168- // this.exInterface.send(two, this.exInterface.createMessage())
169- // this.exInterface.send(three, this.exInterface.createMessage())
170-
171- // return new Promise((resolve, reject) => {
172- // setTimeout(() => {
173- // this.exInterface.incrementTicks(6)
174- // resolve()
175- // }, 200)
176- // })
177- // }
178- // }
179-
180- // class Child extends BaseContainer {
181- // run (m) {
182- // runs++
183- // this.exInterface.incrementTicks(2)
184- // }
185- // }
186-
187- // const hypervisor = new Hypervisor(node.dag)
188-
189- // hypervisor.registerContainer('root', Root)
190- // hypervisor.registerContainer('child', Child)
191-
192- // const root = await hypervisor.createInstance('root')
193- // const port = root.ports.create('root')
194- // root.ports.bind('first', port)
195-
196- // await root.send(port, root.createMessage())
197- // await hypervisor.scheduler.wait(Infinity)
198-
199- // t.equals(runs, 3, 'the number of run should be 3')
200- // const state = await hypervisor.createStateRoot(Infinity)
201- // t.deepEquals(state, stateRoot, 'should generate the correct state root')
202- // })
203-
204150 tape('traps', async t => {
205151 t.plan(1)
206152 class Root extends BaseContainer {
207153 async run (m) {
@@ -246,28 +192,27 @@
246192
247193 this.exInterface.send(one, this.exInterface.createMessage())
248194 this.exInterface.send(two, this.exInterface.createMessage())
249195
250- this.exInterface.incrementTicks(6)
251196 } else if (runs === 1) {
252197 runs++
198+ t.equals(m.data, 'second', 'should recived the second message')
199+ } else if (runs === 2) {
253200 t.equals(m.data, 'first', 'should recive the first message')
254- } else if (runs === 2) {
255- t.equals(m.data, 'second', 'should recived the second message')
256201 }
257202 }
258203 }
259204
260205 class First extends BaseContainer {
261206 run (m) {
262- this.exInterface.incrementTicks(1)
207+ this.exInterface.incrementTicks(2)
263208 this.exInterface.send(m.fromPort, this.exInterface.createMessage({data: 'first'}))
264209 }
265210 }
266211
267212 class Second extends BaseContainer {
268213 run (m) {
269- this.exInterface.incrementTicks(2)
214+ this.exInterface.incrementTicks(1)
270215 this.exInterface.send(m.fromPort, this.exInterface.createMessage({data: 'second'}))
271216 }
272217 }
273218
@@ -283,123 +228,10 @@
283228
284229 root.send(port, root.createMessage())
285230 })
286231
287- // tape('message should arrive in the correct order, even if sent out of order', async t => {
288- // t.plan(2)
289-
290- // let runs = 0
291-
292- // class Root extends BaseContainer {
293- // run (m) {
294- // if (!runs) {
295- // runs++
296- // const one = this.exInterface.ports.create('first')
297- // const two = this.exInterface.ports.create('second')
298-
299- // this.exInterface.ports.bind('one', one)
300- // this.exInterface.ports.bind('two', two)
301-
302- // this.exInterface.send(two, this.exInterface.createMessage())
303- // this.exInterface.send(one, this.exInterface.createMessage())
304-
305- // this.exInterface.incrementTicks(6)
306- // } else if (runs === 1) {
307- // runs++
308- // t.equals(m.data, 'first', 'should recive the first message')
309- // } else if (runs === 2) {
310- // t.equals(m.data, 'second', 'should recived the second message')
311- // }
312- // }
313- // }
314-
315- // class First extends BaseContainer {
316- // run (m) {
317- // this.exInterface.incrementTicks(2)
318- // return this.exInterface.send(m.fromPort, this.exInterface.createMessage({data: 'first'}))
319- // }
320- // }
321-
322- // class Second extends BaseContainer {
323- // run (m) {
324- // this.exInterface.incrementTicks(1)
325- // this.exInterface.send(m.fromPort, this.exInterface.createMessage({data: 'second'}))
326- // }
327- // }
328-
329- // const hypervisor = new Hypervisor(node.dag)
330-
331- // hypervisor.registerContainer('root', Root)
332- // hypervisor.registerContainer('first', First)
333- // hypervisor.registerContainer('second', Second)
334-
335- // const root = await hypervisor.createInstance('root')
336- // const port = root.ports.create('root')
337- // root.ports.bind('first', port)
338-
339- // root.send(port, root.createMessage())
340- // })
341-
342- // tape('message should arrive in the correct order, even in a tie of ticks', async t => {
343- // t.plan(2)
344- // let runs = 0
345-
346- // class Root extends BaseContainer {
347- // run (m) {
348- // if (!runs) {
349- // runs++
350- // const one = this.exInterface.ports.create('first')
351- // const two = this.exInterface.ports.create('second')
352-
353- // this.exInterface.ports.bind('one', one)
354- // this.exInterface.ports.bind('two', two)
355-
356- // this.exInterface.send(one, this.exInterface.createMessage())
357- // this.exInterface.send(two, this.exInterface.createMessage())
358-
359- // this.exInterface.incrementTicks(6)
360- // } else if (runs === 1) {
361- // runs++
362- // t.equals(m.data, 'first', 'should recive the first message')
363- // } else if (runs === 2) {
364- // t.equals(m.data, 'second', 'should recived the second message')
365- // }
366- // }
367- // }
368-
369- // class First extends BaseContainer {
370- // run (m) {
371- // this.exInterface.incrementTicks(2)
372- // return this.exInterface.send(m.fromPort, this.exInterface.createMessage({
373- // data: 'first'
374- // }))
375- // }
376- // }
377-
378- // class Second extends BaseContainer {
379- // run (m) {
380- // this.exInterface.incrementTicks(2)
381- // return this.exInterface.send(m.fromPort, this.exInterface.createMessage({
382- // data: 'second'
383- // }))
384- // }
385- // }
386-
387- // const hypervisor = new Hypervisor(node.dag)
388-
389- // hypervisor.registerContainer('root', Root)
390- // hypervisor.registerContainer('first', First)
391- // hypervisor.registerContainer('second', Second)
392-
393- // const root = await hypervisor.createInstance('root')
394- // const port = await root.ports.create('root')
395- // root.ports.bind('first', port)
396- // root.send(port, root.createMessage())
397- // })
398-
399- tape('message should arrive in the correct order, even in a tie of ticks', async t => {
232+ tape('message should arrive in the correct oder if sent in order', async t => {
400233 t.plan(2)
401-
402234 let runs = 0
403235
404236 class Root extends BaseContainer {
405237 run (m) {
@@ -407,39 +239,35 @@
407239 runs++
408240 const one = this.exInterface.ports.create('first')
409241 const two = this.exInterface.ports.create('second')
410242
243+ this.exInterface.ports.bind('one', one)
411244 this.exInterface.ports.bind('two', two)
412- this.exInterface.ports.bind('one', one)
413245
414246 this.exInterface.send(one, this.exInterface.createMessage())
415247 this.exInterface.send(two, this.exInterface.createMessage())
416248
417249 this.exInterface.incrementTicks(6)
418250 } else if (runs === 1) {
419251 runs++
252+ t.equals(m.data, 'first', 'should recive the first message')
253+ } else if (runs === 2) {
420254 t.equals(m.data, 'second', 'should recived the second message')
421- } else if (runs === 2) {
422- t.equals(m.data, 'first', 'should recive the first message')
423255 }
424256 }
425257 }
426258
427259 class First extends BaseContainer {
428260 run (m) {
429- this.exInterface.incrementTicks(2)
430- return this.exInterface.send(m.fromPort, this.exInterface.createMessage({
431- data: 'first'
432- }))
261+ this.exInterface.incrementTicks(1)
262+ this.exInterface.send(m.fromPort, this.exInterface.createMessage({data: 'first'}))
433263 }
434264 }
435265
436266 class Second extends BaseContainer {
437267 run (m) {
438268 this.exInterface.incrementTicks(2)
439- return this.exInterface.send(m.fromPort, this.exInterface.createMessage({
440- data: 'second'
441- }))
269+ this.exInterface.send(m.fromPort, this.exInterface.createMessage({data: 'second'}))
442270 }
443271 }
444272
445273 const hypervisor = new Hypervisor(node.dag)
@@ -448,16 +276,15 @@
448276 hypervisor.registerContainer('first', First)
449277 hypervisor.registerContainer('second', Second)
450278
451279 const root = await hypervisor.createInstance('root')
452-
453280 const port = root.ports.create('root')
454281 root.ports.bind('first', port)
455282
456283 root.send(port, root.createMessage())
457284 })
458285
459- tape('message should arrive in the correct order, with a tie in ticks but with differnt proity', async t => {
286+ tape('message should arrive in the correct order, even in a tie of ticks', async t => {
460287 t.plan(2)
461288
462289 let runs = 0
463290
@@ -467,31 +294,28 @@
467294 runs++
468295 const one = this.exInterface.ports.create('first')
469296 const two = this.exInterface.ports.create('second')
470297
298+ this.exInterface.ports.bind('two', two)
471299 this.exInterface.ports.bind('one', one)
472- this.exInterface.ports.bind('two', two)
473300
301+ this.exInterface.send(one, this.exInterface.createMessage())
474302 this.exInterface.send(two, this.exInterface.createMessage())
475- this.exInterface.send(one, this.exInterface.createMessage())
476303
477304 this.exInterface.incrementTicks(6)
478305 } else if (runs === 1) {
479306 runs++
307+ t.equals(m.data, 'second', 'should recived the second message')
308+ } else if (runs === 2) {
480309 t.equals(m.data, 'first', 'should recive the first message')
481- } else if (runs === 2) {
482- t.equals(m.data, 'second', 'should recived the second message')
483310 }
484311 }
485312 }
486313
487314 class First extends BaseContainer {
488315 run (m) {
489316 this.exInterface.incrementTicks(2)
490317 return this.exInterface.send(m.fromPort, this.exInterface.createMessage({
491- resources: {
492- priority: 100
493- },
494318 data: 'first'
495319 }))
496320 }
497321 }
@@ -511,44 +335,50 @@
511335 hypervisor.registerContainer('first', First)
512336 hypervisor.registerContainer('second', Second)
513337
514338 const root = await hypervisor.createInstance('root')
339+
515340 const port = root.ports.create('root')
516341 root.ports.bind('first', port)
342+
517343 root.send(port, root.createMessage())
518344 })
519345
520- tape.skip('message should arrive in the correct order, with a tie in ticks but with differnt proity', async t => {
346+ tape('message should arrive in the correct order, with a tie in ticks but with differnt proity', async t => {
521347 t.plan(2)
522348
349+ let runs = 0
350+
523351 class Root extends BaseContainer {
524352 run (m) {
525- if (!this.runs) {
526- this.runs = 1
527-
353+ if (!runs) {
354+ runs++
528355 const one = this.exInterface.ports.create('first')
529356 const two = this.exInterface.ports.create('second')
530357
531- this.exInterface.ports.bind(one, 'one')
532- this.exInterface.ports.bind(two, 'two')
358+ this.exInterface.ports.bind('one', one)
359+ this.exInterface.ports.bind('two', two)
533360
534- return Promise.all([
535- this.exInterface.send(two, this.exInterface.createMessage()),
536- this.exInterface.send(one, this.exInterface.createMessage())
537- ])
538- } else if (this.runs === 1) {
539- this.runs++
540- t.equals(m.data, 'second', 'should recive the first message')
541- } else if (this.runs === 2) {
542- t.equals(m.data, 'first', 'should recived the second message')
361+ this.exInterface.send(two, this.exInterface.createMessage())
362+ this.exInterface.send(one, this.exInterface.createMessage())
363+
364+ this.exInterface.incrementTicks(6)
365+ } else if (runs === 1) {
366+ runs++
367+ t.equals(m.data, 'first', 'should recive the first message')
368+ } else if (runs === 2) {
369+ t.equals(m.data, 'second', 'should recived the second message')
543370 }
544371 }
545372 }
546373
547374 class First extends BaseContainer {
548375 run (m) {
549376 this.exInterface.incrementTicks(2)
550377 return this.exInterface.send(m.fromPort, this.exInterface.createMessage({
378+ resources: {
379+ priority: 100
380+ },
551381 data: 'first'
552382 }))
553383 }
554384 }
@@ -556,11 +386,8 @@
556386 class Second extends BaseContainer {
557387 run (m) {
558388 this.exInterface.incrementTicks(2)
559389 return this.exInterface.send(m.fromPort, this.exInterface.createMessage({
560- resources: {
561- priority: 100
562- },
563390 data: 'second'
564391 }))
565392 }
566393 }
@@ -572,80 +399,12 @@
572399 hypervisor.registerContainer('second', Second)
573400
574401 const root = await hypervisor.createInstance('root')
575402 const port = root.ports.create('root')
576- root.ports.bind(port, 'first')
403+ root.ports.bind('first', port)
577404 root.send(port, root.createMessage())
578405 })
579406
580- tape.skip('should order parent messages correctly', async t => {
581- t.plan(1)
582- class Middle extends BaseContainer {
583- run (m) {
584- if (!this.runs) {
585- this.runs = 1
586- this.exInterface.incrementTicks(1)
587-
588- const leaf = this.exInterface.ports.create('leaf')
589- this.exInterface.ports.bind(leaf, 'leaf')
590-
591- return this.exInterface.send(leaf, this.exInterface.createMessage())
592- } else {
593- ++this.runs
594- if (this.runs === 3) {
595- t.equals(m.data, 'first')
596- }
597- }
598- }
599- }
600-
601- class Leaf extends BaseContainer {
602- run (m) {
603- this.exInterface.incrementTicks(2)
604- return this.exInterface.send(m.fromPort, this.exInterface.createMessage({
605- data: 'first'
606- }))
607- }
608- }
609-
610- const hypervisor = new Hypervisor(node.dag)
611-
612- hypervisor.registerContainer('root', BaseContainer)
613- hypervisor.registerContainer('middle', Middle)
614- hypervisor.registerContainer('leaf', Leaf)
615-
616- const root = await hypervisor.createInstance('root')
617- root.incrementTicks(2)
618-
619- const port = root.ports.create('middle')
620- root.ports.bind(port, 'first')
621-
622- await root.send(port, root.createMessage())
623- root.send(port, root.createMessage())
624- })
625-
626- tape.skip('get container instance by path', async t => {
627- t.plan(1)
628- const hypervisor = new Hypervisor(node.dag)
629- hypervisor.registerContainer('base', BaseContainer)
630-
631- const root = await hypervisor.createInstance('base')
632- let port = root.ports.create('base')
633- root.ports.bind(port, 'first')
634-
635- const first = await root.getInstance(port)
636- port = first.ports.create('base')
637- first.ports.bind(port, 'second')
638-
639- const second = await first.getInstance(port)
640- port = second.ports.create('base')
641- second.ports.bind(port, 'third')
642-
643- const third = await second.getInstance(port)
644- const foundThird = await hypervisor.getInstanceByPath(root, 'first/second/third')
645- t.equals(third, foundThird, 'should find by path')
646- })
647-
648407 tape('checking ports', async t => {
649408 t.plan(4)
650409 const hypervisor = new Hypervisor(node.dag)
651410 hypervisor.registerContainer('base', BaseContainer)

Built with git-ssb-web