blob: 355f95c9d798ed930b08e1af00e7efba83f1e24f [file] [log] [blame]
Craig Tiller06059952015-02-18 08:34:56 -08001# Copyright 2015, Google Inc.
nnoble097ef9b2014-12-01 17:06:10 -08002# 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
30require 'grpc'
Tim Emiola72f14c32015-01-26 18:41:00 -080031
nnoble0c475f02014-12-05 15:37:39 -080032def 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 }
36end
nnoble097ef9b2014-12-01 17:06:10 -080037
nnoble0c475f02014-12-05 15:37:39 -080038describe GRPC::Core::Channel do
Tim Emiola564719d2015-03-27 13:07:34 -070039 let(:fake_host) { 'localhost:0' }
40 let(:cq) { GRPC::Core::CompletionQueue.new }
Tim Emiola36066532015-01-29 16:27:07 -080041
nnoble0c475f02014-12-05 15:37:39 -080042 def create_test_cert
Tim Emiola43a7e4e2015-10-28 00:17:14 -070043 GRPC::Core::ChannelCredentials.new(load_test_certs[0])
nnoble0c475f02014-12-05 15:37:39 -080044 end
45
nnoble0c475f02014-12-05 15:37:39 -080046 shared_examples '#new' do
nnoble0c475f02014-12-05 15:37:39 -080047 it 'take a host name without channel args' do
murgatroid99afe39742015-12-16 13:04:37 -080048 blk = proc do
49 GRPC::Core::Channel.new('dummy_host', nil, :this_channel_is_insecure)
50 end
51 expect(&blk).not_to raise_error
nnoble097ef9b2014-12-01 17:06:10 -080052 end
53
nnoble0c475f02014-12-05 15:37:39 -080054 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
nnoble097ef9b2014-12-01 17:06:10 -080059 end
60
nnoble0c475f02014-12-05 15:37:39 -080061 it 'does not take a hash with bad values as channel args' do
Tim Emiolae2860c52015-01-16 02:58:41 -080062 blk = construct_with_args(symbol: Object.new)
nnoble0c475f02014-12-05 15:37:39 -080063 expect(&blk).to raise_error TypeError
Tim Emiola1e098122015-04-14 09:42:09 -070064 blk = construct_with_args('1' => {})
nnoble0c475f02014-12-05 15:37:39 -080065 expect(&blk).to raise_error TypeError
nnoble097ef9b2014-12-01 17:06:10 -080066 end
67
nnoble0c475f02014-12-05 15:37:39 -080068 it 'can take a hash with a symbol key as channel args' do
Tim Emiolae2860c52015-01-16 02:58:41 -080069 blk = construct_with_args(a_symbol: 1)
nnoble0c475f02014-12-05 15:37:39 -080070 expect(&blk).to_not raise_error
nnoble097ef9b2014-12-01 17:06:10 -080071 end
72
nnoble0c475f02014-12-05 15:37:39 -080073 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
nnoble097ef9b2014-12-01 17:06:10 -080076 end
77
nnoble0c475f02014-12-05 15:37:39 -080078 it 'can take a hash with a string value as channel args' do
Tim Emiolae2860c52015-01-16 02:58:41 -080079 blk = construct_with_args(a_symbol: '1')
nnoble0c475f02014-12-05 15:37:39 -080080 expect(&blk).to_not raise_error
81 end
82
83 it 'can take a hash with a symbol value as channel args' do
Tim Emiolae2860c52015-01-16 02:58:41 -080084 blk = construct_with_args(a_symbol: :another_symbol)
nnoble0c475f02014-12-05 15:37:39 -080085 expect(&blk).to_not raise_error
86 end
87
88 it 'can take a hash with a numeric value as channel args' do
Tim Emiolae2860c52015-01-16 02:58:41 -080089 blk = construct_with_args(a_symbol: 1)
nnoble0c475f02014-12-05 15:37:39 -080090 expect(&blk).to_not raise_error
91 end
92
93 it 'can take a hash with many args as channel args' do
Tim Emiolae2860c52015-01-16 02:58:41 -080094 args = Hash[127.times.collect { |x| [x.to_s, x] }]
nnoble0c475f02014-12-05 15:37:39 -080095 blk = construct_with_args(args)
96 expect(&blk).to_not raise_error
97 end
nnoble0c475f02014-12-05 15:37:39 -080098 end
99
100 describe '#new for secure channels' do
nnoble097ef9b2014-12-01 17:06:10 -0800101 def construct_with_args(a)
Tim Emiolae2860c52015-01-16 02:58:41 -0800102 proc { GRPC::Core::Channel.new('dummy_host', a, create_test_cert) }
nnoble097ef9b2014-12-01 17:06:10 -0800103 end
104
nnoble0c475f02014-12-05 15:37:39 -0800105 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)
murgatroid99afe39742015-12-16 13:04:37 -0800112 proc do
113 GRPC::Core::Channel.new('dummy_host', a, :this_channel_is_insecure)
114 end
nnoble0c475f02014-12-05 15:37:39 -0800115 end
116 end
117
118 describe '#create_call' do
119 it 'creates a call OK' do
murgatroid99afe39742015-12-16 13:04:37 -0800120 ch = GRPC::Core::Channel.new(fake_host, nil, :this_channel_is_insecure)
nnoble0c475f02014-12-05 15:37:39 -0800121
122 deadline = Time.now + 5
123
Tim Emiolae2860c52015-01-16 02:58:41 -0800124 blk = proc do
Tim Emiolafde3dbf2015-08-11 16:35:41 -0700125 ch.create_call(cq, nil, nil, 'dummy_method', nil, deadline)
nnoble0c475f02014-12-05 15:37:39 -0800126 end
127 expect(&blk).to_not raise_error
128 end
129
130 it 'raises an error if called on a closed channel' do
murgatroid99afe39742015-12-16 13:04:37 -0800131 ch = GRPC::Core::Channel.new(fake_host, nil, :this_channel_is_insecure)
nnoble0c475f02014-12-05 15:37:39 -0800132 ch.close
133
134 deadline = Time.now + 5
Tim Emiolae2860c52015-01-16 02:58:41 -0800135 blk = proc do
Tim Emiolafde3dbf2015-08-11 16:35:41 -0700136 ch.create_call(cq, nil, nil, 'dummy_method', nil, deadline)
nnoble0c475f02014-12-05 15:37:39 -0800137 end
138 expect(&blk).to raise_error(RuntimeError)
139 end
nnoble0c475f02014-12-05 15:37:39 -0800140 end
141
142 describe '#destroy' do
143 it 'destroys a channel ok' do
murgatroid99afe39742015-12-16 13:04:37 -0800144 ch = GRPC::Core::Channel.new(fake_host, nil, :this_channel_is_insecure)
Tim Emiolae2860c52015-01-16 02:58:41 -0800145 blk = proc { ch.destroy }
nnoble0c475f02014-12-05 15:37:39 -0800146 expect(&blk).to_not raise_error
147 end
148
149 it 'can be called more than once without error' do
murgatroid99afe39742015-12-16 13:04:37 -0800150 ch = GRPC::Core::Channel.new(fake_host, nil, :this_channel_is_insecure)
Tim Emiolae2860c52015-01-16 02:58:41 -0800151 blk = proc { ch.destroy }
nnoble0c475f02014-12-05 15:37:39 -0800152 blk.call
153 expect(&blk).to_not raise_error
154 end
155 end
156
157 describe '::SSL_TARGET' do
nnoble0c475f02014-12-05 15:37:39 -0800158 it 'is a symbol' do
159 expect(GRPC::Core::Channel::SSL_TARGET).to be_a(Symbol)
160 end
nnoble0c475f02014-12-05 15:37:39 -0800161 end
162
163 describe '#close' do
164 it 'closes a channel ok' do
murgatroid99afe39742015-12-16 13:04:37 -0800165 ch = GRPC::Core::Channel.new(fake_host, nil, :this_channel_is_insecure)
Tim Emiolae2860c52015-01-16 02:58:41 -0800166 blk = proc { ch.close }
nnoble0c475f02014-12-05 15:37:39 -0800167 expect(&blk).to_not raise_error
168 end
169
170 it 'can be called more than once without error' do
murgatroid99afe39742015-12-16 13:04:37 -0800171 ch = GRPC::Core::Channel.new(fake_host, nil, :this_channel_is_insecure)
Tim Emiolae2860c52015-01-16 02:58:41 -0800172 blk = proc { ch.close }
nnoble0c475f02014-12-05 15:37:39 -0800173 blk.call
174 expect(&blk).to_not raise_error
175 end
nnoble097ef9b2014-12-01 17:06:10 -0800176 end
Craig Tiller190d3602015-02-18 09:23:38 -0800177end