blob: 18178e49e403d240a149a4bc905159dd2193c07c [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 });
261});
murgatroid99778c61b2015-05-18 16:52:47 -0700262describe('Other conditions', function() {
murgatroid99d07a2782015-03-26 10:07:15 -0700263 var client;
264 var server;
murgatroid99778c61b2015-05-18 16:52:47 -0700265 var port;
murgatroid99d07a2782015-03-26 10:07:15 -0700266 before(function() {
267 var test_proto = ProtoBuf.loadProtoFile(__dirname + '/test_service.proto');
268 var test_service = test_proto.lookup('TestService');
murgatroid99366e64d2015-07-15 17:01:49 -0700269 server = new grpc.Server();
270 server.addProtoService(test_service, {
271 unary: function(call, cb) {
272 var req = call.request;
273 if (req.error) {
murgatroid9902c160a2015-07-20 15:16:09 -0700274 cb(new Error('Requested error'), null, {trailer_present: ['yes']});
murgatroid99366e64d2015-07-15 17:01:49 -0700275 } else {
murgatroid9902c160a2015-07-20 15:16:09 -0700276 cb(null, {count: 1}, {trailer_present: ['yes']});
murgatroid99366e64d2015-07-15 17:01:49 -0700277 }
278 },
279 clientStream: function(stream, cb){
280 var count = 0;
281 var errored;
282 stream.on('data', function(data) {
283 if (data.error) {
284 errored = true;
murgatroid9902c160a2015-07-20 15:16:09 -0700285 cb(new Error('Requested error'), null, {trailer_present: ['yes']});
murgatroid99d07a2782015-03-26 10:07:15 -0700286 } else {
murgatroid99366e64d2015-07-15 17:01:49 -0700287 count += 1;
murgatroid99d07a2782015-03-26 10:07:15 -0700288 }
murgatroid99366e64d2015-07-15 17:01:49 -0700289 });
290 stream.on('end', function() {
291 if (!errored) {
murgatroid9902c160a2015-07-20 15:16:09 -0700292 cb(null, {count: count}, {trailer_present: ['yes']});
murgatroid99366e64d2015-07-15 17:01:49 -0700293 }
294 });
295 },
296 serverStream: function(stream) {
297 var req = stream.request;
298 if (req.error) {
299 var err = new Error('Requested error');
murgatroid9902c160a2015-07-20 15:16:09 -0700300 err.metadata = {trailer_present: ['yes']};
murgatroid99366e64d2015-07-15 17:01:49 -0700301 stream.emit('error', err);
302 } else {
303 for (var i = 0; i < 5; i++) {
304 stream.write({count: i});
305 }
murgatroid9902c160a2015-07-20 15:16:09 -0700306 stream.end({trailer_present: ['yes']});
murgatroid99366e64d2015-07-15 17:01:49 -0700307 }
308 },
309 bidiStream: function(stream) {
310 var count = 0;
311 stream.on('data', function(data) {
312 if (data.error) {
murgatroid99d07a2782015-03-26 10:07:15 -0700313 var err = new Error('Requested error');
murgatroid99366e64d2015-07-15 17:01:49 -0700314 err.metadata = {
murgatroid9902c160a2015-07-20 15:16:09 -0700315 trailer_present: ['yes'],
murgatroid99366e64d2015-07-15 17:01:49 -0700316 count: ['' + count]
317 };
murgatroid99eb21bdd2015-03-26 15:19:54 -0700318 stream.emit('error', err);
murgatroid99d07a2782015-03-26 10:07:15 -0700319 } else {
murgatroid99366e64d2015-07-15 17:01:49 -0700320 stream.write({count: count});
321 count += 1;
murgatroid99d07a2782015-03-26 10:07:15 -0700322 }
murgatroid99366e64d2015-07-15 17:01:49 -0700323 });
324 stream.on('end', function() {
murgatroid9902c160a2015-07-20 15:16:09 -0700325 stream.end({trailer_present: ['yes']});
murgatroid99366e64d2015-07-15 17:01:49 -0700326 });
murgatroid99d07a2782015-03-26 10:07:15 -0700327 }
328 });
murgatroid99778c61b2015-05-18 16:52:47 -0700329 port = server.bind('localhost:0');
murgatroid99d07a2782015-03-26 10:07:15 -0700330 var Client = surface_client.makeProtobufClientConstructor(test_service);
331 client = new Client('localhost:' + port);
murgatroid99366e64d2015-07-15 17:01:49 -0700332 server.start();
murgatroid99d07a2782015-03-26 10:07:15 -0700333 });
334 after(function() {
335 server.shutdown();
336 });
murgatroid99778c61b2015-05-18 16:52:47 -0700337 describe('Server recieving bad input', function() {
338 var misbehavingClient;
339 var badArg = new Buffer([0xFF]);
340 before(function() {
341 var test_service_attrs = {
342 unary: {
343 path: '/TestService/Unary',
344 requestStream: false,
345 responseStream: false,
346 requestSerialize: _.identity,
347 responseDeserialize: _.identity
348 },
349 clientStream: {
350 path: '/TestService/ClientStream',
351 requestStream: true,
352 responseStream: false,
353 requestSerialize: _.identity,
354 responseDeserialize: _.identity
355 },
356 serverStream: {
357 path: '/TestService/ServerStream',
358 requestStream: false,
359 responseStream: true,
360 requestSerialize: _.identity,
361 responseDeserialize: _.identity
362 },
363 bidiStream: {
364 path: '/TestService/BidiStream',
365 requestStream: true,
366 responseStream: true,
367 requestSerialize: _.identity,
368 responseDeserialize: _.identity
369 }
370 };
371 var Client = surface_client.makeClientConstructor(test_service_attrs,
372 'TestService');
373 misbehavingClient = new Client('localhost:' + port);
murgatroid99d07a2782015-03-26 10:07:15 -0700374 });
murgatroid99778c61b2015-05-18 16:52:47 -0700375 it('should respond correctly to a unary call', function(done) {
murgatroid9904589a72015-05-19 09:51:26 -0700376 misbehavingClient.unary(badArg, function(err, data) {
murgatroid99778c61b2015-05-18 16:52:47 -0700377 assert(err);
378 assert.strictEqual(err.code, grpc.status.INVALID_ARGUMENT);
379 done();
380 });
381 });
382 it('should respond correctly to a client stream', function(done) {
383 var call = misbehavingClient.clientStream(function(err, data) {
384 assert(err);
385 assert.strictEqual(err.code, grpc.status.INVALID_ARGUMENT);
386 done();
387 });
388 call.write(badArg);
murgatroid9904589a72015-05-19 09:51:26 -0700389 // TODO(mlumish): Remove call.end()
390 call.end();
murgatroid99778c61b2015-05-18 16:52:47 -0700391 });
392 it('should respond correctly to a server stream', function(done) {
393 var call = misbehavingClient.serverStream(badArg);
394 call.on('data', function(data) {
murgatroid9904589a72015-05-19 09:51:26 -0700395 assert.fail(data, null, 'Unexpected data', '===');
murgatroid99778c61b2015-05-18 16:52:47 -0700396 });
397 call.on('error', function(err) {
398 assert.strictEqual(err.code, grpc.status.INVALID_ARGUMENT);
399 done();
400 });
401 });
402 it('should respond correctly to a bidi stream', function(done) {
403 var call = misbehavingClient.bidiStream();
404 call.on('data', function(data) {
murgatroid9904589a72015-05-19 09:51:26 -0700405 assert.fail(data, null, 'Unexpected data', '===');
murgatroid99778c61b2015-05-18 16:52:47 -0700406 });
407 call.on('error', function(err) {
408 assert.strictEqual(err.code, grpc.status.INVALID_ARGUMENT);
409 done();
410 });
411 call.write(badArg);
murgatroid9904589a72015-05-19 09:51:26 -0700412 // TODO(mlumish): Remove call.end()
413 call.end();
murgatroid99d07a2782015-03-26 10:07:15 -0700414 });
415 });
murgatroid99778c61b2015-05-18 16:52:47 -0700416 describe('Trailing metadata', function() {
417 it('should be present when a unary call succeeds', function(done) {
418 var call = client.unary({error: false}, function(err, data) {
419 assert.ifError(err);
420 });
421 call.on('status', function(status) {
murgatroid9902c160a2015-07-20 15:16:09 -0700422 assert.deepEqual(status.metadata.trailer_present, ['yes']);
murgatroid99778c61b2015-05-18 16:52:47 -0700423 done();
424 });
murgatroid99d07a2782015-03-26 10:07:15 -0700425 });
murgatroid99778c61b2015-05-18 16:52:47 -0700426 it('should be present when a unary call fails', function(done) {
427 var call = client.unary({error: true}, function(err, data) {
428 assert(err);
429 });
430 call.on('status', function(status) {
murgatroid9902c160a2015-07-20 15:16:09 -0700431 assert.deepEqual(status.metadata.trailer_present, ['yes']);
murgatroid99778c61b2015-05-18 16:52:47 -0700432 done();
433 });
murgatroid99d07a2782015-03-26 10:07:15 -0700434 });
murgatroid99778c61b2015-05-18 16:52:47 -0700435 it('should be present when a client stream call succeeds', function(done) {
436 var call = client.clientStream(function(err, data) {
437 assert.ifError(err);
438 });
439 call.write({error: false});
440 call.write({error: false});
441 call.end();
442 call.on('status', function(status) {
murgatroid9902c160a2015-07-20 15:16:09 -0700443 assert.deepEqual(status.metadata.trailer_present, ['yes']);
murgatroid99778c61b2015-05-18 16:52:47 -0700444 done();
445 });
murgatroid99d07a2782015-03-26 10:07:15 -0700446 });
murgatroid99778c61b2015-05-18 16:52:47 -0700447 it('should be present when a client stream call fails', function(done) {
448 var call = client.clientStream(function(err, data) {
449 assert(err);
450 });
451 call.write({error: false});
452 call.write({error: true});
453 call.end();
454 call.on('status', function(status) {
murgatroid9902c160a2015-07-20 15:16:09 -0700455 assert.deepEqual(status.metadata.trailer_present, ['yes']);
murgatroid99778c61b2015-05-18 16:52:47 -0700456 done();
457 });
murgatroid99d07a2782015-03-26 10:07:15 -0700458 });
murgatroid99778c61b2015-05-18 16:52:47 -0700459 it('should be present when a server stream call succeeds', function(done) {
460 var call = client.serverStream({error: false});
461 call.on('data', function(){});
462 call.on('status', function(status) {
463 assert.strictEqual(status.code, grpc.status.OK);
murgatroid9902c160a2015-07-20 15:16:09 -0700464 assert.deepEqual(status.metadata.trailer_present, ['yes']);
murgatroid99778c61b2015-05-18 16:52:47 -0700465 done();
466 });
murgatroid99d07a2782015-03-26 10:07:15 -0700467 });
murgatroid99778c61b2015-05-18 16:52:47 -0700468 it('should be present when a server stream call fails', function(done) {
469 var call = client.serverStream({error: true});
470 call.on('data', function(){});
471 call.on('error', function(error) {
murgatroid9902c160a2015-07-20 15:16:09 -0700472 assert.deepEqual(error.metadata.trailer_present, ['yes']);
murgatroid99778c61b2015-05-18 16:52:47 -0700473 done();
474 });
murgatroid99d07a2782015-03-26 10:07:15 -0700475 });
murgatroid99778c61b2015-05-18 16:52:47 -0700476 it('should be present when a bidi stream succeeds', function(done) {
477 var call = client.bidiStream();
478 call.write({error: false});
479 call.write({error: false});
480 call.end();
481 call.on('data', function(){});
482 call.on('status', function(status) {
483 assert.strictEqual(status.code, grpc.status.OK);
murgatroid9902c160a2015-07-20 15:16:09 -0700484 assert.deepEqual(status.metadata.trailer_present, ['yes']);
murgatroid99778c61b2015-05-18 16:52:47 -0700485 done();
486 });
murgatroid99eb21bdd2015-03-26 15:19:54 -0700487 });
murgatroid99778c61b2015-05-18 16:52:47 -0700488 it('should be present when a bidi stream fails', function(done) {
489 var call = client.bidiStream();
490 call.write({error: false});
491 call.write({error: true});
492 call.end();
493 call.on('data', function(){});
494 call.on('error', function(error) {
murgatroid9902c160a2015-07-20 15:16:09 -0700495 assert.deepEqual(error.metadata.trailer_present, ['yes']);
murgatroid99778c61b2015-05-18 16:52:47 -0700496 done();
497 });
murgatroid99eb21bdd2015-03-26 15:19:54 -0700498 });
499 });
murgatroid9906f09722015-07-17 11:26:54 -0700500 describe('Error object should contain the status', function() {
501 it('for a unary call', function(done) {
502 client.unary({error: true}, function(err, data) {
503 assert(err);
murgatroid99560b82c2015-07-17 13:06:15 -0700504 assert.strictEqual(err.code, grpc.status.UNKNOWN);
murgatroid9906f09722015-07-17 11:26:54 -0700505 assert.strictEqual(err.message, 'Requested error');
506 done();
507 });
508 });
509 it('for a client stream call', function(done) {
510 var call = client.clientStream(function(err, data) {
511 assert(err);
murgatroid99560b82c2015-07-17 13:06:15 -0700512 assert.strictEqual(err.code, grpc.status.UNKNOWN);
murgatroid9906f09722015-07-17 11:26:54 -0700513 assert.strictEqual(err.message, 'Requested error');
514 done();
515 });
516 call.write({error: false});
517 call.write({error: true});
518 call.end();
519 });
520 it('for a server stream call', function(done) {
521 var call = client.serverStream({error: true});
522 call.on('data', function(){});
523 call.on('error', function(error) {
murgatroid99560b82c2015-07-17 13:06:15 -0700524 assert.strictEqual(error.code, grpc.status.UNKNOWN);
murgatroid9906f09722015-07-17 11:26:54 -0700525 assert.strictEqual(error.message, 'Requested error');
526 done();
527 });
528 });
529 it('for a bidi stream call', function(done) {
530 var call = client.bidiStream();
531 call.write({error: false});
532 call.write({error: true});
533 call.end();
534 call.on('data', function(){});
535 call.on('error', function(error) {
murgatroid99560b82c2015-07-17 13:06:15 -0700536 assert.strictEqual(error.code, grpc.status.UNKNOWN);
murgatroid9906f09722015-07-17 11:26:54 -0700537 assert.strictEqual(error.message, 'Requested error');
538 done();
539 });
540 });
541 });
murgatroid99d07a2782015-03-26 10:07:15 -0700542});
murgatroid994d2d0f02015-01-29 11:44:46 -0800543describe('Cancelling surface client', function() {
murgatroid9955dd2ba2015-01-26 14:11:18 -0800544 var client;
545 var server;
546 before(function() {
murgatroid99366e64d2015-07-15 17:01:49 -0700547 server = new grpc.Server();
548 server.addProtoService(mathService, {
549 'div': function(stream) {},
550 'divMany': function(stream) {},
551 'fib': function(stream) {},
552 'sum': function(stream) {}
murgatroid9955dd2ba2015-01-26 14:11:18 -0800553 });
554 var port = server.bind('localhost:0');
murgatroid99e023e982015-03-18 17:17:33 -0700555 var Client = surface_client.makeProtobufClientConstructor(mathService);
murgatroid9955dd2ba2015-01-26 14:11:18 -0800556 client = new Client('localhost:' + port);
murgatroid99366e64d2015-07-15 17:01:49 -0700557 server.start();
murgatroid9955dd2ba2015-01-26 14:11:18 -0800558 });
559 after(function() {
560 server.shutdown();
561 });
562 it('Should correctly cancel a unary call', function(done) {
563 var call = client.div({'divisor': 0, 'dividend': 0}, function(err, resp) {
564 assert.strictEqual(err.code, surface_client.status.CANCELLED);
565 done();
566 });
567 call.cancel();
568 });
569 it('Should correctly cancel a client stream call', function(done) {
570 var call = client.sum(function(err, resp) {
571 assert.strictEqual(err.code, surface_client.status.CANCELLED);
572 done();
573 });
574 call.cancel();
575 });
576 it('Should correctly cancel a server stream call', function(done) {
577 var call = client.fib({'limit': 5});
murgatroid9965b784e2015-05-06 16:46:19 -0700578 call.on('error', function(error) {
579 assert.strictEqual(error.code, surface_client.status.CANCELLED);
murgatroid9955dd2ba2015-01-26 14:11:18 -0800580 done();
581 });
582 call.cancel();
583 });
584 it('Should correctly cancel a bidi stream call', function(done) {
585 var call = client.divMany();
murgatroid9965b784e2015-05-06 16:46:19 -0700586 call.on('error', function(error) {
587 assert.strictEqual(error.code, surface_client.status.CANCELLED);
murgatroid9955dd2ba2015-01-26 14:11:18 -0800588 done();
589 });
590 call.cancel();
591 });
Craig Tillerce5021b2015-02-18 09:25:21 -0800592});