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 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 33 | def load_test_certs |
| 34 | test_root = File.join(File.dirname(__FILE__), 'testdata') |
| 35 | files = ['ca.pem', 'server1.key', 'server1.pem'] |
| 36 | files.map { |f| File.open(File.join(test_root, f)).read } |
| 37 | end |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 38 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 39 | describe GRPC::Core::Channel do |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 40 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 41 | |
| 42 | def create_test_cert |
| 43 | GRPC::Core::Credentials.new(load_test_certs[0]) |
| 44 | end |
| 45 | |
| 46 | before(:each) do |
| 47 | @cq = GRPC::Core::CompletionQueue.new |
| 48 | end |
| 49 | |
| 50 | shared_examples '#new' do |
| 51 | |
| 52 | it 'take a host name without channel args' do |
| 53 | expect { GRPC::Core::Channel.new('dummy_host', nil) }.not_to raise_error |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 54 | end |
| 55 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 56 | it 'does not take a hash with bad keys as channel args' do |
| 57 | blk = construct_with_args(Object.new => 1) |
| 58 | expect(&blk).to raise_error TypeError |
| 59 | blk = construct_with_args(1 => 1) |
| 60 | expect(&blk).to raise_error TypeError |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 61 | end |
| 62 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 63 | it 'does not take a hash with bad values as channel args' do |
| 64 | blk = construct_with_args(:symbol => Object.new) |
| 65 | expect(&blk).to raise_error TypeError |
| 66 | blk = construct_with_args('1' => Hash.new) |
| 67 | expect(&blk).to raise_error TypeError |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 68 | end |
| 69 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 70 | it 'can take a hash with a symbol key as channel args' do |
| 71 | blk = construct_with_args(:a_symbol => 1) |
| 72 | expect(&blk).to_not raise_error |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 73 | end |
| 74 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 75 | it 'can take a hash with a string key as channel args' do |
| 76 | blk = construct_with_args('a_symbol' => 1) |
| 77 | expect(&blk).to_not raise_error |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 78 | end |
| 79 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 80 | it 'can take a hash with a string value as channel args' do |
| 81 | blk = construct_with_args(:a_symbol => '1') |
| 82 | expect(&blk).to_not raise_error |
| 83 | end |
| 84 | |
| 85 | it 'can take a hash with a symbol value as channel args' do |
| 86 | blk = construct_with_args(:a_symbol => :another_symbol) |
| 87 | expect(&blk).to_not raise_error |
| 88 | end |
| 89 | |
| 90 | it 'can take a hash with a numeric value as channel args' do |
| 91 | blk = construct_with_args(:a_symbol => 1) |
| 92 | expect(&blk).to_not raise_error |
| 93 | end |
| 94 | |
| 95 | it 'can take a hash with many args as channel args' do |
| 96 | args = Hash[127.times.collect { |x| [x.to_s, x] } ] |
| 97 | blk = construct_with_args(args) |
| 98 | expect(&blk).to_not raise_error |
| 99 | end |
| 100 | |
| 101 | end |
| 102 | |
| 103 | describe '#new for secure channels' do |
| 104 | |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 105 | def construct_with_args(a) |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 106 | Proc.new { GRPC::Core::Channel.new('dummy_host', a, create_test_cert) } |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 107 | end |
| 108 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 109 | it_behaves_like '#new' |
| 110 | end |
| 111 | |
| 112 | describe '#new for insecure channels' do |
| 113 | it_behaves_like '#new' |
| 114 | |
| 115 | def construct_with_args(a) |
| 116 | Proc.new { GRPC::Core::Channel.new('dummy_host', a) } |
| 117 | end |
| 118 | end |
| 119 | |
| 120 | describe '#create_call' do |
| 121 | it 'creates a call OK' do |
| 122 | port = find_unused_tcp_port |
| 123 | host = "localhost:#{port}" |
| 124 | ch = GRPC::Core::Channel.new(host, nil) |
| 125 | |
| 126 | deadline = Time.now + 5 |
| 127 | |
| 128 | blk = Proc.new do |
| 129 | ch.create_call('dummy_method', 'dummy_host', deadline) |
| 130 | end |
| 131 | expect(&blk).to_not raise_error |
| 132 | end |
| 133 | |
| 134 | it 'raises an error if called on a closed channel' do |
| 135 | port = find_unused_tcp_port |
| 136 | host = "localhost:#{port}" |
| 137 | ch = GRPC::Core::Channel.new(host, nil) |
| 138 | ch.close |
| 139 | |
| 140 | deadline = Time.now + 5 |
| 141 | blk = Proc.new do |
| 142 | ch.create_call('dummy_method', 'dummy_host', deadline) |
| 143 | end |
| 144 | expect(&blk).to raise_error(RuntimeError) |
| 145 | end |
| 146 | |
| 147 | end |
| 148 | |
| 149 | describe '#destroy' do |
| 150 | it 'destroys a channel ok' do |
| 151 | port = find_unused_tcp_port |
| 152 | host = "localhost:#{port}" |
| 153 | ch = GRPC::Core::Channel.new(host, nil) |
| 154 | blk = Proc.new { ch.destroy } |
| 155 | expect(&blk).to_not raise_error |
| 156 | end |
| 157 | |
| 158 | it 'can be called more than once without error' do |
| 159 | port = find_unused_tcp_port |
| 160 | host = "localhost:#{port}" |
| 161 | ch = GRPC::Core::Channel.new(host, nil) |
| 162 | blk = Proc.new { ch.destroy } |
| 163 | blk.call |
| 164 | expect(&blk).to_not raise_error |
| 165 | end |
| 166 | end |
| 167 | |
| 168 | describe '::SSL_TARGET' do |
| 169 | |
| 170 | it 'is a symbol' do |
| 171 | expect(GRPC::Core::Channel::SSL_TARGET).to be_a(Symbol) |
| 172 | end |
| 173 | |
| 174 | end |
| 175 | |
| 176 | describe '#close' do |
| 177 | it 'closes a channel ok' do |
| 178 | port = find_unused_tcp_port |
| 179 | host = "localhost:#{port}" |
| 180 | ch = GRPC::Core::Channel.new(host, nil) |
| 181 | blk = Proc.new { ch.close } |
| 182 | expect(&blk).to_not raise_error |
| 183 | end |
| 184 | |
| 185 | it 'can be called more than once without error' do |
| 186 | port = find_unused_tcp_port |
| 187 | host = "localhost:#{port}" |
| 188 | ch = GRPC::Core::Channel.new(host, nil) |
| 189 | blk = Proc.new { ch.close } |
| 190 | blk.call |
| 191 | expect(&blk).to_not raise_error |
| 192 | end |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 193 | end |
| 194 | |
| 195 | end |