Add incompressible responses and status echoing to Node interop server

pull/3746/head
murgatroid99 9 years ago
parent 77556c36bb
commit a6a9a6df85
  1. 63
      src/node/interop/interop_server.js

@ -44,6 +44,9 @@ var testProto = grpc.load({
var ECHO_INITIAL_KEY = 'x-grpc-test-echo-initial';
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
* @param {number} size The length of the buffer
@ -83,6 +86,19 @@ function getEchoTrailer(call) {
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.
* NOTE: this currently does not work due to issue #137
@ -104,13 +120,14 @@ function handleEmpty(call, callback) {
function handleUnary(call, callback) {
echoHeader(call);
var req = call.request;
var zeros = zeroBuffer(req.response_size);
var payload_type = req.response_type;
if (payload_type === 'RANDOM') {
payload_type = ['COMPRESSABLE',
'UNCOMPRESSABLE'][Math.random() < 0.5 ? 0 : 1];
if (req.response_status) {
var status = req.response_status;
status.metadata = getEchoTrailer(call);
callback(status);
return;
}
callback(null, {payload: {type: payload_type, body: zeros}},
var payload = getPayload(req.response_type, req.response_size);
callback(null, {payload: payload},
getEchoTrailer(call));
}
@ -139,18 +156,14 @@ function handleStreamingInput(call, callback) {
function handleStreamingOutput(call) {
echoHeader(call);
var req = call.request;
var payload_type = req.response_type;
if (payload_type === 'RANDOM') {
payload_type = ['COMPRESSABLE',
'UNCOMPRESSABLE'][Math.random() < 0.5 ? 0 : 1];
if (req.response_status) {
var status = req.response_status;
status.metadata = getEchoTrailer(call);
call.emit('error', status);
return;
}
_.each(req.response_parameters, function(resp_param) {
call.write({
payload: {
body: zeroBuffer(resp_param.size),
type: payload_type
}
});
call.write({payload: getPayload(req.response_type, resp_param.size)});
});
call.end(getEchoTrailer(call));
}
@ -163,18 +176,14 @@ function handleStreamingOutput(call) {
function handleFullDuplex(call) {
echoHeader(call);
call.on('data', function(value) {
var payload_type = value.response_type;
if (payload_type === 'RANDOM') {
payload_type = ['COMPRESSABLE',
'UNCOMPRESSABLE'][Math.random() < 0.5 ? 0 : 1];
if (value.response_status) {
var status = value.response_status;
status.metadata = getEchoTrailer(call);
call.emit('error', status);
return;
}
_.each(value.response_parameters, function(resp_param) {
call.write({
payload: {
body: zeroBuffer(resp_param.size),
type: payload_type
}
});
call.write({payload: getPayload(value.response_type, resp_param.size)});
});
});
call.on('end', function() {
@ -188,7 +197,7 @@ function handleFullDuplex(call) {
* @param {Call} call Call to handle
*/
function handleHalfDuplex(call) {
throw new Error('HalfDuplexCall not yet implemented');
call.emit('error', Error('HalfDuplexCall not yet implemented'));
}
/**

Loading…
Cancel
Save