murgatroid99 | 749666e | 2015-01-12 18:25:58 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
murgatroid99 | 1578c6a | 2015-02-11 16:19:55 -0800 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
murgatroid99 | 749666e | 2015-01-12 18:25:58 -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'; |
| 35 | |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 36 | var assert = require('assert'); |
murgatroid99 | 2af89e4 | 2015-10-01 11:54:00 -0700 | [diff] [blame] | 37 | var grpc = require('bindings')('grpc_node'); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 38 | |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 39 | /** |
| 40 | * Helper function to return an absolute deadline given a relative timeout in |
| 41 | * seconds. |
| 42 | * @param {number} timeout_secs The number of seconds to wait before timing out |
| 43 | * @return {Date} A date timeout_secs in the future |
| 44 | */ |
| 45 | function getDeadline(timeout_secs) { |
| 46 | var deadline = new Date(); |
| 47 | deadline.setSeconds(deadline.getSeconds() + timeout_secs); |
| 48 | return deadline; |
| 49 | } |
| 50 | |
murgatroid99 | 5f709ca | 2015-09-30 14:22:54 -0700 | [diff] [blame] | 51 | var insecureCreds = grpc.ChannelCredentials.createInsecure(); |
murgatroid99 | 893690f | 2015-07-27 14:56:40 -0700 | [diff] [blame] | 52 | |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 53 | describe('call', function() { |
murgatroid99 | 4b962f7 | 2015-01-26 14:31:03 -0800 | [diff] [blame] | 54 | var channel; |
| 55 | var server; |
| 56 | before(function() { |
| 57 | server = new grpc.Server(); |
murgatroid99 | 1a7dcac | 2015-07-27 16:13:28 -0700 | [diff] [blame] | 58 | var port = server.addHttp2Port('localhost:0', |
| 59 | grpc.ServerCredentials.createInsecure()); |
murgatroid99 | 4b962f7 | 2015-01-26 14:31:03 -0800 | [diff] [blame] | 60 | server.start(); |
murgatroid99 | 893690f | 2015-07-27 14:56:40 -0700 | [diff] [blame] | 61 | channel = new grpc.Channel('localhost:' + port, insecureCreds); |
murgatroid99 | 4b962f7 | 2015-01-26 14:31:03 -0800 | [diff] [blame] | 62 | }); |
| 63 | after(function() { |
murgatroid99 | cb951f6 | 2015-08-18 17:38:11 -0700 | [diff] [blame] | 64 | server.forceShutdown(); |
murgatroid99 | 4b962f7 | 2015-01-26 14:31:03 -0800 | [diff] [blame] | 65 | }); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 66 | describe('constructor', function() { |
| 67 | it('should reject anything less than 3 arguments', function() { |
| 68 | assert.throws(function() { |
| 69 | new grpc.Call(); |
| 70 | }, TypeError); |
| 71 | assert.throws(function() { |
| 72 | new grpc.Call(channel); |
| 73 | }, TypeError); |
| 74 | assert.throws(function() { |
| 75 | new grpc.Call(channel, 'method'); |
| 76 | }, TypeError); |
| 77 | }); |
| 78 | it('should succeed with a Channel, a string, and a date or number', |
| 79 | function() { |
| 80 | assert.doesNotThrow(function() { |
| 81 | new grpc.Call(channel, 'method', new Date()); |
| 82 | }); |
| 83 | assert.doesNotThrow(function() { |
| 84 | new grpc.Call(channel, 'method', 0); |
| 85 | }); |
| 86 | }); |
murgatroid99 | a16b5ef | 2015-08-03 15:18:23 -0700 | [diff] [blame] | 87 | it('should accept an optional fourth string parameter', function() { |
| 88 | assert.doesNotThrow(function() { |
| 89 | new grpc.Call(channel, 'method', new Date(), 'host_override'); |
| 90 | }); |
| 91 | }); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 92 | it('should fail with a closed channel', function() { |
murgatroid99 | 893690f | 2015-07-27 14:56:40 -0700 | [diff] [blame] | 93 | var local_channel = new grpc.Channel('hostname', insecureCreds); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 94 | local_channel.close(); |
| 95 | assert.throws(function() { |
| 96 | new grpc.Call(channel, 'method'); |
| 97 | }); |
| 98 | }); |
| 99 | it('should fail with other types', function() { |
| 100 | assert.throws(function() { |
| 101 | new grpc.Call({}, 'method', 0); |
| 102 | }, TypeError); |
| 103 | assert.throws(function() { |
| 104 | new grpc.Call(channel, null, 0); |
| 105 | }, TypeError); |
| 106 | assert.throws(function() { |
| 107 | new grpc.Call(channel, 'method', 'now'); |
| 108 | }, TypeError); |
| 109 | }); |
murgatroid99 | 71f5036 | 2015-10-12 16:12:18 -0700 | [diff] [blame] | 110 | it('should succeed without the new keyword', function() { |
| 111 | assert.doesNotThrow(function() { |
| 112 | var call = grpc.Call(channel, 'method', new Date()); |
| 113 | assert(call instanceof grpc.Call); |
| 114 | }); |
| 115 | }); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 116 | }); |
murgatroid99 | 6fe015e | 2015-10-12 13:18:06 -0700 | [diff] [blame] | 117 | describe('deadline', function() { |
| 118 | it('should time out immediately with negative deadline', function(done) { |
| 119 | var call = new grpc.Call(channel, 'method', -Infinity); |
| 120 | var batch = {}; |
| 121 | batch[grpc.opType.RECV_STATUS_ON_CLIENT] = true; |
| 122 | call.startBatch(batch, function(err, response) { |
| 123 | assert.strictEqual(response.status.code, grpc.status.DEADLINE_EXCEEDED); |
| 124 | done(); |
| 125 | }); |
| 126 | }); |
| 127 | }); |
murgatroid99 | 016bb50 | 2015-02-09 15:55:10 -0800 | [diff] [blame] | 128 | describe('startBatch', function() { |
| 129 | it('should fail without an object and a function', function() { |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 130 | var call = new grpc.Call(channel, 'method', getDeadline(1)); |
murgatroid99 | 016bb50 | 2015-02-09 15:55:10 -0800 | [diff] [blame] | 131 | assert.throws(function() { |
| 132 | call.startBatch(); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 133 | }); |
murgatroid99 | 016bb50 | 2015-02-09 15:55:10 -0800 | [diff] [blame] | 134 | assert.throws(function() { |
| 135 | call.startBatch({}); |
| 136 | }); |
| 137 | assert.throws(function() { |
| 138 | call.startBatch(null, function(){}); |
murgatroid99 | 7625db4 | 2015-01-28 15:36:27 -0800 | [diff] [blame] | 139 | }); |
| 140 | }); |
murgatroid99 | c55ee61 | 2015-02-12 13:58:24 -0800 | [diff] [blame] | 141 | it('should succeed with an empty object', function(done) { |
murgatroid99 | 7625db4 | 2015-01-28 15:36:27 -0800 | [diff] [blame] | 142 | var call = new grpc.Call(channel, 'method', getDeadline(1)); |
| 143 | assert.doesNotThrow(function() { |
murgatroid99 | 016bb50 | 2015-02-09 15:55:10 -0800 | [diff] [blame] | 144 | call.startBatch({}, function(err) { |
| 145 | assert.ifError(err); |
| 146 | done(); |
| 147 | }); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 148 | }); |
murgatroid99 | 016bb50 | 2015-02-09 15:55:10 -0800 | [diff] [blame] | 149 | }); |
| 150 | }); |
| 151 | describe('startBatch with metadata', function() { |
| 152 | it('should succeed with a map of strings to string arrays', function(done) { |
| 153 | var call = new grpc.Call(channel, 'method', getDeadline(1)); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 154 | assert.doesNotThrow(function() { |
murgatroid99 | 016bb50 | 2015-02-09 15:55:10 -0800 | [diff] [blame] | 155 | var batch = {}; |
| 156 | batch[grpc.opType.SEND_INITIAL_METADATA] = {'key1': ['value1'], |
| 157 | 'key2': ['value2']}; |
| 158 | call.startBatch(batch, function(err, resp) { |
| 159 | assert.ifError(err); |
murgatroid99 | a627d89 | 2015-07-23 10:40:19 -0700 | [diff] [blame] | 160 | assert.deepEqual(resp, {'send_metadata': true}); |
murgatroid99 | 016bb50 | 2015-02-09 15:55:10 -0800 | [diff] [blame] | 161 | done(); |
| 162 | }); |
| 163 | }); |
| 164 | }); |
| 165 | it('should succeed with a map of strings to buffer arrays', function(done) { |
| 166 | var call = new grpc.Call(channel, 'method', getDeadline(1)); |
| 167 | assert.doesNotThrow(function() { |
| 168 | var batch = {}; |
| 169 | batch[grpc.opType.SEND_INITIAL_METADATA] = { |
murgatroid99 | da77151 | 2015-03-17 18:13:55 -0700 | [diff] [blame] | 170 | 'key1-bin': [new Buffer('value1')], |
| 171 | 'key2-bin': [new Buffer('value2')] |
murgatroid99 | 016bb50 | 2015-02-09 15:55:10 -0800 | [diff] [blame] | 172 | }; |
| 173 | call.startBatch(batch, function(err, resp) { |
| 174 | assert.ifError(err); |
murgatroid99 | a627d89 | 2015-07-23 10:40:19 -0700 | [diff] [blame] | 175 | assert.deepEqual(resp, {'send_metadata': true}); |
murgatroid99 | 016bb50 | 2015-02-09 15:55:10 -0800 | [diff] [blame] | 176 | done(); |
| 177 | }); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 178 | }); |
| 179 | }); |
| 180 | it('should fail with other parameter types', function() { |
| 181 | var call = new grpc.Call(channel, 'method', getDeadline(1)); |
| 182 | assert.throws(function() { |
murgatroid99 | 016bb50 | 2015-02-09 15:55:10 -0800 | [diff] [blame] | 183 | var batch = {}; |
| 184 | batch[grpc.opType.SEND_INITIAL_METADATA] = undefined; |
| 185 | call.startBatch(batch, function(){}); |
murgatroid99 | 7625db4 | 2015-01-28 15:36:27 -0800 | [diff] [blame] | 186 | }); |
| 187 | assert.throws(function() { |
murgatroid99 | 016bb50 | 2015-02-09 15:55:10 -0800 | [diff] [blame] | 188 | var batch = {}; |
| 189 | batch[grpc.opType.SEND_INITIAL_METADATA] = null; |
| 190 | call.startBatch(batch, function(){}); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 191 | }, TypeError); |
| 192 | assert.throws(function() { |
murgatroid99 | 016bb50 | 2015-02-09 15:55:10 -0800 | [diff] [blame] | 193 | var batch = {}; |
| 194 | batch[grpc.opType.SEND_INITIAL_METADATA] = 'value'; |
| 195 | call.startBatch(batch, function(){}); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 196 | }, TypeError); |
| 197 | assert.throws(function() { |
murgatroid99 | 016bb50 | 2015-02-09 15:55:10 -0800 | [diff] [blame] | 198 | var batch = {}; |
| 199 | batch[grpc.opType.SEND_INITIAL_METADATA] = 5; |
| 200 | call.startBatch(batch, function(){}); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 201 | }, TypeError); |
| 202 | }); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 203 | }); |
| 204 | describe('cancel', function() { |
| 205 | it('should succeed', function() { |
| 206 | var call = new grpc.Call(channel, 'method', getDeadline(1)); |
| 207 | assert.doesNotThrow(function() { |
| 208 | call.cancel(); |
| 209 | }); |
| 210 | }); |
| 211 | }); |
murgatroid99 | 6fe015e | 2015-10-12 13:18:06 -0700 | [diff] [blame] | 212 | describe('cancelWithStatus', function() { |
| 213 | it('should reject anything other than an integer and a string', function() { |
| 214 | assert.doesNotThrow(function() { |
| 215 | var call = new grpc.Call(channel, 'method', getDeadline(1)); |
| 216 | call.cancelWithStatus(1, 'details'); |
| 217 | }); |
| 218 | assert.throws(function() { |
| 219 | var call = new grpc.Call(channel, 'method', getDeadline(1)); |
| 220 | call.cancelWithStatus(); |
| 221 | }); |
| 222 | assert.throws(function() { |
| 223 | var call = new grpc.Call(channel, 'method', getDeadline(1)); |
| 224 | call.cancelWithStatus(''); |
| 225 | }); |
| 226 | assert.throws(function() { |
| 227 | var call = new grpc.Call(channel, 'method', getDeadline(1)); |
| 228 | call.cancelWithStatus(5, {}); |
| 229 | }); |
| 230 | }); |
| 231 | it('should reject the OK status code', function() { |
| 232 | assert.throws(function() { |
| 233 | var call = new grpc.Call(channel, 'method', getDeadline(1)); |
| 234 | call.cancelWithStatus(0, 'details'); |
| 235 | }); |
| 236 | }); |
| 237 | it('should result in the call ending with a status', function(done) { |
| 238 | var call = new grpc.Call(channel, 'method', getDeadline(1)); |
| 239 | var batch = {}; |
| 240 | batch[grpc.opType.RECV_STATUS_ON_CLIENT] = true; |
| 241 | call.startBatch(batch, function(err, response) { |
| 242 | assert.strictEqual(response.status.code, 5); |
| 243 | assert.strictEqual(response.status.details, 'details'); |
| 244 | done(); |
| 245 | }); |
| 246 | call.cancelWithStatus(5, 'details'); |
| 247 | }); |
| 248 | }); |
murgatroid99 | ea12b97 | 2015-07-24 10:43:27 -0700 | [diff] [blame] | 249 | describe('getPeer', function() { |
| 250 | it('should return a string', function() { |
| 251 | var call = new grpc.Call(channel, 'method', getDeadline(1)); |
| 252 | assert.strictEqual(typeof call.getPeer(), 'string'); |
| 253 | }); |
| 254 | }); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 255 | }); |