Commit 420c091edd500452248946f7f519a3f0bd7b3a3e
fixed tests
wanderer committed on 5/8/2017, 1:59:21 PMParent: 463dc84f2cfff964ba0c0ea55de6f903a4f344a3
Files changed
index.js | changed |
kernel.js | changed |
port.js | changed |
portManager.js | changed |
tests/index.js | changed |
index.js | ||
---|---|---|
@@ -8,19 +8,20 @@ | ||
8 | 8 | this._vmInstances = new Map() |
9 | 9 | this._VMs = {} |
10 | 10 | } |
11 | 11 | |
12 | - async getInstance (port) { | |
13 | - let id = await this.generateID(port) | |
12 | + async getInstance (port, createIfNotFound = true) { | |
13 | + const id = await this.generateID(port) | |
14 | 14 | let kernel = this._vmInstances.get(id) |
15 | - if (!kernel) { | |
15 | + if (!kernel && createIfNotFound) { | |
16 | 16 | // load the the ID from the merkle store |
17 | 17 | await this.graph.tree(port.id, 1) |
18 | 18 | const parentID = await this.generateID({id: port.id['/'].parent}) |
19 | 19 | const parentKernel = await this._vmInstances.get(parentID) |
20 | 20 | const parentPort = parentKernel.entryPort |
21 | 21 | |
22 | 22 | kernel = await this.createInstanceFromPort(port, parentPort) |
23 | + kernel.id = id | |
23 | 24 | kernel.on('idle', () => { |
24 | 25 | this._vmInstances.delete(id) |
25 | 26 | }) |
26 | 27 | } |
@@ -29,10 +30,14 @@ | ||
29 | 30 | |
30 | 31 | // given a port, wait untill its source contract has reached the threshold |
31 | 32 | // tick count |
32 | 33 | 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 | + } | |
35 | 40 | } |
36 | 41 | |
37 | 42 | async createInstance (type, state, entryPort = null, parentPort) { |
38 | 43 | const VM = this._VMs[type] |
kernel.js | ||
---|---|---|
@@ -28,11 +28,9 @@ | ||
28 | 28 | this.on('result', this._runNextMessage) |
29 | 29 | this.on('idle', () => { |
30 | 30 | while (!this._waitingQueue.isEmpty()) { |
31 | 31 | const waiter = this._waitingQueue.poll() |
32 | - this.wait(waiter.ticks, waiter.from).then(ticks => { | |
33 | - waiter.resolve(ticks) | |
34 | - }) | |
32 | + waiter.resolve(this.ticks) | |
35 | 33 | } |
36 | 34 | }) |
37 | 35 | } |
38 | 36 | |
@@ -53,23 +51,19 @@ | ||
53 | 51 | this.emit(vmState, message) |
54 | 52 | } |
55 | 53 | |
56 | 54 | 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) | |
72 | 66 | } |
73 | 67 | } |
74 | 68 | |
75 | 69 | _updateEntryPort (entryPort) { |
@@ -106,9 +100,8 @@ | ||
106 | 100 | exceptionError: e |
107 | 101 | } |
108 | 102 | clearObject(this.state) |
109 | 103 | Object.assign(this.state, oldState) |
110 | - console.log(e) | |
111 | 104 | } |
112 | 105 | |
113 | 106 | this.emit('result', result) |
114 | 107 | return result |
@@ -171,15 +164,8 @@ | ||
171 | 164 | return portRef |
172 | 165 | } |
173 | 166 | |
174 | 167 | 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 | - | |
182 | 168 | const id = await this.hypervisor.generateID(this.entryPort) |
183 | 169 | message._fromPort = id |
184 | 170 | message._ticks = this.ticks |
185 | 171 |
port.js | ||
---|---|---|
@@ -1,8 +1,7 @@ | ||
1 | 1 | module.exports = class Port { |
2 | 2 | constructor (name) { |
3 | 3 | this.name = name |
4 | - this.hasSent = false | |
5 | 4 | this._queue = [] |
6 | 5 | this.ticks = 0 |
7 | 6 | } |
8 | 7 |
portManager.js | ||
---|---|---|
@@ -66,19 +66,21 @@ | ||
66 | 66 | return this._portMap.get(id) |
67 | 67 | } |
68 | 68 | |
69 | 69 | getRef (key) { |
70 | - return this.ports[key] | |
70 | + if (key === ENTRY) { | |
71 | + return this.entryPort | |
72 | + } else { | |
73 | + return this.ports[key] | |
74 | + } | |
71 | 75 | } |
72 | 76 | |
73 | 77 | // waits till all ports have reached a threshold tick count |
74 | 78 | async wait (threshold, fromPort) { |
75 | 79 | // find the ports that have a smaller tick count then the threshold tick count |
76 | 80 | const unkownPorts = [...this._portMap].filter(([id, port]) => { |
77 | 81 | 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 | |
81 | 83 | }) |
82 | 84 | |
83 | 85 | const promises = unkownPorts.map(async ([id, port]) => { |
84 | 86 | const portObj = port.name === ENTRY ? this.parentPort : this.ports[port.name] |
tests/index.js | ||
---|---|---|
@@ -34,29 +34,25 @@ | ||
34 | 34 | t.true(m === message, 'should recive a message') |
35 | 35 | } |
36 | 36 | } |
37 | 37 | |
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) | |
41 | 40 | |
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') | |
44 | 43 | |
45 | - await rootContainer.send(port, message) | |
44 | + await rootContainer.send(port, message) | |
46 | 45 | |
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!') | |
52 | 48 | t.end() |
53 | 49 | }) |
54 | 50 | |
55 | 51 | tape('one child contract', async t => { |
56 | 52 | let message = new Message() |
57 | 53 | const expectedState = { |
58 | - '/': 'zdpuAtYQujwQMR9SpmFwmkr7d2cD4vzeQk2GCzcEku2nomWj6' | |
54 | + '/': 'zdpuAofSzrBqwYs6z1r28fMeb8z5oSKF6CcWA6m22RqazgoTB' | |
59 | 55 | } |
60 | 56 | let hasResolved = false |
61 | 57 | |
62 | 58 | class testVMContainer2 extends BaseContainer { |
@@ -94,29 +90,26 @@ | ||
94 | 90 | |
95 | 91 | // test reviving the state |
96 | 92 | class testVMContainer3 extends BaseContainer { |
97 | 93 | 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) | |
100 | 96 | this.kernel.incrementTicks(1) |
101 | 97 | } |
102 | 98 | } |
103 | 99 | |
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') | |
108 | 103 | |
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) | |
117 | 106 | |
118 | 107 | t.end() |
108 | + | |
109 | + node.stop(() => { | |
110 | + process.exit() | |
111 | + }) | |
119 | 112 | }) |
120 | 113 | |
121 | 114 | tape.skip('should wait on parent', async t => { |
122 | 115 | let r |
@@ -173,9 +166,6 @@ | ||
173 | 166 | console.log(e) |
174 | 167 | } |
175 | 168 | |
176 | 169 | t.end() |
177 | - node.stop(() => { | |
178 | - process.exit() | |
179 | - }) | |
180 | 170 | }) |
181 | 171 | }) |
Built with git-ssb-web