git ssb

0+

wanderer🌟 / js-primea-hypervisor



Commit 4edd1fee9342f57e8ccc90c989eccfbf4aa9a15f

no locks on creation

Signed-off-by: wanderer <mjbecze@gmail.com>
wanderer committed on 8/10/2017, 10:40:48 PM
Parent: 25eb4441882c6d401ef140311f14d62d3ff4f9e1

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,15 @@
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 +
181 + // console.log(JSON.stringify(this.state, null, 2))
173182 return this.graph.flush(this.state)
174183 }
175184
176185 /**
kernel.jsView
@@ -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
@@ -108,15 +108,14 @@
108108 /**
109109 * clears any unbounded ports referances
110110 */
111111 clearUnboundedPorts () {
112 + const waits = []
112113 this._unboundPorts.forEach(port => {
113- this.kernel.send(port, new DeleteMessage())
114 + waits.push(this.kernel.send(port, new DeleteMessage()))
114115 })
115116 this._unboundPorts.clear()
116- if (!Object.keys(this.ports).length) {
117- this.hypervisor.addNodeToCheck(this.id)
118- }
117 + return Promise.all(waits)
119118 }
120119
121120 /**
122121 * check if a port object is still valid
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
@@ -108,34 +109,36 @@
108109 if (!this.instances.size) {
109110 // clear any remanding waits
110111 this._waits.forEach(wait => wait.resolve())
111112 this._waits = []
112- } else if (!this._running.size) {
113- // if there are no containers running find the oldest wait and update
114- // the oldest containers to it ticks
115- const oldest = this._waits[0].ticks
116- for (let instance of this.instances) {
117- instance = instance[1]
118- if (instance.ticks > oldest) {
119- break
120- } else {
121- instance.ticks = oldest
122- this._update(instance)
123- }
124- }
125- return this._checkWaits()
126113 } else {
127114 // find the old container and see if to can resolve any of the waits
128115 const oldest = this.oldest()
129116 for (const index in this._waits) {
130117 const wait = this._waits[index]
131118 if (wait.ticks <= oldest) {
132119 wait.resolve()
120 + this._running.add(wait.id)
133121 } else {
134122 this._waits.splice(0, index)
135123 break
136124 }
137125 }
126 + if (!this._running.size) {
127 + // if there are no containers running find the oldest wait and update
128 + // the oldest containers to it ticks
129 + const oldest = this._waits[0].ticks
130 + for (let instance of this.instances) {
131 + instance = instance[1]
132 + if (instance.ticks > oldest) {
133 + break
134 + } else {
135 + instance.ticks = oldest
136 + this._update(instance)
137 + }
138 + }
139 + return this._checkWaits()
140 + }
138141 }
139142 }
140143 }
141144 }
tests/index.jsView
@@ -47,9 +47,9 @@
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)
@@ -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)
@@ -110,11 +110,11 @@
110110 }
111111 }
112112
113113 class testVMContainer extends BaseContainer {
114- onMessage (m) {
114 + async onMessage (m) {
115115 const [portRef1, portRef2] = this.kernel.ports.createChannel()
116- this.kernel.createInstance(testVMContainer2.typeId, this.kernel.createMessage({
116 + await this.kernel.createInstance(testVMContainer2.typeId, this.kernel.createMessage({
117117 ports: [portRef2]
118118 }))
119119 this.kernel.incrementTicks(2)
120120 this.kernel.send(portRef1, m)
@@ -127,9 +127,9 @@
127127 hypervisor.registerContainer(testVMContainer2)
128128
129129 const root = await hypervisor.createInstance(testVMContainer.typeId)
130130 const [portRef1, portRef2] = root.ports.createChannel()
131- root.createInstance(testVMContainer.typeId, root.createMessage({
131 + await root.createInstance(testVMContainer.typeId, root.createMessage({
132132 ports: [portRef2]
133133 }))
134134
135135 await root.ports.bind('first', portRef1)
@@ -170,11 +170,11 @@
170170 }
171171 }
172172
173173 class testVMContainer extends BaseContainer {
174- onMessage (m) {
174 + async onMessage (m) {
175175 const [portRef1, portRef2] = this.kernel.ports.createChannel()
176- this.kernel.createInstance(testVMContainer2.typeId, this.kernel.createMessage({
176 + await this.kernel.createInstance(testVMContainer2.typeId, this.kernel.createMessage({
177177 ports: [portRef2]
178178 }))
179179 this.kernel.send(portRef1, m)
180180 this.kernel.incrementTicks(1)
@@ -188,9 +188,9 @@
188188
189189 let root = await hypervisor.createInstance(testVMContainer.typeId)
190190 const rootId = root.id
191191 const [portRef1, portRef2] = root.ports.createChannel()
192- root.createInstance(testVMContainer.typeId, root.createMessage({
192 + await root.createInstance(testVMContainer.typeId, root.createMessage({
193193 ports: [portRef2]
194194 }))
195195
196196 await root.ports.bind('first', portRef1)
@@ -242,11 +242,13 @@
242242 const message3 = this.kernel.createMessage({
243243 ports: [portRef6]
244244 })
245245
246- this.kernel.createInstance(Root.typeId, message1)
247- this.kernel.createInstance(Root.typeId, message2)
248- this.kernel.createInstance(Root.typeId, message3)
246 + await Promise.all([
247 + this.kernel.createInstance(Root.typeId, message1),
248 + this.kernel.createInstance(Root.typeId, message2),
249 + this.kernel.createInstance(Root.typeId, message3)
250 + ])
249251
250252 throw new Error('it is a trap!!!')
251253 }
252254 }
@@ -267,9 +269,9 @@
267269 t.plan(2)
268270 let runs = 0
269271
270272 class Root extends BaseContainer {
271- onMessage (m) {
273 + async onMessage (m) {
272274 if (!runs) {
273275 runs++
274276
275277 const [portRef1, portRef2] = this.kernel.ports.createChannel()
@@ -281,10 +283,10 @@
281283 const message2 = this.kernel.createMessage({
282284 ports: [portRef4]
283285 })
284286
285- this.kernel.createInstance(First.typeId, message1)
286- this.kernel.createInstance(Second.typeId, message2)
287 + await this.kernel.createInstance(First.typeId, message1)
288 + await this.kernel.createInstance(Second.typeId, message2)
287289
288290 this.kernel.send(portRef1, this.kernel.createMessage())
289291 this.kernel.send(portRef3, this.kernel.createMessage())
290292 return Promise.all(
@@ -334,9 +336,9 @@
334336
335337 const root = await hypervisor.createInstance(Root.typeId)
336338
337339 const [portRef1, portRef2] = root.ports.createChannel()
338- root.createInstance(Root.typeId, root.createMessage({
340 + await root.createInstance(Root.typeId, root.createMessage({
339341 ports: [portRef2]
340342 }))
341343
342344 await root.ports.bind('first', portRef1)
@@ -348,9 +350,9 @@
348350 t.plan(2)
349351 let runs = 0
350352
351353 class Root extends BaseContainer {
352- onMessage (m) {
354 + async onMessage (m) {
353355 if (!runs) {
354356 runs++
355357
356358 const [portRef1, portRef2] = this.kernel.ports.createChannel()
@@ -362,10 +364,10 @@
362364 const message2 = this.kernel.createMessage({
363365 ports: [portRef4]
364366 })
365367
366- this.kernel.createInstance(First.typeId, message1)
367- this.kernel.createInstance(Second.typeId, message2)
368 + await this.kernel.createInstance(First.typeId, message1)
369 + await this.kernel.createInstance(Second.typeId, message2)
368370
369371 this.kernel.send(portRef1, this.kernel.createMessage())
370372 this.kernel.send(portRef3, this.kernel.createMessage())
371373
@@ -416,9 +418,9 @@
416418
417419 const root = await hypervisor.createInstance(Root.typeId)
418420
419421 const [portRef1, portRef2] = root.ports.createChannel()
420- root.createInstance(Root.typeId, root.createMessage({
422 + await root.createInstance(Root.typeId, root.createMessage({
421423 ports: [portRef2]
422424 }))
423425
424426 await root.ports.bind('first', portRef1)
@@ -443,17 +445,16 @@
443445 const message2 = this.kernel.createMessage({
444446 ports: [portRef4]
445447 })
446448
447- this.kernel.createInstance(First.typeId, message1)
448- this.kernel.createInstance(Second.typeId, message2)
449-
450449 this.kernel.send(portRef1, this.kernel.createMessage())
451450 this.kernel.send(portRef3, this.kernel.createMessage())
452451
453452 this.kernel.incrementTicks(6)
454453
455454 return Promise.all([
455 + this.kernel.createInstance(First.typeId, message1),
456 + this.kernel.createInstance(Second.typeId, message2),
456457 this.kernel.ports.bind('one', portRef1),
457458 this.kernel.ports.bind('two', portRef3)
458459 ])
459460 } else if (runs === 1) {
@@ -496,9 +497,9 @@
496497 hypervisor.registerContainer(Second)
497498
498499 const root = await hypervisor.createInstance(Root.typeId)
499500 const [portRef1, portRef2] = root.ports.createChannel()
500- root.createInstance(Root.typeId, root.createMessage({
501 + await root.createInstance(Root.typeId, root.createMessage({
501502 ports: [portRef2]
502503 }))
503504
504505 await root.ports.bind('first', portRef1)
@@ -510,9 +511,9 @@
510511 t.plan(2)
511512 let runs = 0
512513
513514 class Root extends BaseContainer {
514- onMessage (m) {
515 + async onMessage (m) {
515516 if (!runs) {
516517 runs++
517518 const [portRef1, portRef2] = this.kernel.ports.createChannel()
518519 const [portRef3, portRef4] = this.kernel.ports.createChannel()
@@ -523,10 +524,10 @@
523524 const message2 = this.kernel.createMessage({
524525 ports: [portRef4]
525526 })
526527
527- this.kernel.createInstance(First.typeId, message1)
528- this.kernel.createInstance(Second.typeId, message2)
528 + await this.kernel.createInstance(First.typeId, message1)
529 + await this.kernel.createInstance(Second.typeId, message2)
529530
530531 this.kernel.send(portRef1, this.kernel.createMessage())
531532 this.kernel.send(portRef3, this.kernel.createMessage())
532533
@@ -581,43 +582,47 @@
581582 })
582583 }
583584 }
584585
585- const hypervisor = new Hypervisor(node.dag)
586 + try {
587 + const hypervisor = new Hypervisor(node.dag)
586588
587- hypervisor.registerContainer(Root)
588- hypervisor.registerContainer(First)
589- hypervisor.registerContainer(Second)
590- hypervisor.registerContainer(Waiter)
589 + hypervisor.registerContainer(Root)
590 + hypervisor.registerContainer(First)
591 + hypervisor.registerContainer(Second)
592 + hypervisor.registerContainer(Waiter)
591593
592- const root = await hypervisor.createInstance(Root.typeId)
593- const [portRef1, portRef2] = root.ports.createChannel()
594 + const root = await hypervisor.createInstance(Root.typeId)
595 + const [portRef1, portRef2] = root.ports.createChannel()
594596
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- }))
597 + const message = root.createMessage()
598 + root.send(portRef1, message)
599 + await root.ports.bind('first', portRef1)
600 + await root.createInstance(Root.typeId, root.createMessage({
601 + ports: [portRef2]
602 + }))
601603
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- }))
604 + const [portRef3, portRef4] = root.ports.createChannel()
605 + await root.ports.bind('sencond', portRef3)
606 + await root.createInstance(Waiter.typeId, root.createMessage({
607 + ports: [portRef4]
608 + }))
607609
608- root.incrementTicks(100)
609- root.send(portRef1, root.createMessage({data: 'testss'}))
610- hypervisor.scheduler.done(root.id)
610 + root.incrementTicks(100)
611 + root.send(portRef1, root.createMessage({data: 'testss'}))
612 + // hypervisor.scheduler.done(root.id)
613 + } catch (e) {
614 + console.log(e)
615 + }
611616 })
612617
613618 tape('message should arrive in the correct order, even in a tie of ticks', async t => {
614619 t.plan(2)
615620
616621 let runs = 0
617622
618623 class Root extends BaseContainer {
619- onMessage (m) {
624 + async onMessage (m) {
620625 if (!runs) {
621626 runs++
622627 const [portRef1, portRef2] = this.kernel.ports.createChannel()
623628 const [portRef3, portRef4] = this.kernel.ports.createChannel()
@@ -628,10 +633,10 @@
628633 const message2 = this.kernel.createMessage({
629634 ports: [portRef4]
630635 })
631636
632- this.kernel.createInstance(First.typeId, message1)
633- this.kernel.createInstance(Second.typeId, message2)
637 + await this.kernel.createInstance(First.typeId, message1)
638 + await this.kernel.createInstance(Second.typeId, message2)
634639
635640 this.kernel.send(portRef1, this.kernel.createMessage())
636641 this.kernel.send(portRef3, this.kernel.createMessage())
637642
@@ -687,9 +692,9 @@
687692 const message = root.createMessage()
688693
689694 root.send(portRef1, message)
690695 await root.ports.bind('first', portRef1)
691- root.createInstance(Root.typeId, root.createMessage({
696 + await root.createInstance(Root.typeId, root.createMessage({
692697 ports: [portRef2]
693698 }))
694699 })
695700
@@ -714,10 +719,10 @@
714719 const message2 = this.kernel.createMessage({
715720 ports: [portRef4]
716721 })
717722
718- this.kernel.createInstance(First.typeId, message1)
719- this.kernel.createInstance(Second.typeId, message2)
723 + await this.kernel.createInstance(First.typeId, message1)
724 + await this.kernel.createInstance(Second.typeId, message2)
720725
721726 this.kernel.send(portRef1, this.kernel.createMessage())
722727 this.kernel.send(portRef3, this.kernel.createMessage())
723728
@@ -766,9 +771,9 @@
766771 const message = root.createMessage()
767772
768773 root.send(portRef1, message)
769774 await root.ports.bind('first', portRef1)
770- root.createInstance(Root.typeId, root.createMessage({
775 + await root.createInstance(Root.typeId, root.createMessage({
771776 ports: [portRef2]
772777 }))
773778 })
774779
@@ -778,16 +783,16 @@
778783 let runs = 0
779784 let instance
780785
781786 class Root extends BaseContainer {
782- onMessage (m) {
787 + async onMessage (m) {
783788 let one = this.kernel.ports.get('one')
784789 if (!one) {
785790 const [portRef1, portRef2] = this.kernel.ports.createChannel()
786791 const message1 = this.kernel.createMessage({
787792 ports: [portRef2]
788793 })
789- this.kernel.createInstance(First.typeId, message1)
794 + await this.kernel.createInstance(First.typeId, message1)
790795 return this.kernel.ports.bind('one', portRef1)
791796 } else {
792797 this.kernel.send(one, this.kernel.createMessage())
793798 this.kernel.send(one, this.kernel.createMessage())
@@ -808,26 +813,30 @@
808813 }
809814 }
810815 }
811816
812- const hypervisor = new Hypervisor(node.dag)
817 + try {
818 + const hypervisor = new Hypervisor(node.dag)
813819
814- hypervisor.registerContainer(Root)
815- hypervisor.registerContainer(First)
820 + hypervisor.registerContainer(Root)
821 + hypervisor.registerContainer(First)
816822
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- }))
823 + const root = await hypervisor.createInstance(Root.typeId)
824 + const [portRef1, portRef2] = root.ports.createChannel()
825 + await root.ports.bind('first', portRef1)
826 + await root.createInstance(Root.typeId, root.createMessage({
827 + ports: [portRef2]
828 + }))
823829
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)
830 + const message = root.createMessage()
831 + root.send(portRef1, message)
832 + await hypervisor.createStateRoot()
833 + root.send(portRef1, root.createMessage())
834 + await hypervisor.createStateRoot()
835 + t.equals(runs, 2)
836 + } catch (e) {
837 + console.log(e)
838 + }
830839 })
831840
832841 tape('checking ports', async t => {
833842 t.plan(4)
@@ -874,15 +883,15 @@
874883 const expectedSr = {
875884 '/': 'zdpuAopMy53q2uvL2a4fhVEAvwXjSDW28fh8zhQUj598tb5md'
876885 }
877886 class Root extends BaseContainer {
878- onMessage (m) {
887 + async onMessage (m) {
879888 const [portRef1, portRef2] = this.kernel.ports.createChannel()
880889 const message1 = this.kernel.createMessage({
881890 ports: [portRef2]
882891 })
883892
884- this.kernel.createInstance(First.typeId, message1)
893 + await this.kernel.createInstance(First.typeId, message1)
885894 this.kernel.send(portRef1, this.kernel.createMessage())
886895 this.kernel.incrementTicks(6)
887896 return this.kernel.ports.bind('one', portRef1)
888897 }
@@ -905,9 +914,9 @@
905914
906915 const root = await hypervisor.createInstance(Root.typeId)
907916 const [portRef1, portRef2] = root.ports.createChannel()
908917 await root.ports.bind('first', portRef1)
909- root.createInstance(Root.typeId, root.createMessage({
918 + await root.createInstance(Root.typeId, root.createMessage({
910919 ports: [portRef2]
911920 }))
912921
913922 const message = root.createMessage()
@@ -925,20 +934,19 @@
925934 '/': 'zdpuAopMy53q2uvL2a4fhVEAvwXjSDW28fh8zhQUj598tb5md'
926935 }
927936 class Root extends BaseContainer {
928937 onMessage (m) {
929- this.kernel.createInstance(Root.typeId)
938 + return this.kernel.createInstance(Root.typeId)
930939 }
931940 }
932941
933942 const hypervisor = new Hypervisor(node.dag)
934-
935943 hypervisor.registerContainer(Root)
936944
937945 const root = await hypervisor.createInstance(Root.typeId)
938946 const [portRef1, portRef2] = root.ports.createChannel()
939947 await root.ports.bind('first', portRef1)
940- root.createInstance(Root.typeId, root.createMessage({
948 + await root.createInstance(Root.typeId, root.createMessage({
941949 ports: [portRef2]
942950 }))
943951
944952 const message = root.createMessage()
@@ -955,9 +963,9 @@
955963 }
956964 class Root extends BaseContainer {
957965 onMessage (m) {
958966 const [, portRef2] = this.kernel.ports.createChannel()
959- this.kernel.createInstance(Sub.typeId, this.kernel.createMessage({
967 + return this.kernel.createInstance(Sub.typeId, this.kernel.createMessage({
960968 ports: [portRef2]
961969 }))
962970 }
963971 }
@@ -966,34 +974,38 @@
966974 async onInitailize (message) {
967975 await this.kernel.ports.bind('root', message.ports[0])
968976 const [portRef1, portRef2] = this.kernel.ports.createChannel()
969977 await this.kernel.ports.bind('child', portRef1)
970- this.kernel.createInstance(Root.typeId, this.kernel.createMessage({
978 + await this.kernel.createInstance(Root.typeId, this.kernel.createMessage({
971979 ports: [portRef2]
972980 }))
973981 }
974982 static get typeId () {
975983 return 299
976984 }
977985 }
978986
979- const hypervisor = new Hypervisor(node.dag)
987 + try {
988 + const hypervisor = new Hypervisor(node.dag)
980989
981- hypervisor.registerContainer(Root)
982- hypervisor.registerContainer(Sub)
990 + hypervisor.registerContainer(Root)
991 + hypervisor.registerContainer(Sub)
983992
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- }))
993 + const root = await hypervisor.createInstance(Root.typeId)
994 + const [portRef1, portRef2] = root.ports.createChannel()
995 + await root.ports.bind('first', portRef1)
996 + await root.createInstance(Root.typeId, root.createMessage({
997 + ports: [portRef2]
998 + }))
990999
991- root.send(portRef1, root.createMessage())
992- const sr = await hypervisor.createStateRoot()
1000 + root.send(portRef1, root.createMessage())
1001 + const sr = await hypervisor.createStateRoot()
9931002
994- t.deepEquals(sr, expectedSr, 'should produce the corret state root')
995- t.end()
1003 + t.deepEquals(sr, expectedSr, 'should produce the corret state root')
1004 + t.end()
1005 + } catch (e) {
1006 + console.log(e)
1007 + }
9961008 })
9971009
9981010 tape('should not remove connected nodes', async t => {
9991011 const expectedSr = {
@@ -1006,15 +1018,15 @@
10061018 this.kernel.send(port, m)
10071019 return this.kernel.ports.unbind('test1')
10081020 } else {
10091021 const [portRef1, portRef2] = this.kernel.ports.createChannel()
1010- this.kernel.createInstance(Sub.typeId, this.kernel.createMessage({
1022 + await this.kernel.createInstance(Sub.typeId, this.kernel.createMessage({
10111023 ports: [portRef2]
10121024 }))
10131025 await this.kernel.ports.bind('test1', portRef1)
10141026
10151027 const [portRef3, portRef4] = this.kernel.ports.createChannel()
1016- this.kernel.createInstance(Sub.typeId, this.kernel.createMessage({
1028 + await this.kernel.createInstance(Sub.typeId, this.kernel.createMessage({
10171029 ports: [portRef4]
10181030 }))
10191031 await this.kernel.ports.bind('test2', portRef3)
10201032 this.kernel.send(portRef3, this.kernel.createMessage({
@@ -1049,9 +1061,9 @@
10491061
10501062 const root = await hypervisor.createInstance(Root.typeId)
10511063 const [portRef1, portRef2] = root.ports.createChannel()
10521064 await root.ports.bind('first', portRef1)
1053- root.createInstance(Root.typeId, root.createMessage({
1065 + await root.createInstance(Root.typeId, root.createMessage({
10541066 ports: [portRef2]
10551067 }))
10561068
10571069 root.send(portRef1, root.createMessage())
@@ -1063,41 +1075,41 @@
10631075 })
10641076
10651077 tape('should remove multiple subgraphs', async t => {
10661078 const expectedSr = {
1067- '/': 'zdpuAohccQTxM82d8N6Q82z234nQskeQoJGJu3eAVmxoQwWde'
1079 + '/': 'zdpuArkZ5yNowNnU4qJ8vayAUncgibQP9goDP1CwFxdmPJF9D'
10681080 }
10691081 class Root extends BaseContainer {
10701082 async onMessage (m) {
10711083 if (m.ports.length) {
10721084 const port = this.kernel.ports.get('test1')
1073- this.kernel.send(port, m)
10741085 await this.kernel.ports.unbind('test1')
10751086 await this.kernel.ports.unbind('test2')
1087 + await this.kernel.send(port, m)
10761088 } else {
10771089 const [portRef1, portRef2] = this.kernel.ports.createChannel()
1078- this.kernel.createInstance(Sub.typeId, this.kernel.createMessage({
1090 + await this.kernel.createInstance(Sub.typeId, this.kernel.createMessage({
10791091 ports: [portRef2]
10801092 }))
10811093 await this.kernel.ports.bind('test1', portRef1)
10821094
10831095 const [portRef3, portRef4] = this.kernel.ports.createChannel()
1084- this.kernel.createInstance(Sub.typeId, this.kernel.createMessage({
1096 + await this.kernel.createInstance(Sub.typeId, this.kernel.createMessage({
10851097 ports: [portRef4]
10861098 }))
10871099 await this.kernel.ports.bind('test2', portRef3)
1088- this.kernel.send(portRef3, this.kernel.createMessage({
1100 + await this.kernel.send(portRef3, this.kernel.createMessage({
10891101 data: 'getChannel'
10901102 }))
10911103 }
10921104 }
10931105 }
10941106
10951107 class Sub extends BaseContainer {
1096- onMessage (message) {
1108 + async onMessage (message) {
10971109 if (message.data === 'getChannel') {
10981110 const ports = this.kernel.ports.createChannel()
1099- this.kernel.send(message.fromPort, this.kernel.createMessage({
1111 + await this.kernel.send(message.fromPort, this.kernel.createMessage({
11001112 data: 'bindPort',
11011113 ports: [ports[1]]
11021114 }))
11031115 return this.kernel.ports.bind('channel', ports[0])
@@ -1109,27 +1121,34 @@
11091121 return 299
11101122 }
11111123 }
11121124
1113- const hypervisor = new Hypervisor(node.dag)
1125 + try {
1126 + const hypervisor = new Hypervisor(node.dag)
11141127
1115- hypervisor.registerContainer(Root)
1116- hypervisor.registerContainer(Sub)
1128 + hypervisor.registerContainer(Root)
1129 + hypervisor.registerContainer(Sub)
11171130
1118- const root = await hypervisor.createInstance(Root.typeId)
1131 + const root = await hypervisor.createInstance(Root.typeId)
11191132
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- }))
1133 + const [portRef1, portRef2] = root.ports.createChannel()
1134 + await root.ports.bind('first', portRef1)
1135 + await root.createInstance(Root.typeId, root.createMessage({
1136 + ports: [portRef2]
1137 + }))
11251138
1126- root.send(portRef1, root.createMessage())
1139 + root.send(portRef1, root.createMessage())
11271140
1128- const sr = await hypervisor.createStateRoot()
1129- t.deepEquals(sr, expectedSr, 'should produce the corret state root')
1141 + const sr = await hypervisor.createStateRoot()
1142 + t.deepEquals(sr, expectedSr, 'should produce the corret state root')
11301143
1131- t.end()
1144 + // await hypervisor.graph.tree(sr, Infinity, true)
1145 + // console.log(JSON.stringify(sr, null, 2))
1146 +
1147 + t.end()
1148 + } catch (e) {
1149 + console.log(e)
1150 + }
11321151 })
11331152
11341153 tape('response ports', async t => {
11351154 t.plan(2)

Built with git-ssb-web