|
|
@ -32,13 +32,14 @@ |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
var assert = require('assert'); |
|
|
|
var assert = require('assert'); |
|
|
|
var client = require('../surface_client.js'); |
|
|
|
|
|
|
|
var ProtoBuf = require('protobufjs'); |
|
|
|
var ProtoBuf = require('protobufjs'); |
|
|
|
var port_picker = require('../port_picker'); |
|
|
|
var port_picker = require('../port_picker'); |
|
|
|
|
|
|
|
|
|
|
|
var builder = ProtoBuf.loadProtoFile(__dirname + '/../examples/math.proto'); |
|
|
|
var builder = ProtoBuf.loadProtoFile(__dirname + '/../examples/math.proto'); |
|
|
|
var math = builder.build('math'); |
|
|
|
var math = builder.build('math'); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var client = require('../surface_client.js'); |
|
|
|
|
|
|
|
var makeConstructor = client.makeClientConstructor; |
|
|
|
/** |
|
|
|
/** |
|
|
|
* Get a function that deserializes a specific type of protobuf. |
|
|
|
* Get a function that deserializes a specific type of protobuf. |
|
|
|
* @param {function()} cls The constructor of the message type to deserialize |
|
|
|
* @param {function()} cls The constructor of the message type to deserialize |
|
|
@ -56,78 +57,60 @@ function deserializeCls(cls) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Serialize an object to a buffer |
|
|
|
* Get a function that serializes objects to a buffer by protobuf class. |
|
|
|
* @param {*} arg The object to serialize |
|
|
|
* @param {function()} Cls The constructor of the message type to serialize |
|
|
|
* @return {Buffer} The serialized object |
|
|
|
* @return {function(Cls):Buffer} The serialization function |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
function serialize(arg) { |
|
|
|
function serializeCls(Cls) { |
|
|
|
return new Buffer(arg.encode().toBuffer()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Sends a Div request on the channel. |
|
|
|
* Serialize an object to a Buffer |
|
|
|
* @param {client.Channel} channel The channel on which to make the request |
|
|
|
* @param {Object} arg The object to serialize |
|
|
|
* @param {DivArg} argument The argument to the call. Should be serializable |
|
|
|
* @return {Buffer} The serialized object |
|
|
|
* with serialize |
|
|
|
|
|
|
|
* @param {function(?Error, value=)} The callback to for when the response is |
|
|
|
|
|
|
|
* received |
|
|
|
|
|
|
|
* @param {array=} Array of metadata key/value pairs to add to the call |
|
|
|
|
|
|
|
* @param {(number|Date)=} deadline The deadline for processing this request. |
|
|
|
|
|
|
|
* Defaults to infinite future |
|
|
|
|
|
|
|
* @return {EventEmitter} An event emitter for stream related events |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
var div = client.makeUnaryRequestFunction( |
|
|
|
|
|
|
|
'/Math/Div', |
|
|
|
|
|
|
|
serialize, |
|
|
|
|
|
|
|
deserializeCls(math.DivReply)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Sends a Fib request on the channel. |
|
|
|
|
|
|
|
* @param {client.Channel} channel The channel on which to make the request |
|
|
|
|
|
|
|
* @param {*} argument The argument to the call. Should be serializable with |
|
|
|
|
|
|
|
* serialize |
|
|
|
|
|
|
|
* @param {array=} Array of metadata key/value pairs to add to the call |
|
|
|
|
|
|
|
* @param {(number|Date)=} deadline The deadline for processing this request. |
|
|
|
|
|
|
|
* Defaults to infinite future |
|
|
|
|
|
|
|
* @return {EventEmitter} An event emitter for stream related events |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
var fib = client.makeServerStreamRequestFunction( |
|
|
|
|
|
|
|
'/Math/Fib', |
|
|
|
|
|
|
|
serialize, |
|
|
|
|
|
|
|
deserializeCls(math.Num)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Sends a Sum request on the channel. |
|
|
|
|
|
|
|
* @param {client.Channel} channel The channel on which to make the request |
|
|
|
|
|
|
|
* @param {function(?Error, value=)} The callback to for when the response is |
|
|
|
|
|
|
|
* received |
|
|
|
|
|
|
|
* @param {array=} Array of metadata key/value pairs to add to the call |
|
|
|
|
|
|
|
* @param {(number|Date)=} deadline The deadline for processing this request. |
|
|
|
|
|
|
|
* Defaults to infinite future |
|
|
|
|
|
|
|
* @return {EventEmitter} An event emitter for stream related events |
|
|
|
|
|
|
|
*/ |
|
|
|
*/ |
|
|
|
var sum = client.makeClientStreamRequestFunction( |
|
|
|
return function serialize(arg) { |
|
|
|
'/Math/Sum', |
|
|
|
return new Buffer(new Cls(arg).encode().toBuffer()); |
|
|
|
serialize, |
|
|
|
}; |
|
|
|
deserializeCls(math.Num)); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/* This function call creates a client constructor for clients that expose the |
|
|
|
* Sends a DivMany request on the channel. |
|
|
|
* four specified methods. This specifies how to serialize messages that the |
|
|
|
* @param {client.Channel} channel The channel on which to make the request |
|
|
|
* client sends and deserialize messages that the server sends, and whether the |
|
|
|
* @param {array=} Array of metadata key/value pairs to add to the call |
|
|
|
* client or the server will send a stream of messages, for each method. This |
|
|
|
* @param {(number|Date)=} deadline The deadline for processing this request. |
|
|
|
* also specifies a prefix that will be added to method names when sending them |
|
|
|
* Defaults to infinite future |
|
|
|
* on the wire. This function call and all of the preceding code in this file |
|
|
|
* @return {EventEmitter} An event emitter for stream related events |
|
|
|
* are intended to approximate what the generated code will look like for the |
|
|
|
*/ |
|
|
|
* math client */ |
|
|
|
var divMany = client.makeBidiStreamRequestFunction( |
|
|
|
var MathClient = makeConstructor({ |
|
|
|
'/Math/DivMany', |
|
|
|
Div: { |
|
|
|
serialize, |
|
|
|
serialize: serializeCls(math.DivArgs), |
|
|
|
deserializeCls(math.DivReply)); |
|
|
|
deserialize: deserializeCls(math.DivReply), |
|
|
|
|
|
|
|
client_stream: false, |
|
|
|
|
|
|
|
server_stream: false |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
Fib: { |
|
|
|
|
|
|
|
serialize: serializeCls(math.FibArgs), |
|
|
|
|
|
|
|
deserialize: deserializeCls(math.Num), |
|
|
|
|
|
|
|
client_stream: false, |
|
|
|
|
|
|
|
server_stream: true |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
Sum: { |
|
|
|
|
|
|
|
serialize: serializeCls(math.Num), |
|
|
|
|
|
|
|
deserialize: deserializeCls(math.Num), |
|
|
|
|
|
|
|
client_stream: true, |
|
|
|
|
|
|
|
server_stream: false |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
DivMany: { |
|
|
|
|
|
|
|
serialize: serializeCls(math.DivArgs), |
|
|
|
|
|
|
|
deserialize: deserializeCls(math.DivReply), |
|
|
|
|
|
|
|
client_stream: true, |
|
|
|
|
|
|
|
server_stream: true |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}, '/Math/'); |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Channel to use to make requests to a running server. |
|
|
|
* Channel to use to make requests to a running server. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
var channel; |
|
|
|
var math_client; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Server to test against |
|
|
|
* Server to test against |
|
|
@ -139,7 +122,7 @@ describe('Math client', function() { |
|
|
|
before(function(done) { |
|
|
|
before(function(done) { |
|
|
|
port_picker.nextAvailablePort(function(port) { |
|
|
|
port_picker.nextAvailablePort(function(port) { |
|
|
|
server.bind(port).listen(); |
|
|
|
server.bind(port).listen(); |
|
|
|
channel = new client.Channel(port); |
|
|
|
math_client = new MathClient(port); |
|
|
|
done(); |
|
|
|
done(); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
@ -147,11 +130,11 @@ describe('Math client', function() { |
|
|
|
server.shutdown(); |
|
|
|
server.shutdown(); |
|
|
|
}); |
|
|
|
}); |
|
|
|
it('should handle a single request', function(done) { |
|
|
|
it('should handle a single request', function(done) { |
|
|
|
var arg = new math.DivArgs({dividend: 7, divisor: 4}); |
|
|
|
var arg = {dividend: 7, divisor: 4}; |
|
|
|
var call = div(channel, arg, function handleDivResult(err, value) { |
|
|
|
var call = math_client.Div(arg, function handleDivResult(err, value) { |
|
|
|
assert.ifError(err); |
|
|
|
assert.ifError(err); |
|
|
|
assert.equal(value.get('quotient'), 1); |
|
|
|
assert.equal(value.quotient, 1); |
|
|
|
assert.equal(value.get('remainder'), 3); |
|
|
|
assert.equal(value.remainder, 3); |
|
|
|
}); |
|
|
|
}); |
|
|
|
call.on('status', function checkStatus(status) { |
|
|
|
call.on('status', function checkStatus(status) { |
|
|
|
assert.strictEqual(status.code, client.status.OK); |
|
|
|
assert.strictEqual(status.code, client.status.OK); |
|
|
@ -159,12 +142,11 @@ describe('Math client', function() { |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
it('should handle a server streaming request', function(done) { |
|
|
|
it('should handle a server streaming request', function(done) { |
|
|
|
var arg = new math.FibArgs({limit: 7}); |
|
|
|
var call = math_client.Fib({limit: 7}); |
|
|
|
var call = fib(channel, arg); |
|
|
|
|
|
|
|
var expected_results = [1, 1, 2, 3, 5, 8, 13]; |
|
|
|
var expected_results = [1, 1, 2, 3, 5, 8, 13]; |
|
|
|
var next_expected = 0; |
|
|
|
var next_expected = 0; |
|
|
|
call.on('data', function checkResponse(value) { |
|
|
|
call.on('data', function checkResponse(value) { |
|
|
|
assert.equal(value.get('num'), expected_results[next_expected]); |
|
|
|
assert.equal(value.num, expected_results[next_expected]); |
|
|
|
next_expected += 1; |
|
|
|
next_expected += 1; |
|
|
|
}); |
|
|
|
}); |
|
|
|
call.on('status', function checkStatus(status) { |
|
|
|
call.on('status', function checkStatus(status) { |
|
|
@ -173,12 +155,12 @@ describe('Math client', function() { |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
it('should handle a client streaming request', function(done) { |
|
|
|
it('should handle a client streaming request', function(done) { |
|
|
|
var call = sum(channel, function handleSumResult(err, value) { |
|
|
|
var call = math_client.Sum(function handleSumResult(err, value) { |
|
|
|
assert.ifError(err); |
|
|
|
assert.ifError(err); |
|
|
|
assert.equal(value.get('num'), 21); |
|
|
|
assert.equal(value.num, 21); |
|
|
|
}); |
|
|
|
}); |
|
|
|
for (var i = 0; i < 7; i++) { |
|
|
|
for (var i = 0; i < 7; i++) { |
|
|
|
call.write(new math.Num({'num': i})); |
|
|
|
call.write({'num': i}); |
|
|
|
} |
|
|
|
} |
|
|
|
call.end(); |
|
|
|
call.end(); |
|
|
|
call.on('status', function checkStatus(status) { |
|
|
|
call.on('status', function checkStatus(status) { |
|
|
@ -188,17 +170,17 @@ describe('Math client', function() { |
|
|
|
}); |
|
|
|
}); |
|
|
|
it('should handle a bidirectional streaming request', function(done) { |
|
|
|
it('should handle a bidirectional streaming request', function(done) { |
|
|
|
function checkResponse(index, value) { |
|
|
|
function checkResponse(index, value) { |
|
|
|
assert.equal(value.get('quotient'), index); |
|
|
|
assert.equal(value.quotient, index); |
|
|
|
assert.equal(value.get('remainder'), 1); |
|
|
|
assert.equal(value.remainder, 1); |
|
|
|
} |
|
|
|
} |
|
|
|
var call = divMany(channel); |
|
|
|
var call = math_client.DivMany(); |
|
|
|
var response_index = 0; |
|
|
|
var response_index = 0; |
|
|
|
call.on('data', function(value) { |
|
|
|
call.on('data', function(value) { |
|
|
|
checkResponse(response_index, value); |
|
|
|
checkResponse(response_index, value); |
|
|
|
response_index += 1; |
|
|
|
response_index += 1; |
|
|
|
}); |
|
|
|
}); |
|
|
|
for (var i = 0; i < 7; i++) { |
|
|
|
for (var i = 0; i < 7; i++) { |
|
|
|
call.write(new math.DivArgs({dividend: 2 * i + 1, divisor: 2})); |
|
|
|
call.write({dividend: 2 * i + 1, divisor: 2}); |
|
|
|
} |
|
|
|
} |
|
|
|
call.end(); |
|
|
|
call.end(); |
|
|
|
call.on('status', function checkStatus(status) { |
|
|
|
call.on('status', function checkStatus(status) { |
|
|
|