Commit 4f7f80fb523a0e4362db909d00f48fe553b0880f
added tests
wanderer committed on 7/5/2017, 1:33:34 AMParent: 5ffd9fccb98bf042c2c2cce718f598e7708efcf4
Files changed
kernel.js | changed |
tests/index.js | changed |
kernel.js | ||
---|---|---|
@@ -75,8 +75,11 @@ | ||
75 | 75 | */ |
76 | 76 | async run (message, method = 'run') { |
77 | 77 | let result |
78 | 78 | |
79 | + const responsePort = message.responsePort | |
80 | + delete message.responsePort | |
81 | + | |
79 | 82 | message.ports.forEach(port => this.ports._unboundPorts.add(port)) |
80 | 83 | message._hops++ |
81 | 84 | |
82 | 85 | if (message.constructor === DeleteMessage) { |
@@ -90,17 +93,32 @@ | ||
90 | 93 | exceptionError: e |
91 | 94 | } |
92 | 95 | } |
93 | 96 | } |
97 | + | |
98 | + if (responsePort) { | |
99 | + this.send(responsePort, new Message({ | |
100 | + data: result | |
101 | + })) | |
102 | + this.ports._unboundPorts.add(responsePort) | |
103 | + } | |
104 | + | |
94 | 105 | this.ports.clearUnboundedPorts() |
95 | - // const responsePort = this.message.responsePort | |
96 | - // if (responsePort) { | |
97 | - // this.send(responsePort, new Message({data: result})) | |
98 | - // } | |
99 | 106 | this._runNextMessage() |
100 | 107 | return result |
101 | 108 | } |
102 | 109 | |
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 | + | |
103 | 121 | /** |
104 | 122 | * updates the number of ticks that the container has run |
105 | 123 | * @param {Number} count - the number of ticks to add |
106 | 124 | */ |
tests/index.js | ||
---|---|---|
@@ -1031,5 +1031,45 @@ | ||
1031 | 1031 | t.deepEquals(sr, expectedSr, 'should produce the corret state root') |
1032 | 1032 | |
1033 | 1033 | t.end() |
1034 | 1034 | }) |
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 | + }) | |
1035 | 1075 | }) |
Built with git-ssb-web