blob: 9005cbd505a7fbc7532882cc3eea3b9bdaa19620 [file] [log] [blame]
murgatroid99cca5ffa2015-01-15 14:06:56 -08001/*
2 *
Craig Tiller06059952015-02-18 08:34:56 -08003 * Copyright 2015, Google Inc.
murgatroid99cca5ffa2015-01-15 14:06:56 -08004 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
murgatroid99dca966d2015-02-19 14:37:18 -080034'use strict';
murgatroid99cca5ffa2015-01-15 14:06:56 -080035
murgatroid99dca966d2015-02-19 14:37:18 -080036var assert = require('assert');
murgatroid99cca5ffa2015-01-15 14:06:56 -080037
murgatroid9910ac96c2015-02-12 13:28:25 -080038var surface_client = require('../src/client.js');
murgatroid9955dd2ba2015-01-26 14:11:18 -080039
murgatroid99cca5ffa2015-01-15 14:06:56 -080040var ProtoBuf = require('protobufjs');
41
42var grpc = require('..');
43
44var math_proto = ProtoBuf.loadProtoFile(__dirname + '/../examples/math.proto');
45
46var mathService = math_proto.lookup('math.Math');
47
murgatroid9955739d52015-06-03 10:58:21 -070048var _ = require('lodash');
murgatroid99778c61b2015-05-18 16:52:47 -070049
murgatroid9971dbb862015-04-20 11:22:51 -070050describe('File loader', function() {
51 it('Should load a proto file by default', function() {
52 assert.doesNotThrow(function() {
53 grpc.load(__dirname + '/test_service.proto');
54 });
55 });
56 it('Should load a proto file with the proto format', function() {
57 assert.doesNotThrow(function() {
58 grpc.load(__dirname + '/test_service.proto', 'proto');
59 });
60 });
61 it('Should load a json file with the json format', function() {
62 assert.doesNotThrow(function() {
63 grpc.load(__dirname + '/test_service.json', 'json');
64 });
65 });
66 it('Should fail to load a file with an unknown format', function() {
67 assert.throws(function() {
68 grpc.load(__dirname + '/test_service.proto', 'fake_format');
69 });
70 });
71});
murgatroid99366e64d2015-07-15 17:01:49 -070072describe('Server.prototype.addProtoService', function() {
73 var server;
74 var dummyImpls = {
75 'div': function() {},
76 'divMany': function() {},
77 'fib': function() {},
78 'sum': function() {}
79 };
80 beforeEach(function() {
81 server = new grpc.Server();
82 });
83 afterEach(function() {
84 server.shutdown();
murgatroid99cca5ffa2015-01-15 14:06:56 -080085 });
86 it('Should succeed with a single service', function() {
87 assert.doesNotThrow(function() {
murgatroid99366e64d2015-07-15 17:01:49 -070088 server.addProtoService(mathService, dummyImpls);
89 });
90 });
91 it('Should fail with conflicting method names', function() {
92 server.addProtoService(mathService, dummyImpls);
93 assert.throws(function() {
94 server.addProtoService(mathService, dummyImpls);
murgatroid99cca5ffa2015-01-15 14:06:56 -080095 });
96 });
97 it('Should fail with missing handlers', function() {
murgatroid99cca5ffa2015-01-15 14:06:56 -080098 assert.throws(function() {
murgatroid99366e64d2015-07-15 17:01:49 -070099 server.addProtoService(mathService, {
100 'div': function() {},
101 'divMany': function() {},
102 'fib': function() {}
murgatroid99cca5ffa2015-01-15 14:06:56 -0800103 });
104 }, /math.Math.Sum/);
105 });
murgatroid99366e64d2015-07-15 17:01:49 -0700106 it('Should fail if the server has been started', function() {
107 server.start();
murgatroid99cca5ffa2015-01-15 14:06:56 -0800108 assert.throws(function() {
murgatroid99366e64d2015-07-15 17:01:49 -0700109 server.addProtoService(mathService, dummyImpls);
110 });
murgatroid99cca5ffa2015-01-15 14:06:56 -0800111 });
112});
murgatroid995828fb92015-05-11 14:27:39 -0700113describe('Echo service', function() {
114 var server;
115 var client;
116 before(function() {
117 var test_proto = ProtoBuf.loadProtoFile(__dirname + '/echo_service.proto');
118 var echo_service = test_proto.lookup('EchoService');
murgatroid99366e64d2015-07-15 17:01:49 -0700119 server = new grpc.Server();
120 server.addProtoService(echo_service, {
121 echo: function(call, callback) {
122 callback(null, call.request);
murgatroid995828fb92015-05-11 14:27:39 -0700123 }
124 });
125 var port = server.bind('localhost:0');
126 var Client = surface_client.makeProtobufClientConstructor(echo_service);
127 client = new Client('localhost:' + port);
murgatroid99366e64d2015-07-15 17:01:49 -0700128 server.start();
murgatroid995828fb92015-05-11 14:27:39 -0700129 });
130 after(function() {
131 server.shutdown();
132 });
133 it('should echo the recieved message directly', function(done) {
134 client.echo({value: 'test value', value2: 3}, function(error, response) {
135 assert.ifError(error);
136 assert.deepEqual(response, {value: 'test value', value2: 3});
137 done();
138 });
139 });
140});
murgatroid99e0f24dc2015-03-19 11:16:48 -0700141describe('Generic client and server', function() {
142 function toString(val) {
143 return val.toString();
144 }
145 function toBuffer(str) {
146 return new Buffer(str);
147 }
148 var string_service_attrs = {
149 'capitalize' : {
150 path: '/string/capitalize',
151 requestStream: false,
152 responseStream: false,
153 requestSerialize: toBuffer,
154 requestDeserialize: toString,
155 responseSerialize: toBuffer,
156 responseDeserialize: toString
157 }
158 };
159 describe('String client and server', function() {
160 var client;
161 var server;
162 before(function() {
murgatroid99366e64d2015-07-15 17:01:49 -0700163 server = new grpc.Server();
164 server.addService(string_service_attrs, {
165 capitalize: function(call, callback) {
166 callback(null, _.capitalize(call.request));
murgatroid99e0f24dc2015-03-19 11:16:48 -0700167 }
168 });
169 var port = server.bind('localhost:0');
murgatroid99366e64d2015-07-15 17:01:49 -0700170 server.start();
murgatroid99e0f24dc2015-03-19 11:16:48 -0700171 var Client = grpc.makeGenericClientConstructor(string_service_attrs);
172 client = new Client('localhost:' + port);
173 });
174 after(function() {
175 server.shutdown();
176 });
177 it('Should respond with a capitalized string', function(done) {
178 client.capitalize('abc', function(err, response) {
179 assert.ifError(err);
180 assert.strictEqual(response, 'Abc');
181 done();
182 });
183 });
184 });
185});
murgatroid99ef9fd532015-07-16 13:33:23 -0700186describe('Echo metadata', function() {
187 var client;
188 var server;
189 before(function() {
190 var test_proto = ProtoBuf.loadProtoFile(__dirname + '/test_service.proto');
191 var test_service = test_proto.lookup('TestService');
192 server = new grpc.Server();
193 server.addProtoService(test_service, {
194 unary: function(call, cb) {
195 call.sendMetadata(call.metadata);
196 cb(null, {});
197 },
198 clientStream: function(stream, cb){
199 stream.on('data', function(data) {});
200 stream.on('end', function() {
201 stream.sendMetadata(stream.metadata);
202 cb(null, {});
203 });
204 },
205 serverStream: function(stream) {
206 stream.sendMetadata(stream.metadata);
207 stream.end();
208 },
209 bidiStream: function(stream) {
210 stream.on('data', function(data) {});
211 stream.on('end', function() {
212 stream.sendMetadata(stream.metadata);
213 stream.end();
214 });
215 }
216 });
217 var port = server.bind('localhost:0');
218 var Client = surface_client.makeProtobufClientConstructor(test_service);
219 client = new Client('localhost:' + port);
220 server.start();
221 });
222 after(function() {
223 server.shutdown();
224 });
225 it('with unary call', function(done) {
226 var call = client.unary({}, function(err, data) {
227 assert.ifError(err);
228 }, {key: ['value']});
229 call.on('metadata', function(metadata) {
230 assert.deepEqual(metadata.key, ['value']);
231 done();
232 });
233 });
234 it('with client stream call', function(done) {
235 var call = client.clientStream(function(err, data) {
236 assert.ifError(err);
237 }, {key: ['value']});
238 call.on('metadata', function(metadata) {
239 assert.deepEqual(metadata.key, ['value']);
240 done();
241 });
242 call.end();
243 });
244 it('with server stream call', function(done) {
245 var call = client.serverStream({}, {key: ['value']});
246 call.on('data', function() {});
247 call.on('metadata', function(metadata) {
248 assert.deepEqual(metadata.key, ['value']);
249 done();
250 });
251 });
252 it('with bidi stream call', function(done) {
253 var call = client.bidiStream({key: ['value']});
254 call.on('data', function() {});
255 call.on('metadata', function(metadata) {
256 assert.deepEqual(metadata.key, ['value']);
257 done();
258 });
259 call.end();
260 });
murgatroid99198a1ad2015-07-21 14:27:56 -0700261 it('shows the correct user-agent string', function(done) {
262 var version = require('../package.json').version;
263 var call = client.unary({}, function(err, data) {
264 assert.ifError(err);
265 }, {key: ['value']});
266 call.on('metadata', function(metadata) {
267 assert(_.startsWith(metadata['user-agent'], 'grpc-node/' + version));
268 done();
269 });
270 });
murgatroid99ef9fd532015-07-16 13:33:23 -0700271});
murgatroid99778c61b2015-05-18 16:52:47 -0700272describe('Other conditions', function() {
murgatroid99d07a2782015-03-26 10:07:15 -0700273 var client;
274 var server;
murgatroid99778c61b2015-05-18 16:52:47 -0700275 var port;
murgatroid99d07a2782015-03-26 10:07:15 -0700276 before(function() {
277 var test_proto = ProtoBuf.loadProtoFile(__dirname + '/test_service.proto');
278 var test_service = test_proto.lookup('TestService');
murgatroid99366e64d2015-07-15 17:01:49 -0700279 server = new grpc.Server();
280 server.addProtoService(test_service, {
281 unary: function(call, cb) {
282 var req = call.request;
283 if (req.error) {
murgatroid9902c160a2015-07-20 15:16:09 -0700284 cb(new Error('Requested error'), null, {trailer_present: ['yes']});
murgatroid99366e64d2015-07-15 17:01:49 -0700285 } else {
murgatroid9902c160a2015-07-20 15:16:09 -0700286 cb(null, {count: 1}, {trailer_present: ['yes']});
murgatroid99366e64d2015-07-15 17:01:49 -0700287 }
288 },
289 clientStream: function(stream, cb){
290 var count = 0;
291 var errored;
292 stream.on('data', function(data) {
293 if (data.error) {
294 errored = true;
murgatroid9902c160a2015-07-20 15:16:09 -0700295 cb(new Error('Requested error'), null, {trailer_present: ['yes']});
murgatroid99d07a2782015-03-26 10:07:15 -0700296 } else {
murgatroid99366e64d2015-07-15 17:01:49 -0700297 count += 1;
murgatroid99d07a2782015-03-26 10:07:15 -0700298 }
murgatroid99366e64d2015-07-15 17:01:49 -0700299 });
300 stream.on('end', function() {
301 if (!errored) {
murgatroid9902c160a2015-07-20 15:16:09 -0700302 cb(null, {count: count}, {trailer_present: ['yes']});
murgatroid99366e64d2015-07-15 17:01:49 -0700303 }
304 });
305 },
306 serverStream: function(stream) {
307 var req = stream.request;
308 if (req.error) {
309 var err = new Error('Requested error');
murgatroid9902c160a2015-07-20 15:16:09 -0700310 err.metadata = {trailer_present: ['yes']};
murgatroid99366e64d2015-07-15 17:01:49 -0700311 stream.emit('error', err);
312 } else {
313 for (var i = 0; i < 5; i++) {
314 stream.write({count: i});
315 }
murgatroid9902c160a2015-07-20 15:16:09 -0700316 stream.end({trailer_present: ['yes']});
murgatroid99366e64d2015-07-15 17:01:49 -0700317 }
318 },
319 bidiStream: function(stream) {
320 var count = 0;
321 stream.on('data', function(data) {
322 if (data.error) {
murgatroid99d07a2782015-03-26 10:07:15 -0700323 var err = new Error('Requested error');
murgatroid99366e64d2015-07-15 17:01:49 -0700324 err.metadata = {
murgatroid9902c160a2015-07-20 15:16:09 -0700325 trailer_present: ['yes'],
murgatroid99366e64d2015-07-15 17:01:49 -0700326 count: ['' + count]
327 };
murgatroid99eb21bdd2015-03-26 15:19:54 -0700328 stream.emit('error', err);
murgatroid99d07a2782015-03-26 10:07:15 -0700329 } else {
murgatroid99366e64d2015-07-15 17:01:49 -0700330 stream.write({count: count});
331 count += 1;
murgatroid99d07a2782015-03-26 10:07:15 -0700332 }
murgatroid99366e64d2015-07-15 17:01:49 -0700333 });
334 stream.on('end', function() {
murgatroid9902c160a2015-07-20 15:16:09 -0700335 stream.end({trailer_present: ['yes']});
murgatroid99366e64d2015-07-15 17:01:49 -0700336 });
murgatroid99d07a2782015-03-26 10:07:15 -0700337 }
338 });
murgatroid99778c61b2015-05-18 16:52:47 -0700339 port = server.bind('localhost:0');
murgatroid99d07a2782015-03-26 10:07:15 -0700340 var Client = surface_client.makeProtobufClientConstructor(test_service);
341 client = new Client('localhost:' + port);
murgatroid99366e64d2015-07-15 17:01:49 -0700342 server.start();
murgatroid99d07a2782015-03-26 10:07:15 -0700343 });
344 after(function() {
345 server.shutdown();
346 });
murgatroid99ea12b972015-07-24 10:43:27 -0700347 it('channel.getTarget should be available', function() {
348 assert.strictEqual(typeof client.channel.getTarget(), 'string');
349 });
murgatroid99778c61b2015-05-18 16:52:47 -0700350 describe('Server recieving bad input', function() {
351 var misbehavingClient;
352 var badArg = new Buffer([0xFF]);
353 before(function() {
354 var test_service_attrs = {
355 unary: {
356 path: '/TestService/Unary',
357 requestStream: false,
358 responseStream: false,
359 requestSerialize: _.identity,
360 responseDeserialize: _.identity
361 },
362 clientStream: {
363 path: '/TestService/ClientStream',
364 requestStream: true,
365 responseStream: false,
366 requestSerialize: _.identity,
367 responseDeserialize: _.identity
368 },
369 serverStream: {
370 path: '/TestService/ServerStream',
371 requestStream: false,
372 responseStream: true,
373 requestSerialize: _.identity,
374 responseDeserialize: _.identity
375 },
376 bidiStream: {
377 path: '/TestService/BidiStream',
378 requestStream: true,
379 responseStream: true,
380 requestSerialize: _.identity,
381 responseDeserialize: _.identity
382 }
383 };
384 var Client = surface_client.makeClientConstructor(test_service_attrs,
385 'TestService');
386 misbehavingClient = new Client('localhost:' + port);
murgatroid99d07a2782015-03-26 10:07:15 -0700387 });
murgatroid99778c61b2015-05-18 16:52:47 -0700388 it('should respond correctly to a unary call', function(done) {
murgatroid9904589a72015-05-19 09:51:26 -0700389 misbehavingClient.unary(badArg, function(err, data) {
murgatroid99778c61b2015-05-18 16:52:47 -0700390 assert(err);
391 assert.strictEqual(err.code, grpc.status.INVALID_ARGUMENT);
392 done();
393 });
394 });
395 it('should respond correctly to a client stream', function(done) {
396 var call = misbehavingClient.clientStream(function(err, data) {
397 assert(err);
398 assert.strictEqual(err.code, grpc.status.INVALID_ARGUMENT);
399 done();
400 });
401 call.write(badArg);
murgatroid9904589a72015-05-19 09:51:26 -0700402 // TODO(mlumish): Remove call.end()
403 call.end();
murgatroid99778c61b2015-05-18 16:52:47 -0700404 });
405 it('should respond correctly to a server stream', function(done) {
406 var call = misbehavingClient.serverStream(badArg);
407 call.on('data', function(data) {
murgatroid9904589a72015-05-19 09:51:26 -0700408 assert.fail(data, null, 'Unexpected data', '===');
murgatroid99778c61b2015-05-18 16:52:47 -0700409 });
410 call.on('error', function(err) {
411 assert.strictEqual(err.code, grpc.status.INVALID_ARGUMENT);
412 done();
413 });
414 });
415 it('should respond correctly to a bidi stream', function(done) {
416 var call = misbehavingClient.bidiStream();
417 call.on('data', function(data) {
murgatroid9904589a72015-05-19 09:51:26 -0700418 assert.fail(data, null, 'Unexpected data', '===');
murgatroid99778c61b2015-05-18 16:52:47 -0700419 });
420 call.on('error', function(err) {
421 assert.strictEqual(err.code, grpc.status.INVALID_ARGUMENT);
422 done();
423 });
424 call.write(badArg);
murgatroid9904589a72015-05-19 09:51:26 -0700425 // TODO(mlumish): Remove call.end()
426 call.end();
murgatroid99d07a2782015-03-26 10:07:15 -0700427 });
428 });
murgatroid99778c61b2015-05-18 16:52:47 -0700429 describe('Trailing metadata', function() {
430 it('should be present when a unary call succeeds', function(done) {
431 var call = client.unary({error: false}, function(err, data) {
432 assert.ifError(err);
433 });
434 call.on('status', function(status) {
murgatroid9902c160a2015-07-20 15:16:09 -0700435 assert.deepEqual(status.metadata.trailer_present, ['yes']);
murgatroid99778c61b2015-05-18 16:52:47 -0700436 done();
437 });
murgatroid99d07a2782015-03-26 10:07:15 -0700438 });
murgatroid99778c61b2015-05-18 16:52:47 -0700439 it('should be present when a unary call fails', function(done) {
440 var call = client.unary({error: true}, function(err, data) {
441 assert(err);
442 });
443 call.on('status', function(status) {
murgatroid9902c160a2015-07-20 15:16:09 -0700444 assert.deepEqual(status.metadata.trailer_present, ['yes']);
murgatroid99778c61b2015-05-18 16:52:47 -0700445 done();
446 });
murgatroid99d07a2782015-03-26 10:07:15 -0700447 });
murgatroid99778c61b2015-05-18 16:52:47 -0700448 it('should be present when a client stream call succeeds', function(done) {
449 var call = client.clientStream(function(err, data) {
450 assert.ifError(err);
451 });
452 call.write({error: false});
453 call.write({error: false});
454 call.end();
455 call.on('status', function(status) {
murgatroid9902c160a2015-07-20 15:16:09 -0700456 assert.deepEqual(status.metadata.trailer_present, ['yes']);
murgatroid99778c61b2015-05-18 16:52:47 -0700457 done();
458 });
murgatroid99d07a2782015-03-26 10:07:15 -0700459 });
murgatroid99778c61b2015-05-18 16:52:47 -0700460 it('should be present when a client stream call fails', function(done) {
461 var call = client.clientStream(function(err, data) {
462 assert(err);
463 });
464 call.write({error: false});
465 call.write({error: true});
466 call.end();
467 call.on('status', function(status) {
murgatroid9902c160a2015-07-20 15:16:09 -0700468 assert.deepEqual(status.metadata.trailer_present, ['yes']);
murgatroid99778c61b2015-05-18 16:52:47 -0700469 done();
470 });
murgatroid99d07a2782015-03-26 10:07:15 -0700471 });
murgatroid99778c61b2015-05-18 16:52:47 -0700472 it('should be present when a server stream call succeeds', function(done) {
473 var call = client.serverStream({error: false});
474 call.on('data', function(){});
475 call.on('status', function(status) {
476 assert.strictEqual(status.code, grpc.status.OK);
murgatroid9902c160a2015-07-20 15:16:09 -0700477 assert.deepEqual(status.metadata.trailer_present, ['yes']);
murgatroid99778c61b2015-05-18 16:52:47 -0700478 done();
479 });
murgatroid99d07a2782015-03-26 10:07:15 -0700480 });
murgatroid99778c61b2015-05-18 16:52:47 -0700481 it('should be present when a server stream call fails', function(done) {
482 var call = client.serverStream({error: true});
483 call.on('data', function(){});
484 call.on('error', function(error) {
murgatroid9902c160a2015-07-20 15:16:09 -0700485 assert.deepEqual(error.metadata.trailer_present, ['yes']);
murgatroid99778c61b2015-05-18 16:52:47 -0700486 done();
487 });
murgatroid99d07a2782015-03-26 10:07:15 -0700488 });
murgatroid99778c61b2015-05-18 16:52:47 -0700489 it('should be present when a bidi stream succeeds', function(done) {
490 var call = client.bidiStream();
491 call.write({error: false});
492 call.write({error: false});
493 call.end();
494 call.on('data', function(){});
495 call.on('status', function(status) {
496 assert.strictEqual(status.code, grpc.status.OK);
murgatroid9902c160a2015-07-20 15:16:09 -0700497 assert.deepEqual(status.metadata.trailer_present, ['yes']);
murgatroid99778c61b2015-05-18 16:52:47 -0700498 done();
499 });
murgatroid99eb21bdd2015-03-26 15:19:54 -0700500 });
murgatroid99778c61b2015-05-18 16:52:47 -0700501 it('should be present when a bidi stream fails', function(done) {
502 var call = client.bidiStream();
503 call.write({error: false});
504 call.write({error: true});
505 call.end();
506 call.on('data', function(){});
507 call.on('error', function(error) {
murgatroid9902c160a2015-07-20 15:16:09 -0700508 assert.deepEqual(error.metadata.trailer_present, ['yes']);
murgatroid99778c61b2015-05-18 16:52:47 -0700509 done();
510 });
murgatroid99eb21bdd2015-03-26 15:19:54 -0700511 });
512 });
murgatroid9906f09722015-07-17 11:26:54 -0700513 describe('Error object should contain the status', function() {
514 it('for a unary call', function(done) {
515 client.unary({error: true}, function(err, data) {
516 assert(err);
murgatroid99560b82c2015-07-17 13:06:15 -0700517 assert.strictEqual(err.code, grpc.status.UNKNOWN);
murgatroid9906f09722015-07-17 11:26:54 -0700518 assert.strictEqual(err.message, 'Requested error');
519 done();
520 });
521 });
522 it('for a client stream call', function(done) {
523 var call = client.clientStream(function(err, data) {
524 assert(err);
murgatroid99560b82c2015-07-17 13:06:15 -0700525 assert.strictEqual(err.code, grpc.status.UNKNOWN);
murgatroid9906f09722015-07-17 11:26:54 -0700526 assert.strictEqual(err.message, 'Requested error');
527 done();
528 });
529 call.write({error: false});
530 call.write({error: true});
531 call.end();
532 });
533 it('for a server stream call', function(done) {
534 var call = client.serverStream({error: true});
535 call.on('data', function(){});
536 call.on('error', function(error) {
murgatroid99560b82c2015-07-17 13:06:15 -0700537 assert.strictEqual(error.code, grpc.status.UNKNOWN);
murgatroid9906f09722015-07-17 11:26:54 -0700538 assert.strictEqual(error.message, 'Requested error');
539 done();
540 });
541 });
542 it('for a bidi stream call', function(done) {
543 var call = client.bidiStream();
544 call.write({error: false});
545 call.write({error: true});
546 call.end();
547 call.on('data', function(){});
548 call.on('error', function(error) {
murgatroid99560b82c2015-07-17 13:06:15 -0700549 assert.strictEqual(error.code, grpc.status.UNKNOWN);
murgatroid9906f09722015-07-17 11:26:54 -0700550 assert.strictEqual(error.message, 'Requested error');
551 done();
552 });
553 });
554 });
murgatroid99ea12b972015-07-24 10:43:27 -0700555 describe('call.getPeer should return the peer', function() {
556 it('for a unary call', function(done) {
557 var call = client.unary({error: false}, function(err, data) {
558 assert.ifError(err);
559 done();
560 });
561 assert.strictEqual(typeof call.getPeer(), 'string');
562 });
563 it('for a client stream call', function(done) {
564 var call = client.clientStream(function(err, data) {
565 assert.ifError(err);
566 done();
567 });
568 assert.strictEqual(typeof call.getPeer(), 'string');
569 call.write({error: false});
570 call.end();
571 });
572 it('for a server stream call', function(done) {
573 var call = client.serverStream({error: false});
574 assert.strictEqual(typeof call.getPeer(), 'string');
575 call.on('data', function(){});
576 call.on('status', function(status) {
577 assert.strictEqual(status.code, grpc.status.OK);
578 done();
579 });
580 });
581 it('for a bidi stream call', function(done) {
582 var call = client.bidiStream();
583 assert.strictEqual(typeof call.getPeer(), 'string');
584 call.write({error: false});
585 call.end();
586 call.on('data', function(){});
587 call.on('status', function(status) {
588 done();
589 });
590 });
591 });
murgatroid99d07a2782015-03-26 10:07:15 -0700592});
murgatroid994d2d0f02015-01-29 11:44:46 -0800593describe('Cancelling surface client', function() {
murgatroid9955dd2ba2015-01-26 14:11:18 -0800594 var client;
595 var server;
596 before(function() {
murgatroid99366e64d2015-07-15 17:01:49 -0700597 server = new grpc.Server();
598 server.addProtoService(mathService, {
599 'div': function(stream) {},
600 'divMany': function(stream) {},
601 'fib': function(stream) {},
602 'sum': function(stream) {}
murgatroid9955dd2ba2015-01-26 14:11:18 -0800603 });
604 var port = server.bind('localhost:0');
murgatroid99e023e982015-03-18 17:17:33 -0700605 var Client = surface_client.makeProtobufClientConstructor(mathService);
murgatroid9955dd2ba2015-01-26 14:11:18 -0800606 client = new Client('localhost:' + port);
murgatroid99366e64d2015-07-15 17:01:49 -0700607 server.start();
murgatroid9955dd2ba2015-01-26 14:11:18 -0800608 });
609 after(function() {
610 server.shutdown();
611 });
612 it('Should correctly cancel a unary call', function(done) {
613 var call = client.div({'divisor': 0, 'dividend': 0}, function(err, resp) {
614 assert.strictEqual(err.code, surface_client.status.CANCELLED);
615 done();
616 });
617 call.cancel();
618 });
619 it('Should correctly cancel a client stream call', function(done) {
620 var call = client.sum(function(err, resp) {
621 assert.strictEqual(err.code, surface_client.status.CANCELLED);
622 done();
623 });
624 call.cancel();
625 });
626 it('Should correctly cancel a server stream call', function(done) {
627 var call = client.fib({'limit': 5});
murgatroid9965b784e2015-05-06 16:46:19 -0700628 call.on('error', function(error) {
629 assert.strictEqual(error.code, surface_client.status.CANCELLED);
murgatroid9955dd2ba2015-01-26 14:11:18 -0800630 done();
631 });
632 call.cancel();
633 });
634 it('Should correctly cancel a bidi stream call', function(done) {
635 var call = client.divMany();
murgatroid9965b784e2015-05-06 16:46:19 -0700636 call.on('error', function(error) {
637 assert.strictEqual(error.code, surface_client.status.CANCELLED);
murgatroid9955dd2ba2015-01-26 14:11:18 -0800638 done();
639 });
640 call.cancel();
641 });
Craig Tillerce5021b2015-02-18 09:25:21 -0800642});