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