git ssb

0+

wanderer🌟 / js-primea-hypervisor



Commit f2c989b4dce29265b4c17a2cde93c68a0b742edc

added checks for loading instances

wanderer committed on 6/28/2017, 1:53:04 AM
Parent: d30608cc6c438ea4527a506e061d183c30099168

Files changed

exoInterface.jschanged
index.jschanged
portManager.jschanged
tests/index.jschanged
exoInterface.jsView
@@ -18,15 +18,20 @@
1818 this.container = new opts.container.Constructor(this, opts.container.args)
1919
2020 this.ticks = 0
2121 this.containerState = 'idle'
22+ this._pendingSends = new Map()
2223
2324 // create the port manager
2425 this.ports = new PortManager(Object.assign({
2526 exInterface: this
2627 }, opts))
2728 }
2829
30+ _addWork (promise) {
31+ this._outStandingWork = Promise.all([this._outStandingWork, promise])
32+ }
33+
2934 /**
3035 * adds a message to this containers message queue
3136 * @param {string} portName
3237 * @param {object} message
index.jsView
@@ -16,8 +16,9 @@
1616 this.scheduler = new Scheduler()
1717 this.state = state
1818 this._containerTypes = {}
1919 this._nodesToCheck = new Set()
20+ this._loadingInstances = new Map()
2021 }
2122
2223 /**
2324 * add a potaintail node in the state graph to check for garbage collection
@@ -71,16 +72,22 @@
7172 * gets an existsing container instances
7273 * @param {string} id - the containers ID
7374 * @returns {Promise}
7475 */
75- async getInstance (id) {
76- let instance = this.scheduler.getInstance(id)
76+ getInstance (id) {
77+ let instance = this.scheduler.getInstance(id) || this._loadingInstances.get(id)
7778 if (instance) {
79+ // console.log('have instance', id)
7880 return instance
7981 } else {
8082 const lock = this.scheduler.getLock()
81- instance = await this._loadInstance(id, lock)
82- return instance
83+ const promise = this._loadInstance(id, lock)
84+ promise.then(() => {
85+ this._loadingInstances.delete(id)
86+ })
87+
88+ this._loadingInstances.set(id, promise)
89+ return promise
8390 }
8491 }
8592
8693 /**
portManager.jsView
@@ -145,15 +145,16 @@
145145 this._oldestMessageResolve = resolve
146146 })
147147 this._messageTickThreshold = Infinity
148148 }
149+
150+ if (this.isSaturated()) {
151+ this._saturationResolve()
152+ this._saturationPromise = new Promise((resolve, reject) => {
153+ this._saturationResolve = resolve
154+ })
155+ }
149156 }
150- if (this.isSaturated()) {
151- this._saturationResolve()
152- this._saturationPromise = new Promise((resolve, reject) => {
153- this._saturationResolve = resolve
154- })
155- }
156157 }
157158
158159 /**
159160 * gets a port given it's name
@@ -185,9 +186,10 @@
185186
186187 // create a new channel for the container
187188 const ports = this.createChannel()
188189 this._unboundPorts.delete(ports[1])
189- this.hypervisor.createInstance(type, data, [ports[1]], id)
190+ const promise = this.hypervisor.createInstance(type, data, [ports[1]], id)
191+ this.exInterface._addWork(promise)
190192
191193 return ports[0]
192194 }
193195
tests/index.jsView
@@ -176,21 +176,19 @@
176176 t.plan(2)
177177 let runs = 0
178178
179179 class Root extends BaseContainer {
180- async run (m) {
180+ run (m) {
181181 if (!runs) {
182182 runs++
183183 const one = this.exInterface.ports.create('first')
184184 const two = this.exInterface.ports.create('second')
185185
186186 this.exInterface.ports.bind('two', two)
187187 this.exInterface.ports.bind('one', one)
188188
189- await Promise.all([
190- this.exInterface.send(one, this.exInterface.createMessage()),
191- this.exInterface.send(two, this.exInterface.createMessage())
192- ])
189+ this.exInterface.send(one, this.exInterface.createMessage())
190+ this.exInterface.send(two, this.exInterface.createMessage())
193191 } else if (runs === 1) {
194192 runs++
195193 t.equals(m.data, 'first', 'should recive the first message')
196194 } else if (runs === 2) {
@@ -535,8 +533,52 @@
535533 root.ports.bind('first', port)
536534 root.send(port, root.createMessage())
537535 })
538536
537+ tape('send to the same container at the same time', async t => {
538+ t.plan(2)
539+
540+ let runs = 0
541+ let instance
542+
543+ class Root extends BaseContainer {
544+ run (m) {
545+ let one = this.exInterface.ports.get('one')
546+ if (!one) {
547+ one = this.exInterface.ports.create('first')
548+ this.exInterface.ports.bind('one', one)
549+ } else {
550+ this.exInterface.send(one, this.exInterface.createMessage())
551+ this.exInterface.send(one, this.exInterface.createMessage())
552+ }
553+ }
554+ }
555+
556+ class First extends BaseContainer {
557+ run (m) {
558+ ++runs
559+ if (runs === 2) {
560+ t.equals(instance, this, 'should have same instances')
561+ } else {
562+ instance = this
563+ }
564+ }
565+ }
566+
567+ const hypervisor = new Hypervisor(node.dag)
568+
569+ hypervisor.registerContainer('root', Root)
570+ hypervisor.registerContainer('first', First)
571+
572+ const root = await hypervisor.createInstance('root')
573+ const port = root.ports.create('root')
574+ root.ports.bind('first', port)
575+ root.send(port, root.createMessage())
576+ await hypervisor.createStateRoot()
577+ root.send(port, root.createMessage())
578+ await hypervisor.createStateRoot()
579+ t.equals(runs, 2)
580+ })
539581 tape('checking ports', async t => {
540582 t.plan(4)
541583 const hypervisor = new Hypervisor(node.dag)
542584 hypervisor.registerContainer('base', BaseContainer)

Built with git-ssb-web