channel_test.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. *
  3. * Copyright 2015, 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. 'use strict';
  34. var assert = require('assert');
  35. var grpc = require('bindings')('grpc_node');
  36. /**
  37. * This is used for testing functions with multiple asynchronous calls that
  38. * can happen in different orders. This should be passed the number of async
  39. * function invocations that can occur last, and each of those should call this
  40. * function's return value
  41. * @param {function()} done The function that should be called when a test is
  42. * complete.
  43. * @param {number} count The number of calls to the resulting function if the
  44. * test passes.
  45. * @return {function()} The function that should be called at the end of each
  46. * sequence of asynchronous functions.
  47. */
  48. function multiDone(done, count) {
  49. return function() {
  50. count -= 1;
  51. if (count <= 0) {
  52. done();
  53. }
  54. };
  55. }
  56. var insecureCreds = grpc.ChannelCredentials.createInsecure();
  57. describe('channel', function() {
  58. describe('constructor', function() {
  59. it('should require a string for the first argument', function() {
  60. assert.doesNotThrow(function() {
  61. new grpc.Channel('hostname', insecureCreds);
  62. });
  63. assert.throws(function() {
  64. new grpc.Channel();
  65. }, TypeError);
  66. assert.throws(function() {
  67. new grpc.Channel(5);
  68. });
  69. });
  70. it('should require a credential for the second argument', function() {
  71. assert.doesNotThrow(function() {
  72. new grpc.Channel('hostname', insecureCreds);
  73. });
  74. assert.throws(function() {
  75. new grpc.Channel('hostname', 5);
  76. });
  77. assert.throws(function() {
  78. new grpc.Channel('hostname');
  79. });
  80. });
  81. it('should accept an object for the third argument', function() {
  82. assert.doesNotThrow(function() {
  83. new grpc.Channel('hostname', insecureCreds, {});
  84. });
  85. assert.throws(function() {
  86. new grpc.Channel('hostname', insecureCreds, 'abc');
  87. });
  88. });
  89. it('should only accept objects with string or int values', function() {
  90. assert.doesNotThrow(function() {
  91. new grpc.Channel('hostname', insecureCreds,{'key' : 'value'});
  92. });
  93. assert.doesNotThrow(function() {
  94. new grpc.Channel('hostname', insecureCreds, {'key' : 5});
  95. });
  96. assert.throws(function() {
  97. new grpc.Channel('hostname', insecureCreds, {'key' : null});
  98. });
  99. assert.throws(function() {
  100. new grpc.Channel('hostname', insecureCreds, {'key' : new Date()});
  101. });
  102. });
  103. it('should succeed without the new keyword', function() {
  104. assert.doesNotThrow(function() {
  105. var channel = grpc.Channel('hostname', insecureCreds);
  106. assert(channel instanceof grpc.Channel);
  107. });
  108. });
  109. });
  110. describe('close', function() {
  111. var channel;
  112. beforeEach(function() {
  113. channel = new grpc.Channel('hostname', insecureCreds, {});
  114. });
  115. it('should succeed silently', function() {
  116. assert.doesNotThrow(function() {
  117. channel.close();
  118. });
  119. });
  120. it('should be idempotent', function() {
  121. assert.doesNotThrow(function() {
  122. channel.close();
  123. channel.close();
  124. });
  125. });
  126. });
  127. describe('getTarget', function() {
  128. var channel;
  129. beforeEach(function() {
  130. channel = new grpc.Channel('hostname', insecureCreds, {});
  131. });
  132. it('should return a string', function() {
  133. assert.strictEqual(typeof channel.getTarget(), 'string');
  134. });
  135. });
  136. describe('getConnectivityState', function() {
  137. var channel;
  138. beforeEach(function() {
  139. channel = new grpc.Channel('hostname', insecureCreds, {});
  140. });
  141. it('should return IDLE for a new channel', function() {
  142. assert.strictEqual(channel.getConnectivityState(),
  143. grpc.connectivityState.IDLE);
  144. });
  145. });
  146. describe('watchConnectivityState', function() {
  147. var channel;
  148. beforeEach(function() {
  149. channel = new grpc.Channel('localhost', insecureCreds, {});
  150. });
  151. afterEach(function() {
  152. channel.close();
  153. });
  154. it('should time out if called alone', function(done) {
  155. var old_state = channel.getConnectivityState();
  156. var deadline = new Date();
  157. deadline.setSeconds(deadline.getSeconds() + 1);
  158. channel.watchConnectivityState(old_state, deadline, function(err, value) {
  159. assert(err);
  160. done();
  161. });
  162. });
  163. it('should complete if a connection attempt is forced', function(done) {
  164. var old_state = channel.getConnectivityState();
  165. var deadline = new Date();
  166. deadline.setSeconds(deadline.getSeconds() + 1);
  167. channel.watchConnectivityState(old_state, deadline, function(err, value) {
  168. assert.ifError(err);
  169. assert.notEqual(value.new_state, old_state);
  170. done();
  171. });
  172. channel.getConnectivityState(true);
  173. });
  174. it('should complete twice if called twice', function(done) {
  175. done = multiDone(done, 2);
  176. var old_state = channel.getConnectivityState();
  177. var deadline = new Date();
  178. deadline.setSeconds(deadline.getSeconds() + 1);
  179. channel.watchConnectivityState(old_state, deadline, function(err, value) {
  180. assert.ifError(err);
  181. assert.notEqual(value.new_state, old_state);
  182. done();
  183. });
  184. channel.watchConnectivityState(old_state, deadline, function(err, value) {
  185. assert.ifError(err);
  186. assert.notEqual(value.new_state, old_state);
  187. done();
  188. });
  189. channel.getConnectivityState(true);
  190. });
  191. });
  192. });