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