OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * |
| 3 * Copyright 2015-2016, Google Inc. |
| 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 |
| 34 'use strict'; |
| 35 |
| 36 var assert = require('assert'); |
| 37 var grpc = require('../src/grpc_extension'); |
| 38 |
| 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 } |
| 59 var insecureCreds = grpc.ChannelCredentials.createInsecure(); |
| 60 |
| 61 describe('channel', function() { |
| 62 describe('constructor', function() { |
| 63 it('should require a string for the first argument', function() { |
| 64 assert.doesNotThrow(function() { |
| 65 new grpc.Channel('hostname', insecureCreds); |
| 66 }); |
| 67 assert.throws(function() { |
| 68 new grpc.Channel(); |
| 69 }, TypeError); |
| 70 assert.throws(function() { |
| 71 new grpc.Channel(5); |
| 72 }); |
| 73 }); |
| 74 it('should require a credential for the second argument', function() { |
| 75 assert.doesNotThrow(function() { |
| 76 new grpc.Channel('hostname', insecureCreds); |
| 77 }); |
| 78 assert.throws(function() { |
| 79 new grpc.Channel('hostname', 5); |
| 80 }); |
| 81 assert.throws(function() { |
| 82 new grpc.Channel('hostname'); |
| 83 }); |
| 84 }); |
| 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 }); |
| 92 }); |
| 93 it('should only accept objects with string or int values', function() { |
| 94 assert.doesNotThrow(function() { |
| 95 new grpc.Channel('hostname', insecureCreds,{'key' : 'value'}); |
| 96 }); |
| 97 assert.doesNotThrow(function() { |
| 98 new grpc.Channel('hostname', insecureCreds, {'key' : 5}); |
| 99 }); |
| 100 assert.throws(function() { |
| 101 new grpc.Channel('hostname', insecureCreds, {'key' : null}); |
| 102 }); |
| 103 assert.throws(function() { |
| 104 new grpc.Channel('hostname', insecureCreds, {'key' : new Date()}); |
| 105 }); |
| 106 }); |
| 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 }); |
| 113 }); |
| 114 describe('close', function() { |
| 115 var channel; |
| 116 beforeEach(function() { |
| 117 channel = new grpc.Channel('hostname', insecureCreds, {}); |
| 118 }); |
| 119 it('should succeed silently', function() { |
| 120 assert.doesNotThrow(function() { |
| 121 channel.close(); |
| 122 }); |
| 123 }); |
| 124 it('should be idempotent', function() { |
| 125 assert.doesNotThrow(function() { |
| 126 channel.close(); |
| 127 channel.close(); |
| 128 }); |
| 129 }); |
| 130 }); |
| 131 describe('getTarget', function() { |
| 132 var channel; |
| 133 beforeEach(function() { |
| 134 channel = new grpc.Channel('hostname', insecureCreds, {}); |
| 135 }); |
| 136 it('should return a string', function() { |
| 137 assert.strictEqual(typeof channel.getTarget(), 'string'); |
| 138 }); |
| 139 }); |
| 140 describe('getConnectivityState', function() { |
| 141 var channel; |
| 142 beforeEach(function() { |
| 143 channel = new grpc.Channel('hostname', insecureCreds, {}); |
| 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() { |
| 153 channel = new grpc.Channel('localhost', insecureCreds, {}); |
| 154 }); |
| 155 afterEach(function() { |
| 156 channel.close(); |
| 157 }); |
| 158 it('should time out if called alone', function(done) { |
| 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 }); |
| 196 }); |
OLD | NEW |