git ssb

0+

wanderer🌟 / js-primea-hypervisor



Commit 4f7f80fb523a0e4362db909d00f48fe553b0880f

added tests

wanderer committed on 7/5/2017, 1:33:34 AM
Parent: 5ffd9fccb98bf042c2c2cce718f598e7708efcf4

Files changed

kernel.jschanged
tests/index.jschanged
kernel.jsView
@@ -75,8 +75,11 @@
7575 */
7676 async run (message, method = 'run') {
7777 let result
7878
79+ const responsePort = message.responsePort
80+ delete message.responsePort
81+
7982 message.ports.forEach(port => this.ports._unboundPorts.add(port))
8083 message._hops++
8184
8285 if (message.constructor === DeleteMessage) {
@@ -90,17 +93,32 @@
9093 exceptionError: e
9194 }
9295 }
9396 }
97+
98+ if (responsePort) {
99+ this.send(responsePort, new Message({
100+ data: result
101+ }))
102+ this.ports._unboundPorts.add(responsePort)
103+ }
104+
94105 this.ports.clearUnboundedPorts()
95- // const responsePort = this.message.responsePort
96- // if (responsePort) {
97- // this.send(responsePort, new Message({data: result}))
98- // }
99106 this._runNextMessage()
100107 return result
101108 }
102109
110+ getResponsePort (message) {
111+ if (message.responsePort) {
112+ return message.responsePort.destPort
113+ } else {
114+ const [portRef1, portRef2] = this.ports.createChannel()
115+ message.responsePort = portRef2
116+ this.ports._unboundPorts.delete(portRef2)
117+ return portRef1
118+ }
119+ }
120+
103121 /**
104122 * updates the number of ticks that the container has run
105123 * @param {Number} count - the number of ticks to add
106124 */
tests/index.jsView
@@ -1031,5 +1031,45 @@
10311031 t.deepEquals(sr, expectedSr, 'should produce the corret state root')
10321032
10331033 t.end()
10341034 })
1035+
1036+ tape('response ports', async t => {
1037+ t.plan(2)
1038+ let runs = 0
1039+ const returnValue = 'this is a test'
1040+
1041+ class testVMContainer extends BaseContainer {
1042+ run (m) {
1043+ runs++
1044+ if (runs === 1) {
1045+ return returnValue
1046+ } else {
1047+ t.equals(m.data, returnValue, 'should have correct return value')
1048+ }
1049+ }
1050+ }
1051+
1052+ const hypervisor = new Hypervisor(node.dag)
1053+
1054+ hypervisor.registerContainer('test', testVMContainer)
1055+
1056+ const rootContainer = await hypervisor.createInstance('test')
1057+
1058+ const [portRef1, portRef2] = rootContainer.ports.createChannel()
1059+ const initMessage = rootContainer.createMessage({
1060+ ports: [portRef2]
1061+ })
1062+
1063+ rootContainer.ports.create('test', initMessage)
1064+
1065+ rootContainer.ports.bind('first', portRef1)
1066+ const message = rootContainer.createMessage()
1067+ const rPort = rootContainer.getResponsePort(message)
1068+ const rPort2 = rootContainer.getResponsePort(message)
1069+
1070+ t.equals(rPort2, rPort)
1071+
1072+ rootContainer.send(portRef1, message)
1073+ rootContainer.ports.bind('response', rPort)
1074+ })
10351075 })

Built with git-ssb-web