server_spec.rb 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. contents = files.map { |f| File.open(File.join(test_root, f)).read }
  34. [contents[0], [{ private_key: contents[1], cert_chain: contents[2] }], false]
  35. end
  36. Server = GRPC::Core::Server
  37. describe Server do
  38. def create_test_cert
  39. GRPC::Core::ServerCredentials.new(*load_test_certs)
  40. end
  41. before(:each) do
  42. @cq = GRPC::Core::CompletionQueue.new
  43. end
  44. describe '#start' do
  45. it 'runs without failing' do
  46. blk = proc { Server.new(@cq, nil).start }
  47. expect(&blk).to_not raise_error
  48. end
  49. it 'fails if the server is closed' do
  50. s = Server.new(@cq, nil)
  51. s.close(@cq)
  52. expect { s.start }.to raise_error(RuntimeError)
  53. end
  54. end
  55. describe '#destroy' do
  56. it 'destroys a server ok' do
  57. s = start_a_server
  58. blk = proc { s.destroy(@cq) }
  59. expect(&blk).to_not raise_error
  60. end
  61. it 'can be called more than once without error' do
  62. s = start_a_server
  63. begin
  64. blk = proc { s.destroy(@cq) }
  65. expect(&blk).to_not raise_error
  66. blk.call
  67. expect(&blk).to_not raise_error
  68. ensure
  69. s.close(@cq)
  70. end
  71. end
  72. end
  73. describe '#close' do
  74. it 'closes a server ok' do
  75. s = start_a_server
  76. begin
  77. blk = proc { s.close(@cq) }
  78. expect(&blk).to_not raise_error
  79. ensure
  80. s.close(@cq)
  81. end
  82. end
  83. it 'can be called more than once without error' do
  84. s = start_a_server
  85. blk = proc { s.close(@cq) }
  86. expect(&blk).to_not raise_error
  87. blk.call
  88. expect(&blk).to_not raise_error
  89. end
  90. end
  91. describe '#add_http_port' do
  92. describe 'for insecure servers' do
  93. it 'runs without failing' do
  94. blk = proc do
  95. s = Server.new(@cq, nil)
  96. s.add_http2_port('localhost:0', :this_port_is_insecure)
  97. s.close(@cq)
  98. end
  99. expect(&blk).to_not raise_error
  100. end
  101. it 'fails if the server is closed' do
  102. s = Server.new(@cq, nil)
  103. s.close(@cq)
  104. blk = proc do
  105. s.add_http2_port('localhost:0', :this_port_is_insecure)
  106. end
  107. expect(&blk).to raise_error(RuntimeError)
  108. end
  109. end
  110. describe 'for secure servers' do
  111. let(:cert) { create_test_cert }
  112. it 'runs without failing' do
  113. blk = proc do
  114. s = Server.new(@cq, nil)
  115. s.add_http2_port('localhost:0', cert)
  116. s.close(@cq)
  117. end
  118. expect(&blk).to_not raise_error
  119. end
  120. it 'fails if the server is closed' do
  121. s = Server.new(@cq, nil)
  122. s.close(@cq)
  123. blk = proc { s.add_http2_port('localhost:0', cert) }
  124. expect(&blk).to raise_error(RuntimeError)
  125. end
  126. end
  127. end
  128. shared_examples '#new' do
  129. it 'takes a completion queue with nil channel args' do
  130. expect { Server.new(@cq, nil) }.to_not raise_error
  131. end
  132. it 'does not take a hash with bad keys as channel args' do
  133. blk = construct_with_args(Object.new => 1)
  134. expect(&blk).to raise_error TypeError
  135. blk = construct_with_args(1 => 1)
  136. expect(&blk).to raise_error TypeError
  137. end
  138. it 'does not take a hash with bad values as channel args' do
  139. blk = construct_with_args(symbol: Object.new)
  140. expect(&blk).to raise_error TypeError
  141. blk = construct_with_args('1' => {})
  142. expect(&blk).to raise_error TypeError
  143. end
  144. it 'can take a hash with a symbol key as channel args' do
  145. blk = construct_with_args(a_symbol: 1)
  146. expect(&blk).to_not raise_error
  147. end
  148. it 'can take a hash with a string key as channel args' do
  149. blk = construct_with_args('a_symbol' => 1)
  150. expect(&blk).to_not raise_error
  151. end
  152. it 'can take a hash with a string value as channel args' do
  153. blk = construct_with_args(a_symbol: '1')
  154. expect(&blk).to_not raise_error
  155. end
  156. it 'can take a hash with a symbol value as channel args' do
  157. blk = construct_with_args(a_symbol: :another_symbol)
  158. expect(&blk).to_not raise_error
  159. end
  160. it 'can take a hash with a numeric value as channel args' do
  161. blk = construct_with_args(a_symbol: 1)
  162. expect(&blk).to_not raise_error
  163. end
  164. it 'can take a hash with many args as channel args' do
  165. args = Hash[127.times.collect { |x| [x.to_s, x] }]
  166. blk = construct_with_args(args)
  167. expect(&blk).to_not raise_error
  168. end
  169. end
  170. describe '#new with an insecure channel' do
  171. def construct_with_args(a)
  172. proc { Server.new(@cq, a) }
  173. end
  174. it_behaves_like '#new'
  175. end
  176. def start_a_server
  177. s = Server.new(@cq, nil)
  178. s.add_http2_port('0.0.0.0:0', :this_port_is_insecure)
  179. s.start
  180. s
  181. end
  182. end