git ssb

0+

wanderer🌟 / js-primea-hypervisor



Commit 420c091edd500452248946f7f519a3f0bd7b3a3e

fixed tests

wanderer committed on 5/8/2017, 1:59:21 PM
Parent: 463dc84f2cfff964ba0c0ea55de6f903a4f344a3

Files changed

index.jschanged
kernel.jschanged
port.jschanged
portManager.jschanged
tests/index.jschanged
index.jsView
@@ -8,19 +8,20 @@
88 this._vmInstances = new Map()
99 this._VMs = {}
1010 }
1111
12- async getInstance (port) {
13- let id = await this.generateID(port)
12+ async getInstance (port, createIfNotFound = true) {
13+ const id = await this.generateID(port)
1414 let kernel = this._vmInstances.get(id)
15- if (!kernel) {
15+ if (!kernel && createIfNotFound) {
1616 // load the the ID from the merkle store
1717 await this.graph.tree(port.id, 1)
1818 const parentID = await this.generateID({id: port.id['/'].parent})
1919 const parentKernel = await this._vmInstances.get(parentID)
2020 const parentPort = parentKernel.entryPort
2121
2222 kernel = await this.createInstanceFromPort(port, parentPort)
23+ kernel.id = id
2324 kernel.on('idle', () => {
2425 this._vmInstances.delete(id)
2526 })
2627 }
@@ -29,10 +30,14 @@
2930
3031 // given a port, wait untill its source contract has reached the threshold
3132 // tick count
3233 async wait (port, threshold, fromPort) {
33- let kernel = await this.getInstance(port)
34- return kernel.wait(threshold, fromPort)
34+ let kernel = await this.getInstance(port, false)
35+ if (kernel) {
36+ return kernel.wait(threshold, fromPort)
37+ } else {
38+ return threshold
39+ }
3540 }
3641
3742 async createInstance (type, state, entryPort = null, parentPort) {
3843 const VM = this._VMs[type]
kernel.jsView
@@ -28,11 +28,9 @@
2828 this.on('result', this._runNextMessage)
2929 this.on('idle', () => {
3030 while (!this._waitingQueue.isEmpty()) {
3131 const waiter = this._waitingQueue.poll()
32- this.wait(waiter.ticks, waiter.from).then(ticks => {
33- waiter.resolve(ticks)
34- })
32+ waiter.resolve(this.ticks)
3533 }
3634 })
3735 }
3836
@@ -53,23 +51,19 @@
5351 this.emit(vmState, message)
5452 }
5553
5654 async _runNextMessage () {
57- console.log('run next message')
58- try {
59- const message = await this.ports.getNextMessage()
60- // if the vm is paused and it gets a message; save that message for use when the VM is resumed
61- if (message && this.vmState === 'paused') {
62- this.ports._portMap(message._fromPort).unshfit(message)
63- } else if (!message && this.vmState !== 'paused') {
64- // if no more messages then shut down
65- this._updateVmState('idle')
66- } else {
67- // run the next message
68- this._run(message)
69- }
70- } catch (e) {
71- console.log(e)
55+ await this.hypervisor.graph.get(this.entryPort, 'id')
56+ const message = await this.ports.getNextMessage()
57+ // if the vm is paused and it gets a message; save that message for use when the VM is resumed
58+ if (message && this.vmState === 'paused') {
59+ this.ports._portMap(message._fromPort).unshfit(message)
60+ } else if (!message && this.vmState !== 'paused') {
61+ // if no more messages then shut down
62+ this._updateVmState('idle')
63+ } else {
64+ // run the next message
65+ this._run(message)
7266 }
7367 }
7468
7569 _updateEntryPort (entryPort) {
@@ -106,9 +100,8 @@
106100 exceptionError: e
107101 }
108102 clearObject(this.state)
109103 Object.assign(this.state, oldState)
110- console.log(e)
111104 }
112105
113106 this.emit('result', result)
114107 return result
@@ -171,15 +164,8 @@
171164 return portRef
172165 }
173166
174167 async send (portRef, message) {
175- try {
176- const portInstance = await this.ports.get(portRef)
177- portInstance.hasSent = true
178- } catch (e) {
179- throw new Error('invalid port referance, which means the port that the port was either moved or destoried')
180- }
181-
182168 const id = await this.hypervisor.generateID(this.entryPort)
183169 message._fromPort = id
184170 message._ticks = this.ticks
185171
port.jsView
@@ -1,8 +1,7 @@
11 module.exports = class Port {
22 constructor (name) {
33 this.name = name
4- this.hasSent = false
54 this._queue = []
65 this.ticks = 0
76 }
87
portManager.jsView
@@ -66,19 +66,21 @@
6666 return this._portMap.get(id)
6767 }
6868
6969 getRef (key) {
70- return this.ports[key]
70+ if (key === ENTRY) {
71+ return this.entryPort
72+ } else {
73+ return this.ports[key]
74+ }
7175 }
7276
7377 // waits till all ports have reached a threshold tick count
7478 async wait (threshold, fromPort) {
7579 // find the ports that have a smaller tick count then the threshold tick count
7680 const unkownPorts = [...this._portMap].filter(([id, port]) => {
7781 const portRef = this.getRef(port.name)
78- return (port.hasSent || port.name === ENTRY) &&
79- port.ticks < threshold &&
80- fromPort !== portRef
82+ return port.ticks < threshold && fromPort !== portRef
8183 })
8284
8385 const promises = unkownPorts.map(async ([id, port]) => {
8486 const portObj = port.name === ENTRY ? this.parentPort : this.ports[port.name]
tests/index.jsView
@@ -34,29 +34,25 @@
3434 t.true(m === message, 'should recive a message')
3535 }
3636 }
3737
38- try {
39- const hypervisor = new Hypervisor({dag: node.dag})
40- hypervisor.registerContainer('test', testVMContainer)
38+ const hypervisor = new Hypervisor({dag: node.dag})
39+ hypervisor.registerContainer('test', testVMContainer)
4140
42- const rootContainer = await hypervisor.createInstance('test')
43- const port = await rootContainer.createPort('test', 'first')
41+ const rootContainer = await hypervisor.createInstance('test')
42+ const port = await rootContainer.createPort('test', 'first')
4443
45- await rootContainer.send(port, message)
44+ await rootContainer.send(port, message)
4645
47- const stateRoot = await hypervisor.createStateRoot(rootContainer, Infinity)
48- t.deepEquals(stateRoot, expectedState, 'expected root!')
49- } catch (e) {
50- console.log(e)
51- }
46+ const stateRoot = await hypervisor.createStateRoot(rootContainer, Infinity)
47+ t.deepEquals(stateRoot, expectedState, 'expected root!')
5248 t.end()
5349 })
5450
5551 tape('one child contract', async t => {
5652 let message = new Message()
5753 const expectedState = {
58- '/': 'zdpuAtYQujwQMR9SpmFwmkr7d2cD4vzeQk2GCzcEku2nomWj6'
54+ '/': 'zdpuAofSzrBqwYs6z1r28fMeb8z5oSKF6CcWA6m22RqazgoTB'
5955 }
6056 let hasResolved = false
6157
6258 class testVMContainer2 extends BaseContainer {
@@ -94,29 +90,26 @@
9490
9591 // test reviving the state
9692 class testVMContainer3 extends BaseContainer {
9793 async run (m) {
98- const port = this.kernel.getPort('child')
99- this.kernel.send(port, m)
94+ const port = this.kernel.ports.getRef('child')
95+ await this.kernel.send(port, m)
10096 this.kernel.incrementTicks(1)
10197 }
10298 }
10399
104- try {
105- hypervisor.registerContainer('test', testVMContainer3)
106- root = await hypervisor.createInstance('test', stateRoot)
107- port = await root.ports.getRef('first')
100+ hypervisor.registerContainer('test', testVMContainer3)
101+ root = await hypervisor.createInstance('test', stateRoot)
102+ port = await root.ports.getRef('first')
108103
109- await root.send(port, message)
110- console.log('creating SR')
111- await hypervisor.createStateRoot(root, Infinity)
112- console.log('end!')
113- // console.log(hypervisor._vmInstances)
114- } catch (e) {
115- console.log(e)
116- }
104+ await root.send(port, message)
105+ await hypervisor.createStateRoot(root, Infinity)
117106
118107 t.end()
108+
109+ node.stop(() => {
110+ process.exit()
111+ })
119112 })
120113
121114 tape.skip('should wait on parent', async t => {
122115 let r
@@ -173,9 +166,6 @@
173166 console.log(e)
174167 }
175168
176169 t.end()
177- node.stop(() => {
178- process.exit()
179- })
180170 })
181171 })

Built with git-ssb-web