blob: 31e38d71b81ee71aeaefa634db7a2e7d87024832 [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
43 GRPC::Core::Credentials.new(load_test_certs[0])
44 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
48 expect { GRPC::Core::Channel.new('dummy_host', nil) }.not_to raise_error
nnoble097ef9b2014-12-01 17:06:10 -080049 end
50
nnoble0c475f02014-12-05 15:37:39 -080051 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
nnoble097ef9b2014-12-01 17:06:10 -080056 end
57
nnoble0c475f02014-12-05 15:37:39 -080058 it 'does not take a hash with bad values as channel args' do
Tim Emiolae2860c52015-01-16 02:58:41 -080059 blk = construct_with_args(symbol: Object.new)
nnoble0c475f02014-12-05 15:37:39 -080060 expect(&blk).to raise_error TypeError
61 blk = construct_with_args('1' => Hash.new)
62 expect(&blk).to raise_error TypeError
nnoble097ef9b2014-12-01 17:06:10 -080063 end
64
nnoble0c475f02014-12-05 15:37:39 -080065 it 'can take a hash with a symbol key as channel args' do
Tim Emiolae2860c52015-01-16 02:58:41 -080066 blk = construct_with_args(a_symbol: 1)
nnoble0c475f02014-12-05 15:37:39 -080067 expect(&blk).to_not raise_error
nnoble097ef9b2014-12-01 17:06:10 -080068 end
69
nnoble0c475f02014-12-05 15:37:39 -080070 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
nnoble097ef9b2014-12-01 17:06:10 -080073 end
74
nnoble0c475f02014-12-05 15:37:39 -080075 it 'can take a hash with a string value as channel args' do
Tim Emiolae2860c52015-01-16 02:58:41 -080076 blk = construct_with_args(a_symbol: '1')
nnoble0c475f02014-12-05 15:37:39 -080077 expect(&blk).to_not raise_error
78 end
79
80 it 'can take a hash with a symbol value as channel args' do
Tim Emiolae2860c52015-01-16 02:58:41 -080081 blk = construct_with_args(a_symbol: :another_symbol)
nnoble0c475f02014-12-05 15:37:39 -080082 expect(&blk).to_not raise_error
83 end
84
85 it 'can take a hash with a numeric value as channel args' do
Tim Emiolae2860c52015-01-16 02:58:41 -080086 blk = construct_with_args(a_symbol: 1)
nnoble0c475f02014-12-05 15:37:39 -080087 expect(&blk).to_not raise_error
88 end
89
90 it 'can take a hash with many args as channel args' do
Tim Emiolae2860c52015-01-16 02:58:41 -080091 args = Hash[127.times.collect { |x| [x.to_s, x] }]
nnoble0c475f02014-12-05 15:37:39 -080092 blk = construct_with_args(args)
93 expect(&blk).to_not raise_error
94 end
nnoble0c475f02014-12-05 15:37:39 -080095 end
96
97 describe '#new for secure channels' do
nnoble097ef9b2014-12-01 17:06:10 -080098 def construct_with_args(a)
Tim Emiolae2860c52015-01-16 02:58:41 -080099 proc { GRPC::Core::Channel.new('dummy_host', a, create_test_cert) }
nnoble097ef9b2014-12-01 17:06:10 -0800100 end
101
nnoble0c475f02014-12-05 15:37:39 -0800102 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 Emiolae2860c52015-01-16 02:58:41 -0800109 proc { GRPC::Core::Channel.new('dummy_host', a) }
nnoble0c475f02014-12-05 15:37:39 -0800110 end
111 end
112
113 describe '#create_call' do
114 it 'creates a call OK' do
Tim Emiola564719d2015-03-27 13:07:34 -0700115 ch = GRPC::Core::Channel.new(fake_host, nil)
nnoble0c475f02014-12-05 15:37:39 -0800116
117 deadline = Time.now + 5
118
Tim Emiolae2860c52015-01-16 02:58:41 -0800119 blk = proc do
Tim Emiola564719d2015-03-27 13:07:34 -0700120 ch.create_call(cq, 'dummy_method', 'dummy_host', deadline)
nnoble0c475f02014-12-05 15:37:39 -0800121 end
122 expect(&blk).to_not raise_error
123 end
124
125 it 'raises an error if called on a closed channel' do
Tim Emiola564719d2015-03-27 13:07:34 -0700126 ch = GRPC::Core::Channel.new(fake_host, nil)
nnoble0c475f02014-12-05 15:37:39 -0800127 ch.close
128
129 deadline = Time.now + 5
Tim Emiolae2860c52015-01-16 02:58:41 -0800130 blk = proc do
Tim Emiola564719d2015-03-27 13:07:34 -0700131 ch.create_call(cq, 'dummy_method', 'dummy_host', deadline)
nnoble0c475f02014-12-05 15:37:39 -0800132 end
133 expect(&blk).to raise_error(RuntimeError)
134 end
nnoble0c475f02014-12-05 15:37:39 -0800135 end
136
137 describe '#destroy' do
138 it 'destroys a channel ok' do
Tim Emiola564719d2015-03-27 13:07:34 -0700139 ch = GRPC::Core::Channel.new(fake_host, nil)
Tim Emiolae2860c52015-01-16 02:58:41 -0800140 blk = proc { ch.destroy }
nnoble0c475f02014-12-05 15:37:39 -0800141 expect(&blk).to_not raise_error
142 end
143
144 it 'can be called more than once without error' do
Tim Emiola564719d2015-03-27 13:07:34 -0700145 ch = GRPC::Core::Channel.new(fake_host, nil)
Tim Emiolae2860c52015-01-16 02:58:41 -0800146 blk = proc { ch.destroy }
nnoble0c475f02014-12-05 15:37:39 -0800147 blk.call
148 expect(&blk).to_not raise_error
149 end
150 end
151
152 describe '::SSL_TARGET' do
nnoble0c475f02014-12-05 15:37:39 -0800153 it 'is a symbol' do
154 expect(GRPC::Core::Channel::SSL_TARGET).to be_a(Symbol)
155 end
nnoble0c475f02014-12-05 15:37:39 -0800156 end
157
158 describe '#close' do
159 it 'closes a channel ok' do
Tim Emiola564719d2015-03-27 13:07:34 -0700160 ch = GRPC::Core::Channel.new(fake_host, nil)
Tim Emiolae2860c52015-01-16 02:58:41 -0800161 blk = proc { ch.close }
nnoble0c475f02014-12-05 15:37:39 -0800162 expect(&blk).to_not raise_error
163 end
164
165 it 'can be called more than once without error' do
Tim Emiola564719d2015-03-27 13:07:34 -0700166 ch = GRPC::Core::Channel.new(fake_host, nil)
Tim Emiolae2860c52015-01-16 02:58:41 -0800167 blk = proc { ch.close }
nnoble0c475f02014-12-05 15:37:39 -0800168 blk.call
169 expect(&blk).to_not raise_error
170 end
nnoble097ef9b2014-12-01 17:06:10 -0800171 end
Craig Tiller190d3602015-02-18 09:23:38 -0800172end