surface_test.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /*
  2. *
  3. * Copyright 2015, 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 + '/../examples/math.proto');
  39. var mathService = math_proto.lookup('math.Math');
  40. var capitalize = require('underscore.string/capitalize');
  41. describe('File loader', function() {
  42. it('Should load a proto file by default', function() {
  43. assert.doesNotThrow(function() {
  44. grpc.load(__dirname + '/test_service.proto');
  45. });
  46. });
  47. it('Should load a proto file with the proto format', function() {
  48. assert.doesNotThrow(function() {
  49. grpc.load(__dirname + '/test_service.proto', 'proto');
  50. });
  51. });
  52. it('Should load a json file with the json format', function() {
  53. assert.doesNotThrow(function() {
  54. grpc.load(__dirname + '/test_service.json', 'json');
  55. });
  56. });
  57. it('Should fail to load a file with an unknown format', function() {
  58. assert.throws(function() {
  59. grpc.load(__dirname + '/test_service.proto', 'fake_format');
  60. });
  61. });
  62. });
  63. describe('Surface server constructor', function() {
  64. it('Should fail with conflicting method names', function() {
  65. assert.throws(function() {
  66. grpc.buildServer([mathService, mathService]);
  67. });
  68. });
  69. it('Should succeed with a single service', function() {
  70. assert.doesNotThrow(function() {
  71. grpc.buildServer([mathService]);
  72. });
  73. });
  74. it('Should fail with missing handlers', function() {
  75. var Server = grpc.buildServer([mathService]);
  76. assert.throws(function() {
  77. new Server({
  78. 'math.Math': {
  79. 'div': function() {},
  80. 'divMany': function() {},
  81. 'fib': function() {}
  82. }
  83. });
  84. }, /math.Math.Sum/);
  85. });
  86. it('Should fail with no handlers for the service', function() {
  87. var Server = grpc.buildServer([mathService]);
  88. assert.throws(function() {
  89. new Server({});
  90. }, /math.Math/);
  91. });
  92. });
  93. describe('Generic client and server', function() {
  94. function toString(val) {
  95. return val.toString();
  96. }
  97. function toBuffer(str) {
  98. return new Buffer(str);
  99. }
  100. var string_service_attrs = {
  101. 'capitalize' : {
  102. path: '/string/capitalize',
  103. requestStream: false,
  104. responseStream: false,
  105. requestSerialize: toBuffer,
  106. requestDeserialize: toString,
  107. responseSerialize: toBuffer,
  108. responseDeserialize: toString
  109. }
  110. };
  111. describe('String client and server', function() {
  112. var client;
  113. var server;
  114. before(function() {
  115. var Server = grpc.makeGenericServerConstructor({
  116. string: string_service_attrs
  117. });
  118. server = new Server({
  119. string: {
  120. capitalize: function(call, callback) {
  121. callback(null, capitalize(call.request));
  122. }
  123. }
  124. });
  125. var port = server.bind('localhost:0');
  126. server.listen();
  127. var Client = grpc.makeGenericClientConstructor(string_service_attrs);
  128. client = new Client('localhost:' + port);
  129. });
  130. after(function() {
  131. server.shutdown();
  132. });
  133. it('Should respond with a capitalized string', function(done) {
  134. client.capitalize('abc', function(err, response) {
  135. assert.ifError(err);
  136. assert.strictEqual(response, 'Abc');
  137. done();
  138. });
  139. });
  140. });
  141. });
  142. describe('Trailing metadata', function() {
  143. var client;
  144. var server;
  145. before(function() {
  146. var test_proto = ProtoBuf.loadProtoFile(__dirname + '/test_service.proto');
  147. var test_service = test_proto.lookup('TestService');
  148. var Server = grpc.buildServer([test_service]);
  149. server = new Server({
  150. TestService: {
  151. unary: function(call, cb) {
  152. var req = call.request;
  153. if (req.error) {
  154. cb(new Error('Requested error'), null, {metadata: ['yes']});
  155. } else {
  156. cb(null, {count: 1}, {metadata: ['yes']});
  157. }
  158. },
  159. clientStream: function(stream, cb){
  160. var count = 0;
  161. var errored;
  162. stream.on('data', function(data) {
  163. if (data.error) {
  164. errored = true;
  165. cb(new Error('Requested error'), null, {metadata: ['yes']});
  166. } else {
  167. count += 1;
  168. }
  169. });
  170. stream.on('end', function() {
  171. if (!errored) {
  172. cb(null, {count: count}, {metadata: ['yes']});
  173. }
  174. });
  175. },
  176. serverStream: function(stream) {
  177. var req = stream.request;
  178. if (req.error) {
  179. var err = new Error('Requested error');
  180. err.metadata = {metadata: ['yes']};
  181. stream.emit('error', err);
  182. } else {
  183. for (var i = 0; i < 5; i++) {
  184. stream.write({count: i});
  185. }
  186. stream.end({metadata: ['yes']});
  187. }
  188. },
  189. bidiStream: function(stream) {
  190. var count = 0;
  191. stream.on('data', function(data) {
  192. if (data.error) {
  193. var err = new Error('Requested error');
  194. err.metadata = {
  195. metadata: ['yes'],
  196. count: ['' + count]
  197. };
  198. stream.emit('error', err);
  199. } else {
  200. stream.write({count: count});
  201. count += 1;
  202. }
  203. });
  204. stream.on('end', function() {
  205. stream.end({metadata: ['yes']});
  206. });
  207. }
  208. }
  209. });
  210. var port = server.bind('localhost:0');
  211. var Client = surface_client.makeProtobufClientConstructor(test_service);
  212. client = new Client('localhost:' + port);
  213. server.listen();
  214. });
  215. after(function() {
  216. server.shutdown();
  217. });
  218. it('should be present when a unary call succeeds', function(done) {
  219. var call = client.unary({error: false}, function(err, data) {
  220. assert.ifError(err);
  221. });
  222. call.on('status', function(status) {
  223. assert.deepEqual(status.metadata.metadata, ['yes']);
  224. done();
  225. });
  226. });
  227. it('should be present when a unary call fails', function(done) {
  228. var call = client.unary({error: true}, function(err, data) {
  229. assert(err);
  230. });
  231. call.on('status', function(status) {
  232. assert.deepEqual(status.metadata.metadata, ['yes']);
  233. done();
  234. });
  235. });
  236. it('should be present when a client stream call succeeds', function(done) {
  237. var call = client.clientStream(function(err, data) {
  238. assert.ifError(err);
  239. });
  240. call.write({error: false});
  241. call.write({error: false});
  242. call.end();
  243. call.on('status', function(status) {
  244. assert.deepEqual(status.metadata.metadata, ['yes']);
  245. done();
  246. });
  247. });
  248. it('should be present when a client stream call fails', function(done) {
  249. var call = client.clientStream(function(err, data) {
  250. assert(err);
  251. });
  252. call.write({error: false});
  253. call.write({error: true});
  254. call.end();
  255. call.on('status', function(status) {
  256. assert.deepEqual(status.metadata.metadata, ['yes']);
  257. done();
  258. });
  259. });
  260. it('should be present when a server stream call succeeds', function(done) {
  261. var call = client.serverStream({error: false});
  262. call.on('data', function(){});
  263. call.on('status', function(status) {
  264. assert.strictEqual(status.code, grpc.status.OK);
  265. assert.deepEqual(status.metadata.metadata, ['yes']);
  266. done();
  267. });
  268. });
  269. it('should be present when a server stream call fails', function(done) {
  270. var call = client.serverStream({error: true});
  271. call.on('data', function(){});
  272. call.on('status', function(status) {
  273. assert.notStrictEqual(status.code, grpc.status.OK);
  274. assert.deepEqual(status.metadata.metadata, ['yes']);
  275. done();
  276. });
  277. });
  278. it('should be present when a bidi stream succeeds', function(done) {
  279. var call = client.bidiStream();
  280. call.write({error: false});
  281. call.write({error: false});
  282. call.end();
  283. call.on('data', function(){});
  284. call.on('status', function(status) {
  285. assert.strictEqual(status.code, grpc.status.OK);
  286. assert.deepEqual(status.metadata.metadata, ['yes']);
  287. done();
  288. });
  289. });
  290. it('should be present when a bidi stream fails', function(done) {
  291. var call = client.bidiStream();
  292. call.write({error: false});
  293. call.write({error: true});
  294. call.end();
  295. call.on('data', function(){});
  296. call.on('status', function(status) {
  297. assert.notStrictEqual(status.code, grpc.status.OK);
  298. assert.deepEqual(status.metadata.metadata, ['yes']);
  299. done();
  300. });
  301. });
  302. });
  303. describe('Cancelling surface client', function() {
  304. var client;
  305. var server;
  306. before(function() {
  307. var Server = grpc.buildServer([mathService]);
  308. server = new Server({
  309. 'math.Math': {
  310. 'div': function(stream) {},
  311. 'divMany': function(stream) {},
  312. 'fib': function(stream) {},
  313. 'sum': function(stream) {}
  314. }
  315. });
  316. var port = server.bind('localhost:0');
  317. var Client = surface_client.makeProtobufClientConstructor(mathService);
  318. client = new Client('localhost:' + port);
  319. });
  320. after(function() {
  321. server.shutdown();
  322. });
  323. it('Should correctly cancel a unary call', function(done) {
  324. var call = client.div({'divisor': 0, 'dividend': 0}, function(err, resp) {
  325. assert.strictEqual(err.code, surface_client.status.CANCELLED);
  326. done();
  327. });
  328. call.cancel();
  329. });
  330. it('Should correctly cancel a client stream call', function(done) {
  331. var call = client.sum(function(err, resp) {
  332. assert.strictEqual(err.code, surface_client.status.CANCELLED);
  333. done();
  334. });
  335. call.cancel();
  336. });
  337. it('Should correctly cancel a server stream call', function(done) {
  338. var call = client.fib({'limit': 5});
  339. call.on('status', function(status) {
  340. assert.strictEqual(status.code, surface_client.status.CANCELLED);
  341. done();
  342. });
  343. call.cancel();
  344. });
  345. it('Should correctly cancel a bidi stream call', function(done) {
  346. var call = client.divMany();
  347. call.on('status', function(status) {
  348. assert.strictEqual(status.code, surface_client.status.CANCELLED);
  349. done();
  350. });
  351. call.cancel();
  352. });
  353. });