git ssb

0+

wanderer🌟 / js-primea-hypervisor



Commit 679caf4c89ce01c38751eb18177edee3a845d23e

updated tests and example

wanderer committed on 7/3/2017, 4:56:23 PM
Parent: 60e1d083f94f993b772846d9d209f845081a9094

Files changed

examples/index.jschanged
index.jschanged
kernel.jschanged
portManager.jschanged
tests/index.jschanged
examples/index.jsView
@@ -17,9 +17,9 @@
1717
1818 // this method runs once when the container is intailly created. It is given
1919 // a message with a single port, which is a channel to its parent with the
2020 // exception of the root container (the container that is intailial created)
21- initailize (message) {
21+ initialize (message) {
2222 const port = message.ports[0]
2323 // the root container doesn't get a port
2424 if (port) {
2525 this.kernel.ports.bind('parent', port)
@@ -27,9 +27,8 @@
2727 }
2828
2929 // the function is called for each message that a container gets
3030 run (message) {
31- console.log('message recived!', message.data)
3231 if (message.data === 'bindPort') {
3332 this.kernel.ports.bind('channel', message.ports[0])
3433 }
3534 }
@@ -43,32 +42,41 @@
4342
4443 // create a root instance of the example container
4544 const root = await hypervisor.createInstance('example')
4645
46+ const [portRef1, portRef2] = root.ports.createChannel()
47+ const [portRef3, portRef4] = root.ports.createChannel()
48+
4749 // create two instances of the example container. These containers wiil be
4850 // given channels to the parent container
49- const portRef1 = root.ports.create('example')
50- const portRef2 = root.ports.create('example')
51+ root.ports.create('example', root.createMessage({
52+ ports: [portRef2]
53+ }))
5154
55+ root.ports.create('example', root.createMessage({
56+ ports: [portRef4]
57+ }))
58+
5259 // bind the ports of the newly created containers. Binding ports allows the
5360 // root container tot receieve messages from the containers. If no other
5461 // container bound these ports then the corrisponding containers would be
5562 // garbage collected
5663 root.ports.bind('one', portRef1)
57- root.ports.bind('two', portRef2)
64+ root.ports.bind('two', portRef3)
5865
5966 // create a new channel. Each channel has two corrisponding ports that
6067 // containers can communicate over
6168 const [chanRef1, chanRef2] = root.ports.createChannel()
69+
6270 // send the newly created ports to each of the containers. Once both the
6371 // recieving containers bind the ports they will be able to communicate
6472 // directly with each other over them
6573 await root.send(portRef1, root.createMessage({
6674 data: 'bindPort',
6775 ports: [chanRef1]
6876 }))
6977
70- await root.send(portRef2, root.createMessage({
78+ await root.send(portRef3, root.createMessage({
7179 data: 'bindPort',
7280 ports: [chanRef2]
7381 }))
7482
@@ -76,15 +84,15 @@
7684 // topology will look like this. Where "[]" are the containers, "*" are the
7785 // ports that the container have and "(name)" is the port name.
7886 //
7987 // root container
80- // [ ]
88+ // [ ]
8189 // (one) * * (two)
8290 // / \
8391 // / \
8492 // / \
8593 // (parent)* * (parent)
86- // [ ]*--------*[ ]
94+ // [ ]*--------*[ ]
8795 // (channel) (channel)
8896
8997 // create a new state root. The state root is not created untill the
9098 // hypervisor has finished all of it's work
index.jsView
@@ -91,13 +91,14 @@
9191 * @param {object} id.nonce
9292 * @param {object} id.parent
9393 * @returns {Promise}
9494 */
95- async createInstance (type, code, entryPorts = [], id = {nonce: 0, parent: null}) {
95+ async createInstance (type, message = new Message(), id = {nonce: 0, parent: null}) {
9696 // create a lock to prevent the scheduler from reloving waits before the
9797 // new container is loaded
9898 const resolve = this.scheduler.getLock(id)
9999 const idHash = await this._getHashFromObj(id)
100+ const code = message.data.byteLength ? message.data : undefined
100101 const state = {
101102 nonce: [0],
102103 ports: {},
103104 type: type,
@@ -106,17 +107,15 @@
106107
107108 // save the container in the state
108109 await this.graph.set(this.state, idHash, state)
109110 // create the container instance
110- const exoInterface = await this._loadInstance(idHash)
111+ const instance = await this._loadInstance(idHash)
111112
112- resolve(exoInterface)
113+ resolve(instance)
113114 // send the intialization message
114- exoInterface.queue(null, new Message({
115- ports: entryPorts
116- }))
115+ instance.initialize(message)
117116
118- return exoInterface
117+ return instance
119118 }
120119
121120 /**
122121 * creates a state root starting from a given container and a given number of
kernel.jsView
@@ -32,22 +32,20 @@
3232 * @param {string} portName
3333 * @param {object} message
3434 */
3535 queue (portName, message) {
36- message._hops++
37- if (portName) {
38- this.ports.queue(portName, message)
39- if (this.containerState !== 'running') {
40- this.containerState = 'running'
41- this._runNextMessage()
42- }
43- } else {
44- // initailiazation message
36+ this.ports.queue(portName, message)
37+ if (this.containerState !== 'running') {
4538 this.containerState = 'running'
46- this.run(message, true)
39+ this._runNextMessage()
4740 }
4841 }
4942
43+ initialize (message) {
44+ this.containerState = 'running'
45+ this.run(message, true)
46+ }
47+
5048 // waits for the next message
5149 async _runNextMessage () {
5250 // check if the ports are saturated, if so we don't have to wait on the
5351 // scheduler
@@ -76,13 +74,16 @@
7674 * @returns {Promise}
7775 */
7876 async run (message, init = false) {
7977 let result
78+
8079 message.ports.forEach(port => this.ports._unboundPorts.add(port))
80+ message._hops++
81+
8182 if (message.constructor === DeleteMessage) {
8283 this.ports._delete(message.fromName)
8384 } else {
84- const method = init ? 'initailize' : 'run'
85+ const method = init ? 'initialize' : 'run'
8586 try {
8687 result = await this.container[method](message) || {}
8788 } catch (e) {
8889 result = {
@@ -91,9 +92,12 @@
9192 }
9293 }
9394 }
9495 this.ports.clearUnboundedPorts()
95- // message.response(result)
96+ // const responsePort = this.message.responsePort
97+ // if (responsePort) {
98+ // this.send(responsePort, new Message({data: result}))
99+ // }
96100 this._runNextMessage()
97101 return result
98102 }
99103
portManager.jsView
@@ -170,9 +170,9 @@
170170 * @param {String} type
171171 * @param {*} data - the data to populate the initail state with
172172 * @returns {Object}
173173 */
174- create (type, data) {
174+ create (type, message) {
175175 let nonce = this.state.nonce
176176
177177 const id = {
178178 nonce: nonce,
@@ -182,15 +182,11 @@
182182 // incerment the nonce
183183 nonce = new BN(nonce)
184184 nonce.iaddn(1)
185185 this.state.nonce = nonce.toArray()
186+ message.ports.forEach(port => this._unboundPorts.delete(port))
186187
187- // create a new channel for the container
188- const ports = this.createChannel()
189- this._unboundPorts.delete(ports[1])
190- this.hypervisor.createInstance(type, data, [ports[1]], id)
191-
192- return ports[0]
188+ this.hypervisor.createInstance(type, message, id)
193189 }
194190
195191 /**
196192 * creates a channel returns the created ports in an Array
@@ -252,8 +248,9 @@
252248 ])
253249
254250 oldestTime = this.hypervisor.scheduler.oldest()
255251 }
252+
256253 return message
257254 }
258255
259256 // tests wether or not all the ports have a message
tests/index.jsView
@@ -11,9 +11,9 @@
1111 constructor (exInterface) {
1212 this.exInterface = exInterface
1313 }
1414
15- initailize (message) {
15+ initialize (message) {
1616 const port = message.ports[0]
1717 if (port) {
1818 this.exInterface.ports.bind('root', port)
1919 }
@@ -24,9 +24,9 @@
2424 tape('basic', async t => {
2525 t.plan(2)
2626 let message
2727 const expectedState = {
28- '/': 'zdpuAyGKaZ3nbBQdgESbEgVYr81TcAFB6LE2MQQPWLZaYxuF3'
28+ '/': 'zdpuB1wc9Pb6jUzfNt4nAxAEUxB7kNhg4vbq7YLcEyBUb6iAB'
2929 }
3030
3131 class testVMContainer extends BaseContainer {
3232 run (m) {
@@ -37,12 +37,20 @@
3737 const hypervisor = new Hypervisor(node.dag)
3838 hypervisor.registerContainer('test', testVMContainer)
3939
4040 const rootContainer = await hypervisor.createInstance('test')
41- const port = rootContainer.ports.create('test')
41+
42+ const [portRef1, portRef2] = rootContainer.ports.createChannel()
43+ const initMessage = rootContainer.createMessage({
44+ data: Buffer.from('test code'),
45+ ports: [portRef2]
46+ })
47+
48+ rootContainer.ports.create('test', initMessage)
49+
50+ rootContainer.ports.bind('first', portRef1)
4251 message = rootContainer.createMessage()
43- rootContainer.ports.bind('first', port)
44- rootContainer.send(port, message)
52+ rootContainer.send(portRef1, message)
4553
4654 const stateRoot = await hypervisor.createStateRoot(Infinity)
4755 t.deepEquals(stateRoot, expectedState, 'expected root!')
4856 })
@@ -53,18 +61,22 @@
5361 '/': 'zdpuAx5LRRwTgzPipKEPgh7MHUKu4Pd1BYjDqBcf9whgzvrqf'
5462 }
5563
5664 class testVMContainer extends BaseContainer {
57- initailize () {}
65+ initialize () {}
5866 }
5967
6068 const hypervisor = new Hypervisor(node.dag)
6169 hypervisor.registerContainer('test', testVMContainer)
6270
6371 const root = await hypervisor.createInstance('test')
64- const port = root.ports.create('test')
65- root.ports.bind('one', port)
72+ const [portRef1, portRef2] = root.ports.createChannel()
6673
74+ root.ports.bind('one', portRef1)
75+ root.ports.create('test', root.createMessage({
76+ ports: [portRef2]
77+ }))
78+
6779 const stateRoot = await hypervisor.createStateRoot(Infinity)
6880 t.deepEquals(stateRoot, expectedState, 'expected root!')
6981 })
7082
@@ -82,26 +94,34 @@
8294 }
8395
8496 class testVMContainer extends BaseContainer {
8597 run (m) {
86- const port = this.exInterface.ports.create('test2')
87- this.exInterface.ports.bind('child', port)
98+ const [portRef1, portRef2] = this.exInterface.ports.createChannel()
99+ this.exInterface.ports.create('test2', this.exInterface.createMessage({
100+ ports: [portRef2]
101+ }))
102+ this.exInterface.ports.bind('child', portRef1)
88103 this.exInterface.incrementTicks(2)
89- this.exInterface.send(port, m)
104+ this.exInterface.send(portRef1, m)
90105 }
91106 }
92107
93108 const hypervisor = new Hypervisor(node.dag)
94109 hypervisor.registerContainer('test', testVMContainer)
95110 hypervisor.registerContainer('test2', testVMContainer2)
96111
97- let root = await hypervisor.createInstance('test')
98- let port = root.ports.create('test')
112+ const root = await hypervisor.createInstance('test')
113+ const [portRef1, portRef2] = root.ports.createChannel()
114+ root.ports.create('test', root.createMessage({
115+ ports: [portRef2]
116+ }))
99117
100- root.ports.bind('first', port)
101- message = root.createMessage()
118+ root.ports.bind('first', portRef1)
119+ message = root.createMessage({
120+ data: 'test'
121+ })
102122
103- root.send(port, message)
123+ root.send(portRef1, message)
104124 const stateRoot = await hypervisor.createStateRoot(Infinity)
105125 t.deepEquals(stateRoot, expectedState, 'expected state')
106126 })
107127
@@ -127,11 +147,14 @@
127147 }
128148
129149 class testVMContainer extends BaseContainer {
130150 run (m) {
131- const port = this.exInterface.ports.create('test2')
132- this.exInterface.ports.bind('child', port)
133- this.exInterface.send(port, m)
151+ const [portRef1, portRef2] = this.exInterface.ports.createChannel()
152+ this.exInterface.ports.create('test2', this.exInterface.createMessage({
153+ ports: [portRef2]
154+ }))
155+ this.exInterface.ports.bind('child', portRef1)
156+ this.exInterface.send(portRef1, m)
134157 this.exInterface.incrementTicks(1)
135158 }
136159 }
137160
@@ -140,17 +163,21 @@
140163 hypervisor.registerContainer('test2', testVMContainer2)
141164
142165 let root = await hypervisor.createInstance('test')
143166 const rootId = root.id
144- let port = root.ports.create('test')
167+ const [portRef1, portRef2] = root.ports.createChannel()
168+ root.ports.create('test', root.createMessage({
169+ ports: [portRef2]
170+ }))
145171
146- root.ports.bind('first', port)
172+ root.ports.bind('first', portRef1)
147173 message = root.createMessage()
148174
149- root.send(port, message)
175+ root.send(portRef1, message)
150176 const stateRoot = await hypervisor.createStateRoot(Infinity)
151177 t.true(hasResolved, 'should resolve before generating the state root')
152178 t.deepEquals(stateRoot, expectedState, 'expected state')
179+
153180 // test reviving the state
154181 class testVMContainer3 extends BaseContainer {
155182 run (m) {
156183 const port = this.exInterface.ports.get('child')
@@ -160,24 +187,38 @@
160187 }
161188
162189 hypervisor.registerContainer('test', testVMContainer3)
163190 root = await hypervisor.getInstance(rootId)
164- port = root.ports.get('first')
191+ const port = root.ports.get('first')
165192 root.send(port, message)
166193 })
167194
168195 tape('traps', async t => {
169196 t.plan(1)
170197 class Root extends BaseContainer {
171198 async run (m) {
172- const one = this.exInterface.ports.create('root')
173- const two = this.exInterface.ports.create('root')
174- const three = this.exInterface.ports.create('root')
199+ const [portRef1, portRef2] = this.exInterface.ports.createChannel()
200+ const [portRef3, portRef4] = this.exInterface.ports.createChannel()
201+ const [portRef5, portRef6] = this.exInterface.ports.createChannel()
175202
176- this.exInterface.ports.bind('one', one)
177- this.exInterface.ports.bind('two', two)
178- this.exInterface.ports.bind('three', three)
203+ this.exInterface.ports.bind('one', portRef1)
204+ this.exInterface.ports.bind('two', portRef3)
205+ this.exInterface.ports.bind('three', portRef5)
179206
207+ const message1 = this.exInterface.createMessage({
208+ ports: [portRef2]
209+ })
210+ const message2 = this.exInterface.createMessage({
211+ ports: [portRef4]
212+ })
213+ const message3 = this.exInterface.createMessage({
214+ ports: [portRef6]
215+ })
216+
217+ this.exInterface.ports.create('root', message1)
218+ this.exInterface.ports.create('root', message2)
219+ this.exInterface.ports.create('root', message3)
220+
180221 throw new Error('it is a trap!!!')
181222 }
182223 }
183224
@@ -200,16 +241,27 @@
200241 class Root extends BaseContainer {
201242 run (m) {
202243 if (!runs) {
203244 runs++
204- const one = this.exInterface.ports.create('first')
205- const two = this.exInterface.ports.create('second')
206245
207- this.exInterface.ports.bind('two', two)
208- this.exInterface.ports.bind('one', one)
246+ const [portRef1, portRef2] = this.exInterface.ports.createChannel()
247+ const [portRef3, portRef4] = this.exInterface.ports.createChannel()
209248
210- this.exInterface.send(one, this.exInterface.createMessage())
211- this.exInterface.send(two, this.exInterface.createMessage())
249+ this.exInterface.ports.bind('two', portRef3)
250+ this.exInterface.ports.bind('one', portRef1)
251+
252+ const message1 = this.exInterface.createMessage({
253+ ports: [portRef2]
254+ })
255+ const message2 = this.exInterface.createMessage({
256+ ports: [portRef4]
257+ })
258+
259+ this.exInterface.ports.create('first', message1)
260+ this.exInterface.ports.create('second', message2)
261+
262+ this.exInterface.send(portRef1, this.exInterface.createMessage())
263+ this.exInterface.send(portRef3, this.exInterface.createMessage())
212264 } else if (runs === 1) {
213265 runs++
214266 t.equals(m.data, 'first', 'should recive the first message')
215267 } else if (runs === 2) {
@@ -242,12 +294,17 @@
242294 hypervisor.registerContainer('first', First)
243295 hypervisor.registerContainer('second', Second)
244296
245297 const root = await hypervisor.createInstance('root')
246- const port = root.ports.create('root')
247- root.ports.bind('first', port)
248298
249- root.send(port, root.createMessage())
299+ const [portRef1, portRef2] = root.ports.createChannel()
300+ root.ports.create('root', root.createMessage({
301+ ports: [portRef2]
302+ }))
303+
304+ root.ports.bind('first', portRef1)
305+ const message = root.createMessage()
306+ root.send(portRef1, message)
250307 })
251308
252309 tape('message should arrive in the correct oder if sent in order', async t => {
253310 t.plan(2)
@@ -256,18 +313,27 @@
256313 class Root extends BaseContainer {
257314 run (m) {
258315 if (!runs) {
259316 runs++
260- const one = this.exInterface.ports.create('first')
261- const two = this.exInterface.ports.create('second')
262317
263- this.exInterface.ports.bind('one', one)
264- this.exInterface.ports.bind('two', two)
318+ const [portRef1, portRef2] = this.exInterface.ports.createChannel()
319+ const [portRef3, portRef4] = this.exInterface.ports.createChannel()
265320
266- Promise.all([
267- this.exInterface.send(one, this.exInterface.createMessage()),
268- this.exInterface.send(two, this.exInterface.createMessage())
269- ])
321+ this.exInterface.ports.bind('one', portRef1)
322+ this.exInterface.ports.bind('two', portRef3)
323+
324+ const message1 = this.exInterface.createMessage({
325+ ports: [portRef2]
326+ })
327+ const message2 = this.exInterface.createMessage({
328+ ports: [portRef4]
329+ })
330+
331+ this.exInterface.ports.create('first', message1)
332+ this.exInterface.ports.create('second', message2)
333+
334+ this.exInterface.send(portRef1, this.exInterface.createMessage())
335+ this.exInterface.send(portRef3, this.exInterface.createMessage())
270336 } else if (runs === 1) {
271337 runs++
272338 t.equals(m.data, 'second', 'should recived the second message')
273339 } else if (runs === 2) {
@@ -300,12 +366,17 @@
300366 hypervisor.registerContainer('first', First)
301367 hypervisor.registerContainer('second', Second)
302368
303369 const root = await hypervisor.createInstance('root')
304- const port = root.ports.create('root')
305- root.ports.bind('first', port)
306370
307- root.send(port, root.createMessage())
371+ const [portRef1, portRef2] = root.ports.createChannel()
372+ root.ports.create('root', root.createMessage({
373+ ports: [portRef2]
374+ }))
375+
376+ root.ports.bind('first', portRef1)
377+ const message = root.createMessage()
378+ root.send(portRef1, message)
308379 })
309380
310381 tape('message should arrive in the correct oder if sent in order', async t => {
311382 t.plan(2)
@@ -314,17 +385,27 @@
314385 class Root extends BaseContainer {
315386 run (m) {
316387 if (!runs) {
317388 runs++
318- const one = this.exInterface.ports.create('first')
319- const two = this.exInterface.ports.create('second')
389+ const [portRef1, portRef2] = this.exInterface.ports.createChannel()
390+ const [portRef3, portRef4] = this.exInterface.ports.createChannel()
320391
321- this.exInterface.ports.bind('one', one)
322- this.exInterface.ports.bind('two', two)
392+ this.exInterface.ports.bind('one', portRef1)
393+ this.exInterface.ports.bind('two', portRef3)
323394
324- this.exInterface.send(one, this.exInterface.createMessage())
325- this.exInterface.send(two, this.exInterface.createMessage())
395+ const message1 = this.exInterface.createMessage({
396+ ports: [portRef2]
397+ })
398+ const message2 = this.exInterface.createMessage({
399+ ports: [portRef4]
400+ })
326401
402+ this.exInterface.ports.create('first', message1)
403+ this.exInterface.ports.create('second', message2)
404+
405+ this.exInterface.send(portRef1, this.exInterface.createMessage())
406+ this.exInterface.send(portRef3, this.exInterface.createMessage())
407+
327408 this.exInterface.incrementTicks(6)
328409 } else if (runs === 1) {
329410 runs++
330411 t.equals(m.data, 'first', 'should recive the first message')
@@ -336,16 +417,20 @@
336417
337418 class First extends BaseContainer {
338419 run (m) {
339420 this.exInterface.incrementTicks(1)
340- this.exInterface.send(m.fromPort, this.exInterface.createMessage({data: 'first'}))
421+ this.exInterface.send(m.fromPort, this.exInterface.createMessage({
422+ data: 'first'
423+ }))
341424 }
342425 }
343426
344427 class Second extends BaseContainer {
345428 run (m) {
346429 this.exInterface.incrementTicks(2)
347- this.exInterface.send(m.fromPort, this.exInterface.createMessage({data: 'second'}))
430+ this.exInterface.send(m.fromPort, this.exInterface.createMessage({
431+ data: 'second'
432+ }))
348433 }
349434 }
350435
351436 const hypervisor = new Hypervisor(node.dag)
@@ -354,12 +439,16 @@
354439 hypervisor.registerContainer('first', First)
355440 hypervisor.registerContainer('second', Second)
356441
357442 const root = await hypervisor.createInstance('root')
358- const port = root.ports.create('root')
359- root.ports.bind('first', port)
443+ const [portRef1, portRef2] = root.ports.createChannel()
444+ root.ports.create('root', root.createMessage({
445+ ports: [portRef2]
446+ }))
360447
361- root.send(port, root.createMessage())
448+ root.ports.bind('first', portRef1)
449+ const message = root.createMessage()
450+ root.send(portRef1, message)
362451 })
363452
364453 tape('saturation', async t => {
365454 t.plan(2)
@@ -368,23 +457,33 @@
368457 class Root extends BaseContainer {
369458 run (m) {
370459 if (!runs) {
371460 runs++
372- const one = this.exInterface.ports.create('first')
373- const two = this.exInterface.ports.create('second')
461+ const [portRef1, portRef2] = this.exInterface.ports.createChannel()
462+ const [portRef3, portRef4] = this.exInterface.ports.createChannel()
374463
375- this.exInterface.ports.bind('two', two)
376- this.exInterface.ports.bind('one', one)
464+ this.exInterface.ports.bind('one', portRef1)
465+ this.exInterface.ports.bind('two', portRef3)
377466
378- Promise.all([
379- this.exInterface.send(one, this.exInterface.createMessage()),
380- this.exInterface.send(two, this.exInterface.createMessage())
381- ])
467+ const message1 = this.exInterface.createMessage({
468+ ports: [portRef2]
469+ })
470+ const message2 = this.exInterface.createMessage({
471+ ports: [portRef4]
472+ })
473+
474+ this.exInterface.ports.create('first', message1)
475+ this.exInterface.ports.create('second', message2)
476+
477+ this.exInterface.send(portRef1, this.exInterface.createMessage())
478+ this.exInterface.send(portRef3, this.exInterface.createMessage())
479+
382480 this.exInterface.incrementTicks(6)
383481 } else if (runs === 1) {
384482 runs++
385483 t.equals(m.data, 'first', 'should recive the first message')
386484 } else if (runs === 2) {
485+ runs++
387486 t.equals(m.data, 'second', 'should recived the second message')
388487 }
389488 }
390489 }
@@ -399,18 +498,18 @@
399498 }
400499
401500 class Second extends BaseContainer {
402501 run (m) {
403- // this.exInterface.incrementTicks(3)
404502 this.exInterface.incrementTicks(3)
503+ console.log('sending second', this.exInterface.ticks)
405504 this.exInterface.send(m.fromPort, this.exInterface.createMessage({
406505 data: 'second'
407506 }))
408507 }
409508 }
410509
411510 class Waiter extends BaseContainer {
412- initailize () {
511+ initialize () {
413512 return new Promise((resolve, reject) => {
414513 setTimeout(() => {
415514 resolve()
416515 }, 200)
@@ -425,17 +524,26 @@
425524 hypervisor.registerContainer('second', Second)
426525 hypervisor.registerContainer('waiter', Waiter)
427526
428527 const root = await hypervisor.createInstance('root')
429- const port = root.ports.create('root')
430- root.ports.bind('first', port)
431- const port1 = root.ports.create('waiter')
432- root.ports.bind('sencond', port1)
528+ const [portRef1, portRef2] = root.ports.createChannel()
433529
434- await root.send(port, root.createMessage())
435- root.incrementTicks(7)
530+ const message = root.createMessage()
531+ root.send(portRef1, message)
532+ root.ports.bind('first', portRef1)
533+ root.ports.create('root', root.createMessage({
534+ ports: [portRef2]
535+ }))
436536
437- root.send(port, root.createMessage())
537+ const [portRef3, portRef4] = root.ports.createChannel()
538+ root.ports.bind('sencond', portRef3)
539+ root.ports.create('waiter', root.createMessage({
540+ ports: [portRef4]
541+ }))
542+
543+ root.incrementTicks(100)
544+ root.send(portRef1, root.createMessage({data: 'testss'}))
545+ hypervisor.scheduler.done(root.id)
438546 })
439547
440548 tape('message should arrive in the correct order, even in a tie of ticks', async t => {
441549 t.plan(2)
@@ -445,17 +553,27 @@
445553 class Root extends BaseContainer {
446554 run (m) {
447555 if (!runs) {
448556 runs++
449- const one = this.exInterface.ports.create('first')
450- const two = this.exInterface.ports.create('second')
557+ const [portRef1, portRef2] = this.exInterface.ports.createChannel()
558+ const [portRef3, portRef4] = this.exInterface.ports.createChannel()
451559
452- this.exInterface.ports.bind('two', two)
453- this.exInterface.ports.bind('one', one)
560+ this.exInterface.ports.bind('two', portRef3)
561+ this.exInterface.ports.bind('one', portRef1)
454562
455- this.exInterface.send(one, this.exInterface.createMessage())
456- this.exInterface.send(two, this.exInterface.createMessage())
563+ const message1 = this.exInterface.createMessage({
564+ ports: [portRef2]
565+ })
566+ const message2 = this.exInterface.createMessage({
567+ ports: [portRef4]
568+ })
457569
570+ this.exInterface.ports.create('first', message1)
571+ this.exInterface.ports.create('second', message2)
572+
573+ this.exInterface.send(portRef1, this.exInterface.createMessage())
574+ this.exInterface.send(portRef3, this.exInterface.createMessage())
575+
458576 this.exInterface.incrementTicks(6)
459577 } else if (runs === 1) {
460578 runs++
461579 t.equals(m.data, 'second', 'should recived the second message')
@@ -489,13 +607,16 @@
489607 hypervisor.registerContainer('first', First)
490608 hypervisor.registerContainer('second', Second)
491609
492610 const root = await hypervisor.createInstance('root')
611+ const [portRef1, portRef2] = root.ports.createChannel()
612+ const message = root.createMessage()
493613
494- const port = root.ports.create('root')
495- root.ports.bind('first', port)
496-
497- root.send(port, root.createMessage())
614+ root.send(portRef1, message)
615+ root.ports.bind('first', portRef1)
616+ root.ports.create('root', root.createMessage({
617+ ports: [portRef2]
618+ }))
498619 })
499620
500621 tape('message should arrive in the correct order, with a tie in ticks but with differnt proity', async t => {
501622 t.plan(2)
@@ -505,17 +626,27 @@
505626 class Root extends BaseContainer {
506627 run (m) {
507628 if (!runs) {
508629 runs++
509- const one = this.exInterface.ports.create('first')
510- const two = this.exInterface.ports.create('second')
630+ const [portRef1, portRef2] = this.exInterface.ports.createChannel()
631+ const [portRef3, portRef4] = this.exInterface.ports.createChannel()
511632
512- this.exInterface.ports.bind('one', one)
513- this.exInterface.ports.bind('two', two)
633+ this.exInterface.ports.bind('one', portRef1)
634+ this.exInterface.ports.bind('two', portRef3)
514635
515- this.exInterface.send(two, this.exInterface.createMessage())
516- this.exInterface.send(one, this.exInterface.createMessage())
636+ const message1 = this.exInterface.createMessage({
637+ ports: [portRef2]
638+ })
639+ const message2 = this.exInterface.createMessage({
640+ ports: [portRef4]
641+ })
517642
643+ this.exInterface.ports.create('first', message1)
644+ this.exInterface.ports.create('second', message2)
645+
646+ this.exInterface.send(portRef1, this.exInterface.createMessage())
647+ this.exInterface.send(portRef3, this.exInterface.createMessage())
648+
518649 this.exInterface.incrementTicks(6)
519650 } else if (runs === 1) {
520651 runs++
521652 t.equals(m.data, 'first', 'should recive the first message')
@@ -549,11 +680,16 @@
549680 hypervisor.registerContainer('first', First)
550681 hypervisor.registerContainer('second', Second)
551682
552683 const root = await hypervisor.createInstance('root')
553- const port = root.ports.create('root')
554- root.ports.bind('first', port)
555- root.send(port, root.createMessage())
684+ const [portRef1, portRef2] = root.ports.createChannel()
685+ const message = root.createMessage()
686+
687+ root.send(portRef1, message)
688+ root.ports.bind('first', portRef1)
689+ root.ports.create('root', root.createMessage({
690+ ports: [portRef2]
691+ }))
556692 })
557693
558694 tape('send to the same container at the same time', async t => {
559695 t.plan(2)
@@ -564,10 +700,14 @@
564700 class Root extends BaseContainer {
565701 run (m) {
566702 let one = this.exInterface.ports.get('one')
567703 if (!one) {
568- one = this.exInterface.ports.create('first')
569- this.exInterface.ports.bind('one', one)
704+ const [portRef1, portRef2] = this.exInterface.ports.createChannel()
705+ this.exInterface.ports.bind('one', portRef1)
706+ const message1 = this.exInterface.createMessage({
707+ ports: [portRef2]
708+ })
709+ this.exInterface.ports.create('first', message1)
570710 } else {
571711 this.exInterface.send(one, this.exInterface.createMessage())
572712 this.exInterface.send(one, this.exInterface.createMessage())
573713 }
@@ -590,60 +730,77 @@
590730 hypervisor.registerContainer('root', Root)
591731 hypervisor.registerContainer('first', First)
592732
593733 const root = await hypervisor.createInstance('root')
594- const port = root.ports.create('root')
595- root.ports.bind('first', port)
596- root.send(port, root.createMessage())
734+ const [portRef1, portRef2] = root.ports.createChannel()
735+ root.ports.bind('first', portRef1)
736+ root.ports.create('root', root.createMessage({
737+ ports: [portRef2]
738+ }))
739+
740+ const message = root.createMessage()
741+ root.send(portRef1, message)
597742 await hypervisor.createStateRoot()
598- root.send(port, root.createMessage())
743+ root.send(portRef1, root.createMessage())
599744 await hypervisor.createStateRoot()
600745 t.equals(runs, 2)
601746 })
747+
602748 tape('checking ports', async t => {
603749 t.plan(4)
604750 const hypervisor = new Hypervisor(node.dag)
605751 hypervisor.registerContainer('base', BaseContainer)
606752
607753 const root = await hypervisor.createInstance('base')
608- let port = root.ports.create('base')
609- await root.ports.bind('test', port)
610754
755+ const [portRef1, portRef2] = root.ports.createChannel()
756+ root.ports.create('base', root.createMessage({
757+ ports: [portRef2]
758+ }))
759+ root.ports.bind('test', portRef1)
760+
611761 try {
612762 root.createMessage({
613- ports: [port]
763+ ports: [portRef1]
614764 })
615765 } catch (e) {
616766 t.pass('should thow if sending a port that is bound')
617767 }
618768
619769 try {
620- await root.ports.bind('test', port)
770+ await root.ports.bind('test', portRef1)
621771 } catch (e) {
622772 t.pass('should thow if binding an already bound port')
623773 }
624774
625775 try {
626- let port2 = root.ports.create('base')
627- await root.ports.bind('test', port2)
776+ const [portRef3] = root.ports.createChannel()
777+ await root.ports.bind('test', portRef3)
628778 } catch (e) {
629779 t.pass('should thow if binding an already bound name')
630780 }
631781
632782 await root.ports.unbind('test')
633- const message = root.createMessage({ports: [port]})
634- t.equals(message.ports[0], port, 'should create a message if the port is unbound')
783+ const message = root.createMessage({
784+ ports: [portRef1]
785+ })
786+ t.equals(message.ports[0], portRef1, 'should create a message if the port is unbound')
635787 })
636788
637789 tape('port deletion', async t => {
638790 const expectedSr = {
639791 '/': 'zdpuB2QXxn1KQtLFfBqaritTRoe5BuKP5sNFSrPtRT6sxkY7Z'
640792 }
641793 class Root extends BaseContainer {
642794 run (m) {
643- const one = this.exInterface.ports.create('first')
644- this.exInterface.ports.bind('one', one)
645- this.exInterface.send(one, this.exInterface.createMessage())
795+ const [portRef1, portRef2] = this.exInterface.ports.createChannel()
796+ this.exInterface.ports.bind('one', portRef1)
797+ const message1 = this.exInterface.createMessage({
798+ ports: [portRef2]
799+ })
800+
801+ this.exInterface.ports.create('first', message1)
802+ this.exInterface.send(portRef1, this.exInterface.createMessage())
646803 this.exInterface.incrementTicks(6)
647804 }
648805 }
649806
@@ -659,11 +816,17 @@
659816 hypervisor.registerContainer('root', Root)
660817 hypervisor.registerContainer('first', First)
661818
662819 const root = await hypervisor.createInstance('root')
663- const port = root.ports.create('root')
664- root.ports.bind('first', port)
665- root.send(port, root.createMessage())
820+ const [portRef1, portRef2] = root.ports.createChannel()
821+ root.ports.bind('first', portRef1)
822+ root.ports.create('root', root.createMessage({
823+ ports: [portRef2]
824+ }))
825+
826+ const message = root.createMessage()
827+ root.send(portRef1, message)
828+
666829 const sr = await hypervisor.createStateRoot()
667830 t.deepEquals(sr, expectedSr, 'should produce the corret state root')
668831
669832 t.end()
@@ -683,11 +846,16 @@
683846
684847 hypervisor.registerContainer('root', Root)
685848
686849 const root = await hypervisor.createInstance('root')
687- const port = root.ports.create('root')
688- root.ports.bind('first', port)
689- root.send(port, root.createMessage())
850+ const [portRef1, portRef2] = root.ports.createChannel()
851+ root.ports.bind('first', portRef1)
852+ root.ports.create('root', root.createMessage({
853+ ports: [portRef2]
854+ }))
855+
856+ const message = root.createMessage()
857+ root.send(portRef1, message)
690858 const sr = await hypervisor.createStateRoot()
691859 t.deepEquals(sr, expectedSr, 'should produce the corret state root')
692860
693861 t.end()
@@ -698,17 +866,23 @@
698866 '/': 'zdpuB2QXxn1KQtLFfBqaritTRoe5BuKP5sNFSrPtRT6sxkY7Z'
699867 }
700868 class Root extends BaseContainer {
701869 run (m) {
702- this.exInterface.ports.create('sub')
870+ const [, portRef2] = this.exInterface.ports.createChannel()
871+ this.exInterface.ports.create('sub', this.exInterface.createMessage({
872+ ports: [portRef2]
873+ }))
703874 }
704875 }
705876
706877 class Sub extends BaseContainer {
707878 initailize (message) {
708879 this.exInterface.ports.bind('root', message.ports[0])
709- const port = this.exInterface.ports.create('root')
710- this.exInterface.ports.bind('child', port)
880+ const [portRef1, portRef2] = root.ports.createChannel()
881+ root.ports.bind('child', portRef1)
882+ root.ports.create('root', root.createMessage({
883+ ports: [portRef2]
884+ }))
711885 }
712886 }
713887
714888 const hypervisor = new Hypervisor(node.dag)
@@ -716,14 +890,18 @@
716890 hypervisor.registerContainer('root', Root)
717891 hypervisor.registerContainer('sub', Sub)
718892
719893 const root = await hypervisor.createInstance('root')
720- const port = root.ports.create('root')
721- root.ports.bind('first', port)
722- root.send(port, root.createMessage())
894+ const [portRef1, portRef2] = root.ports.createChannel()
895+ root.ports.bind('first', portRef1)
896+ root.ports.create('root', root.createMessage({
897+ ports: [portRef2]
898+ }))
899+
900+ root.send(portRef1, root.createMessage())
723901 const sr = await hypervisor.createStateRoot()
902+
724903 t.deepEquals(sr, expectedSr, 'should produce the corret state root')
725-
726904 t.end()
727905 })
728906
729907 tape('should not remove connected nodes', async t => {
@@ -735,15 +913,23 @@
735913 if (m.ports.length) {
736914 const port = this.exInterface.ports.get('test1')
737915 this.exInterface.send(port, m)
738916 this.exInterface.ports.unbind('test1')
739- // this.exInterface.ports.unbind('test2')
740917 } else {
741- const port1 = this.exInterface.ports.create('sub')
742- this.exInterface.ports.bind('test1', port1)
743- const port2 = this.exInterface.ports.create('sub')
744- this.exInterface.ports.bind('test2', port2)
745- this.exInterface.send(port2, this.exInterface.createMessage({data: 'getChannel'}))
918+ const [portRef1, portRef2] = this.exInterface.ports.createChannel()
919+ this.exInterface.ports.create('sub', this.exInterface.createMessage({
920+ ports: [portRef2]
921+ }))
922+ this.exInterface.ports.bind('test1', portRef1)
923+
924+ const [portRef3, portRef4] = this.exInterface.ports.createChannel()
925+ this.exInterface.ports.create('sub', this.exInterface.createMessage({
926+ ports: [portRef4]
927+ }))
928+ this.exInterface.ports.bind('test2', portRef3)
929+ this.exInterface.send(portRef3, this.exInterface.createMessage({
930+ data: 'getChannel'
931+ }))
746932 }
747933 }
748934 }
749935
@@ -767,14 +953,18 @@
767953 hypervisor.registerContainer('root', Root)
768954 hypervisor.registerContainer('sub', Sub)
769955
770956 const root = await hypervisor.createInstance('root')
771- const port = root.ports.create('root')
772- root.ports.bind('first', port)
773- root.send(port, root.createMessage())
957+ const [portRef1, portRef2] = root.ports.createChannel()
958+ root.ports.bind('first', portRef1)
959+ root.ports.create('root', root.createMessage({
960+ ports: [portRef2]
961+ }))
962+
963+ root.send(portRef1, root.createMessage())
774964 const sr = await hypervisor.createStateRoot()
775965 t.deepEquals(sr, expectedSr, 'should produce the corret state root')
776- // await hypervisor.graph.tree(sr, Infinity)
966+ // await hypervisor.graph.tree(sr, Infinity)
777967
778968 t.end()
779969 })
780970
@@ -789,13 +979,22 @@
789979 this.exInterface.send(port, m)
790980 this.exInterface.ports.unbind('test1')
791981 this.exInterface.ports.unbind('test2')
792982 } else {
793- const port1 = this.exInterface.ports.create('sub')
794- this.exInterface.ports.bind('test1', port1)
795- const port2 = this.exInterface.ports.create('sub')
796- this.exInterface.ports.bind('test2', port2)
797- this.exInterface.send(port2, this.exInterface.createMessage({data: 'getChannel'}))
983+ const [portRef1, portRef2] = this.exInterface.ports.createChannel()
984+ this.exInterface.ports.create('sub', this.exInterface.createMessage({
985+ ports: [portRef2]
986+ }))
987+ this.exInterface.ports.bind('test1', portRef1)
988+
989+ const [portRef3, portRef4] = this.exInterface.ports.createChannel()
990+ this.exInterface.ports.create('sub', this.exInterface.createMessage({
991+ ports: [portRef4]
992+ }))
993+ this.exInterface.ports.bind('test2', portRef3)
994+ this.exInterface.send(portRef3, this.exInterface.createMessage({
995+ data: 'getChannel'
996+ }))
798997 }
799998 }
800999 }
8011000
@@ -819,11 +1018,17 @@
8191018 hypervisor.registerContainer('root', Root)
8201019 hypervisor.registerContainer('sub', Sub)
8211020
8221021 const root = await hypervisor.createInstance('root')
823- const port = root.ports.create('root')
824- root.ports.bind('first', port)
825- root.send(port, root.createMessage())
1022+
1023+ const [portRef1, portRef2] = root.ports.createChannel()
1024+ root.ports.bind('first', portRef1)
1025+ root.ports.create('root', root.createMessage({
1026+ ports: [portRef2]
1027+ }))
1028+
1029+ root.send(portRef1, root.createMessage())
1030+
8261031 const sr = await hypervisor.createStateRoot()
8271032 t.deepEquals(sr, expectedSr, 'should produce the corret state root')
8281033
8291034 t.end()

Built with git-ssb-web