git ssb

0+

wanderer🌟 / js-primea-hypervisor



Commit ce9d7d51405b9e45388197443ff818f21fafee7a

added tests for error response

Signed-off-by: wanderer <mjbecze@gmail.com>
wanderer committed on 11/16/2017, 2:10:57 AM
Parent: e1893bc91e787943f70b1b087b1b268da0553c3f

Files changed

actor.jschanged
tests/index.jschanged
actor.jsView
@@ -91,24 +91,23 @@
9191 * @param {boolean} init - whether or not to run the intialization routine
9292 * @returns {Promise}
9393 */
9494 async message (message, method = 'onMessage') {
95- const response = message.response
96- message.response = false
95+ const responseCap = message.responseCap
96+ delete message.responseCap
9797
9898 let result
9999 try {
100100 result = await this.container[method](message)
101101 } catch (e) {
102- console.log(e)
103102 result = {
104103 exception: true,
105104 exceptionError: e
106105 }
107106 }
108107
109- if (response) {
110- this.send(message.from, new Message({
108+ if (responseCap) {
109+ this.send(responseCap, new Message({
111110 data: result
112111 }))
113112 }
114113 this.caps.clist.clear()
tests/index.jsView
@@ -57,11 +57,9 @@
5757 })
5858
5959 class testVMContainerA extends BaseContainer {
6060 onCreation (m) {
61- const cap = this.kernel.mintCap()
6261 message = new Message()
63- message.caps.push(cap)
6462 return this.kernel.send(m.caps[0], message)
6563 }
6664 }
6765
@@ -102,11 +100,9 @@
102100 })
103101
104102 class testVMContainerA extends BaseContainer {
105103 onCreation (m) {
106- const cap = this.kernel.mintCap()
107104 message = new Message()
108- message.caps.push(cap)
109105 return this.kernel.send(m.caps[0], message)
110106 }
111107 }
112108
@@ -191,4 +187,101 @@
191187
192188 t.deepEquals(stateRoot, expectedState, 'expected root!')
193189 t.equals(hypervisor.scheduler.leastNumberOfTicks(), 0)
194190 })
191+
192+tape('response caps', async t => {
193+ t.plan(4)
194+ let message
195+ const expectedState = {
196+ '/': Buffer.from('fc935489953ed357f06171dd23439d83190b3a1b', 'hex')
197+ }
198+
199+ const tree = new RadixTree({
200+ db: db
201+ })
202+
203+ class testVMContainerA extends BaseContainer {
204+ onCreation (m) {
205+ message = new Message()
206+ message.responseCap = this.kernel.mintCap()
207+ return this.kernel.send(m.caps[0], message)
208+ }
209+
210+ onMessage (m) {
211+ t.true(m, 'should recive a response message')
212+ }
213+ }
214+
215+ class testVMContainerB extends BaseContainer {
216+ onMessage (m) {
217+ t.true(m === message, 'should recive a message')
218+ }
219+
220+ static get typeId () {
221+ return 8
222+ }
223+ }
224+
225+ const hypervisor = new Hypervisor(tree)
226+ hypervisor.registerContainer(testVMContainerA)
227+ hypervisor.registerContainer(testVMContainerB)
228+
229+ let capB = await hypervisor.createInstance(testVMContainerB.typeId, new Message())
230+ await hypervisor.createInstance(testVMContainerA.typeId, new Message({
231+ caps: [capB]
232+ }))
233+
234+ const stateRoot = await hypervisor.createStateRoot(Infinity)
235+
236+ t.deepEquals(stateRoot, expectedState, 'expected root!')
237+ t.equals(hypervisor.scheduler.leastNumberOfTicks(), 0)
238+})
239+
240+tape('response caps for errors', async t => {
241+ t.plan(4)
242+ let message
243+ const expectedState = {
244+ '/': Buffer.from('fc935489953ed357f06171dd23439d83190b3a1b', 'hex')
245+ }
246+
247+ const tree = new RadixTree({
248+ db: db
249+ })
250+
251+ class testVMContainerA extends BaseContainer {
252+ onCreation (m) {
253+ message = new Message()
254+ message.responseCap = this.kernel.mintCap()
255+ return this.kernel.send(m.caps[0], message)
256+ }
257+
258+ onMessage (m) {
259+ t.true(m.data.exceptionError instanceof Error, 'should recive a response message')
260+ }
261+ }
262+
263+ class testVMContainerB extends BaseContainer {
264+ onMessage (m) {
265+ t.true(m === message, 'should recive a message')
266+ throw new Error('test error')
267+ }
268+
269+ static get typeId () {
270+ return 8
271+ }
272+ }
273+
274+ const hypervisor = new Hypervisor(tree)
275+ hypervisor.registerContainer(testVMContainerA)
276+ hypervisor.registerContainer(testVMContainerB)
277+
278+ let capB = await hypervisor.createInstance(testVMContainerB.typeId, new Message())
279+ await hypervisor.createInstance(testVMContainerA.typeId, new Message({
280+ caps: [capB]
281+ }))
282+
283+ const stateRoot = await hypervisor.createStateRoot(Infinity)
284+
285+ t.deepEquals(stateRoot, expectedState, 'expected root!')
286+ t.equals(hypervisor.scheduler.leastNumberOfTicks(), 0)
287+})

Built with git-ssb-web