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