git ssb

0+

wanderer🌟 / js-primea-hypervisor



Commit e85aa24998d9d41baaabe9b8afed7754a926d0be

Merge pull request #148 from primea/removeLocks

Remove locks
wanderer authored on 8/13/2017, 3:57:40 PM
GitHub committed on 8/13/2017, 3:57:40 PM
Parent: 9e075d6fa8d8d5137146130915af95c57bc1b231
Parent: 4d55c78be4ffb7631d68b6f18e18e09d8bd4b510

Files changed

index.jschanged
kernel.jschanged
portManager.jschanged
scheduler.jschanged
tests/index.jschanged
index.jsView
@@ -53,18 +53,20 @@
5353 async send (port, message) {
5454 if (port.destId) {
5555 const id = port.destId
5656 const instance = await this.getInstance(id)
57- return instance.queue(port.destName, message)
57 + instance.queue(port.destName, message)
5858 } else {
5959 // port is unbound
6060 port.destPort.messages.push(message)
6161 }
6262 }
6363
6464 // loads an instance of a container from the state
65- async _loadInstance (id) {
66- const state = await this.tree.get(id)
65 + async _loadInstance (id, state) {
66 + if (!state) {
67 + state = await this.tree.get(id)
68 + }
6769 const container = this._containerTypes[state.type]
6870 let code
6971
7072 // checks if the code stored in the state is an array and that the elements
@@ -125,9 +127,9 @@
125127 */
126128 async createInstance (type, message = new Message(), id = {nonce: 0, parent: null}) {
127129 // create a lock to prevent the scheduler from reloving waits before the
128130 // new container is loaded
129- const resolve = this.scheduler.getLock(id)
131 + // const unlock = this.scheduler.getLock(id)
130132 const idHash = await this._getHashFromObj(id)
131133 // const code = message.data.byteLength ? message.data : undefined
132134 const state = {
133135 nonce: [0],
@@ -138,22 +140,26 @@
138140 if (message.data.length) {
139141 state.code = message.data
140142 }
141143
142- // save the container in the state
143- await this.tree.set(idHash, state)
144144 // create the container instance
145- const instance = await this._loadInstance(idHash)
146- resolve(instance)
145 + const instance = await this._loadInstance(idHash, state)
146 +
147147 // send the intialization message
148148 await instance.create(message)
149149
150- if (state.code && state.code.length > this.MAX_DATA_BYTES) {
151- state.code = chunk(state.code, this.MAX_DATA_BYTES).map(chk => {
152- return {
153- '/': chk
154- }
155- })
150 + if (Object.keys(instance.ports.ports).length || instance.id === this.ROOT_ID) {
151 + if (state.code && state.code.length > this.MAX_DATA_BYTES) {
152 + state.code = chunk(state.code, this.MAX_DATA_BYTES).map(chk => {
153 + return {
154 + '/': chk
155 + }
156 + })
157 + }
158 + // save the container in the state
159 + await this.tree.set(idHash, state)
160 + } else {
161 + this.scheduler.done(idHash)
156162 }
157163
158164 return instance
159165 }
@@ -165,12 +171,14 @@
165171 * @returns {Promise}
166172 */
167173 async createStateRoot (ticks) {
168174 await this.scheduler.wait(ticks)
175 +
169176 const unlinked = await DFSchecker(this.tree, this.ROOT_ID, this._nodesToCheck)
170- unlinked.forEach(id => {
171- this.tree.delete(id)
172- })
177 + for (const id of unlinked) {
178 + await this.tree.delete(id)
179 + }
180 +
173181 return this.graph.flush(this.state)
174182 }
175183
176184 /**
kernel.jsView
@@ -36,14 +36,14 @@
3636 * @param {object} message
3737 */
3838 queue (portName, message) {
3939 this.ports.queue(portName, message)
40- return this._startMessageLoop()
40 + this._startMessageLoop()
4141 }
4242
4343 async create (message) {
4444 await this.message(message, 'onCreation')
45- return this._startMessageLoop()
45 + this._startMessageLoop()
4646 }
4747
4848 // waits for the next message
4949 async _startMessageLoop () {
@@ -108,11 +108,10 @@
108108 this.send(responsePort, new Message({
109109 data: result
110110 }))
111111 }
112 + await this.ports.clearUnboundedPorts()
112113 }
113-
114- this.ports.clearUnboundedPorts()
115114 }
116115
117116 getResponsePort (message) {
118117 if (message.responsePort) {
portManager.jsView
@@ -54,18 +54,17 @@
5454 throw new Error('cannot bind port to a name that is alread bound')
5555 } else {
5656 this._unboundPorts.delete(port)
5757
58- port.messages.forEach(message => {
59- message._fromPort = port
60- message.fromName = name
61- })
62-
6358 // save the port instance
6459 this.ports[name] = port
6560
6661 // update the dest port
6762 const destPort = await this.hypervisor.getDestPort(port)
63 + port.messages.forEach(message => {
64 + message._fromPort = port
65 + message.fromName = name
66 + })
6867 destPort.destName = name
6968 destPort.destId = this.id
7069 delete destPort.destPort
7170 }
@@ -93,11 +92,11 @@
9392 /**
9493 * delete an port given the name it is bound to
9594 * @param {string} name
9695 */
97- delete (name) {
96 + async delete (name) {
9897 const port = this.ports[name]
99- this.kernel.send(port, new DeleteMessage())
98 + await this.kernel.send(port, new DeleteMessage())
10099 this._delete(name)
101100 }
102101
103102 _delete (name) {
@@ -108,15 +107,14 @@
108107 /**
109108 * clears any unbounded ports referances
110109 */
111110 clearUnboundedPorts () {
111 + const waits = []
112112 this._unboundPorts.forEach(port => {
113- this.kernel.send(port, new DeleteMessage())
113 + waits.push(this.kernel.send(port, new DeleteMessage()))
114114 })
115115 this._unboundPorts.clear()
116- if (!Object.keys(this.ports).length) {
117- this.hypervisor.addNodeToCheck(this.id)
118- }
116 + return Promise.all(waits)
119117 }
120118
121119 /**
122120 * check if a port object is still valid
@@ -200,9 +198,9 @@
200198 */
201199 async getNextMessage () {
202200 let message = this._peekNextMessage()
203201 let saturated = this._isSaturated()
204- let oldestTime = this.hypervisor.scheduler.oldest()
202 + let oldestTime = this.hypervisor.scheduler.leastNumberOfTicks()
205203
206204 while (!saturated && // end if there are messages on all the ports
207205 // end if we have a message older then slowest containers
208206 !((message && oldestTime >= message._fromTicks) ||
@@ -222,9 +220,9 @@
222220 message = this._peekNextMessage()
223221 })
224222 ])
225223
226- oldestTime = this.hypervisor.scheduler.oldest()
224 + oldestTime = this.hypervisor.scheduler.leastNumberOfTicks()
227225 }
228226
229227 return message
230228 }
scheduler.jsView
@@ -34,13 +34,13 @@
3434 * @param {Object} instance - a container instance
3535 */
3636 update (instance) {
3737 this._update(instance)
38 + this._running.add(instance.id)
3839 this._checkWaits()
3940 }
4041
4142 _update (instance) {
42- this._running.add(instance.id)
4343 // sorts the container instance map by tick count
4444 this.instances.delete(instance.id)
4545 const instanceArray = [...this.instances]
4646 binarySearchInsert(instanceArray, comparator, [instance.id, instance])
@@ -81,9 +81,10 @@
8181 this._running.delete(id)
8282 return new Promise((resolve, reject) => {
8383 binarySearchInsert(this._waits, comparator, {
8484 ticks: ticks,
85- resolve: resolve
85 + resolve: resolve,
86 + id: id
8687 })
8788 this._checkWaits()
8889 })
8990
@@ -95,22 +96,34 @@
9596 /**
9697 * returns the oldest container's ticks
9798 * @return {integer}
9899 */
99- oldest () {
100 + leastNumberOfTicks () {
100101 const nextValue = this.instances.values().next().value
101102 return nextValue ? nextValue.ticks : 0
102103 }
103104
104105 // checks outstanding waits to see if they can be resolved
105106 _checkWaits () {
106- if (!this._loadingInstances.size) {
107- // if there are no running containers
108- if (!this.instances.size) {
109- // clear any remanding waits
110- this._waits.forEach(wait => wait.resolve())
111- this._waits = []
112- } else if (!this._running.size) {
107 + // if there are no running containers
108 + if (!this.instances.size) {
109 + // clear any remanding waits
110 + this._waits.forEach(wait => wait.resolve())
111 + this._waits = []
112 + } else {
113 + // find the old container and see if to can resolve any of the waits
114 + const least = this.leastNumberOfTicks()
115 + for (const index in this._waits) {
116 + const wait = this._waits[index]
117 + if (wait.ticks <= least) {
118 + wait.resolve()
119 + this._running.add(wait.id)
120 + } else {
121 + this._waits.splice(0, index)
122 + break
123 + }
124 + }
125 + if (!this._running.size) {
113126 // if there are no containers running find the oldest wait and update
114127 // the oldest containers to it ticks
115128 const oldest = this._waits[0].ticks
116129 for (let instance of this.instances) {
@@ -122,20 +135,8 @@
122135 this._update(instance)
123136 }
124137 }
125138 return this._checkWaits()
126- } else {
127- // find the old container and see if to can resolve any of the waits
128- const oldest = this.oldest()
129- for (const index in this._waits) {
130- const wait = this._waits[index]
131- if (wait.ticks <= oldest) {
132- wait.resolve()
133- } else {
134- this._waits.splice(0, index)
135- break
136- }
137- }
138139 }
139140 }
140141 }
141142 }
tests/index.jsView
@@ -47,17 +47,17 @@
4747 data: Buffer.from('test code'),
4848 ports: [portRef2]
4949 })
5050
51- rootContainer.createInstance(testVMContainer.typeId, initMessage)
51 + await rootContainer.createInstance(testVMContainer.typeId, initMessage)
5252
5353 await rootContainer.ports.bind('first', portRef1)
5454 message = rootContainer.createMessage()
5555 rootContainer.send(portRef1, message)
5656
5757 const stateRoot = await hypervisor.createStateRoot(Infinity)
5858 t.deepEquals(stateRoot, expectedState, 'expected root!')
59- t.equals(hypervisor.scheduler.oldest(), 0)
59 + t.equals(hypervisor.scheduler.leastNumberOfTicks(), 0)
6060 } catch (e) {
6161 console.log(e)
6262 }
6363 })
@@ -79,9 +79,9 @@
7979 const root = await hypervisor.createInstance(testVMContainer.typeId)
8080 const [portRef1, portRef2] = root.ports.createChannel()
8181
8282 await root.ports.bind('one', portRef1)
83- root.createInstance(testVMContainer.typeId, root.createMessage({
83 + await root.createInstance(testVMContainer.typeId, root.createMessage({
8484 ports: [portRef2]
8585 }))
8686
8787 const stateRoot = await hypervisor.createStateRoot(Infinity)
@@ -93,59 +93,8 @@
9393 console.log(e)
9494 }
9595 })
9696
97- tape('one child contract with saturated ports', async t => {
98- t.plan(2)
99- let message
100- const expectedState = {
101- '/': 'zdpuArCqpDZtEqjrXrRhMiYLE7QQ1szVr1qLVkiwtDLincGWU'
102- }
103-
104- class testVMContainer2 extends BaseContainer {
105- onMessage (m) {
106- t.true(m === message, 'should recive a message')
107- }
108- static get typeId () {
109- return 99
110- }
111- }
112-
113- class testVMContainer extends BaseContainer {
114- onMessage (m) {
115- const [portRef1, portRef2] = this.kernel.ports.createChannel()
116- this.kernel.createInstance(testVMContainer2.typeId, this.kernel.createMessage({
117- ports: [portRef2]
118- }))
119- this.kernel.incrementTicks(2)
120- this.kernel.send(portRef1, m)
121- return this.kernel.ports.bind('child', portRef1)
122- }
123- }
124-
125- const hypervisor = new Hypervisor(node.dag)
126- hypervisor.registerContainer(testVMContainer)
127- hypervisor.registerContainer(testVMContainer2)
128-
129- const root = await hypervisor.createInstance(testVMContainer.typeId)
130- const [portRef1, portRef2] = root.ports.createChannel()
131- root.createInstance(testVMContainer.typeId, root.createMessage({
132- ports: [portRef2]
133- }))
134-
135- await root.ports.bind('first', portRef1)
136- message = root.createMessage({
137- data: 'test'
138- })
139-
140- root.send(portRef1, message)
141- const stateRoot = await hypervisor.createStateRoot(Infinity)
142-
143- t.deepEquals(stateRoot, expectedState, 'expected state')
144- // await hypervisor.graph.tree(stateRoot, Infinity, true)
145- // console.log(JSON.stringify(stateRoot, null, 2))
146- })
147-
14897 tape('one child contract', async t => {
14998 t.plan(4)
15099 let message
151100 const expectedState = {
@@ -170,14 +119,14 @@
170119 }
171120 }
172121
173122 class testVMContainer extends BaseContainer {
174- onMessage (m) {
123 + async onMessage (m) {
175124 const [portRef1, portRef2] = this.kernel.ports.createChannel()
176- this.kernel.createInstance(testVMContainer2.typeId, this.kernel.createMessage({
125 + await this.kernel.createInstance(testVMContainer2.typeId, this.kernel.createMessage({
177126 ports: [portRef2]
178127 }))
179- this.kernel.send(portRef1, m)
128 + await this.kernel.send(portRef1, m)
180129 this.kernel.incrementTicks(1)
181130 return this.kernel.ports.bind('child', portRef1)
182131 }
183132 }
@@ -188,16 +137,16 @@
188137
189138 let root = await hypervisor.createInstance(testVMContainer.typeId)
190139 const rootId = root.id
191140 const [portRef1, portRef2] = root.ports.createChannel()
192- root.createInstance(testVMContainer.typeId, root.createMessage({
141 + await root.createInstance(testVMContainer.typeId, root.createMessage({
193142 ports: [portRef2]
194143 }))
195144
196145 await root.ports.bind('first', portRef1)
197146 message = root.createMessage()
198147
199- root.send(portRef1, message)
148 + await root.send(portRef1, message)
200149 const stateRoot = await hypervisor.createStateRoot(Infinity)
201150 t.true(hasResolved, 'should resolve before generating the state root')
202151
203152 // await hypervisor.graph.tree(stateRoot, Infinity, true)
@@ -242,11 +191,13 @@
242191 const message3 = this.kernel.createMessage({
243192 ports: [portRef6]
244193 })
245194
246- this.kernel.createInstance(Root.typeId, message1)
247- this.kernel.createInstance(Root.typeId, message2)
248- this.kernel.createInstance(Root.typeId, message3)
195 + await Promise.all([
196 + this.kernel.createInstance(Root.typeId, message1),
197 + this.kernel.createInstance(Root.typeId, message2),
198 + this.kernel.createInstance(Root.typeId, message3)
199 + ])
249200
250201 throw new Error('it is a trap!!!')
251202 }
252203 }
@@ -262,17 +213,16 @@
262213 '/': 'zdpuAoifKuJkWz9Fjvt79NmGq3tcefhfCyq8iM8YhcFdV9bmZ'
263214 }, 'should revert the state')
264215 })
265216
266- tape('message should arrive in the correct oder if sent in order', async t => {
217 + tape('recieving older messages', async t => {
267218 t.plan(2)
268219 let runs = 0
269220
270221 class Root extends BaseContainer {
271- onMessage (m) {
222 + async onMessage (m) {
272223 if (!runs) {
273224 runs++
274-
275225 const [portRef1, portRef2] = this.kernel.ports.createChannel()
276226 const [portRef3, portRef4] = this.kernel.ports.createChannel()
277227
278228 const message1 = this.kernel.createMessage({
@@ -281,238 +231,86 @@
281231 const message2 = this.kernel.createMessage({
282232 ports: [portRef4]
283233 })
284234
285- this.kernel.createInstance(First.typeId, message1)
286- this.kernel.createInstance(Second.typeId, message2)
287-
288- this.kernel.send(portRef1, this.kernel.createMessage())
289- this.kernel.send(portRef3, this.kernel.createMessage())
290- return Promise.all(
291- this.kernel.ports.bind('two', portRef3),
292- this.kernel.ports.bind('one', portRef1)
293- )
235 + await Promise.all([
236 + this.kernel.createInstance(First.typeId, message1),
237 + this.kernel.send(portRef1, this.kernel.createMessage()),
238 + this.kernel.send(portRef3, this.kernel.createMessage()),
239 + this.kernel.ports.bind('one', portRef1),
240 + this.kernel.ports.bind('two', portRef3)
241 + ])
242 + return this.kernel.createInstance(Waiter.typeId, message2)
294243 } else if (runs === 1) {
295244 runs++
296245 t.equals(m.data, 'first', 'should recive the first message')
297246 } else if (runs === 2) {
298- t.equals(m.data, 'second', 'should recived the second message')
299- }
300- }
301- }
302-
303- class First extends BaseContainer {
304- onMessage (m) {
305- this.kernel.incrementTicks(2)
306- this.kernel.send(m.fromPort, this.kernel.createMessage({
307- data: 'first'
308- }))
309- }
310-
311- static get typeId () {
312- return 99
313- }
314- }
315-
316- class Second extends BaseContainer {
317- onMessage (m) {
318- this.kernel.incrementTicks(3)
319- this.kernel.send(m.fromPort, this.kernel.createMessage({
320- data: 'second'
321- }))
322- }
323-
324- static get typeId () {
325- return 299
326- }
327- }
328-
329- const hypervisor = new Hypervisor(node.dag)
330-
331- hypervisor.registerContainer(Root)
332- hypervisor.registerContainer(First)
333- hypervisor.registerContainer(Second)
334-
335- const root = await hypervisor.createInstance(Root.typeId)
336-
337- const [portRef1, portRef2] = root.ports.createChannel()
338- root.createInstance(Root.typeId, root.createMessage({
339- ports: [portRef2]
340- }))
341-
342- await root.ports.bind('first', portRef1)
343- const message = root.createMessage()
344- root.send(portRef1, message)
345- })
346-
347- tape('message should arrive in the correct oder if sent in order', async t => {
348- t.plan(2)
349- let runs = 0
350-
351- class Root extends BaseContainer {
352- onMessage (m) {
353- if (!runs) {
354247 runs++
355-
356- const [portRef1, portRef2] = this.kernel.ports.createChannel()
357- const [portRef3, portRef4] = this.kernel.ports.createChannel()
358-
359- const message1 = this.kernel.createMessage({
360- ports: [portRef2]
361- })
362- const message2 = this.kernel.createMessage({
363- ports: [portRef4]
364- })
365-
366- this.kernel.createInstance(First.typeId, message1)
367- this.kernel.createInstance(Second.typeId, message2)
368-
369- this.kernel.send(portRef1, this.kernel.createMessage())
370- this.kernel.send(portRef3, this.kernel.createMessage())
371-
372- return Promise.all([
373- this.kernel.ports.bind('one', portRef1),
374- this.kernel.ports.bind('two', portRef3)
375- ])
376- } else if (runs === 1) {
248 + t.equals(m.data, 'second', 'should recive the second message')
249 + } else if (runs === 3) {
377250 runs++
378- t.equals(m.data, 'second', 'should recived the second message')
379- } else if (runs === 2) {
380- t.equals(m.data, 'first', 'should recive the first message')
251 + // t.equals(m.data, 'third', 'should recived the second message')
381252 }
382253 }
383-
384254 static get typeId () {
385- return 99
255 + return 299
386256 }
387257 }
388258
389259 class First extends BaseContainer {
390260 onMessage (m) {
391261 this.kernel.incrementTicks(2)
392- this.kernel.send(m.fromPort, this.kernel.createMessage({
393- data: 'first'
262 + return this.kernel.send(m.fromPort, this.kernel.createMessage({
263 + data: 'second'
394264 }))
395265 }
396-
397266 static get typeId () {
398- return 299
267 + return 29
399268 }
400269 }
401270
402- class Second extends BaseContainer {
271 + class Waiter extends BaseContainer {
403272 onMessage (m) {
404- this.kernel.incrementTicks(1)
405- this.kernel.send(m.fromPort, this.kernel.createMessage({
406- data: 'second'
407- }))
273 + return new Promise((resolve, reject) => {
274 + setTimeout(() => {
275 + this.kernel.send(m.fromPort, this.kernel.createMessage({
276 + data: 'first'
277 + })).then(resolve)
278 + }, 200)
279 + })
408280 }
409281 }
410282
411- const hypervisor = new Hypervisor(node.dag)
283 + try {
284 + const hypervisor = new Hypervisor(node.dag)
412285
413- hypervisor.registerContainer(Root)
414- hypervisor.registerContainer(First)
415- hypervisor.registerContainer(Second)
286 + hypervisor.registerContainer(Root)
287 + hypervisor.registerContainer(First)
288 + hypervisor.registerContainer(Waiter)
416289
417- const root = await hypervisor.createInstance(Root.typeId)
290 + const root = await hypervisor.createInstance(Root.typeId)
291 + const [portRef1, portRef2] = root.ports.createChannel()
418292
419- const [portRef1, portRef2] = root.ports.createChannel()
420- root.createInstance(Root.typeId, root.createMessage({
421- ports: [portRef2]
422- }))
423-
424- await root.ports.bind('first', portRef1)
425- const message = root.createMessage()
426- root.send(portRef1, message)
427- })
428-
429- tape('message should arrive in the correct oder if sent in order', async t => {
430- t.plan(2)
431- let runs = 0
432-
433- class Root extends BaseContainer {
434- onMessage (m) {
435- if (!runs) {
436- runs++
437- const [portRef1, portRef2] = this.kernel.ports.createChannel()
438- const [portRef3, portRef4] = this.kernel.ports.createChannel()
439-
440- const message1 = this.kernel.createMessage({
441- ports: [portRef2]
442- })
443- const message2 = this.kernel.createMessage({
444- ports: [portRef4]
445- })
446-
447- this.kernel.createInstance(First.typeId, message1)
448- this.kernel.createInstance(Second.typeId, message2)
449-
450- this.kernel.send(portRef1, this.kernel.createMessage())
451- this.kernel.send(portRef3, this.kernel.createMessage())
452-
453- this.kernel.incrementTicks(6)
454-
455- return Promise.all([
456- this.kernel.ports.bind('one', portRef1),
457- this.kernel.ports.bind('two', portRef3)
458- ])
459- } else if (runs === 1) {
460- runs++
461- t.equals(m.data, 'first', 'should recive the first message')
462- } else if (runs === 2) {
463- t.equals(m.data, 'second', 'should recived the second message')
464- }
465- }
466- static get typeId () {
467- return 299
468- }
469- }
470-
471- class First extends BaseContainer {
472- onMessage (m) {
473- this.kernel.incrementTicks(1)
474- this.kernel.send(m.fromPort, this.kernel.createMessage({
475- data: 'first'
293 + const message = root.createMessage()
294 + await Promise.all([
295 + root.send(portRef1, message),
296 + root.ports.bind('first', portRef1),
297 + root.createInstance(Root.typeId, root.createMessage({
298 + ports: [portRef2]
476299 }))
477- }
478- static get typeId () {
479- return 2
480- }
300 + ])
301 + } catch (e) {
302 + console.log(e)
481303 }
482-
483- class Second extends BaseContainer {
484- onMessage (m) {
485- this.kernel.incrementTicks(2)
486- this.kernel.send(m.fromPort, this.kernel.createMessage({
487- data: 'second'
488- }))
489- }
490- }
491-
492- const hypervisor = new Hypervisor(node.dag)
493-
494- hypervisor.registerContainer(Root)
495- hypervisor.registerContainer(First)
496- hypervisor.registerContainer(Second)
497-
498- const root = await hypervisor.createInstance(Root.typeId)
499- const [portRef1, portRef2] = root.ports.createChannel()
500- root.createInstance(Root.typeId, root.createMessage({
501- ports: [portRef2]
502- }))
503-
504- await root.ports.bind('first', portRef1)
505- const message = root.createMessage()
506- root.send(portRef1, message)
507304 })
508305
509306 tape('saturation', async t => {
510- t.plan(2)
307 + t.plan(3)
511308 let runs = 0
512309
513310 class Root extends BaseContainer {
514- onMessage (m) {
311 + onIdle () {}
312 + async onMessage (m) {
515313 if (!runs) {
516314 runs++
517315 const [portRef1, portRef2] = this.kernel.ports.createChannel()
518316 const [portRef3, portRef4] = this.kernel.ports.createChannel()
@@ -523,25 +321,26 @@
523321 const message2 = this.kernel.createMessage({
524322 ports: [portRef4]
525323 })
526324
527- this.kernel.createInstance(First.typeId, message1)
528- this.kernel.createInstance(Second.typeId, message2)
529-
530- this.kernel.send(portRef1, this.kernel.createMessage())
531- this.kernel.send(portRef3, this.kernel.createMessage())
532-
533325 this.kernel.incrementTicks(6)
534326 return Promise.all([
327 + this.kernel.createInstance(First.typeId, message1),
328 + this.kernel.createInstance(Second.typeId, message2),
329 + this.kernel.send(portRef1, this.kernel.createMessage()),
330 + this.kernel.send(portRef3, this.kernel.createMessage()),
535331 this.kernel.ports.bind('one', portRef1),
536332 this.kernel.ports.bind('two', portRef3)
537333 ])
538334 } else if (runs === 1) {
539335 runs++
540336 t.equals(m.data, 'first', 'should recive the first message')
541337 } else if (runs === 2) {
542338 runs++
543- t.equals(m.data, 'second', 'should recived the second message')
339 + t.equals(m.data, 'second', 'should recive the second message')
340 + } else if (runs === 3) {
341 + runs++
342 + t.equals(m.data, 'third', 'should recived the third message')
544343 }
545344 }
546345 static get typeId () {
547346 return 299
@@ -550,10 +349,10 @@
550349
551350 class First extends BaseContainer {
552351 onMessage (m) {
553352 this.kernel.incrementTicks(2)
554- this.kernel.send(m.fromPort, this.kernel.createMessage({
555- data: 'first'
353 + return this.kernel.send(m.fromPort, this.kernel.createMessage({
354 + data: 'second'
556355 }))
557356 }
558357 static get typeId () {
559358 return 29
@@ -562,236 +361,84 @@
562361
563362 class Second extends BaseContainer {
564363 onMessage (m) {
565364 this.kernel.incrementTicks(3)
566- this.kernel.send(m.fromPort, this.kernel.createMessage({
567- data: 'second'
365 + return this.kernel.send(m.fromPort, this.kernel.createMessage({
366 + data: 'third'
568367 }))
569368 }
570369 static get typeId () {
571370 return 2
572371 }
573372 }
574373
575374 class Waiter extends BaseContainer {
576- onCreation () {
375 + onCreation (m) {
577376 return new Promise((resolve, reject) => {
578377 setTimeout(() => {
579- resolve()
378 + this.kernel.send(m.ports[0], this.kernel.createMessage({
379 + data: 'first'
380 + })).then(resolve)
580381 }, 200)
581382 })
582383 }
583384 }
584385
585- const hypervisor = new Hypervisor(node.dag)
386 + try {
387 + const hypervisor = new Hypervisor(node.dag)
586388
587- hypervisor.registerContainer(Root)
588- hypervisor.registerContainer(First)
589- hypervisor.registerContainer(Second)
590- hypervisor.registerContainer(Waiter)
389 + hypervisor.registerContainer(Root)
390 + hypervisor.registerContainer(First)
391 + hypervisor.registerContainer(Second)
392 + hypervisor.registerContainer(Waiter)
591393
592- const root = await hypervisor.createInstance(Root.typeId)
593- const [portRef1, portRef2] = root.ports.createChannel()
394 + let root = await hypervisor.createInstance(Root.typeId)
395 + const [portRef1, portRef2] = root.ports.createChannel()
396 + const [portRef3, portRef4] = root.ports.createChannel()
594397
595- const message = root.createMessage()
596- root.send(portRef1, message)
597- await root.ports.bind('first', portRef1)
598- root.createInstance(Root.typeId, root.createMessage({
599- ports: [portRef2]
600- }))
601-
602- const [portRef3, portRef4] = root.ports.createChannel()
603- await root.ports.bind('sencond', portRef3)
604- root.createInstance(Waiter.typeId, root.createMessage({
605- ports: [portRef4]
606- }))
607-
608- root.incrementTicks(100)
609- root.send(portRef1, root.createMessage({data: 'testss'}))
610- hypervisor.scheduler.done(root.id)
611- })
612-
613- tape('message should arrive in the correct order, even in a tie of ticks', async t => {
614- t.plan(2)
615-
616- let runs = 0
617-
618- class Root extends BaseContainer {
619- onMessage (m) {
620- if (!runs) {
621- runs++
622- const [portRef1, portRef2] = this.kernel.ports.createChannel()
623- const [portRef3, portRef4] = this.kernel.ports.createChannel()
624-
625- const message1 = this.kernel.createMessage({
626- ports: [portRef2]
627- })
628- const message2 = this.kernel.createMessage({
629- ports: [portRef4]
630- })
631-
632- this.kernel.createInstance(First.typeId, message1)
633- this.kernel.createInstance(Second.typeId, message2)
634-
635- this.kernel.send(portRef1, this.kernel.createMessage())
636- this.kernel.send(portRef3, this.kernel.createMessage())
637-
638- this.kernel.incrementTicks(6)
639- return Promise.all([
640- this.kernel.ports.bind('two', portRef3),
641- this.kernel.ports.bind('one', portRef1)
642- ])
643- } else if (runs === 1) {
644- runs++
645- t.equals(m.data, 'second', 'should recived the second message')
646- } else if (runs === 2) {
647- t.equals(m.data, 'first', 'should recive the first message')
648- }
649- }
650- static get typeId () {
651- return 299
652- }
653- }
654-
655- class First extends BaseContainer {
656- onMessage (m) {
657- this.kernel.incrementTicks(2)
658- this.kernel.send(m.fromPort, this.kernel.createMessage({
659- data: 'first'
398 + const message = root.createMessage()
399 + await Promise.all([
400 + root.send(portRef1, message),
401 + root.ports.bind('first', portRef1),
402 + root.createInstance(Root.typeId, root.createMessage({
403 + ports: [portRef2]
404 + })),
405 + root.ports.bind('sencond', portRef3),
406 + root.createInstance(Waiter.typeId, root.createMessage({
407 + ports: [portRef4]
660408 }))
661- }
662- static get typeId () {
663- return 29
664- }
665- }
409 + ])
666410
667- class Second extends BaseContainer {
668- onMessage (m) {
669- this.kernel.incrementTicks(2)
670- this.kernel.send(m.fromPort, this.kernel.createMessage({
671- data: 'second'
672- }))
673- }
674- static get typeId () {
675- return 2
676- }
411 + // root = await hypervisor.getInstance(root.id)
412 + root.incrementTicks(100)
413 + await root.send(portRef1, root.createMessage({data: 'testss'}))
414 + root.shutdown()
415 + } catch (e) {
416 + console.log(e)
677417 }
678-
679- const hypervisor = new Hypervisor(node.dag)
680-
681- hypervisor.registerContainer(Root)
682- hypervisor.registerContainer(First)
683- hypervisor.registerContainer(Second)
684-
685- const root = await hypervisor.createInstance(Root.typeId)
686- const [portRef1, portRef2] = root.ports.createChannel()
687- const message = root.createMessage()
688-
689- root.send(portRef1, message)
690- await root.ports.bind('first', portRef1)
691- root.createInstance(Root.typeId, root.createMessage({
692- ports: [portRef2]
693- }))
694418 })
695419
696- tape('message should arrive in the correct order, with a tie in ticks but with differnt proity', async t => {
697- t.plan(2)
698-
699- let runs = 0
700-
701- class Root extends BaseContainer {
702- async onMessage (m) {
703- if (!runs) {
704- runs++
705- const [portRef1, portRef2] = this.kernel.ports.createChannel()
706- const [portRef3, portRef4] = this.kernel.ports.createChannel()
707-
708- await this.kernel.ports.bind('one', portRef1)
709- await this.kernel.ports.bind('two', portRef3)
710-
711- const message1 = this.kernel.createMessage({
712- ports: [portRef2]
713- })
714- const message2 = this.kernel.createMessage({
715- ports: [portRef4]
716- })
717-
718- this.kernel.createInstance(First.typeId, message1)
719- this.kernel.createInstance(Second.typeId, message2)
720-
721- this.kernel.send(portRef1, this.kernel.createMessage())
722- this.kernel.send(portRef3, this.kernel.createMessage())
723-
724- this.kernel.incrementTicks(6)
725- } else if (runs === 1) {
726- runs++
727- t.equals(m.data, 'first', 'should recive the first message')
728- } else if (runs === 2) {
729- t.equals(m.data, 'second', 'should recived the second message')
730- }
731- }
732- static get typeId () {
733- return 299
734- }
735- }
736-
737- class First extends BaseContainer {
738- onMessage (m) {
739- this.kernel.incrementTicks(2)
740- this.kernel.send(m.fromPort, this.kernel.createMessage({
741- data: 'first'
742- }))
743- }
744- static get typeId () {
745- return 29
746- }
747- }
748-
749- class Second extends BaseContainer {
750- onMessage (m) {
751- this.kernel.incrementTicks(2)
752- this.kernel.send(m.fromPort, this.kernel.createMessage({
753- data: 'second'
754- }))
755- }
756- }
757-
758- const hypervisor = new Hypervisor(node.dag)
759-
760- hypervisor.registerContainer(Root)
761- hypervisor.registerContainer(First)
762- hypervisor.registerContainer(Second)
763-
764- const root = await hypervisor.createInstance(Root.typeId)
765- const [portRef1, portRef2] = root.ports.createChannel()
766- const message = root.createMessage()
767-
768- root.send(portRef1, message)
769- await root.ports.bind('first', portRef1)
770- root.createInstance(Root.typeId, root.createMessage({
771- ports: [portRef2]
772- }))
773- })
774-
775420 tape('send to the same container at the same time', async t => {
776421 t.plan(2)
777422
778423 let runs = 0
779424 let instance
780425
781426 class Root extends BaseContainer {
782- onMessage (m) {
427 + async onMessage (m) {
783428 let one = this.kernel.ports.get('one')
784429 if (!one) {
785430 const [portRef1, portRef2] = this.kernel.ports.createChannel()
786431 const message1 = this.kernel.createMessage({
787432 ports: [portRef2]
788433 })
789- this.kernel.createInstance(First.typeId, message1)
434 + await this.kernel.createInstance(First.typeId, message1)
790435 return this.kernel.ports.bind('one', portRef1)
791436 } else {
792- this.kernel.send(one, this.kernel.createMessage())
793- this.kernel.send(one, this.kernel.createMessage())
437 + return Promise.all([
438 + this.kernel.send(one, this.kernel.createMessage()),
439 + this.kernel.send(one, this.kernel.createMessage())
440 + ])
794441 }
795442 }
796443 static get typeId () {
797444 return 299
@@ -808,26 +455,32 @@
808455 }
809456 }
810457 }
811458
812- const hypervisor = new Hypervisor(node.dag)
459 + try {
460 + const hypervisor = new Hypervisor(node.dag)
813461
814- hypervisor.registerContainer(Root)
815- hypervisor.registerContainer(First)
462 + hypervisor.registerContainer(Root)
463 + hypervisor.registerContainer(First)
816464
817- const root = await hypervisor.createInstance(Root.typeId)
818- const [portRef1, portRef2] = root.ports.createChannel()
819- await root.ports.bind('first', portRef1)
820- root.createInstance(Root.typeId, root.createMessage({
821- ports: [portRef2]
822- }))
465 + const root = await hypervisor.createInstance(Root.typeId)
466 + const [portRef1, portRef2] = root.ports.createChannel()
467 + await Promise.all([
468 + root.ports.bind('first', portRef1),
469 + root.createInstance(Root.typeId, root.createMessage({
470 + ports: [portRef2]
471 + }))
472 + ])
823473
824- const message = root.createMessage()
825- root.send(portRef1, message)
826- await hypervisor.createStateRoot()
827- root.send(portRef1, root.createMessage())
828- await hypervisor.createStateRoot()
829- t.equals(runs, 2)
474 + const message = root.createMessage()
475 + await root.send(portRef1, message)
476 + await hypervisor.createStateRoot()
477 + await root.send(portRef1, root.createMessage())
478 + await hypervisor.createStateRoot()
479 + t.equals(runs, 2)
480 + } catch (e) {
481 + console.log(e)
482 + }
830483 })
831484
832485 tape('checking ports', async t => {
833486 t.plan(4)
@@ -874,25 +527,25 @@
874527 const expectedSr = {
875528 '/': 'zdpuAopMy53q2uvL2a4fhVEAvwXjSDW28fh8zhQUj598tb5md'
876529 }
877530 class Root extends BaseContainer {
878- onMessage (m) {
531 + async onMessage (m) {
879532 const [portRef1, portRef2] = this.kernel.ports.createChannel()
880533 const message1 = this.kernel.createMessage({
881534 ports: [portRef2]
882535 })
883536
884- this.kernel.createInstance(First.typeId, message1)
885- this.kernel.send(portRef1, this.kernel.createMessage())
537 + await this.kernel.createInstance(First.typeId, message1)
538 + await this.kernel.send(portRef1, this.kernel.createMessage())
886539 this.kernel.incrementTicks(6)
887540 return this.kernel.ports.bind('one', portRef1)
888541 }
889542 }
890543
891544 class First extends BaseContainer {
892545 onMessage (m) {
893546 this.kernel.incrementTicks(2)
894- this.kernel.ports.delete('root')
547 + return this.kernel.ports.delete('root')
895548 }
896549 static get typeId () {
897550 return 299
898551 }
@@ -905,14 +558,14 @@
905558
906559 const root = await hypervisor.createInstance(Root.typeId)
907560 const [portRef1, portRef2] = root.ports.createChannel()
908561 await root.ports.bind('first', portRef1)
909- root.createInstance(Root.typeId, root.createMessage({
562 + await root.createInstance(Root.typeId, root.createMessage({
910563 ports: [portRef2]
911564 }))
912565
913566 const message = root.createMessage()
914- root.send(portRef1, message)
567 + await root.send(portRef1, message)
915568
916569 const sr = await hypervisor.createStateRoot()
917570 t.deepEquals(sr, expectedSr, 'should produce the corret state root')
918571 await hypervisor.graph.tree(sr, Infinity, true)
@@ -925,25 +578,24 @@
925578 '/': 'zdpuAopMy53q2uvL2a4fhVEAvwXjSDW28fh8zhQUj598tb5md'
926579 }
927580 class Root extends BaseContainer {
928581 onMessage (m) {
929- this.kernel.createInstance(Root.typeId)
582 + return this.kernel.createInstance(Root.typeId)
930583 }
931584 }
932585
933586 const hypervisor = new Hypervisor(node.dag)
934-
935587 hypervisor.registerContainer(Root)
936588
937589 const root = await hypervisor.createInstance(Root.typeId)
938590 const [portRef1, portRef2] = root.ports.createChannel()
939591 await root.ports.bind('first', portRef1)
940- root.createInstance(Root.typeId, root.createMessage({
592 + await root.createInstance(Root.typeId, root.createMessage({
941593 ports: [portRef2]
942594 }))
943595
944596 const message = root.createMessage()
945- root.send(portRef1, message)
597 + await root.send(portRef1, message)
946598 const sr = await hypervisor.createStateRoot()
947599 t.deepEquals(sr, expectedSr, 'should produce the corret state root')
948600
949601 t.end()
@@ -955,9 +607,9 @@
955607 }
956608 class Root extends BaseContainer {
957609 onMessage (m) {
958610 const [, portRef2] = this.kernel.ports.createChannel()
959- this.kernel.createInstance(Sub.typeId, this.kernel.createMessage({
611 + return this.kernel.createInstance(Sub.typeId, this.kernel.createMessage({
960612 ports: [portRef2]
961613 }))
962614 }
963615 }
@@ -966,34 +618,38 @@
966618 async onInitailize (message) {
967619 await this.kernel.ports.bind('root', message.ports[0])
968620 const [portRef1, portRef2] = this.kernel.ports.createChannel()
969621 await this.kernel.ports.bind('child', portRef1)
970- this.kernel.createInstance(Root.typeId, this.kernel.createMessage({
622 + await this.kernel.createInstance(Root.typeId, this.kernel.createMessage({
971623 ports: [portRef2]
972624 }))
973625 }
974626 static get typeId () {
975627 return 299
976628 }
977629 }
978630
979- const hypervisor = new Hypervisor(node.dag)
631 + try {
632 + const hypervisor = new Hypervisor(node.dag)
980633
981- hypervisor.registerContainer(Root)
982- hypervisor.registerContainer(Sub)
634 + hypervisor.registerContainer(Root)
635 + hypervisor.registerContainer(Sub)
983636
984- const root = await hypervisor.createInstance(Root.typeId)
985- const [portRef1, portRef2] = root.ports.createChannel()
986- await root.ports.bind('first', portRef1)
987- root.createInstance(Root.typeId, root.createMessage({
988- ports: [portRef2]
989- }))
637 + const root = await hypervisor.createInstance(Root.typeId)
638 + const [portRef1, portRef2] = root.ports.createChannel()
639 + await root.ports.bind('first', portRef1)
640 + await root.createInstance(Root.typeId, root.createMessage({
641 + ports: [portRef2]
642 + }))
990643
991- root.send(portRef1, root.createMessage())
992- const sr = await hypervisor.createStateRoot()
644 + await root.send(portRef1, root.createMessage())
645 + const sr = await hypervisor.createStateRoot()
993646
994- t.deepEquals(sr, expectedSr, 'should produce the corret state root')
995- t.end()
647 + t.deepEquals(sr, expectedSr, 'should produce the corret state root')
648 + t.end()
649 + } catch (e) {
650 + console.log(e)
651 + }
996652 })
997653
998654 tape('should not remove connected nodes', async t => {
999655 const expectedSr = {
@@ -1002,34 +658,34 @@
1002658 class Root extends BaseContainer {
1003659 async onMessage (m) {
1004660 if (m.ports.length) {
1005661 const port = this.kernel.ports.get('test1')
1006- this.kernel.send(port, m)
662 + await this.kernel.send(port, m)
1007663 return this.kernel.ports.unbind('test1')
1008664 } else {
1009665 const [portRef1, portRef2] = this.kernel.ports.createChannel()
1010- this.kernel.createInstance(Sub.typeId, this.kernel.createMessage({
666 + await this.kernel.createInstance(Sub.typeId, this.kernel.createMessage({
1011667 ports: [portRef2]
1012668 }))
1013669 await this.kernel.ports.bind('test1', portRef1)
1014670
1015671 const [portRef3, portRef4] = this.kernel.ports.createChannel()
1016- this.kernel.createInstance(Sub.typeId, this.kernel.createMessage({
672 + await this.kernel.createInstance(Sub.typeId, this.kernel.createMessage({
1017673 ports: [portRef4]
1018674 }))
1019675 await this.kernel.ports.bind('test2', portRef3)
1020- this.kernel.send(portRef3, this.kernel.createMessage({
676 + await this.kernel.send(portRef3, this.kernel.createMessage({
1021677 data: 'getChannel'
1022678 }))
1023679 }
1024680 }
1025681 }
1026682
1027683 class Sub extends BaseContainer {
1028- onMessage (message) {
684 + async onMessage (message) {
1029685 if (message.data === 'getChannel') {
1030686 const ports = this.kernel.ports.createChannel()
1031- this.kernel.send(message.fromPort, this.kernel.createMessage({
687 + await this.kernel.send(message.fromPort, this.kernel.createMessage({
1032688 data: 'bindPort',
1033689 ports: [ports[1]]
1034690 }))
1035691 return this.kernel.ports.bind('channel', ports[0])
@@ -1049,13 +705,13 @@
1049705
1050706 const root = await hypervisor.createInstance(Root.typeId)
1051707 const [portRef1, portRef2] = root.ports.createChannel()
1052708 await root.ports.bind('first', portRef1)
1053- root.createInstance(Root.typeId, root.createMessage({
709 + await root.createInstance(Root.typeId, root.createMessage({
1054710 ports: [portRef2]
1055711 }))
1056712
1057- root.send(portRef1, root.createMessage())
713 + await root.send(portRef1, root.createMessage())
1058714 const sr = await hypervisor.createStateRoot()
1059715 t.deepEquals(sr, expectedSr, 'should produce the corret state root')
1060716 // await hypervisor.graph.tree(sr, Infinity)
1061717
@@ -1063,41 +719,44 @@
1063719 })
1064720
1065721 tape('should remove multiple subgraphs', async t => {
1066722 const expectedSr = {
1067- '/': 'zdpuAohccQTxM82d8N6Q82z234nQskeQoJGJu3eAVmxoQwWde'
723 + '/': 'zdpuArkZ5yNowNnU4qJ8vayAUncgibQP9goDP1CwFxdmPJF9D'
1068724 }
1069725 class Root extends BaseContainer {
1070- async onMessage (m) {
726 + onMessage (m) {
1071727 if (m.ports.length) {
1072728 const port = this.kernel.ports.get('test1')
1073- this.kernel.send(port, m)
1074- await this.kernel.ports.unbind('test1')
1075- await this.kernel.ports.unbind('test2')
729 + return Promise.all([
730 + this.kernel.send(port, m),
731 + this.kernel.ports.unbind('test1'),
732 + this.kernel.ports.unbind('test2')
733 + ])
1076734 } else {
1077735 const [portRef1, portRef2] = this.kernel.ports.createChannel()
1078- this.kernel.createInstance(Sub.typeId, this.kernel.createMessage({
1079- ports: [portRef2]
1080- }))
1081- await this.kernel.ports.bind('test1', portRef1)
1082-
1083736 const [portRef3, portRef4] = this.kernel.ports.createChannel()
1084- this.kernel.createInstance(Sub.typeId, this.kernel.createMessage({
1085- ports: [portRef4]
1086- }))
1087- await this.kernel.ports.bind('test2', portRef3)
1088- this.kernel.send(portRef3, this.kernel.createMessage({
1089- data: 'getChannel'
1090- }))
737 + return Promise.all([
738 + this.kernel.createInstance(Sub.typeId, this.kernel.createMessage({
739 + ports: [portRef2]
740 + })),
741 + this.kernel.ports.bind('test1', portRef1),
742 + this.kernel.createInstance(Sub.typeId, this.kernel.createMessage({
743 + ports: [portRef4]
744 + })),
745 + this.kernel.ports.bind('test2', portRef3),
746 + this.kernel.send(portRef3, this.kernel.createMessage({
747 + data: 'getChannel'
748 + }))
749 + ])
1091750 }
1092751 }
1093752 }
1094753
1095754 class Sub extends BaseContainer {
1096- onMessage (message) {
755 + async onMessage (message) {
1097756 if (message.data === 'getChannel') {
1098757 const ports = this.kernel.ports.createChannel()
1099- this.kernel.send(message.fromPort, this.kernel.createMessage({
758 + await this.kernel.send(message.fromPort, this.kernel.createMessage({
1100759 data: 'bindPort',
1101760 ports: [ports[1]]
1102761 }))
1103762 return this.kernel.ports.bind('channel', ports[0])
@@ -1109,27 +768,32 @@
1109768 return 299
1110769 }
1111770 }
1112771
1113- const hypervisor = new Hypervisor(node.dag)
772 + try {
773 + const hypervisor = new Hypervisor(node.dag)
1114774
1115- hypervisor.registerContainer(Root)
1116- hypervisor.registerContainer(Sub)
775 + hypervisor.registerContainer(Root)
776 + hypervisor.registerContainer(Sub)
1117777
1118- const root = await hypervisor.createInstance(Root.typeId)
778 + const root = await hypervisor.createInstance(Root.typeId)
1119779
1120- const [portRef1, portRef2] = root.ports.createChannel()
1121- await root.ports.bind('first', portRef1)
1122- root.createInstance(Root.typeId, root.createMessage({
1123- ports: [portRef2]
1124- }))
780 + const [portRef1, portRef2] = root.ports.createChannel()
781 + await Promise.all([
782 + root.ports.bind('first', portRef1),
783 + root.createInstance(Root.typeId, root.createMessage({
784 + ports: [portRef2]
785 + })),
786 + root.send(portRef1, root.createMessage())
787 + ])
1125788
1126- root.send(portRef1, root.createMessage())
789 + const sr = await hypervisor.createStateRoot()
790 + t.deepEquals(sr, expectedSr, 'should produce the corret state root')
1127791
1128- const sr = await hypervisor.createStateRoot()
1129- t.deepEquals(sr, expectedSr, 'should produce the corret state root')
1130-
1131- t.end()
792 + t.end()
793 + } catch (e) {
794 + console.log(e)
795 + }
1132796 })
1133797
1134798 tape('response ports', async t => {
1135799 t.plan(2)

Built with git-ssb-web