blob: 5b81f195371d9b8a3b2301cc335b8bf160d7f63b [file] [log] [blame]
Craig Tiller06059952015-02-18 08:34:56 -08001# Copyright 2015, Google Inc.
nnoble097ef9b2014-12-01 17:06:10 -08002# 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
30require 'grpc'
nnoble097ef9b2014-12-01 17:06:10 -080031
nnoble0c475f02014-12-05 15:37:39 -080032def load_test_certs
33 test_root = File.join(File.dirname(__FILE__), 'testdata')
34 files = ['ca.pem', 'server1.key', 'server1.pem']
35 files.map { |f| File.open(File.join(test_root, f)).read }
36end
nnoble097ef9b2014-12-01 17:06:10 -080037
nnoble0c475f02014-12-05 15:37:39 -080038Server = GRPC::Core::Server
nnoble097ef9b2014-12-01 17:06:10 -080039
nnoble0c475f02014-12-05 15:37:39 -080040describe Server do
nnoble0c475f02014-12-05 15:37:39 -080041 def create_test_cert
42 GRPC::Core::ServerCredentials.new(*load_test_certs)
43 end
44
45 before(:each) do
46 @cq = GRPC::Core::CompletionQueue.new
47 end
48
49 describe '#start' do
nnoble0c475f02014-12-05 15:37:39 -080050 it 'runs without failing' do
Tim Emiolae2860c52015-01-16 02:58:41 -080051 blk = proc { Server.new(@cq, nil).start }
nnoble0c475f02014-12-05 15:37:39 -080052 expect(&blk).to_not raise_error
nnoble097ef9b2014-12-01 17:06:10 -080053 end
54
nnoble0c475f02014-12-05 15:37:39 -080055 it 'fails if the server is closed' do
56 s = Server.new(@cq, nil)
57 s.close
58 expect { s.start }.to raise_error(RuntimeError)
nnoble097ef9b2014-12-01 17:06:10 -080059 end
nnoble0c475f02014-12-05 15:37:39 -080060 end
61
62 describe '#destroy' do
63 it 'destroys a server ok' do
64 s = start_a_server
Tim Emiolae2860c52015-01-16 02:58:41 -080065 blk = proc { s.destroy }
nnoble0c475f02014-12-05 15:37:39 -080066 expect(&blk).to_not raise_error
67 end
68
69 it 'can be called more than once without error' do
70 s = start_a_server
71 begin
Tim Emiolae2860c52015-01-16 02:58:41 -080072 blk = proc { s.destroy }
nnoble097ef9b2014-12-01 17:06:10 -080073 expect(&blk).to_not raise_error
nnoble097ef9b2014-12-01 17:06:10 -080074 blk.call
75 expect(&blk).to_not raise_error
nnoble0c475f02014-12-05 15:37:39 -080076 ensure
77 s.close
78 end
79 end
80 end
81
82 describe '#close' do
83 it 'closes a server ok' do
84 s = start_a_server
85 begin
Tim Emiolae2860c52015-01-16 02:58:41 -080086 blk = proc { s.close }
nnoble0c475f02014-12-05 15:37:39 -080087 expect(&blk).to_not raise_error
88 ensure
89 s.close
nnoble097ef9b2014-12-01 17:06:10 -080090 end
91 end
92
nnoble0c475f02014-12-05 15:37:39 -080093 it 'can be called more than once without error' do
94 s = start_a_server
Tim Emiolae2860c52015-01-16 02:58:41 -080095 blk = proc { s.close }
nnoble0c475f02014-12-05 15:37:39 -080096 expect(&blk).to_not raise_error
97 blk.call
98 expect(&blk).to_not raise_error
99 end
100 end
101
102 describe '#add_http_port' do
nnoble0c475f02014-12-05 15:37:39 -0800103 describe 'for insecure servers' do
nnoble097ef9b2014-12-01 17:06:10 -0800104 it 'runs without failing' do
Tim Emiolae2860c52015-01-16 02:58:41 -0800105 blk = proc do
nnoble097ef9b2014-12-01 17:06:10 -0800106 s = Server.new(@cq, nil)
107 s.add_http2_port('localhost:0')
108 s.close
109 end
110 expect(&blk).to_not raise_error
111 end
112
113 it 'fails if the server is closed' do
114 s = Server.new(@cq, nil)
115 s.close
116 expect { s.add_http2_port('localhost:0') }.to raise_error(RuntimeError)
117 end
nnoble097ef9b2014-12-01 17:06:10 -0800118 end
119
nnoble0c475f02014-12-05 15:37:39 -0800120 describe 'for secure servers' do
nnoble0c475f02014-12-05 15:37:39 -0800121 it 'runs without failing' do
Tim Emiolae2860c52015-01-16 02:58:41 -0800122 blk = proc do
nnoble0c475f02014-12-05 15:37:39 -0800123 s = Server.new(@cq, nil)
124 s.add_http2_port('localhost:0', true)
125 s.close
126 end
nnoble097ef9b2014-12-01 17:06:10 -0800127 expect(&blk).to_not raise_error
128 end
129
nnoble0c475f02014-12-05 15:37:39 -0800130 it 'fails if the server is closed' do
131 s = Server.new(@cq, nil)
132 s.close
Tim Emiolae2860c52015-01-16 02:58:41 -0800133 blk = proc { s.add_http2_port('localhost:0', true) }
nnoble0c475f02014-12-05 15:37:39 -0800134 expect(&blk).to raise_error(RuntimeError)
nnoble097ef9b2014-12-01 17:06:10 -0800135 end
nnoble097ef9b2014-12-01 17:06:10 -0800136 end
nnoble0c475f02014-12-05 15:37:39 -0800137 end
138
139 shared_examples '#new' do
nnoble0c475f02014-12-05 15:37:39 -0800140 it 'takes a completion queue with nil channel args' do
141 expect { Server.new(@cq, nil, create_test_cert) }.to_not raise_error
142 end
143
144 it 'does not take a hash with bad keys as channel args' do
145 blk = construct_with_args(Object.new => 1)
146 expect(&blk).to raise_error TypeError
147 blk = construct_with_args(1 => 1)
148 expect(&blk).to raise_error TypeError
149 end
150
151 it 'does not take a hash with bad values as channel args' do
Tim Emiolae2860c52015-01-16 02:58:41 -0800152 blk = construct_with_args(symbol: Object.new)
nnoble0c475f02014-12-05 15:37:39 -0800153 expect(&blk).to raise_error TypeError
154 blk = construct_with_args('1' => Hash.new)
155 expect(&blk).to raise_error TypeError
156 end
157
158 it 'can take a hash with a symbol key as channel args' do
Tim Emiolae2860c52015-01-16 02:58:41 -0800159 blk = construct_with_args(a_symbol: 1)
nnoble0c475f02014-12-05 15:37:39 -0800160 expect(&blk).to_not raise_error
161 end
162
163 it 'can take a hash with a string key as channel args' do
164 blk = construct_with_args('a_symbol' => 1)
165 expect(&blk).to_not raise_error
166 end
167
168 it 'can take a hash with a string value as channel args' do
Tim Emiolae2860c52015-01-16 02:58:41 -0800169 blk = construct_with_args(a_symbol: '1')
nnoble0c475f02014-12-05 15:37:39 -0800170 expect(&blk).to_not raise_error
171 end
172
173 it 'can take a hash with a symbol value as channel args' do
Tim Emiolae2860c52015-01-16 02:58:41 -0800174 blk = construct_with_args(a_symbol: :another_symbol)
nnoble0c475f02014-12-05 15:37:39 -0800175 expect(&blk).to_not raise_error
176 end
177
178 it 'can take a hash with a numeric value as channel args' do
Tim Emiolae2860c52015-01-16 02:58:41 -0800179 blk = construct_with_args(a_symbol: 1)
nnoble0c475f02014-12-05 15:37:39 -0800180 expect(&blk).to_not raise_error
181 end
182
183 it 'can take a hash with many args as channel args' do
Tim Emiolae2860c52015-01-16 02:58:41 -0800184 args = Hash[127.times.collect { |x| [x.to_s, x] }]
nnoble0c475f02014-12-05 15:37:39 -0800185 blk = construct_with_args(args)
186 expect(&blk).to_not raise_error
187 end
nnoble0c475f02014-12-05 15:37:39 -0800188 end
189
190 describe '#new with an insecure channel' do
nnoble097ef9b2014-12-01 17:06:10 -0800191 def construct_with_args(a)
Tim Emiolae2860c52015-01-16 02:58:41 -0800192 proc { Server.new(@cq, a) }
nnoble097ef9b2014-12-01 17:06:10 -0800193 end
194
nnoble0c475f02014-12-05 15:37:39 -0800195 it_behaves_like '#new'
nnoble0c475f02014-12-05 15:37:39 -0800196 end
197
198 describe '#new with a secure channel' do
nnoble0c475f02014-12-05 15:37:39 -0800199 def construct_with_args(a)
Tim Emiolae2860c52015-01-16 02:58:41 -0800200 proc { Server.new(@cq, a, create_test_cert) }
nnoble097ef9b2014-12-01 17:06:10 -0800201 end
202
nnoble0c475f02014-12-05 15:37:39 -0800203 it_behaves_like '#new'
nnoble0c475f02014-12-05 15:37:39 -0800204 end
205
206 def start_a_server
nnoble0c475f02014-12-05 15:37:39 -0800207 s = Server.new(@cq, nil)
Tim Emiola61ddba32015-01-26 18:33:20 -0800208 s.add_http2_port('0.0.0.0:0')
nnoble0c475f02014-12-05 15:37:39 -0800209 s.start
210 s
nnoble097ef9b2014-12-01 17:06:10 -0800211 end
Craig Tiller190d3602015-02-18 09:23:38 -0800212end