git ssb

0+

wanderer🌟 / js-primea-hypervisor



Commit 9fbf8df01199fda8b2e900caefc75b880c9e095e

fixed tests

wanderer committed on 6/20/2017, 5:21:04 AM
Parent: 4be3537c21407395dbf5e91fc51c42b49f9bfbe5

Files changed

exoInterface.jschanged
index.jschanged
portManager.jschanged
scheduler.jschanged
tests/index.jschanged
exoInterface.jsView
@@ -43,43 +43,52 @@
4343 }
4444 }
4545 }
4646
47- _updateContainerState (containerState, message) {
48- this.containerState = containerState
49- }
47+ async _runNextMessage () {
48+ if (!this.ports.isSaturated()) {
49+ await this.hypervisor.scheduler.wait(this.ticks, this.id)
50+ }
5051
51- async _runNextMessage () {
5252 if (this.ports.hasMessages()) {
53- await this.hypervisor.scheduler.wait(this.ticks)
5453 const message = this.ports.nextMessage()
55- this.ticks = message._ticks
56- this.hypervisor.scheduler.update(this, this.ticks)
54+ if (this.ticks < message._fromTicks) {
55+ this.ticks = message._fromTicks
56+ this.hypervisor.scheduler.update(this)
57+ }
5758 this.currentMessage = message
58- // run the next message
59+
60+ // run the next message
5961 this.run(message)
6062 } else {
6163 // if no more messages then shut down
6264 this.hypervisor.scheduler.done(this)
6365 }
6466 }
67+
6568 /**
6669 * run the kernels code with a given enviroment
6770 * The Kernel Stores all of its state in the Environment. The Interface is used
6871 * to by the VM to retrive infromation from the Environment.
6972 * @returns {Promise}
7073 */
7174 async run (message, init = false) {
7275 let result
73- const method = init ? 'initailize' : 'run'
74- try {
75- result = await this.container[method](message) || {}
76- } catch (e) {
77- result = {
78- exception: true,
79- exceptionError: e
76+ if (message.data === 'delete') {
77+ this.ports._delete(message.portName)
78+ } else {
79+ const method = init ? 'initailize' : 'run'
80+
81+ try {
82+ result = await this.container[method](message) || {}
83+ } catch (e) {
84+ result = {
85+ exception: true,
86+ exceptionError: e
87+ }
8088 }
8189 }
90+ // message.response(result)
8291 this._runNextMessage()
8392 return result
8493 }
8594
@@ -88,9 +97,9 @@
8897 * @param {Number} count - the number of ticks to add
8998 */
9099 incrementTicks (count) {
91100 this.ticks += count
92- this.hypervisor.scheduler.update(this, this.ticks)
101+ this.hypervisor.scheduler.update(this)
93102 }
94103
95104 /**
96105 * creates a new message
@@ -112,9 +121,14 @@
112121 * @param {Message} message - the message
113122 */
114123 async send (port, message) {
115124 // set the port that the message came from
116- message._fromPortTicks = this.ticks
125+ message._fromTicks = this.ticks
126+
127+ // if (this.currentMessage !== message && !message.responsePort) {
128+ // this.currentMessage._addSubMessage(message)
129+ // }
130+
117131 if (port.destId) {
118132 const id = port.destId
119133 const instance = await this.hypervisor.getInstance(id)
120134 instance.queue(port.destName, message)
index.jsView
@@ -49,10 +49,10 @@
4949 id: id
5050 })
5151
5252 // save the newly created instance
53+ this.scheduler.releaseLock(lock)
5354 this.scheduler.update(exoInterface)
54- this.scheduler.releaseLock(lock)
5555 return exoInterface
5656 }
5757
5858 async createInstance (type, code, entryPorts = [], id = {nonce: 0, parent: null}) {
@@ -79,9 +79,9 @@
7979 * ticks
8080 * @param {Number} ticks the number of ticks at which to create the state root
8181 * @returns {Promise}
8282 */
83- async createStateRoot (ticks) {
83+ async createStateRoot (ticks = Infinity) {
8484 await this.scheduler.wait(ticks)
8585 return this.graph.flush(this._state)
8686 }
8787
portManager.jsView
@@ -11,10 +11,10 @@
1111 return nameA
1212 }
1313
1414 // order by number of ticks if messages have different number of ticks
15- if (a._fromPortTicks !== b._fromPortTicks) {
16- return a._fromPortTicks < b._fromPortTicks ? nameA : nameB
15+ if (a._fromTicks !== b._fromTicks) {
16+ return a._fromTicks < b._fromTicks ? nameA : nameB
1717 } else if (a.priority !== b.priority) {
1818 // decide by priority
1919 return a.priority > b.priority ? nameA : nameB
2020 } else {
@@ -36,15 +36,12 @@
3636 */
3737 constructor (opts) {
3838 Object.assign(this, opts)
3939 this.ports = this.state.ports
40- this._unboundPorts = new WeakSet()
40+ this._unboundPorts = new Set()
4141 this._waitingPorts = {}
4242 }
4343
44- addUnboundedPorts (ports) {
45- }
46-
4744 /**
4845 * binds a port to a name
4946 * @param {Object} port - the port to bind
5047 * @param {String} name - the name of the port
@@ -53,28 +50,31 @@
5350 if (this.isBound(port)) {
5451 throw new Error('cannot bind a port that is already bound')
5552 } else if (this.ports[name]) {
5653 throw new Error('cannot bind port to a name that is alread bound')
57- }
54+ } else {
55+ this._unboundPorts.delete(port)
5856
59- // save the port instance
60- this.ports[name] = port
57+ // save the port instance
58+ this.ports[name] = port
6159
62- // update the dest port
63- const destPort = await this.hypervisor.getDestPort(port)
64- destPort.destName = name
65- destPort.destId = this.id
66- delete destPort.destPort
60+ // update the dest port
61+ const destPort = await this.hypervisor.getDestPort(port)
62+ destPort.destName = name
63+ destPort.destId = this.id
64+ delete destPort.destPort
65+ }
6766 }
6867
6968 /**
7069 * unbinds a port given its name
7170 * @param {String} name
7271 * @returns {boolean} whether or not the port was deleted
7372 */
74- async unbind (name, del) {
73+ async unbind (name) {
7574 const port = this.ports[name]
7675 delete this.ports[name]
76+ this._unboundPorts.add(port)
7777
7878 let destPort = port.destPort
7979 // if the dest is unbound
8080 if (destPort) {
@@ -82,16 +82,24 @@
8282 delete destPort.destId
8383 } else {
8484 destPort = await this.hypervisor.getDestPort(port)
8585 }
86- if (del) {
87- delete destPort.destPort
88- } else {
89- destPort.destPort = port
90- return port
91- }
86+ destPort.destPort = port
87+ return port
9288 }
9389
90+ delete (name) {
91+
92+ }
93+
94+ _deleteDestPort (port) {
95+ this.exInterface.send(port, 'delete')
96+ }
97+
98+ _delete (name) {
99+ delete this.ports[name]
100+ }
101+
94102 /**
95103 * check if a port object is still valid
96104 * @param {Object} port
97105 * @return {Boolean}
@@ -107,8 +115,9 @@
107115 queue (name, message) {
108116 message.ports.forEach(port => {
109117 this._unboundPorts.add(port)
110118 })
119+
111120 const resolve = this._waitingPorts[name]
112121 if (resolve) {
113122 resolve(message)
114123 } else if (name) {
@@ -190,12 +199,20 @@
190199 * @param {Array} ports
191200 * @returns {Promise}
192201 */
193202 nextMessage () {
194- const portName = Object.keys(this.ports).reduce(messageArbiter)
195- return this.ports[portName].messages.shift()
203+ const portName = Object.keys(this.ports).reduce(messageArbiter.bind(this))
204+ const port = this.ports[portName]
205+ const message = port.messages.shift()
206+ message._fromPort = port
207+ message.fromName = portName
208+ return message
196209 }
197210
198211 hasMessages () {
199212 return Object.keys(this.ports).some(name => this.ports[name].messages.length)
200213 }
214+
215+ isSaturated () {
216+ return Object.keys(this.ports).every(name => this.ports[name].messages.length)
217+ }
201218 }
scheduler.jsView
@@ -20,13 +20,13 @@
2020 releaseLock (id) {
2121 this.locks.delete(id)
2222 }
2323
24- update (instance, ticks = this.oldest()) {
24+ update (instance) {
2525 this.instances.delete(instance.id)
2626 const instanceArray = [...this.instances]
2727 binarySearchInsert(instanceArray, comparator, [instance.id, {
28- ticks: ticks,
28+ ticks: instance.ticks,
2929 instance: instance
3030 }])
3131 this.instances = new Map(instanceArray)
3232 this._checkWaits()
@@ -43,46 +43,48 @@
4343 this.instances.delete(instance.id)
4444 this._checkWaits()
4545 }
4646
47- wait (ticks) {
48- if (ticks <= this.oldest() || !this.isRunning()) {
47+ wait (ticks, id) {
48+ if (!this.locks.size && (ticks <= this.smallest() || !this.isRunning())) {
4949 return
5050 } else {
5151 return new Promise((resolve, reject) => {
5252 binarySearchInsert(this._waits, comparator, {
5353 ticks: ticks,
54- resolve: resolve
54+ resolve: resolve,
55+ id: id
5556 })
5657 })
5758 }
5859 }
5960
60- oldest () {
61- const oldest = [...this.instances][0]
62- return oldest ? oldest[1].ticks : 0
61+ smallest () {
62+ const smallest = [...this.instances][0]
63+ return smallest ? smallest[1].ticks : 0
6364 }
6465
6566 _checkWaits () {
66- if (!this.isRunning()) {
67- // clear any remanding waits
68- this._waits.forEach(wait => {
69- wait.resolve()
70- })
71- this._waits = []
72- } else {
73- const oldest = this.oldest()
74- for (const wait in this._waits) {
75- if (wait.ticks <= oldest) {
76- wait.resolve()
77- this._waits.shift()
78- } else {
79- break
67+ if (!this.locks.size) {
68+ if (!this.isRunning()) {
69+ // clear any remanding waits
70+ this._waits.forEach(wait => wait.resolve())
71+ this._waits = []
72+ } else {
73+ const smallest = this.smallest()
74+ for (const index in this._waits) {
75+ const wait = this._waits[index]
76+ if (wait.ticks <= smallest) {
77+ wait.resolve()
78+ } else {
79+ this._waits.splice(0, index)
80+ break
81+ }
8082 }
8183 }
8284 }
8385 }
8486
8587 isRunning () {
86- return this.instances.size || this.locks.size
88+ return this.instances.size
8789 }
8890 }
tests/index.jsView
@@ -10,8 +10,16 @@
1010 class BaseContainer {
1111 constructor (exInterface) {
1212 this.exInterface = exInterface
1313 }
14+
15+ initailize (message) {
16+ // console.log('- init', this.exInterface.id)
17+ const port = message.ports[0]
18+ if (port) {
19+ this.exInterface.ports.bind('root', port)
20+ }
21+ }
1422 }
1523
1624 node.on('ready', () => {
1725 tape('basic', async t => {
@@ -21,14 +29,8 @@
2129 '/': 'zdpuAyGKaZ3nbBQdgESbEgVYr81TcAFB6LE2MQQPWLZaYxuF3'
2230 }
2331
2432 class testVMContainer extends BaseContainer {
25- initailize (message) {
26- const port = message.ports[0]
27- if (port) {
28- this.exInterface.ports.bind('root', port)
29- }
30- }
3133 run (m) {
3234 t.true(m === message, 'should recive a message')
3335 }
3436 }
@@ -49,19 +51,14 @@
4951 tape('one child contract', async t => {
5052 t.plan(4)
5153 let message
5254 const expectedState = {
53- '/': 'zdpuAofSzrBqwYs6z1r28fMeb8z5oSKF6CcWA6m22RqazgoTB'
55+ '/': 'zdpuAtVcH6MUnvt2RXnLsDXyLB3CBSQ7aydfh2ogSKGCejJCQ'
5456 }
5557 let hasResolved = false
5658
5759 class testVMContainer2 extends BaseContainer {
58- async initailize (m) {
59- console.log('init')
60- await this.exInterface.ports.bind('root', m.ports[0])
61- }
6260 run (m) {
63- console.log('here')
6461 t.true(m === message, 'should recive a message')
6562 return new Promise((resolve, reject) => {
6663 setTimeout(() => {
6764 this.exInterface.incrementTicks(1)
@@ -72,80 +69,69 @@
7269 }
7370 }
7471
7572 class testVMContainer extends BaseContainer {
76- async initailize (m) {
77- const port = m.ports[0]
78- if (port) {
79- await this.exInterface.ports.bind('root', port)
80- }
81- }
82- async run (m) {
83- const port = await this.exInterface.ports.create('test2')
84- await this.exInterface.ports.bind('child', port)
85- await this.exInterface.send(port, m)
73+ run (m) {
74+ const port = this.exInterface.ports.create('test2')
75+ this.exInterface.ports.bind('child', port)
76+ this.exInterface.send(port, m)
8677 this.exInterface.incrementTicks(1)
87- console.log('run')
8878 }
8979 }
9080
91- try {
92- const hypervisor = new Hypervisor(node.dag)
93- hypervisor.registerContainer('test', testVMContainer)
94- hypervisor.registerContainer('test2', testVMContainer2)
81+ const hypervisor = new Hypervisor(node.dag)
82+ hypervisor.registerContainer('test', testVMContainer)
83+ hypervisor.registerContainer('test2', testVMContainer2)
9584
96- let root = await hypervisor.createInstance('test')
97- let port = root.ports.create('test')
85+ let root = await hypervisor.createInstance('test')
86+ const rootId = root.id
87+ let port = root.ports.create('test')
9888
99- await root.ports.bind('first', port)
100- message = root.createMessage()
89+ root.ports.bind('first', port)
90+ message = root.createMessage()
10191
102- await root.send(port, message)
103- console.log('state', hypervisor._state)
104- const stateRoot = await hypervisor.createStateRoot(Infinity)
105- // t.true(hasResolved, 'should resolve before generating the state root')
106- // t.deepEquals(stateRoot, expectedState, 'expected state')
107- } catch (e) {
108- console.log(e)
92+ root.send(port, message)
93+ const stateRoot = await hypervisor.createStateRoot(Infinity)
94+ t.true(hasResolved, 'should resolve before generating the state root')
95+ t.deepEquals(stateRoot, expectedState, 'expected state')
96+ // await hypervisor.graph.tree(hypervisor._state, Infinity)
97+ // console.log(JSON.stringify(hypervisor._state, null, 2))
98+ // test reviving the state
99+ class testVMContainer3 extends BaseContainer {
100+ run (m) {
101+ const port = this.exInterface.ports.get('child')
102+ this.exInterface.send(port, m)
103+ this.exInterface.incrementTicks(1)
104+ }
109105 }
110106
111- // test reviving the state
112- // class testVMContainer3 extends BaseContainer {
113- // async run (m) {
114- // const port = this.exInterface.ports.get('child')
115- // await this.exInterface.send(port, m)
116- // this.kernel.incrementTicks(1)
117- // }
118- // }
119-
120- // hypervisor.registerContainer('test', testVMContainer3)
121- // root = await hypervisor.createInstance('test', stateRoot)
122- // port = root.ports.get('first')
123-
124- // root.send(port, message)
107+ hypervisor.registerContainer('test', testVMContainer3)
108+ root = await hypervisor.getInstance(rootId)
109+ port = root.ports.get('first')
110+ root.send(port, message)
125111 })
126112
127113 tape.skip('ping pong', async t => {
128114 class Ping extends BaseContainer {
129115 async run (m) {
130- let port = this.kernel.ports.get('child')
116+ let port = this.exInterface.ports.get('child')
131117 if (!port) {
132- port = this.kernel.ports.create('pong')
133- this.kernel.ports.bind(port, 'child')
118+ port = this.exInterface.ports.create('pong')
119+ this.exInterface.ports.bind(port, 'child')
134120 }
135121
136- if (this.kernel.ticks < 100) {
137- this.kernel.incrementTicks(1)
138- return this.kernel.send(port, this.kernel.createMessage())
122+ if (this.exInterface.ticks < 100) {
123+ this.exInterface.incrementTicks(1)
124+ return this.exInterface.send(port, this.exInterface.createMessage())
139125 }
140126 }
141127 }
142128
143129 class Pong extends BaseContainer {
144130 run (m) {
145131 const port = m.fromPort
146- this.kernel.incrementTicks(2)
147- return this.kernel.send(port, this.kernel.createMessage())
132+ this.exInterface.incrementTicks(2)
133+ return this.exInterface.send(port, this.exInterface.createMessage())
148134 }
149135 }
150136
151137 const hypervisor = new Hypervisor(node.dag)
@@ -161,74 +147,72 @@
161147
162148 t.end()
163149 })
164150
165- tape.skip('queing multiple messages', async t => {
166- t.plan(2)
167- let runs = 0
151+ // tape('queing multiple messages', async t => {
152+ // t.plan(2)
153+ // let runs = 0
154+ // const stateRoot = {
155+ // '/': 'zdpuAoNgKdoeLeGQz9AwBr9xfi6MZNAiHUW2WPGQF7JiBofzg'
156+ // }
168157
169- class Root extends BaseContainer {
170- async run (m) {
171- const one = this.kernel.ports.create('child')
172- const two = this.kernel.ports.create('child')
173- const three = this.kernel.ports.create('child')
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')
174163
175- this.kernel.ports.bind(one, 'one')
176- this.kernel.ports.bind(two, 'two')
177- this.kernel.ports.bind(three, 'three')
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())
178170
179- await Promise.all([
180- this.kernel.send(one, this.kernel.createMessage()),
181- this.kernel.send(two, this.kernel.createMessage()),
182- this.kernel.send(three, this.kernel.createMessage())
183- ])
171+ // return new Promise((resolve, reject) => {
172+ // setTimeout(() => {
173+ // this.exInterface.incrementTicks(6)
174+ // resolve()
175+ // }, 200)
176+ // })
177+ // }
178+ // }
184179
185- return new Promise((resolve, reject) => {
186- setTimeout(() => {
187- this.kernel.incrementTicks(6)
188- resolve()
189- }, 200)
190- })
191- }
192- }
180+ // class Child extends BaseContainer {
181+ // run (m) {
182+ // runs++
183+ // this.exInterface.incrementTicks(2)
184+ // }
185+ // }
193186
194- class Child extends BaseContainer {
195- run (m) {
196- runs++
197- this.kernel.incrementTicks(2)
198- }
199- }
187+ // const hypervisor = new Hypervisor(node.dag)
200188
201- const hypervisor = new Hypervisor(node.dag)
189+ // hypervisor.registerContainer('root', Root)
190+ // hypervisor.registerContainer('child', Child)
202191
203- hypervisor.registerContainer('root', Root)
204- hypervisor.registerContainer('child', Child)
192+ // const root = await hypervisor.createInstance('root')
193+ // const port = root.ports.create('root')
194+ // root.ports.bind('first', port)
205195
206- const root = await hypervisor.createInstance('root')
207- const port = root.ports.create('root')
208- root.ports.bind(port, 'first')
196+ // await root.send(port, root.createMessage())
197+ // await hypervisor.scheduler.wait(Infinity)
209198
210- await root.send(port, root.createMessage())
211- await root.wait(Infinity)
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+ // })
212203
213- t.equals(runs, 3, 'the number of run should be 3')
214- const nonce = await hypervisor.graph.get(root.state, 'ports/first/link/nonce/0')
215- t.equals(nonce, 3, 'should have the correct nonce')
216-
217- await hypervisor.graph.tree(root.state, Infinity)
218- })
219-
220- tape.skip('traps', async t => {
204+ tape('traps', async t => {
221205 t.plan(1)
222206 class Root extends BaseContainer {
223207 async run (m) {
224- const one = this.kernel.ports.create('child')
225- const two = this.kernel.ports.create('child')
226- const three = this.kernel.ports.create('child')
208+ const one = this.exInterface.ports.create('root')
209+ const two = this.exInterface.ports.create('root')
210+ const three = this.exInterface.ports.create('root')
227211
228- this.kernel.ports.bind(one, 'one')
229- this.kernel.ports.bind(two, 'two')
230- this.kernel.ports.bind(three, 'three')
212+ this.exInterface.ports.bind('one', one)
213+ this.exInterface.ports.bind('two', two)
214+ this.exInterface.ports.bind('three', three)
231215
232216 throw new Error('it is a trap!!!')
233217 }
234218 }
@@ -236,57 +220,55 @@
236220 const hypervisor = new Hypervisor(node.dag)
237221
238222 hypervisor.registerContainer('root', Root)
239223 const root = await hypervisor.createInstance('root')
240- await root.run()
224+ await root.run(root.createMessage())
225+ // console.log('here', hypervisor.scheduler)
226+ const stateRoot = await hypervisor.createStateRoot()
241227
242- t.deepEquals(root.state, {
243- '/': {
244- nonce: [0],
245- ports: {}
246- }
228+ t.deepEquals(stateRoot, {
229+ '/': 'zdpuAwrMmQXqFusve7zcRYxVUuji4NVzZR5GyjwyStsjteCoW'
247230 }, 'should revert the state')
248231 })
249232
250- tape.skip('message should arrive in the correct oder if sent in order', async t => {
233+ tape('message should arrive in the correct oder if sent in order', async t => {
251234 t.plan(2)
235+ let runs = 0
252236
253237 class Root extends BaseContainer {
254- async run (m) {
255- if (!this.runs) {
256- this.runs = 1
257- const one = this.kernel.ports.create('first')
258- const two = this.kernel.ports.create('second')
238+ run (m) {
239+ if (!runs) {
240+ runs++
241+ const one = this.exInterface.ports.create('first')
242+ const two = this.exInterface.ports.create('second')
259243
260- this.kernel.ports.bind(one, 'one')
261- this.kernel.ports.bind(two, 'two')
244+ this.exInterface.ports.bind('one', one)
245+ this.exInterface.ports.bind('two', two)
262246
263- await Promise.all([
264- this.kernel.send(one, this.kernel.createMessage()),
265- this.kernel.send(two, this.kernel.createMessage())
266- ])
247+ this.exInterface.send(one, this.exInterface.createMessage())
248+ this.exInterface.send(two, this.exInterface.createMessage())
267249
268- this.kernel.incrementTicks(6)
269- } else if (this.runs === 1) {
270- this.runs++
250+ this.exInterface.incrementTicks(6)
251+ } else if (runs === 1) {
252+ runs++
271253 t.equals(m.data, 'first', 'should recive the first message')
272- } else if (this.runs === 2) {
254+ } else if (runs === 2) {
273255 t.equals(m.data, 'second', 'should recived the second message')
274256 }
275257 }
276258 }
277259
278260 class First extends BaseContainer {
279- async run (m) {
280- this.kernel.incrementTicks(1)
281- await this.kernel.send(m.fromPort, this.kernel.createMessage({data: 'first'}))
261+ run (m) {
262+ this.exInterface.incrementTicks(1)
263+ this.exInterface.send(m.fromPort, this.exInterface.createMessage({data: 'first'}))
282264 }
283265 }
284266
285267 class Second extends BaseContainer {
286- async run (m) {
287- this.kernel.incrementTicks(2)
288- await this.kernel.send(m.fromPort, this.kernel.createMessage({data: 'second'}))
268+ run (m) {
269+ this.exInterface.incrementTicks(2)
270+ this.exInterface.send(m.fromPort, this.exInterface.createMessage({data: 'second'}))
289271 }
290272 }
291273
292274 const hypervisor = new Hypervisor(node.dag)
@@ -296,163 +278,166 @@
296278 hypervisor.registerContainer('second', Second)
297279
298280 const root = await hypervisor.createInstance('root')
299281 const port = root.ports.create('root')
300- root.ports.bind(port, 'first')
282+ root.ports.bind('first', port)
301283
302284 root.send(port, root.createMessage())
303285 })
304286
305- tape.skip('message should arrive in the correct order, even if sent out of order', async t => {
306- t.plan(2)
287+ // tape('message should arrive in the correct order, even if sent out of order', async t => {
288+ // t.plan(2)
307289
308- class Root extends BaseContainer {
309- run (m) {
310- if (!this.runs) {
311- this.runs = 1
312- const one = this.kernel.ports.create('first')
313- const two = this.kernel.ports.create('second')
290+ // let runs = 0
314291
315- this.kernel.ports.bind(one, 'one')
316- this.kernel.ports.bind(two, 'two')
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')
317298
318- return Promise.all([
319- this.kernel.send(one, this.kernel.createMessage()),
320- this.kernel.send(two, this.kernel.createMessage())
321- ])
322- } else if (this.runs === 1) {
323- this.runs++
324- t.equals(m.data, 'second', 'should recive the first message')
325- } else if (this.runs === 2) {
326- t.equals(m.data, 'first', 'should recived the second message')
327- }
328- }
329- }
299+ // this.exInterface.ports.bind('one', one)
300+ // this.exInterface.ports.bind('two', two)
330301
331- class First extends BaseContainer {
332- run (m) {
333- this.kernel.incrementTicks(2)
334- return this.kernel.send(m.fromPort, this.kernel.createMessage({data: 'first'}))
335- }
336- }
302+ // this.exInterface.send(two, this.exInterface.createMessage())
303+ // this.exInterface.send(one, this.exInterface.createMessage())
337304
338- class Second extends BaseContainer {
339- run (m) {
340- this.kernel.incrementTicks(1)
341- this.kernel.send(m.fromPort, this.kernel.createMessage({data: 'second'}))
342- }
343- }
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+ // }
344314
345- const hypervisor = new Hypervisor(node.dag)
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+ // }
346321
347- hypervisor.registerContainer('root', Root)
348- hypervisor.registerContainer('first', First)
349- hypervisor.registerContainer('second', Second)
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+ // }
350328
351- const root = await hypervisor.createInstance('root')
352- const port = root.ports.create('root')
353- root.ports.bind(port, 'first')
329+ // const hypervisor = new Hypervisor(node.dag)
354330
355- root.send(port, root.createMessage())
356- })
331+ // hypervisor.registerContainer('root', Root)
332+ // hypervisor.registerContainer('first', First)
333+ // hypervisor.registerContainer('second', Second)
357334
358- tape.skip('message should arrive in the correct order, even in a tie of ticks', async t => {
359- t.plan(2)
335+ // const root = await hypervisor.createInstance('root')
336+ // const port = root.ports.create('root')
337+ // root.ports.bind('first', port)
360338
361- class Root extends BaseContainer {
362- async run (m) {
363- if (!this.runs) {
364- this.runs = 1
365- const one = this.kernel.ports.create('first')
366- const two = this.kernel.ports.create('second')
339+ // root.send(port, root.createMessage())
340+ // })
367341
368- this.kernel.ports.bind(one, 'one')
369- this.kernel.ports.bind(two, 'two')
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
370345
371- await Promise.all([
372- this.kernel.send(one, this.kernel.createMessage()),
373- this.kernel.send(two, this.kernel.createMessage())
374- ])
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')
375352
376- this.kernel.incrementTicks(6)
377- } else if (this.runs === 1) {
378- this.runs++
379- t.equals(m.data, 'first', 'should recived the second message')
380- } else if (this.runs === 2) {
381- t.equals(m.data, 'second', 'should recive the first message')
382- }
383- }
384- }
353+ // this.exInterface.ports.bind('one', one)
354+ // this.exInterface.ports.bind('two', two)
385355
386- class First extends BaseContainer {
387- run (m) {
388- this.kernel.incrementTicks(2)
389- return this.kernel.send(m.fromPort, this.kernel.createMessage({
390- data: 'first'
391- }))
392- }
393- }
356+ // this.exInterface.send(one, this.exInterface.createMessage())
357+ // this.exInterface.send(two, this.exInterface.createMessage())
394358
395- class Second extends BaseContainer {
396- run (m) {
397- this.kernel.incrementTicks(2)
398- return this.kernel.send(m.fromPort, this.kernel.createMessage({
399- data: 'second'
400- }))
401- }
402- }
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+ // }
403368
404- const hypervisor = new Hypervisor(node.dag)
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+ // }
405377
406- hypervisor.registerContainer('root', Root)
407- hypervisor.registerContainer('first', First)
408- hypervisor.registerContainer('second', Second)
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+ // }
409386
410- const root = await hypervisor.createInstance('root')
411- const port = await root.ports.create('root')
412- root.ports.bind(port, 'first')
413- root.send(port, root.createMessage())
414- })
387+ // const hypervisor = new Hypervisor(node.dag)
415388
416- tape.skip('message should arrive in the correct order, even in a tie of ticks', async t => {
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 => {
417400 t.plan(2)
418401
402+ let runs = 0
403+
419404 class Root extends BaseContainer {
420405 run (m) {
421- if (!this.runs) {
422- this.runs = 1
423- const two = this.kernel.ports.create('second')
424- const one = this.kernel.ports.create('first')
406+ if (!runs) {
407+ runs++
408+ const one = this.exInterface.ports.create('first')
409+ const two = this.exInterface.ports.create('second')
425410
426- this.kernel.ports.bind(two, 'two')
427- this.kernel.ports.bind(one, 'one')
411+ this.exInterface.ports.bind('two', two)
412+ this.exInterface.ports.bind('one', one)
428413
429- return Promise.all([
430- this.kernel.send(two, this.kernel.createMessage()),
431- this.kernel.send(one, this.kernel.createMessage())
432- ])
433- } else if (this.runs === 1) {
434- this.runs++
435- t.equals(m.data, 'second', 'should recive the first message')
436- } else if (this.runs === 2) {
437- t.equals(m.data, 'first', 'should recived the second message')
414+ this.exInterface.send(one, this.exInterface.createMessage())
415+ this.exInterface.send(two, this.exInterface.createMessage())
416+
417+ this.exInterface.incrementTicks(6)
418+ } else if (runs === 1) {
419+ runs++
420+ 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')
438423 }
439424 }
440425 }
441426
442427 class First extends BaseContainer {
443428 run (m) {
444- this.kernel.incrementTicks(2)
445- return this.kernel.send(m.fromPort, this.kernel.createMessage({
429+ this.exInterface.incrementTicks(2)
430+ return this.exInterface.send(m.fromPort, this.exInterface.createMessage({
446431 data: 'first'
447432 }))
448433 }
449434 }
450435
451436 class Second extends BaseContainer {
452437 run (m) {
453- this.kernel.incrementTicks(2)
454- return this.kernel.send(m.fromPort, this.kernel.createMessage({
438+ this.exInterface.incrementTicks(2)
439+ return this.exInterface.send(m.fromPort, this.exInterface.createMessage({
455440 data: 'second'
456441 }))
457442 }
458443 }
@@ -465,43 +450,45 @@
465450
466451 const root = await hypervisor.createInstance('root')
467452
468453 const port = root.ports.create('root')
469- root.ports.bind(port, 'first')
454+ root.ports.bind('first', port)
470455
471456 root.send(port, root.createMessage())
472457 })
473458
474- tape.skip('message should arrive in the correct order, with a tie in ticks but with differnt proity', async t => {
459+ tape('message should arrive in the correct order, with a tie in ticks but with differnt proity', async t => {
475460 t.plan(2)
476461
462+ let runs = 0
463+
477464 class Root extends BaseContainer {
478465 run (m) {
479- if (!this.runs) {
480- this.runs = 1
481- const one = this.kernel.ports.create('first')
482- const two = this.kernel.ports.create('second')
466+ if (!runs) {
467+ runs++
468+ const one = this.exInterface.ports.create('first')
469+ const two = this.exInterface.ports.create('second')
483470
484- this.kernel.ports.bind(one, 'one')
485- this.kernel.ports.bind(two, 'two')
471+ this.exInterface.ports.bind('one', one)
472+ this.exInterface.ports.bind('two', two)
486473
487- return Promise.all([
488- this.kernel.send(two, this.kernel.createMessage()),
489- this.kernel.send(one, this.kernel.createMessage())
490- ])
491- } else if (this.runs === 1) {
492- this.runs++
474+ this.exInterface.send(two, this.exInterface.createMessage())
475+ this.exInterface.send(one, this.exInterface.createMessage())
476+
477+ this.exInterface.incrementTicks(6)
478+ } else if (runs === 1) {
479+ runs++
493480 t.equals(m.data, 'first', 'should recive the first message')
494- } else if (this.runs === 2) {
481+ } else if (runs === 2) {
495482 t.equals(m.data, 'second', 'should recived the second message')
496483 }
497484 }
498485 }
499486
500487 class First extends BaseContainer {
501488 run (m) {
502- this.kernel.incrementTicks(2)
503- return this.kernel.send(m.fromPort, this.kernel.createMessage({
489+ this.exInterface.incrementTicks(2)
490+ return this.exInterface.send(m.fromPort, this.exInterface.createMessage({
504491 resources: {
505492 priority: 100
506493 },
507494 data: 'first'
@@ -510,10 +497,10 @@
510497 }
511498
512499 class Second extends BaseContainer {
513500 run (m) {
514- this.kernel.incrementTicks(2)
515- return this.kernel.send(m.fromPort, this.kernel.createMessage({
501+ this.exInterface.incrementTicks(2)
502+ return this.exInterface.send(m.fromPort, this.exInterface.createMessage({
516503 data: 'second'
517504 }))
518505 }
519506 }
@@ -525,9 +512,9 @@
525512 hypervisor.registerContainer('second', Second)
526513
527514 const root = await hypervisor.createInstance('root')
528515 const port = root.ports.create('root')
529- root.ports.bind(port, 'first')
516+ root.ports.bind('first', port)
530517 root.send(port, root.createMessage())
531518 })
532519
533520 tape.skip('message should arrive in the correct order, with a tie in ticks but with differnt proity', async t => {
@@ -537,17 +524,17 @@
537524 run (m) {
538525 if (!this.runs) {
539526 this.runs = 1
540527
541- const one = this.kernel.ports.create('first')
542- const two = this.kernel.ports.create('second')
528+ const one = this.exInterface.ports.create('first')
529+ const two = this.exInterface.ports.create('second')
543530
544- this.kernel.ports.bind(one, 'one')
545- this.kernel.ports.bind(two, 'two')
531+ this.exInterface.ports.bind(one, 'one')
532+ this.exInterface.ports.bind(two, 'two')
546533
547534 return Promise.all([
548- this.kernel.send(two, this.kernel.createMessage()),
549- this.kernel.send(one, this.kernel.createMessage())
535+ this.exInterface.send(two, this.exInterface.createMessage()),
536+ this.exInterface.send(one, this.exInterface.createMessage())
550537 ])
551538 } else if (this.runs === 1) {
552539 this.runs++
553540 t.equals(m.data, 'second', 'should recive the first message')
@@ -558,19 +545,19 @@
558545 }
559546
560547 class First extends BaseContainer {
561548 run (m) {
562- this.kernel.incrementTicks(2)
563- return this.kernel.send(m.fromPort, this.kernel.createMessage({
549+ this.exInterface.incrementTicks(2)
550+ return this.exInterface.send(m.fromPort, this.exInterface.createMessage({
564551 data: 'first'
565552 }))
566553 }
567554 }
568555
569556 class Second extends BaseContainer {
570557 run (m) {
571- this.kernel.incrementTicks(2)
572- return this.kernel.send(m.fromPort, this.kernel.createMessage({
558+ this.exInterface.incrementTicks(2)
559+ return this.exInterface.send(m.fromPort, this.exInterface.createMessage({
573560 resources: {
574561 priority: 100
575562 },
576563 data: 'second'
@@ -595,14 +582,14 @@
595582 class Middle extends BaseContainer {
596583 run (m) {
597584 if (!this.runs) {
598585 this.runs = 1
599- this.kernel.incrementTicks(1)
586+ this.exInterface.incrementTicks(1)
600587
601- const leaf = this.kernel.ports.create('leaf')
602- this.kernel.ports.bind(leaf, 'leaf')
588+ const leaf = this.exInterface.ports.create('leaf')
589+ this.exInterface.ports.bind(leaf, 'leaf')
603590
604- return this.kernel.send(leaf, this.kernel.createMessage())
591+ return this.exInterface.send(leaf, this.exInterface.createMessage())
605592 } else {
606593 ++this.runs
607594 if (this.runs === 3) {
608595 t.equals(m.data, 'first')
@@ -612,10 +599,10 @@
612599 }
613600
614601 class Leaf extends BaseContainer {
615602 run (m) {
616- this.kernel.incrementTicks(2)
617- return this.kernel.send(m.fromPort, this.kernel.createMessage({
603+ this.exInterface.incrementTicks(2)
604+ return this.exInterface.send(m.fromPort, this.exInterface.createMessage({
618605 data: 'first'
619606 }))
620607 }
621608 }
@@ -657,19 +644,17 @@
657644 const foundThird = await hypervisor.getInstanceByPath(root, 'first/second/third')
658645 t.equals(third, foundThird, 'should find by path')
659646 })
660647
661- tape.skip('checking ports', async t => {
662- t.plan(5)
648+ tape('checking ports', async t => {
649+ t.plan(4)
663650 const hypervisor = new Hypervisor(node.dag)
664651 hypervisor.registerContainer('base', BaseContainer)
665652
666653 const root = await hypervisor.createInstance('base')
667654 let port = root.ports.create('base')
668- root.ports.bind(port, 'test')
655+ await root.ports.bind('test', port)
669656
670- t.equals(root.ports.getBoundName(port), 'test', 'should get the ports name')
671-
672657 try {
673658 root.createMessage({
674659 ports: [port]
675660 })
@@ -677,21 +662,21 @@
677662 t.pass('should thow if sending a port that is bound')
678663 }
679664
680665 try {
681- root.ports.bind(port, 'test')
666+ await root.ports.bind('test', port)
682667 } catch (e) {
683668 t.pass('should thow if binding an already bound port')
684669 }
685670
686671 try {
687672 let port2 = root.ports.create('base')
688- root.ports.bind(port2, 'test')
673+ await root.ports.bind('test', port2)
689674 } catch (e) {
690675 t.pass('should thow if binding an already bound name')
691676 }
692677
693- root.ports.unbind('test')
678+ await root.ports.unbind('test')
694679 const message = root.createMessage({ports: [port]})
695680 t.equals(message.ports[0], port, 'should create a message if the port is unbound')
696681 })
697682 })

Built with git-ssb-web