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