blob: 189d1c67ab2831011a4e3b2bf7497c5a6e839082 [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'
Tim Emiola72f14c32015-01-26 18:41:00 -080031
32FAKE_HOST='localhost:0'
nnoble097ef9b2014-12-01 17:06:10 -080033
nnoble0c475f02014-12-05 15:37:39 -080034def load_test_certs
35 test_root = File.join(File.dirname(__FILE__), 'testdata')
36 files = ['ca.pem', 'server1.key', 'server1.pem']
37 files.map { |f| File.open(File.join(test_root, f)).read }
38end
nnoble097ef9b2014-12-01 17:06:10 -080039
nnoble0c475f02014-12-05 15:37:39 -080040describe GRPC::Core::Channel do
nnoble0c475f02014-12-05 15:37:39 -080041 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
nnoble0c475f02014-12-05 15:37:39 -080050 it 'take a host name without channel args' do
51 expect { GRPC::Core::Channel.new('dummy_host', nil) }.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
64 blk = construct_with_args('1' => Hash.new)
65 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)
Tim Emiolae2860c52015-01-16 02:58:41 -0800112 proc { GRPC::Core::Channel.new('dummy_host', a) }
nnoble0c475f02014-12-05 15:37:39 -0800113 end
114 end
115
116 describe '#create_call' do
117 it 'creates a call OK' do
Tim Emiola72f14c32015-01-26 18:41:00 -0800118 host = FAKE_HOST
nnoble0c475f02014-12-05 15:37:39 -0800119 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
Tim Emiola72f14c32015-01-26 18:41:00 -0800130 host = FAKE_HOST
nnoble0c475f02014-12-05 15:37:39 -0800131 ch = GRPC::Core::Channel.new(host, nil)
132 ch.close
133
134 deadline = Time.now + 5
Tim Emiolae2860c52015-01-16 02:58:41 -0800135 blk = proc do
nnoble0c475f02014-12-05 15:37:39 -0800136 ch.create_call('dummy_method', 'dummy_host', deadline)
137 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
Tim Emiola72f14c32015-01-26 18:41:00 -0800144 host = FAKE_HOST
nnoble0c475f02014-12-05 15:37:39 -0800145 ch = GRPC::Core::Channel.new(host, nil)
Tim Emiolae2860c52015-01-16 02:58:41 -0800146 blk = proc { ch.destroy }
nnoble0c475f02014-12-05 15:37:39 -0800147 expect(&blk).to_not raise_error
148 end
149
150 it 'can be called more than once without error' do
Tim Emiola72f14c32015-01-26 18:41:00 -0800151 host = FAKE_HOST
nnoble0c475f02014-12-05 15:37:39 -0800152 ch = GRPC::Core::Channel.new(host, nil)
Tim Emiolae2860c52015-01-16 02:58:41 -0800153 blk = proc { ch.destroy }
nnoble0c475f02014-12-05 15:37:39 -0800154 blk.call
155 expect(&blk).to_not raise_error
156 end
157 end
158
159 describe '::SSL_TARGET' do
nnoble0c475f02014-12-05 15:37:39 -0800160 it 'is a symbol' do
161 expect(GRPC::Core::Channel::SSL_TARGET).to be_a(Symbol)
162 end
nnoble0c475f02014-12-05 15:37:39 -0800163 end
164
165 describe '#close' do
166 it 'closes a channel ok' do
Tim Emiola72f14c32015-01-26 18:41:00 -0800167 host = FAKE_HOST
nnoble0c475f02014-12-05 15:37:39 -0800168 ch = GRPC::Core::Channel.new(host, nil)
Tim Emiolae2860c52015-01-16 02:58:41 -0800169 blk = proc { ch.close }
nnoble0c475f02014-12-05 15:37:39 -0800170 expect(&blk).to_not raise_error
171 end
172
173 it 'can be called more than once without error' do
Tim Emiola72f14c32015-01-26 18:41:00 -0800174 host = FAKE_HOST
nnoble0c475f02014-12-05 15:37:39 -0800175 ch = GRPC::Core::Channel.new(host, nil)
Tim Emiolae2860c52015-01-16 02:58:41 -0800176 blk = proc { ch.close }
nnoble0c475f02014-12-05 15:37:39 -0800177 blk.call
178 expect(&blk).to_not raise_error
179 end
nnoble097ef9b2014-12-01 17:06:10 -0800180 end
nnoble097ef9b2014-12-01 17:06:10 -0800181end