surface_test.js 11 KB

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