surface_test.js 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147
  1. /*
  2. *
  3. * Copyright 2015-2016, Google Inc.
  4. * 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. 'use strict';
  34. var assert = require('assert');
  35. var surface_client = require('../src/client.js');
  36. var ProtoBuf = require('protobufjs');
  37. var grpc = require('..');
  38. var math_proto = ProtoBuf.loadProtoFile(__dirname +
  39. '/../../proto/math/math.proto');
  40. var mathService = math_proto.lookup('math.Math');
  41. var _ = require('lodash');
  42. /**
  43. * This is used for testing functions with multiple asynchronous calls that
  44. * can happen in different orders. This should be passed the number of async
  45. * function invocations that can occur last, and each of those should call this
  46. * function's return value
  47. * @param {function()} done The function that should be called when a test is
  48. * complete.
  49. * @param {number} count The number of calls to the resulting function if the
  50. * test passes.
  51. * @return {function()} The function that should be called at the end of each
  52. * sequence of asynchronous functions.
  53. */
  54. function multiDone(done, count) {
  55. return function() {
  56. count -= 1;
  57. if (count <= 0) {
  58. done();
  59. }
  60. };
  61. }
  62. var server_insecure_creds = grpc.ServerCredentials.createInsecure();
  63. describe('File loader', function() {
  64. it('Should load a proto file by default', function() {
  65. assert.doesNotThrow(function() {
  66. grpc.load(__dirname + '/test_service.proto');
  67. });
  68. });
  69. it('Should load a proto file with the proto format', function() {
  70. assert.doesNotThrow(function() {
  71. grpc.load(__dirname + '/test_service.proto', 'proto');
  72. });
  73. });
  74. it('Should load a json file with the json format', function() {
  75. assert.doesNotThrow(function() {
  76. grpc.load(__dirname + '/test_service.json', 'json');
  77. });
  78. });
  79. it('Should fail to load a file with an unknown format', function() {
  80. assert.throws(function() {
  81. grpc.load(__dirname + '/test_service.proto', 'fake_format');
  82. });
  83. });
  84. });
  85. describe('surface Server', function() {
  86. var server;
  87. beforeEach(function() {
  88. server = new grpc.Server();
  89. });
  90. afterEach(function() {
  91. server.forceShutdown();
  92. });
  93. it('should error if started twice', function() {
  94. server.start();
  95. assert.throws(function() {
  96. server.start();
  97. });
  98. });
  99. it('should error if a port is bound after the server starts', function() {
  100. server.start();
  101. assert.throws(function() {
  102. server.bind('localhost:0', grpc.ServerCredentials.createInsecure());
  103. });
  104. });
  105. it('should successfully shutdown if tryShutdown is called', function(done) {
  106. server.start();
  107. server.tryShutdown(done);
  108. });
  109. });
  110. describe('Server.prototype.addProtoService', function() {
  111. var server;
  112. var dummyImpls = {
  113. 'div': function() {},
  114. 'divMany': function() {},
  115. 'fib': function() {},
  116. 'sum': function() {}
  117. };
  118. beforeEach(function() {
  119. server = new grpc.Server();
  120. });
  121. afterEach(function() {
  122. server.forceShutdown();
  123. });
  124. it('Should succeed with a single service', function() {
  125. assert.doesNotThrow(function() {
  126. server.addProtoService(mathService, dummyImpls);
  127. });
  128. });
  129. it('Should fail with conflicting method names', function() {
  130. server.addProtoService(mathService, dummyImpls);
  131. assert.throws(function() {
  132. server.addProtoService(mathService, dummyImpls);
  133. });
  134. });
  135. it('Should fail with missing handlers', function() {
  136. assert.throws(function() {
  137. server.addProtoService(mathService, {
  138. 'div': function() {},
  139. 'divMany': function() {},
  140. 'fib': function() {}
  141. });
  142. }, /math.Math.Sum/);
  143. });
  144. it('Should fail if the server has been started', function() {
  145. server.start();
  146. assert.throws(function() {
  147. server.addProtoService(mathService, dummyImpls);
  148. });
  149. });
  150. });
  151. describe('Client constructor building', function() {
  152. var illegal_service_attrs = {
  153. $method : {
  154. path: '/illegal/$method',
  155. requestStream: false,
  156. responseStream: false,
  157. requestSerialize: _.identity,
  158. requestDeserialize: _.identity,
  159. responseSerialize: _.identity,
  160. responseDeserialize: _.identity
  161. }
  162. };
  163. it('Should reject method names starting with $', function() {
  164. assert.throws(function() {
  165. grpc.makeGenericClientConstructor(illegal_service_attrs);
  166. }, /\$/);
  167. });
  168. });
  169. describe('waitForClientReady', function() {
  170. var server;
  171. var port;
  172. var Client;
  173. var client;
  174. before(function() {
  175. server = new grpc.Server();
  176. port = server.bind('localhost:0', grpc.ServerCredentials.createInsecure());
  177. server.start();
  178. Client = surface_client.makeProtobufClientConstructor(mathService);
  179. });
  180. beforeEach(function() {
  181. client = new Client('localhost:' + port, grpc.credentials.createInsecure());
  182. });
  183. after(function() {
  184. server.forceShutdown();
  185. });
  186. it('should complete when called alone', function(done) {
  187. grpc.waitForClientReady(client, Infinity, function(error) {
  188. assert.ifError(error);
  189. done();
  190. });
  191. });
  192. it('should complete when a call is initiated', function(done) {
  193. grpc.waitForClientReady(client, Infinity, function(error) {
  194. assert.ifError(error);
  195. done();
  196. });
  197. var call = client.div({}, function(err, response) {});
  198. call.cancel();
  199. });
  200. it('should complete if called more than once', function(done) {
  201. done = multiDone(done, 2);
  202. grpc.waitForClientReady(client, Infinity, function(error) {
  203. assert.ifError(error);
  204. done();
  205. });
  206. grpc.waitForClientReady(client, Infinity, function(error) {
  207. assert.ifError(error);
  208. done();
  209. });
  210. });
  211. it('should complete if called when already ready', function(done) {
  212. grpc.waitForClientReady(client, Infinity, function(error) {
  213. assert.ifError(error);
  214. grpc.waitForClientReady(client, Infinity, function(error) {
  215. assert.ifError(error);
  216. done();
  217. });
  218. });
  219. });
  220. it('should time out if the server does not exist', function(done) {
  221. var bad_client = new Client('nonexistent_hostname',
  222. grpc.credentials.createInsecure());
  223. var deadline = new Date();
  224. deadline.setSeconds(deadline.getSeconds() + 1);
  225. grpc.waitForClientReady(bad_client, deadline, function(error) {
  226. assert(error);
  227. done();
  228. });
  229. });
  230. });
  231. describe('Echo service', function() {
  232. var server;
  233. var client;
  234. before(function() {
  235. var test_proto = ProtoBuf.loadProtoFile(__dirname + '/echo_service.proto');
  236. var echo_service = test_proto.lookup('EchoService');
  237. server = new grpc.Server();
  238. server.addProtoService(echo_service, {
  239. echo: function(call, callback) {
  240. callback(null, call.request);
  241. }
  242. });
  243. var port = server.bind('localhost:0', server_insecure_creds);
  244. var Client = surface_client.makeProtobufClientConstructor(echo_service);
  245. client = new Client('localhost:' + port, grpc.credentials.createInsecure());
  246. server.start();
  247. });
  248. after(function() {
  249. server.forceShutdown();
  250. });
  251. it('should echo the recieved message directly', function(done) {
  252. client.echo({value: 'test value', value2: 3}, function(error, response) {
  253. assert.ifError(error);
  254. assert.deepEqual(response, {value: 'test value', value2: 3});
  255. done();
  256. });
  257. });
  258. });
  259. describe('Generic client and server', function() {
  260. function toString(val) {
  261. return val.toString();
  262. }
  263. function toBuffer(str) {
  264. return new Buffer(str);
  265. }
  266. var string_service_attrs = {
  267. 'capitalize' : {
  268. path: '/string/capitalize',
  269. requestStream: false,
  270. responseStream: false,
  271. requestSerialize: toBuffer,
  272. requestDeserialize: toString,
  273. responseSerialize: toBuffer,
  274. responseDeserialize: toString
  275. }
  276. };
  277. describe('String client and server', function() {
  278. var client;
  279. var server;
  280. before(function() {
  281. server = new grpc.Server();
  282. server.addService(string_service_attrs, {
  283. capitalize: function(call, callback) {
  284. callback(null, _.capitalize(call.request));
  285. }
  286. });
  287. var port = server.bind('localhost:0', server_insecure_creds);
  288. server.start();
  289. var Client = grpc.makeGenericClientConstructor(string_service_attrs);
  290. client = new Client('localhost:' + port,
  291. grpc.credentials.createInsecure());
  292. });
  293. after(function() {
  294. server.forceShutdown();
  295. });
  296. it('Should respond with a capitalized string', function(done) {
  297. client.capitalize('abc', function(err, response) {
  298. assert.ifError(err);
  299. assert.strictEqual(response, 'Abc');
  300. done();
  301. });
  302. });
  303. });
  304. });
  305. describe('Server-side getPeer', function() {
  306. function toString(val) {
  307. return val.toString();
  308. }
  309. function toBuffer(str) {
  310. return new Buffer(str);
  311. }
  312. var string_service_attrs = {
  313. 'getPeer' : {
  314. path: '/string/getPeer',
  315. requestStream: false,
  316. responseStream: false,
  317. requestSerialize: toBuffer,
  318. requestDeserialize: toString,
  319. responseSerialize: toBuffer,
  320. responseDeserialize: toString
  321. }
  322. };
  323. var client;
  324. var server;
  325. before(function() {
  326. server = new grpc.Server();
  327. server.addService(string_service_attrs, {
  328. getPeer: function(call, callback) {
  329. try {
  330. callback(null, call.getPeer());
  331. } catch (e) {
  332. call.emit('error', e);
  333. }
  334. }
  335. });
  336. var port = server.bind('localhost:0', server_insecure_creds);
  337. server.start();
  338. var Client = grpc.makeGenericClientConstructor(string_service_attrs);
  339. client = new Client('localhost:' + port,
  340. grpc.credentials.createInsecure());
  341. });
  342. after(function() {
  343. server.forceShutdown();
  344. });
  345. it('should respond with a string representing the client', function(done) {
  346. client.getPeer('', function(err, response) {
  347. assert.ifError(err);
  348. // We don't expect a specific value, just that it worked without error
  349. done();
  350. });
  351. });
  352. });
  353. describe('Echo metadata', function() {
  354. var client;
  355. var server;
  356. var metadata;
  357. before(function() {
  358. var test_proto = ProtoBuf.loadProtoFile(__dirname + '/test_service.proto');
  359. var test_service = test_proto.lookup('TestService');
  360. server = new grpc.Server();
  361. server.addProtoService(test_service, {
  362. unary: function(call, cb) {
  363. call.sendMetadata(call.metadata);
  364. cb(null, {});
  365. },
  366. clientStream: function(stream, cb){
  367. stream.on('data', function(data) {});
  368. stream.on('end', function() {
  369. stream.sendMetadata(stream.metadata);
  370. cb(null, {});
  371. });
  372. },
  373. serverStream: function(stream) {
  374. stream.sendMetadata(stream.metadata);
  375. stream.end();
  376. },
  377. bidiStream: function(stream) {
  378. stream.on('data', function(data) {});
  379. stream.on('end', function() {
  380. stream.sendMetadata(stream.metadata);
  381. stream.end();
  382. });
  383. }
  384. });
  385. var port = server.bind('localhost:0', server_insecure_creds);
  386. var Client = surface_client.makeProtobufClientConstructor(test_service);
  387. client = new Client('localhost:' + port, grpc.credentials.createInsecure());
  388. server.start();
  389. metadata = new grpc.Metadata();
  390. metadata.set('key', 'value');
  391. });
  392. after(function() {
  393. server.forceShutdown();
  394. });
  395. it('with unary call', function(done) {
  396. var call = client.unary({}, function(err, data) {
  397. assert.ifError(err);
  398. }, metadata);
  399. call.on('metadata', function(metadata) {
  400. assert.deepEqual(metadata.get('key'), ['value']);
  401. done();
  402. });
  403. });
  404. it('with client stream call', function(done) {
  405. var call = client.clientStream(function(err, data) {
  406. assert.ifError(err);
  407. }, metadata);
  408. call.on('metadata', function(metadata) {
  409. assert.deepEqual(metadata.get('key'), ['value']);
  410. done();
  411. });
  412. call.end();
  413. });
  414. it('with server stream call', function(done) {
  415. var call = client.serverStream({}, metadata);
  416. call.on('data', function() {});
  417. call.on('metadata', function(metadata) {
  418. assert.deepEqual(metadata.get('key'), ['value']);
  419. done();
  420. });
  421. });
  422. it('with bidi stream call', function(done) {
  423. var call = client.bidiStream(metadata);
  424. call.on('data', function() {});
  425. call.on('metadata', function(metadata) {
  426. assert.deepEqual(metadata.get('key'), ['value']);
  427. done();
  428. });
  429. call.end();
  430. });
  431. it('shows the correct user-agent string', function(done) {
  432. var version = require('../../../package.json').version;
  433. var call = client.unary({}, function(err, data) { assert.ifError(err); },
  434. metadata);
  435. call.on('metadata', function(metadata) {
  436. assert(_.startsWith(metadata.get('user-agent')[0],
  437. 'grpc-node/' + version));
  438. done();
  439. });
  440. });
  441. it('properly handles duplicate values', function(done) {
  442. var dup_metadata = metadata.clone();
  443. dup_metadata.add('key', 'value2');
  444. var call = client.unary({}, function(err, data) {assert.ifError(err); },
  445. dup_metadata);
  446. call.on('metadata', function(resp_metadata) {
  447. // Two arrays are equal iff their symmetric difference is empty
  448. assert.deepEqual(_.xor(dup_metadata.get('key'), resp_metadata.get('key')),
  449. []);
  450. done();
  451. });
  452. });
  453. });
  454. describe('Client malformed response handling', function() {
  455. var server;
  456. var client;
  457. var badArg = new Buffer([0xFF]);
  458. before(function() {
  459. var test_proto = ProtoBuf.loadProtoFile(__dirname + '/test_service.proto');
  460. var test_service = test_proto.lookup('TestService');
  461. var malformed_test_service = {
  462. unary: {
  463. path: '/TestService/Unary',
  464. requestStream: false,
  465. responseStream: false,
  466. requestDeserialize: _.identity,
  467. responseSerialize: _.identity
  468. },
  469. clientStream: {
  470. path: '/TestService/ClientStream',
  471. requestStream: true,
  472. responseStream: false,
  473. requestDeserialize: _.identity,
  474. responseSerialize: _.identity
  475. },
  476. serverStream: {
  477. path: '/TestService/ServerStream',
  478. requestStream: false,
  479. responseStream: true,
  480. requestDeserialize: _.identity,
  481. responseSerialize: _.identity
  482. },
  483. bidiStream: {
  484. path: '/TestService/BidiStream',
  485. requestStream: true,
  486. responseStream: true,
  487. requestDeserialize: _.identity,
  488. responseSerialize: _.identity
  489. }
  490. };
  491. server = new grpc.Server();
  492. server.addService(malformed_test_service, {
  493. unary: function(call, cb) {
  494. cb(null, badArg);
  495. },
  496. clientStream: function(stream, cb) {
  497. stream.on('data', function() {/* Ignore requests */});
  498. stream.on('end', function() {
  499. cb(null, badArg);
  500. });
  501. },
  502. serverStream: function(stream) {
  503. stream.write(badArg);
  504. stream.end();
  505. },
  506. bidiStream: function(stream) {
  507. stream.on('data', function() {
  508. // Ignore requests
  509. stream.write(badArg);
  510. });
  511. stream.on('end', function() {
  512. stream.end();
  513. });
  514. }
  515. });
  516. var port = server.bind('localhost:0', server_insecure_creds);
  517. var Client = surface_client.makeProtobufClientConstructor(test_service);
  518. client = new Client('localhost:' + port, grpc.credentials.createInsecure());
  519. server.start();
  520. });
  521. after(function() {
  522. server.forceShutdown();
  523. });
  524. it('should get an INTERNAL status with a unary call', function(done) {
  525. client.unary({}, function(err, data) {
  526. assert(err);
  527. assert.strictEqual(err.code, grpc.status.INTERNAL);
  528. done();
  529. });
  530. });
  531. it('should get an INTERNAL status with a client stream call', function(done) {
  532. var call = client.clientStream(function(err, data) {
  533. assert(err);
  534. assert.strictEqual(err.code, grpc.status.INTERNAL);
  535. done();
  536. });
  537. call.write({});
  538. call.end();
  539. });
  540. it('should get an INTERNAL status with a server stream call', function(done) {
  541. var call = client.serverStream({});
  542. call.on('data', function(){});
  543. call.on('error', function(err) {
  544. assert.strictEqual(err.code, grpc.status.INTERNAL);
  545. done();
  546. });
  547. });
  548. it('should get an INTERNAL status with a bidi stream call', function(done) {
  549. var call = client.bidiStream();
  550. call.on('data', function(){});
  551. call.on('error', function(err) {
  552. assert.strictEqual(err.code, grpc.status.INTERNAL);
  553. done();
  554. });
  555. call.write({});
  556. call.end();
  557. });
  558. });
  559. describe('Other conditions', function() {
  560. var test_service;
  561. var Client;
  562. var client;
  563. var server;
  564. var port;
  565. before(function() {
  566. var test_proto = ProtoBuf.loadProtoFile(__dirname + '/test_service.proto');
  567. test_service = test_proto.lookup('TestService');
  568. server = new grpc.Server();
  569. var trailer_metadata = new grpc.Metadata();
  570. trailer_metadata.add('trailer-present', 'yes');
  571. server.addProtoService(test_service, {
  572. unary: function(call, cb) {
  573. var req = call.request;
  574. if (req.error) {
  575. cb({code: grpc.status.UNKNOWN,
  576. details: 'Requested error'}, null, trailer_metadata);
  577. } else {
  578. cb(null, {count: 1}, trailer_metadata);
  579. }
  580. },
  581. clientStream: function(stream, cb){
  582. var count = 0;
  583. var errored;
  584. stream.on('data', function(data) {
  585. if (data.error) {
  586. errored = true;
  587. cb(new Error('Requested error'), null, trailer_metadata);
  588. } else {
  589. count += 1;
  590. }
  591. });
  592. stream.on('end', function() {
  593. if (!errored) {
  594. cb(null, {count: count}, trailer_metadata);
  595. }
  596. });
  597. },
  598. serverStream: function(stream) {
  599. var req = stream.request;
  600. if (req.error) {
  601. var err = {code: grpc.status.UNKNOWN,
  602. details: 'Requested error'};
  603. err.metadata = trailer_metadata;
  604. stream.emit('error', err);
  605. } else {
  606. for (var i = 0; i < 5; i++) {
  607. stream.write({count: i});
  608. }
  609. stream.end(trailer_metadata);
  610. }
  611. },
  612. bidiStream: function(stream) {
  613. var count = 0;
  614. stream.on('data', function(data) {
  615. if (data.error) {
  616. var err = new Error('Requested error');
  617. err.metadata = trailer_metadata.clone();
  618. err.metadata.add('count', '' + count);
  619. stream.emit('error', err);
  620. } else {
  621. stream.write({count: count});
  622. count += 1;
  623. }
  624. });
  625. stream.on('end', function() {
  626. stream.end(trailer_metadata);
  627. });
  628. }
  629. });
  630. port = server.bind('localhost:0', server_insecure_creds);
  631. Client = surface_client.makeProtobufClientConstructor(test_service);
  632. client = new Client('localhost:' + port, grpc.credentials.createInsecure());
  633. server.start();
  634. });
  635. after(function() {
  636. server.forceShutdown();
  637. });
  638. it('channel.getTarget should be available', function() {
  639. assert.strictEqual(typeof grpc.getClientChannel(client).getTarget(),
  640. 'string');
  641. });
  642. it('client should be able to pause and resume a stream', function(done) {
  643. var call = client.bidiStream();
  644. call.on('data', function(data) {
  645. assert(data.count < 3);
  646. call.pause();
  647. setTimeout(function() {
  648. call.resume();
  649. }, 10);
  650. });
  651. call.on('end', function() {
  652. done();
  653. });
  654. call.write({});
  655. call.write({});
  656. call.write({});
  657. call.end();
  658. });
  659. describe('Server recieving bad input', function() {
  660. var misbehavingClient;
  661. var badArg = new Buffer([0xFF]);
  662. before(function() {
  663. var test_service_attrs = {
  664. unary: {
  665. path: '/TestService/Unary',
  666. requestStream: false,
  667. responseStream: false,
  668. requestSerialize: _.identity,
  669. responseDeserialize: _.identity
  670. },
  671. clientStream: {
  672. path: '/TestService/ClientStream',
  673. requestStream: true,
  674. responseStream: false,
  675. requestSerialize: _.identity,
  676. responseDeserialize: _.identity
  677. },
  678. serverStream: {
  679. path: '/TestService/ServerStream',
  680. requestStream: false,
  681. responseStream: true,
  682. requestSerialize: _.identity,
  683. responseDeserialize: _.identity
  684. },
  685. bidiStream: {
  686. path: '/TestService/BidiStream',
  687. requestStream: true,
  688. responseStream: true,
  689. requestSerialize: _.identity,
  690. responseDeserialize: _.identity
  691. }
  692. };
  693. var Client = surface_client.makeClientConstructor(test_service_attrs,
  694. 'TestService');
  695. misbehavingClient = new Client('localhost:' + port,
  696. grpc.credentials.createInsecure());
  697. });
  698. it('should respond correctly to a unary call', function(done) {
  699. misbehavingClient.unary(badArg, function(err, data) {
  700. assert(err);
  701. assert.strictEqual(err.code, grpc.status.INVALID_ARGUMENT);
  702. done();
  703. });
  704. });
  705. it('should respond correctly to a client stream', function(done) {
  706. var call = misbehavingClient.clientStream(function(err, data) {
  707. assert(err);
  708. assert.strictEqual(err.code, grpc.status.INVALID_ARGUMENT);
  709. done();
  710. });
  711. call.write(badArg);
  712. // TODO(mlumish): Remove call.end()
  713. call.end();
  714. });
  715. it('should respond correctly to a server stream', function(done) {
  716. var call = misbehavingClient.serverStream(badArg);
  717. call.on('data', function(data) {
  718. assert.fail(data, null, 'Unexpected data', '===');
  719. });
  720. call.on('error', function(err) {
  721. assert.strictEqual(err.code, grpc.status.INVALID_ARGUMENT);
  722. done();
  723. });
  724. });
  725. it('should respond correctly to a bidi stream', function(done) {
  726. var call = misbehavingClient.bidiStream();
  727. call.on('data', function(data) {
  728. assert.fail(data, null, 'Unexpected data', '===');
  729. });
  730. call.on('error', function(err) {
  731. assert.strictEqual(err.code, grpc.status.INVALID_ARGUMENT);
  732. done();
  733. });
  734. call.write(badArg);
  735. // TODO(mlumish): Remove call.end()
  736. call.end();
  737. });
  738. });
  739. describe('Trailing metadata', function() {
  740. it('should be present when a unary call succeeds', function(done) {
  741. var call = client.unary({error: false}, function(err, data) {
  742. assert.ifError(err);
  743. });
  744. call.on('status', function(status) {
  745. assert.deepEqual(status.metadata.get('trailer-present'), ['yes']);
  746. done();
  747. });
  748. });
  749. it('should be present when a unary call fails', function(done) {
  750. var call = client.unary({error: true}, function(err, data) {
  751. assert(err);
  752. });
  753. call.on('status', function(status) {
  754. assert.deepEqual(status.metadata.get('trailer-present'), ['yes']);
  755. done();
  756. });
  757. });
  758. it('should be present when a client stream call succeeds', function(done) {
  759. var call = client.clientStream(function(err, data) {
  760. assert.ifError(err);
  761. });
  762. call.write({error: false});
  763. call.write({error: false});
  764. call.end();
  765. call.on('status', function(status) {
  766. assert.deepEqual(status.metadata.get('trailer-present'), ['yes']);
  767. done();
  768. });
  769. });
  770. it('should be present when a client stream call fails', function(done) {
  771. var call = client.clientStream(function(err, data) {
  772. assert(err);
  773. });
  774. call.write({error: false});
  775. call.write({error: true});
  776. call.end();
  777. call.on('status', function(status) {
  778. assert.deepEqual(status.metadata.get('trailer-present'), ['yes']);
  779. done();
  780. });
  781. });
  782. it('should be present when a server stream call succeeds', function(done) {
  783. var call = client.serverStream({error: false});
  784. call.on('data', function(){});
  785. call.on('status', function(status) {
  786. assert.strictEqual(status.code, grpc.status.OK);
  787. assert.deepEqual(status.metadata.get('trailer-present'), ['yes']);
  788. done();
  789. });
  790. });
  791. it('should be present when a server stream call fails', function(done) {
  792. var call = client.serverStream({error: true});
  793. call.on('data', function(){});
  794. call.on('error', function(error) {
  795. assert.deepEqual(error.metadata.get('trailer-present'), ['yes']);
  796. done();
  797. });
  798. });
  799. it('should be present when a bidi stream succeeds', function(done) {
  800. var call = client.bidiStream();
  801. call.write({error: false});
  802. call.write({error: false});
  803. call.end();
  804. call.on('data', function(){});
  805. call.on('status', function(status) {
  806. assert.strictEqual(status.code, grpc.status.OK);
  807. assert.deepEqual(status.metadata.get('trailer-present'), ['yes']);
  808. done();
  809. });
  810. });
  811. it('should be present when a bidi stream fails', function(done) {
  812. var call = client.bidiStream();
  813. call.write({error: false});
  814. call.write({error: true});
  815. call.end();
  816. call.on('data', function(){});
  817. call.on('error', function(error) {
  818. assert.deepEqual(error.metadata.get('trailer-present'), ['yes']);
  819. done();
  820. });
  821. });
  822. });
  823. describe('Error object should contain the status', function() {
  824. it('for a unary call', function(done) {
  825. client.unary({error: true}, function(err, data) {
  826. assert(err);
  827. assert.strictEqual(err.code, grpc.status.UNKNOWN);
  828. assert.strictEqual(err.message, 'Requested error');
  829. done();
  830. });
  831. });
  832. it('for a client stream call', function(done) {
  833. var call = client.clientStream(function(err, data) {
  834. assert(err);
  835. assert.strictEqual(err.code, grpc.status.UNKNOWN);
  836. assert.strictEqual(err.message, 'Requested error');
  837. done();
  838. });
  839. call.write({error: false});
  840. call.write({error: true});
  841. call.end();
  842. });
  843. it('for a server stream call', function(done) {
  844. var call = client.serverStream({error: true});
  845. call.on('data', function(){});
  846. call.on('error', function(error) {
  847. assert.strictEqual(error.code, grpc.status.UNKNOWN);
  848. assert.strictEqual(error.message, 'Requested error');
  849. done();
  850. });
  851. });
  852. it('for a bidi stream call', function(done) {
  853. var call = client.bidiStream();
  854. call.write({error: false});
  855. call.write({error: true});
  856. call.end();
  857. call.on('data', function(){});
  858. call.on('error', function(error) {
  859. assert.strictEqual(error.code, grpc.status.UNKNOWN);
  860. assert.strictEqual(error.message, 'Requested error');
  861. done();
  862. });
  863. });
  864. });
  865. describe('call.getPeer should return the peer', function() {
  866. it('for a unary call', function(done) {
  867. var call = client.unary({error: false}, function(err, data) {
  868. assert.ifError(err);
  869. done();
  870. });
  871. assert.strictEqual(typeof call.getPeer(), 'string');
  872. });
  873. it('for a client stream call', function(done) {
  874. var call = client.clientStream(function(err, data) {
  875. assert.ifError(err);
  876. done();
  877. });
  878. assert.strictEqual(typeof call.getPeer(), 'string');
  879. call.write({error: false});
  880. call.end();
  881. });
  882. it('for a server stream call', function(done) {
  883. var call = client.serverStream({error: false});
  884. assert.strictEqual(typeof call.getPeer(), 'string');
  885. call.on('data', function(){});
  886. call.on('status', function(status) {
  887. assert.strictEqual(status.code, grpc.status.OK);
  888. done();
  889. });
  890. });
  891. it('for a bidi stream call', function(done) {
  892. var call = client.bidiStream();
  893. assert.strictEqual(typeof call.getPeer(), 'string');
  894. call.write({error: false});
  895. call.end();
  896. call.on('data', function(){});
  897. call.on('status', function(status) {
  898. done();
  899. });
  900. });
  901. });
  902. });
  903. describe('Call propagation', function() {
  904. var proxy;
  905. var proxy_impl;
  906. var test_service;
  907. var Client;
  908. var client;
  909. var server;
  910. before(function() {
  911. var test_proto = ProtoBuf.loadProtoFile(__dirname + '/test_service.proto');
  912. test_service = test_proto.lookup('TestService');
  913. server = new grpc.Server();
  914. server.addProtoService(test_service, {
  915. unary: function(call) {},
  916. clientStream: function(stream) {},
  917. serverStream: function(stream) {},
  918. bidiStream: function(stream) {}
  919. });
  920. var port = server.bind('localhost:0', server_insecure_creds);
  921. Client = surface_client.makeProtobufClientConstructor(test_service);
  922. client = new Client('localhost:' + port, grpc.credentials.createInsecure());
  923. server.start();
  924. });
  925. after(function() {
  926. server.forceShutdown();
  927. });
  928. beforeEach(function() {
  929. proxy = new grpc.Server();
  930. proxy_impl = {
  931. unary: function(call) {},
  932. clientStream: function(stream) {},
  933. serverStream: function(stream) {},
  934. bidiStream: function(stream) {}
  935. };
  936. });
  937. afterEach(function() {
  938. proxy.forceShutdown();
  939. });
  940. describe('Cancellation', function() {
  941. it('With a unary call', function(done) {
  942. done = multiDone(done, 2);
  943. var call;
  944. proxy_impl.unary = function(parent, callback) {
  945. client.unary(parent.request, function(err, value) {
  946. try {
  947. assert(err);
  948. assert.strictEqual(err.code, grpc.status.CANCELLED);
  949. } finally {
  950. callback(err, value);
  951. done();
  952. }
  953. }, null, {parent: parent});
  954. call.cancel();
  955. };
  956. proxy.addProtoService(test_service, proxy_impl);
  957. var proxy_port = proxy.bind('localhost:0', server_insecure_creds);
  958. proxy.start();
  959. var proxy_client = new Client('localhost:' + proxy_port,
  960. grpc.credentials.createInsecure());
  961. call = proxy_client.unary({}, function(err, value) { done(); });
  962. });
  963. it('With a client stream call', function(done) {
  964. done = multiDone(done, 2);
  965. var call;
  966. proxy_impl.clientStream = function(parent, callback) {
  967. client.clientStream(function(err, value) {
  968. try {
  969. assert(err);
  970. assert.strictEqual(err.code, grpc.status.CANCELLED);
  971. } finally {
  972. callback(err, value);
  973. done();
  974. }
  975. }, null, {parent: parent});
  976. call.cancel();
  977. };
  978. proxy.addProtoService(test_service, proxy_impl);
  979. var proxy_port = proxy.bind('localhost:0', server_insecure_creds);
  980. proxy.start();
  981. var proxy_client = new Client('localhost:' + proxy_port,
  982. grpc.credentials.createInsecure());
  983. call = proxy_client.clientStream(function(err, value) { done(); });
  984. });
  985. it('With a server stream call', function(done) {
  986. done = multiDone(done, 2);
  987. var call;
  988. proxy_impl.serverStream = function(parent) {
  989. var child = client.serverStream(parent.request, null,
  990. {parent: parent});
  991. child.on('error', function(err) {
  992. assert(err);
  993. assert.strictEqual(err.code, grpc.status.CANCELLED);
  994. done();
  995. });
  996. call.cancel();
  997. };
  998. proxy.addProtoService(test_service, proxy_impl);
  999. var proxy_port = proxy.bind('localhost:0', server_insecure_creds);
  1000. proxy.start();
  1001. var proxy_client = new Client('localhost:' + proxy_port,
  1002. grpc.credentials.createInsecure());
  1003. call = proxy_client.serverStream({});
  1004. call.on('error', function(err) {
  1005. done();
  1006. });
  1007. });
  1008. it('With a bidi stream call', function(done) {
  1009. done = multiDone(done, 2);
  1010. var call;
  1011. proxy_impl.bidiStream = function(parent) {
  1012. var child = client.bidiStream(null, {parent: parent});
  1013. child.on('error', function(err) {
  1014. assert(err);
  1015. assert.strictEqual(err.code, grpc.status.CANCELLED);
  1016. done();
  1017. });
  1018. call.cancel();
  1019. };
  1020. proxy.addProtoService(test_service, proxy_impl);
  1021. var proxy_port = proxy.bind('localhost:0', server_insecure_creds);
  1022. proxy.start();
  1023. var proxy_client = new Client('localhost:' + proxy_port,
  1024. grpc.credentials.createInsecure());
  1025. call = proxy_client.bidiStream();
  1026. call.on('error', function(err) {
  1027. done();
  1028. });
  1029. });
  1030. });
  1031. describe('Deadline', function() {
  1032. /* jshint bitwise:false */
  1033. var deadline_flags = (grpc.propagate.DEFAULTS &
  1034. ~grpc.propagate.CANCELLATION);
  1035. it('With a client stream call', function(done) {
  1036. done = multiDone(done, 2);
  1037. proxy_impl.clientStream = function(parent, callback) {
  1038. client.clientStream(function(err, value) {
  1039. try {
  1040. assert(err);
  1041. assert(err.code === grpc.status.DEADLINE_EXCEEDED ||
  1042. err.code === grpc.status.INTERNAL);
  1043. } finally {
  1044. callback(err, value);
  1045. done();
  1046. }
  1047. }, null, {parent: parent, propagate_flags: deadline_flags});
  1048. };
  1049. proxy.addProtoService(test_service, proxy_impl);
  1050. var proxy_port = proxy.bind('localhost:0', server_insecure_creds);
  1051. proxy.start();
  1052. var proxy_client = new Client('localhost:' + proxy_port,
  1053. grpc.credentials.createInsecure());
  1054. var deadline = new Date();
  1055. deadline.setSeconds(deadline.getSeconds() + 1);
  1056. proxy_client.clientStream(function(err, value) {
  1057. done();
  1058. }, null, {deadline: deadline});
  1059. });
  1060. it('With a bidi stream call', function(done) {
  1061. done = multiDone(done, 2);
  1062. proxy_impl.bidiStream = function(parent) {
  1063. var child = client.bidiStream(
  1064. null, {parent: parent, propagate_flags: deadline_flags});
  1065. child.on('error', function(err) {
  1066. assert(err);
  1067. assert(err.code === grpc.status.DEADLINE_EXCEEDED ||
  1068. err.code === grpc.status.INTERNAL);
  1069. done();
  1070. });
  1071. };
  1072. proxy.addProtoService(test_service, proxy_impl);
  1073. var proxy_port = proxy.bind('localhost:0', server_insecure_creds);
  1074. proxy.start();
  1075. var proxy_client = new Client('localhost:' + proxy_port,
  1076. grpc.credentials.createInsecure());
  1077. var deadline = new Date();
  1078. deadline.setSeconds(deadline.getSeconds() + 1);
  1079. var call = proxy_client.bidiStream(null, {deadline: deadline});
  1080. call.on('error', function(err) {
  1081. done();
  1082. });
  1083. });
  1084. });
  1085. });
  1086. describe('Cancelling surface client', function() {
  1087. var client;
  1088. var server;
  1089. before(function() {
  1090. server = new grpc.Server();
  1091. server.addProtoService(mathService, {
  1092. 'div': function(stream) {},
  1093. 'divMany': function(stream) {},
  1094. 'fib': function(stream) {},
  1095. 'sum': function(stream) {}
  1096. });
  1097. var port = server.bind('localhost:0', server_insecure_creds);
  1098. var Client = surface_client.makeProtobufClientConstructor(mathService);
  1099. client = new Client('localhost:' + port, grpc.credentials.createInsecure());
  1100. server.start();
  1101. });
  1102. after(function() {
  1103. server.forceShutdown();
  1104. });
  1105. it('Should correctly cancel a unary call', function(done) {
  1106. var call = client.div({'divisor': 0, 'dividend': 0}, function(err, resp) {
  1107. assert.strictEqual(err.code, surface_client.status.CANCELLED);
  1108. done();
  1109. });
  1110. call.cancel();
  1111. });
  1112. it('Should correctly cancel a client stream call', function(done) {
  1113. var call = client.sum(function(err, resp) {
  1114. assert.strictEqual(err.code, surface_client.status.CANCELLED);
  1115. done();
  1116. });
  1117. call.cancel();
  1118. });
  1119. it('Should correctly cancel a server stream call', function(done) {
  1120. var call = client.fib({'limit': 5});
  1121. call.on('error', function(error) {
  1122. assert.strictEqual(error.code, surface_client.status.CANCELLED);
  1123. done();
  1124. });
  1125. call.cancel();
  1126. });
  1127. it('Should correctly cancel a bidi stream call', function(done) {
  1128. var call = client.divMany();
  1129. call.on('error', function(error) {
  1130. assert.strictEqual(error.code, surface_client.status.CANCELLED);
  1131. done();
  1132. });
  1133. call.cancel();
  1134. });
  1135. });