blob: 8d0f20b074736be68cef9914e3ff34eeeda62422 [file] [log] [blame]
murgatroid99749666e2015-01-12 18:25:58 -08001/*
2 *
murgatroid991578c6a2015-02-11 16:19:55 -08003 * Copyright 2015, Google Inc.
murgatroid99749666e2015-01-12 18:25:58 -08004 * 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
murgatroid99dca966d2015-02-19 14:37:18 -080034'use strict';
35
murgatroid99e5061512015-01-12 18:14:35 -080036var assert = require('assert');
37var grpc = require('bindings')('grpc.node');
38
murgatroid99e5061512015-01-12 18:14:35 -080039/**
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 */
45function getDeadline(timeout_secs) {
46 var deadline = new Date();
47 deadline.setSeconds(deadline.getSeconds() + timeout_secs);
48 return deadline;
49}
50
murgatroid99893690f2015-07-27 14:56:40 -070051var insecureCreds = grpc.Credentials.createInsecure();
52
murgatroid99e5061512015-01-12 18:14:35 -080053describe('call', function() {
murgatroid994b962f72015-01-26 14:31:03 -080054 var channel;
55 var server;
56 before(function() {
57 server = new grpc.Server();
murgatroid991a7dcac2015-07-27 16:13:28 -070058 var port = server.addHttp2Port('localhost:0',
59 grpc.ServerCredentials.createInsecure());
murgatroid994b962f72015-01-26 14:31:03 -080060 server.start();
murgatroid99893690f2015-07-27 14:56:40 -070061 channel = new grpc.Channel('localhost:' + port, insecureCreds);
murgatroid994b962f72015-01-26 14:31:03 -080062 });
63 after(function() {
64 server.shutdown();
65 });
murgatroid99e5061512015-01-12 18:14:35 -080066 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 });
murgatroid99a16b5ef2015-08-03 15:18:23 -070087 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 });
murgatroid99e5061512015-01-12 18:14:35 -080092 it('should fail with a closed channel', function() {
murgatroid99893690f2015-07-27 14:56:40 -070093 var local_channel = new grpc.Channel('hostname', insecureCreds);
murgatroid99e5061512015-01-12 18:14:35 -080094 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 });
110 });
murgatroid99016bb502015-02-09 15:55:10 -0800111 describe('startBatch', function() {
112 it('should fail without an object and a function', function() {
murgatroid99e5061512015-01-12 18:14:35 -0800113 var call = new grpc.Call(channel, 'method', getDeadline(1));
murgatroid99016bb502015-02-09 15:55:10 -0800114 assert.throws(function() {
115 call.startBatch();
murgatroid99e5061512015-01-12 18:14:35 -0800116 });
murgatroid99016bb502015-02-09 15:55:10 -0800117 assert.throws(function() {
118 call.startBatch({});
119 });
120 assert.throws(function() {
121 call.startBatch(null, function(){});
murgatroid997625db42015-01-28 15:36:27 -0800122 });
123 });
murgatroid99c55ee612015-02-12 13:58:24 -0800124 it('should succeed with an empty object', function(done) {
murgatroid997625db42015-01-28 15:36:27 -0800125 var call = new grpc.Call(channel, 'method', getDeadline(1));
126 assert.doesNotThrow(function() {
murgatroid99016bb502015-02-09 15:55:10 -0800127 call.startBatch({}, function(err) {
128 assert.ifError(err);
129 done();
130 });
murgatroid99e5061512015-01-12 18:14:35 -0800131 });
murgatroid99016bb502015-02-09 15:55:10 -0800132 });
133 });
134 describe('startBatch with metadata', function() {
135 it('should succeed with a map of strings to string arrays', function(done) {
136 var call = new grpc.Call(channel, 'method', getDeadline(1));
murgatroid99e5061512015-01-12 18:14:35 -0800137 assert.doesNotThrow(function() {
murgatroid99016bb502015-02-09 15:55:10 -0800138 var batch = {};
139 batch[grpc.opType.SEND_INITIAL_METADATA] = {'key1': ['value1'],
140 'key2': ['value2']};
141 call.startBatch(batch, function(err, resp) {
142 assert.ifError(err);
murgatroid99a627d892015-07-23 10:40:19 -0700143 assert.deepEqual(resp, {'send_metadata': true});
murgatroid99016bb502015-02-09 15:55:10 -0800144 done();
145 });
146 });
147 });
148 it('should succeed with a map of strings to buffer arrays', function(done) {
149 var call = new grpc.Call(channel, 'method', getDeadline(1));
150 assert.doesNotThrow(function() {
151 var batch = {};
152 batch[grpc.opType.SEND_INITIAL_METADATA] = {
murgatroid99da771512015-03-17 18:13:55 -0700153 'key1-bin': [new Buffer('value1')],
154 'key2-bin': [new Buffer('value2')]
murgatroid99016bb502015-02-09 15:55:10 -0800155 };
156 call.startBatch(batch, function(err, resp) {
157 assert.ifError(err);
murgatroid99a627d892015-07-23 10:40:19 -0700158 assert.deepEqual(resp, {'send_metadata': true});
murgatroid99016bb502015-02-09 15:55:10 -0800159 done();
160 });
murgatroid99e5061512015-01-12 18:14:35 -0800161 });
162 });
163 it('should fail with other parameter types', function() {
164 var call = new grpc.Call(channel, 'method', getDeadline(1));
165 assert.throws(function() {
murgatroid99016bb502015-02-09 15:55:10 -0800166 var batch = {};
167 batch[grpc.opType.SEND_INITIAL_METADATA] = undefined;
168 call.startBatch(batch, function(){});
murgatroid997625db42015-01-28 15:36:27 -0800169 });
170 assert.throws(function() {
murgatroid99016bb502015-02-09 15:55:10 -0800171 var batch = {};
172 batch[grpc.opType.SEND_INITIAL_METADATA] = null;
173 call.startBatch(batch, function(){});
murgatroid99e5061512015-01-12 18:14:35 -0800174 }, TypeError);
175 assert.throws(function() {
murgatroid99016bb502015-02-09 15:55:10 -0800176 var batch = {};
177 batch[grpc.opType.SEND_INITIAL_METADATA] = 'value';
178 call.startBatch(batch, function(){});
murgatroid99e5061512015-01-12 18:14:35 -0800179 }, TypeError);
180 assert.throws(function() {
murgatroid99016bb502015-02-09 15:55:10 -0800181 var batch = {};
182 batch[grpc.opType.SEND_INITIAL_METADATA] = 5;
183 call.startBatch(batch, function(){});
murgatroid99e5061512015-01-12 18:14:35 -0800184 }, TypeError);
185 });
murgatroid99e5061512015-01-12 18:14:35 -0800186 });
187 describe('cancel', function() {
188 it('should succeed', function() {
189 var call = new grpc.Call(channel, 'method', getDeadline(1));
190 assert.doesNotThrow(function() {
191 call.cancel();
192 });
193 });
194 });
murgatroid99ea12b972015-07-24 10:43:27 -0700195 describe('getPeer', function() {
196 it('should return a string', function() {
197 var call = new grpc.Call(channel, 'method', getDeadline(1));
198 assert.strictEqual(typeof call.getPeer(), 'string');
199 });
200 });
murgatroid99e5061512015-01-12 18:14:35 -0800201});