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 |
| 43 | GRPC::Core::Credentials.new(load_test_certs[0]) |
| 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 |
| 48 | expect { GRPC::Core::Channel.new('dummy_host', nil) }.not_to raise_error |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 49 | end |
| 50 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 51 | it 'does not take a hash with bad keys as channel args' do |
| 52 | blk = construct_with_args(Object.new => 1) |
| 53 | expect(&blk).to raise_error TypeError |
| 54 | blk = construct_with_args(1 => 1) |
| 55 | expect(&blk).to raise_error TypeError |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 56 | end |
| 57 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 58 | 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] | 59 | blk = construct_with_args(symbol: Object.new) |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 60 | expect(&blk).to raise_error TypeError |
| 61 | blk = construct_with_args('1' => Hash.new) |
| 62 | expect(&blk).to raise_error TypeError |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 63 | end |
| 64 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 65 | 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] | 66 | blk = construct_with_args(a_symbol: 1) |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 67 | expect(&blk).to_not raise_error |
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 string 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 value as channel args' do |
Tim Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 76 | blk = construct_with_args(a_symbol: '1') |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 77 | expect(&blk).to_not raise_error |
| 78 | end |
| 79 | |
| 80 | 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] | 81 | blk = construct_with_args(a_symbol: :another_symbol) |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 82 | expect(&blk).to_not raise_error |
| 83 | end |
| 84 | |
| 85 | 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] | 86 | blk = construct_with_args(a_symbol: 1) |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 87 | expect(&blk).to_not raise_error |
| 88 | end |
| 89 | |
| 90 | it 'can take a hash with many args as channel args' do |
Tim Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 91 | args = Hash[127.times.collect { |x| [x.to_s, x] }] |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 92 | blk = construct_with_args(args) |
| 93 | expect(&blk).to_not raise_error |
| 94 | end |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 95 | end |
| 96 | |
| 97 | describe '#new for secure channels' do |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 98 | def construct_with_args(a) |
Tim Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 99 | proc { GRPC::Core::Channel.new('dummy_host', a, create_test_cert) } |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 100 | end |
| 101 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 102 | it_behaves_like '#new' |
| 103 | end |
| 104 | |
| 105 | describe '#new for insecure channels' do |
| 106 | it_behaves_like '#new' |
| 107 | |
| 108 | def construct_with_args(a) |
Tim Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 109 | proc { GRPC::Core::Channel.new('dummy_host', a) } |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 110 | end |
| 111 | end |
| 112 | |
| 113 | describe '#create_call' do |
| 114 | it 'creates a call OK' do |
Tim Emiola | 564719d | 2015-03-27 13:07:34 -0700 | [diff] [blame] | 115 | ch = GRPC::Core::Channel.new(fake_host, nil) |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 116 | |
| 117 | deadline = Time.now + 5 |
| 118 | |
Tim Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 119 | blk = proc do |
Tim Emiola | 564719d | 2015-03-27 13:07:34 -0700 | [diff] [blame] | 120 | ch.create_call(cq, 'dummy_method', 'dummy_host', deadline) |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 121 | end |
| 122 | expect(&blk).to_not raise_error |
| 123 | end |
| 124 | |
| 125 | it 'raises an error if called on a closed channel' do |
Tim Emiola | 564719d | 2015-03-27 13:07:34 -0700 | [diff] [blame] | 126 | ch = GRPC::Core::Channel.new(fake_host, nil) |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 127 | ch.close |
| 128 | |
| 129 | deadline = Time.now + 5 |
Tim Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 130 | blk = proc do |
Tim Emiola | 564719d | 2015-03-27 13:07:34 -0700 | [diff] [blame] | 131 | ch.create_call(cq, 'dummy_method', 'dummy_host', deadline) |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 132 | end |
| 133 | expect(&blk).to raise_error(RuntimeError) |
| 134 | end |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 135 | end |
| 136 | |
| 137 | describe '#destroy' do |
| 138 | it 'destroys a channel ok' do |
Tim Emiola | 564719d | 2015-03-27 13:07:34 -0700 | [diff] [blame] | 139 | ch = GRPC::Core::Channel.new(fake_host, nil) |
Tim Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 140 | blk = proc { ch.destroy } |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 141 | expect(&blk).to_not raise_error |
| 142 | end |
| 143 | |
| 144 | it 'can be called more than once without error' do |
Tim Emiola | 564719d | 2015-03-27 13:07:34 -0700 | [diff] [blame] | 145 | ch = GRPC::Core::Channel.new(fake_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 | blk.call |
| 148 | expect(&blk).to_not raise_error |
| 149 | end |
| 150 | end |
| 151 | |
| 152 | describe '::SSL_TARGET' do |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 153 | it 'is a symbol' do |
| 154 | expect(GRPC::Core::Channel::SSL_TARGET).to be_a(Symbol) |
| 155 | end |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 156 | end |
| 157 | |
| 158 | describe '#close' do |
| 159 | it 'closes a channel ok' do |
Tim Emiola | 564719d | 2015-03-27 13:07:34 -0700 | [diff] [blame] | 160 | ch = GRPC::Core::Channel.new(fake_host, nil) |
Tim Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 161 | blk = proc { ch.close } |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 162 | expect(&blk).to_not raise_error |
| 163 | end |
| 164 | |
| 165 | it 'can be called more than once without error' do |
Tim Emiola | 564719d | 2015-03-27 13:07:34 -0700 | [diff] [blame] | 166 | ch = GRPC::Core::Channel.new(fake_host, nil) |
Tim Emiola | e2860c5 | 2015-01-16 02:58:41 -0800 | [diff] [blame] | 167 | blk = proc { ch.close } |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 168 | blk.call |
| 169 | expect(&blk).to_not raise_error |
| 170 | end |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 171 | end |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 172 | end |