channel_spec.rb 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. # Copyright 2015, Google Inc.
  2. # All rights reserved.
  3. #
  4. # Redistribution and use in source and binary forms, with or without
  5. # modification, are permitted provided that the following conditions are
  6. # met:
  7. #
  8. # * Redistributions of source code must retain the above copyright
  9. # notice, this list of conditions and the following disclaimer.
  10. # * Redistributions in binary form must reproduce the above
  11. # copyright notice, this list of conditions and the following disclaimer
  12. # in the documentation and/or other materials provided with the
  13. # distribution.
  14. # * Neither the name of Google Inc. nor the names of its
  15. # contributors may be used to endorse or promote products derived from
  16. # this software without specific prior written permission.
  17. #
  18. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. require 'grpc'
  30. def load_test_certs
  31. test_root = File.join(File.dirname(__FILE__), 'testdata')
  32. files = ['ca.pem', 'server1.key', 'server1.pem']
  33. files.map { |f| File.open(File.join(test_root, f)).read }
  34. end
  35. describe GRPC::Core::Channel do
  36. let(:fake_host) { 'localhost:0' }
  37. def create_test_cert
  38. GRPC::Core::ChannelCredentials.new(load_test_certs[0])
  39. end
  40. shared_examples '#new' do
  41. it 'take a host name without channel args' do
  42. blk = proc do
  43. GRPC::Core::Channel.new('dummy_host', nil, :this_channel_is_insecure)
  44. end
  45. expect(&blk).not_to raise_error
  46. end
  47. it 'does not take a hash with bad keys as channel args' do
  48. blk = construct_with_args(Object.new => 1)
  49. expect(&blk).to raise_error TypeError
  50. blk = construct_with_args(1 => 1)
  51. expect(&blk).to raise_error TypeError
  52. end
  53. it 'does not take a hash with bad values as channel args' do
  54. blk = construct_with_args(symbol: Object.new)
  55. expect(&blk).to raise_error TypeError
  56. blk = construct_with_args('1' => {})
  57. expect(&blk).to raise_error TypeError
  58. end
  59. it 'can take a hash with a symbol key as channel args' do
  60. blk = construct_with_args(a_symbol: 1)
  61. expect(&blk).to_not raise_error
  62. end
  63. it 'can take a hash with a string key as channel args' do
  64. blk = construct_with_args('a_symbol' => 1)
  65. expect(&blk).to_not raise_error
  66. end
  67. it 'can take a hash with a string value as channel args' do
  68. blk = construct_with_args(a_symbol: '1')
  69. expect(&blk).to_not raise_error
  70. end
  71. it 'can take a hash with a symbol value as channel args' do
  72. blk = construct_with_args(a_symbol: :another_symbol)
  73. expect(&blk).to_not raise_error
  74. end
  75. it 'can take a hash with a numeric value as channel args' do
  76. blk = construct_with_args(a_symbol: 1)
  77. expect(&blk).to_not raise_error
  78. end
  79. it 'can take a hash with many args as channel args' do
  80. args = Hash[127.times.collect { |x| [x.to_s, x] }]
  81. blk = construct_with_args(args)
  82. expect(&blk).to_not raise_error
  83. end
  84. end
  85. describe '#new for secure channels' do
  86. def construct_with_args(a)
  87. proc { GRPC::Core::Channel.new('dummy_host', a, create_test_cert) }
  88. end
  89. it_behaves_like '#new'
  90. end
  91. describe '#new for insecure channels' do
  92. it_behaves_like '#new'
  93. def construct_with_args(a)
  94. proc do
  95. GRPC::Core::Channel.new('dummy_host', a, :this_channel_is_insecure)
  96. end
  97. end
  98. end
  99. describe '#create_call' do
  100. it 'creates a call OK' do
  101. ch = GRPC::Core::Channel.new(fake_host, nil, :this_channel_is_insecure)
  102. deadline = Time.now + 5
  103. blk = proc do
  104. ch.create_call(nil, nil, 'dummy_method', nil, deadline)
  105. end
  106. expect(&blk).to_not raise_error
  107. end
  108. it 'raises an error if called on a closed channel' do
  109. ch = GRPC::Core::Channel.new(fake_host, nil, :this_channel_is_insecure)
  110. ch.close
  111. deadline = Time.now + 5
  112. blk = proc do
  113. ch.create_call(nil, nil, 'dummy_method', nil, deadline)
  114. end
  115. expect(&blk).to raise_error(RuntimeError)
  116. end
  117. end
  118. describe '#destroy' do
  119. it 'destroys a channel ok' do
  120. ch = GRPC::Core::Channel.new(fake_host, nil, :this_channel_is_insecure)
  121. blk = proc { ch.destroy }
  122. expect(&blk).to_not raise_error
  123. end
  124. it 'can be called more than once without error' do
  125. ch = GRPC::Core::Channel.new(fake_host, nil, :this_channel_is_insecure)
  126. blk = proc { ch.destroy }
  127. blk.call
  128. expect(&blk).to_not raise_error
  129. end
  130. end
  131. describe '::SSL_TARGET' do
  132. it 'is a symbol' do
  133. expect(GRPC::Core::Channel::SSL_TARGET).to be_a(Symbol)
  134. end
  135. end
  136. describe '#close' do
  137. it 'closes a channel ok' do
  138. ch = GRPC::Core::Channel.new(fake_host, nil, :this_channel_is_insecure)
  139. blk = proc { ch.close }
  140. expect(&blk).to_not raise_error
  141. end
  142. it 'can be called more than once without error' do
  143. ch = GRPC::Core::Channel.new(fake_host, nil, :this_channel_is_insecure)
  144. blk = proc { ch.close }
  145. blk.call
  146. expect(&blk).to_not raise_error
  147. end
  148. end
  149. end