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