channel_spec.rb 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 'grpc'
  15. def load_test_certs
  16. test_root = File.join(File.dirname(__FILE__), 'testdata')
  17. files = ['ca.pem', 'server1.key', 'server1.pem']
  18. files.map { |f| File.open(File.join(test_root, f)).read }
  19. end
  20. describe GRPC::Core::Channel do
  21. let(:fake_host) { 'localhost:0' }
  22. def create_test_cert
  23. GRPC::Core::ChannelCredentials.new(load_test_certs[0])
  24. end
  25. shared_examples '#new' do
  26. it 'take a host name without channel args' do
  27. blk = proc do
  28. GRPC::Core::Channel.new('dummy_host', nil, :this_channel_is_insecure)
  29. end
  30. expect(&blk).not_to raise_error
  31. end
  32. it 'does not take a hash with bad keys as channel args' do
  33. blk = construct_with_args(Object.new => 1)
  34. expect(&blk).to raise_error TypeError
  35. blk = construct_with_args(1 => 1)
  36. expect(&blk).to raise_error TypeError
  37. end
  38. it 'does not take a hash with bad values as channel args' do
  39. blk = construct_with_args(symbol: Object.new)
  40. expect(&blk).to raise_error TypeError
  41. blk = construct_with_args('1' => {})
  42. expect(&blk).to raise_error TypeError
  43. end
  44. it 'can take a hash with a symbol key as channel args' do
  45. blk = construct_with_args(a_symbol: 1)
  46. expect(&blk).to_not raise_error
  47. end
  48. it 'can take a hash with a string key as channel args' do
  49. blk = construct_with_args('a_symbol' => 1)
  50. expect(&blk).to_not raise_error
  51. end
  52. it 'can take a hash with a string value as channel args' do
  53. blk = construct_with_args(a_symbol: '1')
  54. expect(&blk).to_not raise_error
  55. end
  56. it 'can take a hash with a symbol value as channel args' do
  57. blk = construct_with_args(a_symbol: :another_symbol)
  58. expect(&blk).to_not raise_error
  59. end
  60. it 'can take a hash with a numeric value as channel args' do
  61. blk = construct_with_args(a_symbol: 1)
  62. expect(&blk).to_not raise_error
  63. end
  64. it 'can take a hash with many args as channel args' do
  65. args = Hash[127.times.collect { |x| [x.to_s, x] }]
  66. blk = construct_with_args(args)
  67. expect(&blk).to_not raise_error
  68. end
  69. end
  70. describe '#new for secure channels' do
  71. def construct_with_args(a)
  72. proc { GRPC::Core::Channel.new('dummy_host', a, create_test_cert) }
  73. end
  74. it_behaves_like '#new'
  75. end
  76. describe '#new for insecure channels' do
  77. it_behaves_like '#new'
  78. def construct_with_args(a)
  79. proc do
  80. GRPC::Core::Channel.new('dummy_host', a, :this_channel_is_insecure)
  81. end
  82. end
  83. end
  84. describe '#create_call' do
  85. it 'creates a call OK' do
  86. ch = GRPC::Core::Channel.new(fake_host, nil, :this_channel_is_insecure)
  87. deadline = Time.now + 5
  88. blk = proc do
  89. ch.create_call(nil, nil, 'dummy_method', nil, deadline)
  90. end
  91. expect(&blk).to_not raise_error
  92. end
  93. it 'raises an error if called on a closed channel' do
  94. ch = GRPC::Core::Channel.new(fake_host, nil, :this_channel_is_insecure)
  95. ch.close
  96. deadline = Time.now + 5
  97. blk = proc do
  98. ch.create_call(nil, nil, 'dummy_method', nil, deadline)
  99. end
  100. expect(&blk).to raise_error(RuntimeError)
  101. end
  102. end
  103. describe '#destroy' do
  104. it 'destroys a channel ok' do
  105. ch = GRPC::Core::Channel.new(fake_host, nil, :this_channel_is_insecure)
  106. blk = proc { ch.destroy }
  107. expect(&blk).to_not raise_error
  108. end
  109. it 'can be called more than once without error' do
  110. ch = GRPC::Core::Channel.new(fake_host, nil, :this_channel_is_insecure)
  111. blk = proc { ch.destroy }
  112. blk.call
  113. expect(&blk).to_not raise_error
  114. end
  115. end
  116. describe '#connectivity_state' do
  117. it 'returns an enum' do
  118. ch = GRPC::Core::Channel.new(fake_host, nil, :this_channel_is_insecure)
  119. valid_states = [
  120. GRPC::Core::ConnectivityStates::IDLE,
  121. GRPC::Core::ConnectivityStates::CONNECTING,
  122. GRPC::Core::ConnectivityStates::READY,
  123. GRPC::Core::ConnectivityStates::TRANSIENT_FAILURE,
  124. GRPC::Core::ConnectivityStates::FATAL_FAILURE
  125. ]
  126. expect(valid_states).to include(ch.connectivity_state)
  127. end
  128. it 'returns an enum when trying to connect' do
  129. ch = GRPC::Core::Channel.new(fake_host, nil, :this_channel_is_insecure)
  130. ch.connectivity_state(true)
  131. valid_states = [
  132. GRPC::Core::ConnectivityStates::IDLE,
  133. GRPC::Core::ConnectivityStates::CONNECTING,
  134. GRPC::Core::ConnectivityStates::READY,
  135. GRPC::Core::ConnectivityStates::TRANSIENT_FAILURE,
  136. GRPC::Core::ConnectivityStates::FATAL_FAILURE
  137. ]
  138. expect(valid_states).to include(ch.connectivity_state)
  139. end
  140. end
  141. describe '::SSL_TARGET' do
  142. it 'is a symbol' do
  143. expect(GRPC::Core::Channel::SSL_TARGET).to be_a(Symbol)
  144. end
  145. end
  146. describe '#close' do
  147. it 'closes a channel ok' do
  148. ch = GRPC::Core::Channel.new(fake_host, nil, :this_channel_is_insecure)
  149. blk = proc { ch.close }
  150. expect(&blk).to_not raise_error
  151. end
  152. it 'can be called more than once without error' do
  153. ch = GRPC::Core::Channel.new(fake_host, nil, :this_channel_is_insecure)
  154. blk = proc { ch.close }
  155. blk.call
  156. expect(&blk).to_not raise_error
  157. end
  158. end
  159. end