blob: 820dbd39e98a9a0904b3b80589af6a062effdc58 [file] [log] [blame]
nnoble097ef9b2014-12-01 17:06:10 -08001# 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
30require 'grpc'
31require 'port_picker'
32
nnoble0c475f02014-12-05 15:37:39 -080033def 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 }
37end
nnoble097ef9b2014-12-01 17:06:10 -080038
nnoble0c475f02014-12-05 15:37:39 -080039describe GRPC::Core::Channel do
nnoble0c475f02014-12-05 15:37:39 -080040 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
nnoble0c475f02014-12-05 15:37:39 -080049 it 'take a host name without channel args' do
50 expect { GRPC::Core::Channel.new('dummy_host', nil) }.not_to raise_error
nnoble097ef9b2014-12-01 17:06:10 -080051 end
52
nnoble0c475f02014-12-05 15:37:39 -080053 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
nnoble097ef9b2014-12-01 17:06:10 -080058 end
59
nnoble0c475f02014-12-05 15:37:39 -080060 it 'does not take a hash with bad values as channel args' do
Tim Emiolae2860c52015-01-16 02:58:41 -080061 blk = construct_with_args(symbol: Object.new)
nnoble0c475f02014-12-05 15:37:39 -080062 expect(&blk).to raise_error TypeError
63 blk = construct_with_args('1' => Hash.new)
64 expect(&blk).to raise_error TypeError
nnoble097ef9b2014-12-01 17:06:10 -080065 end
66
nnoble0c475f02014-12-05 15:37:39 -080067 it 'can take a hash with a symbol key as channel args' do
Tim Emiolae2860c52015-01-16 02:58:41 -080068 blk = construct_with_args(a_symbol: 1)
nnoble0c475f02014-12-05 15:37:39 -080069 expect(&blk).to_not raise_error
nnoble097ef9b2014-12-01 17:06:10 -080070 end
71
nnoble0c475f02014-12-05 15:37:39 -080072 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
nnoble097ef9b2014-12-01 17:06:10 -080075 end
76
nnoble0c475f02014-12-05 15:37:39 -080077 it 'can take a hash with a string value as channel args' do
Tim Emiolae2860c52015-01-16 02:58:41 -080078 blk = construct_with_args(a_symbol: '1')
nnoble0c475f02014-12-05 15:37:39 -080079 expect(&blk).to_not raise_error
80 end
81
82 it 'can take a hash with a symbol value as channel args' do
Tim Emiolae2860c52015-01-16 02:58:41 -080083 blk = construct_with_args(a_symbol: :another_symbol)
nnoble0c475f02014-12-05 15:37:39 -080084 expect(&blk).to_not raise_error
85 end
86
87 it 'can take a hash with a numeric value as channel args' do
Tim Emiolae2860c52015-01-16 02:58:41 -080088 blk = construct_with_args(a_symbol: 1)
nnoble0c475f02014-12-05 15:37:39 -080089 expect(&blk).to_not raise_error
90 end
91
92 it 'can take a hash with many args as channel args' do
Tim Emiolae2860c52015-01-16 02:58:41 -080093 args = Hash[127.times.collect { |x| [x.to_s, x] }]
nnoble0c475f02014-12-05 15:37:39 -080094 blk = construct_with_args(args)
95 expect(&blk).to_not raise_error
96 end
nnoble0c475f02014-12-05 15:37:39 -080097 end
98
99 describe '#new for secure channels' do
nnoble097ef9b2014-12-01 17:06:10 -0800100 def construct_with_args(a)
Tim Emiolae2860c52015-01-16 02:58:41 -0800101 proc { GRPC::Core::Channel.new('dummy_host', a, create_test_cert) }
nnoble097ef9b2014-12-01 17:06:10 -0800102 end
103
nnoble0c475f02014-12-05 15:37:39 -0800104 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 Emiolae2860c52015-01-16 02:58:41 -0800111 proc { GRPC::Core::Channel.new('dummy_host', a) }
nnoble0c475f02014-12-05 15:37:39 -0800112 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 Emiolae2860c52015-01-16 02:58:41 -0800123 blk = proc do
nnoble0c475f02014-12-05 15:37:39 -0800124 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 Emiolae2860c52015-01-16 02:58:41 -0800136 blk = proc do
nnoble0c475f02014-12-05 15:37:39 -0800137 ch.create_call('dummy_method', 'dummy_host', deadline)
138 end
139 expect(&blk).to raise_error(RuntimeError)
140 end
nnoble0c475f02014-12-05 15:37:39 -0800141 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 Emiolae2860c52015-01-16 02:58:41 -0800148 blk = proc { ch.destroy }
nnoble0c475f02014-12-05 15:37:39 -0800149 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 Emiolae2860c52015-01-16 02:58:41 -0800156 blk = proc { ch.destroy }
nnoble0c475f02014-12-05 15:37:39 -0800157 blk.call
158 expect(&blk).to_not raise_error
159 end
160 end
161
162 describe '::SSL_TARGET' do
nnoble0c475f02014-12-05 15:37:39 -0800163 it 'is a symbol' do
164 expect(GRPC::Core::Channel::SSL_TARGET).to be_a(Symbol)
165 end
nnoble0c475f02014-12-05 15:37:39 -0800166 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 Emiolae2860c52015-01-16 02:58:41 -0800173 blk = proc { ch.close }
nnoble0c475f02014-12-05 15:37:39 -0800174 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 Emiolae2860c52015-01-16 02:58:41 -0800181 blk = proc { ch.close }
nnoble0c475f02014-12-05 15:37:39 -0800182 blk.call
183 expect(&blk).to_not raise_error
184 end
nnoble097ef9b2014-12-01 17:06:10 -0800185 end
nnoble097ef9b2014-12-01 17:06:10 -0800186end