murgatroid99 | 749666e | 2015-01-12 18:25:58 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 0605995 | 2015-02-18 08:34:56 -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 | c7f4d4f | 2015-07-28 15:18:57 -0700 | [diff] [blame] | 39 | /** |
| 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 | */ |
| 51 | function multiDone(done, count) { |
| 52 | return function() { |
| 53 | count -= 1; |
| 54 | if (count <= 0) { |
| 55 | done(); |
| 56 | } |
| 57 | }; |
| 58 | } |
murgatroid99 | 5f709ca | 2015-09-30 14:22:54 -0700 | [diff] [blame] | 59 | var insecureCreds = grpc.ChannelCredentials.createInsecure(); |
murgatroid99 | c7f4d4f | 2015-07-28 15:18:57 -0700 | [diff] [blame] | 60 | |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 61 | describe('channel', function() { |
| 62 | describe('constructor', function() { |
| 63 | it('should require a string for the first argument', function() { |
| 64 | assert.doesNotThrow(function() { |
murgatroid99 | 893690f | 2015-07-27 14:56:40 -0700 | [diff] [blame] | 65 | new grpc.Channel('hostname', insecureCreds); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 66 | }); |
| 67 | assert.throws(function() { |
| 68 | new grpc.Channel(); |
| 69 | }, TypeError); |
| 70 | assert.throws(function() { |
| 71 | new grpc.Channel(5); |
| 72 | }); |
| 73 | }); |
murgatroid99 | 928a0cc | 2015-07-27 15:54:13 -0700 | [diff] [blame] | 74 | it('should require a credential for the second argument', function() { |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 75 | assert.doesNotThrow(function() { |
murgatroid99 | 893690f | 2015-07-27 14:56:40 -0700 | [diff] [blame] | 76 | new grpc.Channel('hostname', insecureCreds); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 77 | }); |
| 78 | assert.throws(function() { |
| 79 | new grpc.Channel('hostname', 5); |
| 80 | }); |
murgatroid99 | 928a0cc | 2015-07-27 15:54:13 -0700 | [diff] [blame] | 81 | assert.throws(function() { |
| 82 | new grpc.Channel('hostname'); |
| 83 | }); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 84 | }); |
murgatroid99 | 893690f | 2015-07-27 14:56:40 -0700 | [diff] [blame] | 85 | 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 | }); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 92 | }); |
| 93 | it('should only accept objects with string or int values', function() { |
| 94 | assert.doesNotThrow(function() { |
murgatroid99 | 893690f | 2015-07-27 14:56:40 -0700 | [diff] [blame] | 95 | new grpc.Channel('hostname', insecureCreds,{'key' : 'value'}); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 96 | }); |
| 97 | assert.doesNotThrow(function() { |
murgatroid99 | 893690f | 2015-07-27 14:56:40 -0700 | [diff] [blame] | 98 | new grpc.Channel('hostname', insecureCreds, {'key' : 5}); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 99 | }); |
| 100 | assert.throws(function() { |
murgatroid99 | 893690f | 2015-07-27 14:56:40 -0700 | [diff] [blame] | 101 | new grpc.Channel('hostname', insecureCreds, {'key' : null}); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 102 | }); |
| 103 | assert.throws(function() { |
murgatroid99 | 893690f | 2015-07-27 14:56:40 -0700 | [diff] [blame] | 104 | new grpc.Channel('hostname', insecureCreds, {'key' : new Date()}); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 105 | }); |
| 106 | }); |
murgatroid99 | 71f5036 | 2015-10-12 16:12:18 -0700 | [diff] [blame] | 107 | 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 | }); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 113 | }); |
| 114 | describe('close', function() { |
murgatroid99 | c7f4d4f | 2015-07-28 15:18:57 -0700 | [diff] [blame] | 115 | var channel; |
| 116 | beforeEach(function() { |
murgatroid99 | c0fb236 | 2015-07-30 16:24:06 -0700 | [diff] [blame] | 117 | channel = new grpc.Channel('hostname', insecureCreds, {}); |
murgatroid99 | c7f4d4f | 2015-07-28 15:18:57 -0700 | [diff] [blame] | 118 | }); |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 119 | it('should succeed silently', function() { |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 120 | assert.doesNotThrow(function() { |
| 121 | channel.close(); |
| 122 | }); |
| 123 | }); |
| 124 | it('should be idempotent', function() { |
murgatroid99 | e506151 | 2015-01-12 18:14:35 -0800 | [diff] [blame] | 125 | assert.doesNotThrow(function() { |
| 126 | channel.close(); |
| 127 | channel.close(); |
| 128 | }); |
| 129 | }); |
| 130 | }); |
murgatroid99 | ea12b97 | 2015-07-24 10:43:27 -0700 | [diff] [blame] | 131 | describe('getTarget', function() { |
murgatroid99 | c7f4d4f | 2015-07-28 15:18:57 -0700 | [diff] [blame] | 132 | var channel; |
| 133 | beforeEach(function() { |
murgatroid99 | c0fb236 | 2015-07-30 16:24:06 -0700 | [diff] [blame] | 134 | channel = new grpc.Channel('hostname', insecureCreds, {}); |
murgatroid99 | c7f4d4f | 2015-07-28 15:18:57 -0700 | [diff] [blame] | 135 | }); |
murgatroid99 | ea12b97 | 2015-07-24 10:43:27 -0700 | [diff] [blame] | 136 | it('should return a string', function() { |
murgatroid99 | ea12b97 | 2015-07-24 10:43:27 -0700 | [diff] [blame] | 137 | assert.strictEqual(typeof channel.getTarget(), 'string'); |
| 138 | }); |
| 139 | }); |
murgatroid99 | c7f4d4f | 2015-07-28 15:18:57 -0700 | [diff] [blame] | 140 | describe('getConnectivityState', function() { |
| 141 | var channel; |
| 142 | beforeEach(function() { |
murgatroid99 | c0fb236 | 2015-07-30 16:24:06 -0700 | [diff] [blame] | 143 | channel = new grpc.Channel('hostname', insecureCreds, {}); |
murgatroid99 | c7f4d4f | 2015-07-28 15:18:57 -0700 | [diff] [blame] | 144 | }); |
| 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() { |
murgatroid99 | c0fb236 | 2015-07-30 16:24:06 -0700 | [diff] [blame] | 153 | channel = new grpc.Channel('localhost', insecureCreds, {}); |
murgatroid99 | c7f4d4f | 2015-07-28 15:18:57 -0700 | [diff] [blame] | 154 | }); |
| 155 | afterEach(function() { |
| 156 | channel.close(); |
| 157 | }); |
murgatroid99 | be13d81 | 2015-10-09 14:45:30 -0700 | [diff] [blame] | 158 | it('should time out if called alone', function(done) { |
murgatroid99 | c7f4d4f | 2015-07-28 15:18:57 -0700 | [diff] [blame] | 159 | 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 Tiller | ce5021b | 2015-02-18 09:25:21 -0800 | [diff] [blame] | 196 | }); |