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 | 564719d | 2015-03-27 13:07:34 -0700 | [diff] [blame] | 39 | let(:fake_host) { 'localhost:0' } |
| 40 | let(:cq) { GRPC::Core::CompletionQueue.new } |
Tim Emiola | 3606653 | 2015-01-29 16:27:07 -0800 | [diff] [blame] | 41 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 42 | def create_test_cert |
Tim Emiola | 43a7e4e | 2015-10-28 00:17:14 -0700 | [diff] [blame] | 43 | GRPC::Core::ChannelCredentials.new(load_test_certs[0]) |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 44 | end |
| 45 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 46 | shared_examples '#new' do |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 47 | it 'take a host name without channel args' do |
murgatroid99 | afe3974 | 2015-12-16 13:04:37 -0800 | [diff] [blame] | 48 | blk = proc do |
| 49 | GRPC::Core::Channel.new('dummy_host', nil, :this_channel_is_insecure) |
| 50 | end |
| 51 | expect(&blk).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 |
Tim Emiola | 1e09812 | 2015-04-14 09:42:09 -0700 | [diff] [blame] | 64 | blk = construct_with_args('1' => {}) |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 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) |
murgatroid99 | afe3974 | 2015-12-16 13:04:37 -0800 | [diff] [blame] | 112 | proc do |
| 113 | GRPC::Core::Channel.new('dummy_host', a, :this_channel_is_insecure) |
| 114 | end |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 115 | end |
| 116 | end |
| 117 | |
| 118 | describe '#create_call' do |
| 119 | it 'creates a call OK' do |
murgatroid99 | afe3974 | 2015-12-16 13:04:37 -0800 | [diff] [blame] | 120 | ch = GRPC::Core::Channel.new(fake_host, nil, :this_channel_is_insecure) |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 121 | |
| 122 | deadline = Time.now + 5 |
| 123 | |
Tim Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 124 | blk = proc do |
Tim Emiola | fde3dbf | 2015-08-11 16:35:41 -0700 | [diff] [blame] | 125 | ch.create_call(cq, nil, nil, 'dummy_method', nil, deadline) |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 126 | end |
| 127 | expect(&blk).to_not raise_error |
| 128 | end |
| 129 | |
| 130 | it 'raises an error if called on a closed channel' do |
murgatroid99 | afe3974 | 2015-12-16 13:04:37 -0800 | [diff] [blame] | 131 | ch = GRPC::Core::Channel.new(fake_host, nil, :this_channel_is_insecure) |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 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 |
Tim Emiola | fde3dbf | 2015-08-11 16:35:41 -0700 | [diff] [blame] | 136 | ch.create_call(cq, nil, nil, 'dummy_method', nil, deadline) |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 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 |
murgatroid99 | afe3974 | 2015-12-16 13:04:37 -0800 | [diff] [blame] | 144 | ch = GRPC::Core::Channel.new(fake_host, nil, :this_channel_is_insecure) |
Tim Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 145 | blk = proc { ch.destroy } |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 146 | expect(&blk).to_not raise_error |
| 147 | end |
| 148 | |
| 149 | it 'can be called more than once without error' do |
murgatroid99 | afe3974 | 2015-12-16 13:04:37 -0800 | [diff] [blame] | 150 | ch = GRPC::Core::Channel.new(fake_host, nil, :this_channel_is_insecure) |
Tim Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 151 | blk = proc { ch.destroy } |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 152 | blk.call |
| 153 | expect(&blk).to_not raise_error |
| 154 | end |
| 155 | end |
| 156 | |
| 157 | describe '::SSL_TARGET' do |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 158 | it 'is a symbol' do |
| 159 | expect(GRPC::Core::Channel::SSL_TARGET).to be_a(Symbol) |
| 160 | end |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 161 | end |
| 162 | |
| 163 | describe '#close' do |
| 164 | it 'closes a channel ok' do |
murgatroid99 | afe3974 | 2015-12-16 13:04:37 -0800 | [diff] [blame] | 165 | ch = GRPC::Core::Channel.new(fake_host, nil, :this_channel_is_insecure) |
Tim Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 166 | blk = proc { ch.close } |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 167 | expect(&blk).to_not raise_error |
| 168 | end |
| 169 | |
| 170 | it 'can be called more than once without error' do |
murgatroid99 | afe3974 | 2015-12-16 13:04:37 -0800 | [diff] [blame] | 171 | ch = GRPC::Core::Channel.new(fake_host, nil, :this_channel_is_insecure) |
Tim Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 172 | blk = proc { ch.close } |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 173 | blk.call |
| 174 | expect(&blk).to_not raise_error |
| 175 | end |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 176 | end |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 177 | end |