Skip to content
Snippets Groups Projects
Commit a6a9a6df authored by murgatroid99's avatar murgatroid99
Browse files

Add incompressible responses and status echoing to Node interop server

parent 77556c36
Branches
Tags
No related merge requests found
...@@ -44,6 +44,9 @@ var testProto = grpc.load({ ...@@ -44,6 +44,9 @@ var testProto = grpc.load({
var ECHO_INITIAL_KEY = 'x-grpc-test-echo-initial'; var ECHO_INITIAL_KEY = 'x-grpc-test-echo-initial';
var ECHO_TRAILING_KEY = 'x-grpc-test-echo-trailing-bin'; var ECHO_TRAILING_KEY = 'x-grpc-test-echo-trailing-bin';
var incompressible_data = fs.readFileSync(
__dirname + '/../../../test/cpp/interop/rnd.dat');
/** /**
* Create a buffer filled with size zeroes * Create a buffer filled with size zeroes
* @param {number} size The length of the buffer * @param {number} size The length of the buffer
...@@ -83,6 +86,19 @@ function getEchoTrailer(call) { ...@@ -83,6 +86,19 @@ function getEchoTrailer(call) {
return response_trailer; return response_trailer;
} }
function getPayload(payload_type, size) {
if (payload_type === 'RANDOM') {
payload_type = ['COMPRESSABLE',
'UNCOMPRESSABLE'][Math.random() < 0.5 ? 0 : 1];
}
var body;
switch (payload_type) {
case 'COMPRESSABLE': body = zeroBuffer(size); break;
case 'UNCOMPRESSABLE': incompressible_data.slice(size); break;
}
return {type: payload_type, body: body};
}
/** /**
* Respond to an empty parameter with an empty response. * Respond to an empty parameter with an empty response.
* NOTE: this currently does not work due to issue #137 * NOTE: this currently does not work due to issue #137
...@@ -104,13 +120,14 @@ function handleEmpty(call, callback) { ...@@ -104,13 +120,14 @@ function handleEmpty(call, callback) {
function handleUnary(call, callback) { function handleUnary(call, callback) {
echoHeader(call); echoHeader(call);
var req = call.request; var req = call.request;
var zeros = zeroBuffer(req.response_size); if (req.response_status) {
var payload_type = req.response_type; var status = req.response_status;
if (payload_type === 'RANDOM') { status.metadata = getEchoTrailer(call);
payload_type = ['COMPRESSABLE', callback(status);
'UNCOMPRESSABLE'][Math.random() < 0.5 ? 0 : 1]; return;
} }
callback(null, {payload: {type: payload_type, body: zeros}}, var payload = getPayload(req.response_type, req.response_size);
callback(null, {payload: payload},
getEchoTrailer(call)); getEchoTrailer(call));
} }
...@@ -139,18 +156,14 @@ function handleStreamingInput(call, callback) { ...@@ -139,18 +156,14 @@ function handleStreamingInput(call, callback) {
function handleStreamingOutput(call) { function handleStreamingOutput(call) {
echoHeader(call); echoHeader(call);
var req = call.request; var req = call.request;
var payload_type = req.response_type; if (req.response_status) {
if (payload_type === 'RANDOM') { var status = req.response_status;
payload_type = ['COMPRESSABLE', status.metadata = getEchoTrailer(call);
'UNCOMPRESSABLE'][Math.random() < 0.5 ? 0 : 1]; call.emit('error', status);
return;
} }
_.each(req.response_parameters, function(resp_param) { _.each(req.response_parameters, function(resp_param) {
call.write({ call.write({payload: getPayload(req.response_type, resp_param.size)});
payload: {
body: zeroBuffer(resp_param.size),
type: payload_type
}
});
}); });
call.end(getEchoTrailer(call)); call.end(getEchoTrailer(call));
} }
...@@ -163,18 +176,14 @@ function handleStreamingOutput(call) { ...@@ -163,18 +176,14 @@ function handleStreamingOutput(call) {
function handleFullDuplex(call) { function handleFullDuplex(call) {
echoHeader(call); echoHeader(call);
call.on('data', function(value) { call.on('data', function(value) {
var payload_type = value.response_type; if (value.response_status) {
if (payload_type === 'RANDOM') { var status = value.response_status;
payload_type = ['COMPRESSABLE', status.metadata = getEchoTrailer(call);
'UNCOMPRESSABLE'][Math.random() < 0.5 ? 0 : 1]; call.emit('error', status);
return;
} }
_.each(value.response_parameters, function(resp_param) { _.each(value.response_parameters, function(resp_param) {
call.write({ call.write({payload: getPayload(value.response_type, resp_param.size)});
payload: {
body: zeroBuffer(resp_param.size),
type: payload_type
}
});
}); });
}); });
call.on('end', function() { call.on('end', function() {
...@@ -188,7 +197,7 @@ function handleFullDuplex(call) { ...@@ -188,7 +197,7 @@ function handleFullDuplex(call) {
* @param {Call} call Call to handle * @param {Call} call Call to handle
*/ */
function handleHalfDuplex(call) { function handleHalfDuplex(call) {
throw new Error('HalfDuplexCall not yet implemented'); call.emit('error', Error('HalfDuplexCall not yet implemented'));
} }
/** /**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment