channel_spec.rb 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. let(:cq) { GRPC::Core::CompletionQueue.new }
  38. def create_test_cert
  39. GRPC::Core::Credentials.new(load_test_certs[0])
  40. end
  41. shared_examples '#new' do
  42. it 'take a host name without channel args' do
  43. expect { GRPC::Core::Channel.new('dummy_host', nil) }.not_to raise_error
  44. end
  45. it 'does not take a hash with bad keys as channel args' do
  46. blk = construct_with_args(Object.new => 1)
  47. expect(&blk).to raise_error TypeError
  48. blk = construct_with_args(1 => 1)
  49. expect(&blk).to raise_error TypeError
  50. end
  51. it 'does not take a hash with bad values as channel args' do
  52. blk = construct_with_args(symbol: Object.new)
  53. expect(&blk).to raise_error TypeError
  54. blk = construct_with_args('1' => {})
  55. expect(&blk).to raise_error TypeError
  56. end
  57. it 'can take a hash with a symbol key as channel args' do
  58. blk = construct_with_args(a_symbol: 1)
  59. expect(&blk).to_not raise_error
  60. end
  61. it 'can take a hash with a string key as channel args' do
  62. blk = construct_with_args('a_symbol' => 1)
  63. expect(&blk).to_not raise_error
  64. end
  65. it 'can take a hash with a string value as channel args' do
  66. blk = construct_with_args(a_symbol: '1')
  67. expect(&blk).to_not raise_error
  68. end
  69. it 'can take a hash with a symbol value as channel args' do
  70. blk = construct_with_args(a_symbol: :another_symbol)
  71. expect(&blk).to_not raise_error
  72. end
  73. it 'can take a hash with a numeric value as channel args' do
  74. blk = construct_with_args(a_symbol: 1)
  75. expect(&blk).to_not raise_error
  76. end
  77. it 'can take a hash with many args as channel args' do
  78. args = Hash[127.times.collect { |x| [x.to_s, x] }]
  79. blk = construct_with_args(args)
  80. expect(&blk).to_not raise_error
  81. end
  82. end
  83. describe '#new for secure channels' do
  84. def construct_with_args(a)
  85. proc { GRPC::Core::Channel.new('dummy_host', a, create_test_cert) }
  86. end
  87. it_behaves_like '#new'
  88. end
  89. describe '#new for insecure channels' do
  90. it_behaves_like '#new'
  91. def construct_with_args(a)
  92. proc { GRPC::Core::Channel.new('dummy_host', a) }
  93. end
  94. end
  95. describe '#create_call' do
  96. it 'creates a call OK' do
  97. ch = GRPC::Core::Channel.new(fake_host, nil)
  98. deadline = Time.now + 5
  99. blk = proc do
  100. ch.create_call(cq, nil, nil, 'dummy_method', nil, deadline)
  101. end
  102. expect(&blk).to_not raise_error
  103. end
  104. it 'raises an error if called on a closed channel' do
  105. ch = GRPC::Core::Channel.new(fake_host, nil)
  106. ch.close
  107. deadline = Time.now + 5
  108. blk = proc do
  109. ch.create_call(cq, nil, nil, 'dummy_method', nil, deadline)
  110. end
  111. expect(&blk).to raise_error(RuntimeError)
  112. end
  113. end
  114. describe '#destroy' do
  115. it 'destroys a channel ok' do
  116. ch = GRPC::Core::Channel.new(fake_host, nil)
  117. blk = proc { ch.destroy }
  118. expect(&blk).to_not raise_error
  119. end
  120. it 'can be called more than once without error' do
  121. ch = GRPC::Core::Channel.new(fake_host, nil)
  122. blk = proc { ch.destroy }
  123. blk.call
  124. expect(&blk).to_not raise_error
  125. end
  126. end
  127. describe '::SSL_TARGET' do
  128. it 'is a symbol' do
  129. expect(GRPC::Core::Channel::SSL_TARGET).to be_a(Symbol)
  130. end
  131. end
  132. describe '#close' do
  133. it 'closes a channel ok' do
  134. ch = GRPC::Core::Channel.new(fake_host, nil)
  135. blk = proc { ch.close }
  136. expect(&blk).to_not raise_error
  137. end
  138. it 'can be called more than once without error' do
  139. ch = GRPC::Core::Channel.new(fake_host, nil)
  140. blk = proc { ch.close }
  141. blk.call
  142. expect(&blk).to_not raise_error
  143. end
  144. end
  145. end