server_spec.rb 5.7 KB

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