blob: 7163a5fb5ee2467ae025a43f6da5623c718d22ff [file] [log] [blame]
murgatroid99749666e2015-01-12 18:25:58 -08001/*
2 *
Craig Tiller06059952015-02-18 08:34:56 -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');
murgatroid992af89e42015-10-01 11:54:00 -070037var grpc = require('bindings')('grpc_node');
murgatroid99e5061512015-01-12 18:14:35 -080038
murgatroid99c7f4d4f2015-07-28 15:18:57 -070039/**
40 * This is used for testing functions with multiple asynchronous calls that
41 * can happen in different orders. This should be passed the number of async
42 * function invocations that can occur last, and each of those should call this
43 * function's return value
44 * @param {function()} done The function that should be called when a test is
45 * complete.
46 * @param {number} count The number of calls to the resulting function if the
47 * test passes.
48 * @return {function()} The function that should be called at the end of each
49 * sequence of asynchronous functions.
50 */
51function multiDone(done, count) {
52 return function() {
53 count -= 1;
54 if (count <= 0) {
55 done();
56 }
57 };
58}
murgatroid995f709ca2015-09-30 14:22:54 -070059var insecureCreds = grpc.ChannelCredentials.createInsecure();
murgatroid99c7f4d4f2015-07-28 15:18:57 -070060
murgatroid99e5061512015-01-12 18:14:35 -080061describe('channel', function() {
62 describe('constructor', function() {
63 it('should require a string for the first argument', function() {
64 assert.doesNotThrow(function() {
murgatroid99893690f2015-07-27 14:56:40 -070065 new grpc.Channel('hostname', insecureCreds);
murgatroid99e5061512015-01-12 18:14:35 -080066 });
67 assert.throws(function() {
68 new grpc.Channel();
69 }, TypeError);
70 assert.throws(function() {
71 new grpc.Channel(5);
72 });
73 });
murgatroid99928a0cc2015-07-27 15:54:13 -070074 it('should require a credential for the second argument', function() {
murgatroid99e5061512015-01-12 18:14:35 -080075 assert.doesNotThrow(function() {
murgatroid99893690f2015-07-27 14:56:40 -070076 new grpc.Channel('hostname', insecureCreds);
murgatroid99e5061512015-01-12 18:14:35 -080077 });
78 assert.throws(function() {
79 new grpc.Channel('hostname', 5);
80 });
murgatroid99928a0cc2015-07-27 15:54:13 -070081 assert.throws(function() {
82 new grpc.Channel('hostname');
83 });
murgatroid99e5061512015-01-12 18:14:35 -080084 });
murgatroid99893690f2015-07-27 14:56:40 -070085 it('should accept an object for the third argument', function() {
86 assert.doesNotThrow(function() {
87 new grpc.Channel('hostname', insecureCreds, {});
88 });
89 assert.throws(function() {
90 new grpc.Channel('hostname', insecureCreds, 'abc');
91 });
murgatroid99e5061512015-01-12 18:14:35 -080092 });
93 it('should only accept objects with string or int values', function() {
94 assert.doesNotThrow(function() {
murgatroid99893690f2015-07-27 14:56:40 -070095 new grpc.Channel('hostname', insecureCreds,{'key' : 'value'});
murgatroid99e5061512015-01-12 18:14:35 -080096 });
97 assert.doesNotThrow(function() {
murgatroid99893690f2015-07-27 14:56:40 -070098 new grpc.Channel('hostname', insecureCreds, {'key' : 5});
murgatroid99e5061512015-01-12 18:14:35 -080099 });
100 assert.throws(function() {
murgatroid99893690f2015-07-27 14:56:40 -0700101 new grpc.Channel('hostname', insecureCreds, {'key' : null});
murgatroid99e5061512015-01-12 18:14:35 -0800102 });
103 assert.throws(function() {
murgatroid99893690f2015-07-27 14:56:40 -0700104 new grpc.Channel('hostname', insecureCreds, {'key' : new Date()});
murgatroid99e5061512015-01-12 18:14:35 -0800105 });
106 });
murgatroid9971f50362015-10-12 16:12:18 -0700107 it('should succeed without the new keyword', function() {
108 assert.doesNotThrow(function() {
109 var channel = grpc.Channel('hostname', insecureCreds);
110 assert(channel instanceof grpc.Channel);
111 });
112 });
murgatroid99e5061512015-01-12 18:14:35 -0800113 });
114 describe('close', function() {
murgatroid99c7f4d4f2015-07-28 15:18:57 -0700115 var channel;
116 beforeEach(function() {
murgatroid99c0fb2362015-07-30 16:24:06 -0700117 channel = new grpc.Channel('hostname', insecureCreds, {});
murgatroid99c7f4d4f2015-07-28 15:18:57 -0700118 });
murgatroid99e5061512015-01-12 18:14:35 -0800119 it('should succeed silently', function() {
murgatroid99e5061512015-01-12 18:14:35 -0800120 assert.doesNotThrow(function() {
121 channel.close();
122 });
123 });
124 it('should be idempotent', function() {
murgatroid99e5061512015-01-12 18:14:35 -0800125 assert.doesNotThrow(function() {
126 channel.close();
127 channel.close();
128 });
129 });
130 });
murgatroid99ea12b972015-07-24 10:43:27 -0700131 describe('getTarget', function() {
murgatroid99c7f4d4f2015-07-28 15:18:57 -0700132 var channel;
133 beforeEach(function() {
murgatroid99c0fb2362015-07-30 16:24:06 -0700134 channel = new grpc.Channel('hostname', insecureCreds, {});
murgatroid99c7f4d4f2015-07-28 15:18:57 -0700135 });
murgatroid99ea12b972015-07-24 10:43:27 -0700136 it('should return a string', function() {
murgatroid99ea12b972015-07-24 10:43:27 -0700137 assert.strictEqual(typeof channel.getTarget(), 'string');
138 });
139 });
murgatroid99c7f4d4f2015-07-28 15:18:57 -0700140 describe('getConnectivityState', function() {
141 var channel;
142 beforeEach(function() {
murgatroid99c0fb2362015-07-30 16:24:06 -0700143 channel = new grpc.Channel('hostname', insecureCreds, {});
murgatroid99c7f4d4f2015-07-28 15:18:57 -0700144 });
145 it('should return IDLE for a new channel', function() {
146 assert.strictEqual(channel.getConnectivityState(),
147 grpc.connectivityState.IDLE);
148 });
149 });
150 describe('watchConnectivityState', function() {
151 var channel;
152 beforeEach(function() {
murgatroid99c0fb2362015-07-30 16:24:06 -0700153 channel = new grpc.Channel('localhost', insecureCreds, {});
murgatroid99c7f4d4f2015-07-28 15:18:57 -0700154 });
155 afterEach(function() {
156 channel.close();
157 });
murgatroid99be13d812015-10-09 14:45:30 -0700158 it('should time out if called alone', function(done) {
murgatroid99c7f4d4f2015-07-28 15:18:57 -0700159 var old_state = channel.getConnectivityState();
160 var deadline = new Date();
161 deadline.setSeconds(deadline.getSeconds() + 1);
162 channel.watchConnectivityState(old_state, deadline, function(err, value) {
163 assert(err);
164 done();
165 });
166 });
167 it('should complete if a connection attempt is forced', function(done) {
168 var old_state = channel.getConnectivityState();
169 var deadline = new Date();
170 deadline.setSeconds(deadline.getSeconds() + 1);
171 channel.watchConnectivityState(old_state, deadline, function(err, value) {
172 assert.ifError(err);
173 assert.notEqual(value.new_state, old_state);
174 done();
175 });
176 channel.getConnectivityState(true);
177 });
178 it('should complete twice if called twice', function(done) {
179 done = multiDone(done, 2);
180 var old_state = channel.getConnectivityState();
181 var deadline = new Date();
182 deadline.setSeconds(deadline.getSeconds() + 1);
183 channel.watchConnectivityState(old_state, deadline, function(err, value) {
184 assert.ifError(err);
185 assert.notEqual(value.new_state, old_state);
186 done();
187 });
188 channel.watchConnectivityState(old_state, deadline, function(err, value) {
189 assert.ifError(err);
190 assert.notEqual(value.new_state, old_state);
191 done();
192 });
193 channel.getConnectivityState(true);
194 });
195 });
Craig Tillerce5021b2015-02-18 09:25:21 -0800196});