channel_spec.rb 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. # Copyright 2015 gRPC authors.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. require 'spec_helper'
  15. require 'English'
  16. def load_test_certs
  17. test_root = File.join(File.dirname(__FILE__), 'testdata')
  18. files = ['ca.pem', 'server1.key', 'server1.pem']
  19. files.map { |f| File.open(File.join(test_root, f)).read }
  20. end
  21. describe GRPC::Core::Channel do
  22. let(:fake_host) { 'localhost:0' }
  23. def create_test_cert
  24. GRPC::Core::ChannelCredentials.new(load_test_certs[0])
  25. end
  26. def fork_with_propagated_error_message
  27. pipe_read, pipe_write = IO.pipe
  28. pid = fork do
  29. pipe_read.close
  30. begin
  31. yield
  32. rescue => exc
  33. pipe_write.syswrite(exc.message)
  34. end
  35. pipe_write.close
  36. end
  37. pipe_write.close
  38. exc_message = pipe_read.read
  39. Process.wait(pid)
  40. unless $CHILD_STATUS.success?
  41. raise "forked process failed with #{$CHILD_STATUS}"
  42. end
  43. raise exc_message unless exc_message.empty?
  44. end
  45. shared_examples '#new' do
  46. it 'take a host name without channel args' do
  47. blk = proc do
  48. GRPC::Core::Channel.new('dummy_host', nil, :this_channel_is_insecure)
  49. end
  50. expect(&blk).not_to raise_error
  51. end
  52. it 'does not take a hash with bad keys as channel args' do
  53. blk = construct_with_args(Object.new => 1)
  54. expect(&blk).to raise_error TypeError
  55. blk = construct_with_args(1 => 1)
  56. expect(&blk).to raise_error TypeError
  57. end
  58. it 'does not take a hash with bad values as channel args' do
  59. blk = construct_with_args(symbol: Object.new)
  60. expect(&blk).to raise_error TypeError
  61. blk = construct_with_args('1' => {})
  62. expect(&blk).to raise_error TypeError
  63. end
  64. it 'can take a hash with a symbol key as channel args' do
  65. blk = construct_with_args(a_symbol: 1)
  66. expect(&blk).to_not raise_error
  67. end
  68. it 'can take a hash with a string key as channel args' do
  69. blk = construct_with_args('a_symbol' => 1)
  70. expect(&blk).to_not raise_error
  71. end
  72. it 'can take a hash with a string value as channel args' do
  73. blk = construct_with_args(a_symbol: '1')
  74. expect(&blk).to_not raise_error
  75. end
  76. it 'can take a hash with a symbol value as channel args' do
  77. blk = construct_with_args(a_symbol: :another_symbol)
  78. expect(&blk).to_not raise_error
  79. end
  80. it 'can take a hash with a numeric value as channel args' do
  81. blk = construct_with_args(a_symbol: 1)
  82. expect(&blk).to_not raise_error
  83. end
  84. it 'can take a hash with many args as channel args' do
  85. args = Hash[127.times.collect { |x| [x.to_s, x] }]
  86. blk = construct_with_args(args)
  87. expect(&blk).to_not raise_error
  88. end
  89. it 'raises if grpc was initialized in another process' do
  90. blk = construct_with_args({})
  91. expect(&blk).not_to raise_error
  92. expect do
  93. fork_with_propagated_error_message(&blk)
  94. end.to raise_error(RuntimeError, 'grpc cannot be used before and after forking')
  95. end
  96. end
  97. describe '#new for secure channels' do
  98. def construct_with_args(a)
  99. proc { GRPC::Core::Channel.new('dummy_host', a, create_test_cert) }
  100. end
  101. it_behaves_like '#new'
  102. end
  103. describe '#new for insecure channels' do
  104. it_behaves_like '#new'
  105. def construct_with_args(a)
  106. proc do
  107. GRPC::Core::Channel.new('dummy_host', a, :this_channel_is_insecure)
  108. end
  109. end
  110. end
  111. describe '#create_call' do
  112. it 'creates a call OK' do
  113. ch = GRPC::Core::Channel.new(fake_host, nil, :this_channel_is_insecure)
  114. deadline = Time.now + 5
  115. blk = proc do
  116. ch.create_call(nil, nil, 'dummy_method', nil, deadline)
  117. end
  118. expect(&blk).to_not raise_error
  119. end
  120. it 'raises an error if called on a closed channel' do
  121. ch = GRPC::Core::Channel.new(fake_host, nil, :this_channel_is_insecure)
  122. ch.close
  123. deadline = Time.now + 5
  124. blk = proc do
  125. ch.create_call(nil, nil, 'dummy_method', nil, deadline)
  126. end
  127. expect(&blk).to raise_error(RuntimeError)
  128. end
  129. it 'raises if grpc was initialized in another process' do
  130. ch = GRPC::Core::Channel.new(fake_host, nil, :this_channel_is_insecure)
  131. deadline = Time.now + 5
  132. blk = proc do
  133. fork_with_propagated_error_message do
  134. ch.create_call(nil, nil, 'dummy_method', nil, deadline)
  135. end
  136. end
  137. expect(&blk).to raise_error(RuntimeError, 'grpc cannot be used before and after forking')
  138. end
  139. end
  140. describe '#destroy' do
  141. it 'destroys a channel ok' do
  142. ch = GRPC::Core::Channel.new(fake_host, nil, :this_channel_is_insecure)
  143. blk = proc { ch.destroy }
  144. expect(&blk).to_not raise_error
  145. end
  146. it 'can be called more than once without error' do
  147. ch = GRPC::Core::Channel.new(fake_host, nil, :this_channel_is_insecure)
  148. blk = proc { ch.destroy }
  149. blk.call
  150. expect(&blk).to_not raise_error
  151. end
  152. end
  153. describe '#connectivity_state' do
  154. it 'returns an enum' do
  155. ch = GRPC::Core::Channel.new(fake_host, nil, :this_channel_is_insecure)
  156. valid_states = [
  157. GRPC::Core::ConnectivityStates::IDLE,
  158. GRPC::Core::ConnectivityStates::CONNECTING,
  159. GRPC::Core::ConnectivityStates::READY,
  160. GRPC::Core::ConnectivityStates::TRANSIENT_FAILURE,
  161. GRPC::Core::ConnectivityStates::FATAL_FAILURE
  162. ]
  163. expect(valid_states).to include(ch.connectivity_state)
  164. end
  165. it 'returns an enum when trying to connect' do
  166. ch = GRPC::Core::Channel.new(fake_host, nil, :this_channel_is_insecure)
  167. ch.connectivity_state(true)
  168. valid_states = [
  169. GRPC::Core::ConnectivityStates::IDLE,
  170. GRPC::Core::ConnectivityStates::CONNECTING,
  171. GRPC::Core::ConnectivityStates::READY,
  172. GRPC::Core::ConnectivityStates::TRANSIENT_FAILURE,
  173. GRPC::Core::ConnectivityStates::FATAL_FAILURE
  174. ]
  175. expect(valid_states).to include(ch.connectivity_state)
  176. end
  177. end
  178. describe '::SSL_TARGET' do
  179. it 'is a symbol' do
  180. expect(GRPC::Core::Channel::SSL_TARGET).to be_a(Symbol)
  181. end
  182. end
  183. describe '#close' do
  184. it 'closes a channel ok' do
  185. ch = GRPC::Core::Channel.new(fake_host, nil, :this_channel_is_insecure)
  186. blk = proc { ch.close }
  187. expect(&blk).to_not raise_error
  188. end
  189. it 'can be called more than once without error' do
  190. ch = GRPC::Core::Channel.new(fake_host, nil, :this_channel_is_insecure)
  191. blk = proc { ch.close }
  192. blk.call
  193. expect(&blk).to_not raise_error
  194. end
  195. end
  196. end