• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2015 gRPC authors.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15require 'spec_helper'
16require 'English'
17
18def load_test_certs
19  test_root = File.join(File.dirname(__FILE__), 'testdata')
20  files = ['ca.pem', 'server1.key', 'server1.pem']
21  files.map { |f| File.open(File.join(test_root, f)).read }
22end
23
24describe GRPC::Core::Channel do
25  let(:fake_host) { 'localhost:0' }
26
27  def create_test_cert
28    GRPC::Core::ChannelCredentials.new(load_test_certs[0])
29  end
30
31  shared_examples '#new' do
32    it 'take a host name without channel args' do
33      blk = proc do
34        GRPC::Core::Channel.new('phony_host', nil, :this_channel_is_insecure)
35      end
36      expect(&blk).not_to raise_error
37    end
38
39    it 'does not take a hash with bad keys as channel args' do
40      blk = construct_with_args(Object.new => 1)
41      expect(&blk).to raise_error TypeError
42      blk = construct_with_args(1 => 1)
43      expect(&blk).to raise_error TypeError
44    end
45
46    it 'does not take a hash with bad values as channel args' do
47      blk = construct_with_args(symbol: Object.new)
48      expect(&blk).to raise_error TypeError
49      blk = construct_with_args('1' => {})
50      expect(&blk).to raise_error TypeError
51    end
52
53    it 'can take a hash with a symbol key as channel args' do
54      blk = construct_with_args(a_symbol: 1)
55      expect(&blk).to_not raise_error
56    end
57
58    it 'can take a hash with a string key as channel args' do
59      blk = construct_with_args('a_symbol' => 1)
60      expect(&blk).to_not raise_error
61    end
62
63    it 'can take a hash with a string value as channel args' do
64      blk = construct_with_args(a_symbol: '1')
65      expect(&blk).to_not raise_error
66    end
67
68    it 'can take a hash with a symbol value as channel args' do
69      blk = construct_with_args(a_symbol: :another_symbol)
70      expect(&blk).to_not raise_error
71    end
72
73    it 'can take a hash with a numeric value as channel args' do
74      blk = construct_with_args(a_symbol: 1)
75      expect(&blk).to_not raise_error
76    end
77
78    it 'can take a hash with many args as channel args' do
79      args = Hash[127.times.collect { |x| [x.to_s, x] }]
80      blk = construct_with_args(args)
81      expect(&blk).to_not raise_error
82    end
83  end
84
85  describe '#new for secure channels' do
86    def construct_with_args(a)
87      proc { GRPC::Core::Channel.new('phony_host', a, create_test_cert) }
88    end
89
90    it_behaves_like '#new'
91  end
92
93  describe '#new for insecure channels' do
94    it_behaves_like '#new'
95
96    def construct_with_args(a)
97      proc do
98        GRPC::Core::Channel.new('phony_host', a, :this_channel_is_insecure)
99      end
100    end
101  end
102
103  describe '#new for XDS channels' do
104    it_behaves_like '#new'
105
106    def construct_with_args(a)
107      proc do
108        xds_creds = GRPC::Core::XdsChannelCredentials.new(create_test_cert)
109        GRPC::Core::Channel.new('dummy_host', a, xds_creds)
110      end
111    end
112  end
113
114  describe '#create_call' do
115    it 'creates a call OK' do
116      ch = GRPC::Core::Channel.new(fake_host, nil, :this_channel_is_insecure)
117
118      deadline = Time.now + 5
119
120      blk = proc do
121        call = ch.create_call(nil, nil, 'phony_method', nil, deadline)
122        call.close
123      end
124      expect(&blk).to_not raise_error
125    end
126
127    it 'raises an error if called on a closed channel' do
128      STDERR.puts "#{Time.now}: begin: raises an error if called on a closed channel"
129      ch = GRPC::Core::Channel.new(fake_host, nil, :this_channel_is_insecure)
130      STDERR.puts "#{Time.now}: created channel"
131      ch.close
132      STDERR.puts "#{Time.now}: closed channel"
133
134      deadline = Time.now + 5
135      blk = proc do
136        call = ch.create_call(nil, nil, 'phony_method', nil, deadline)
137        STDERR.puts "#{Time.now}: created call"
138        call.close
139      end
140      expect(&blk).to raise_error(RuntimeError)
141      STDERR.puts "#{Time.now}: finished: raises an error if called on a closed channel"
142    end
143  end
144
145  describe '#destroy' do
146    it 'destroys a channel ok' do
147      ch = GRPC::Core::Channel.new(fake_host, nil, :this_channel_is_insecure)
148      blk = proc { ch.destroy }
149      expect(&blk).to_not raise_error
150    end
151
152    it 'can be called more than once without error' do
153      ch = GRPC::Core::Channel.new(fake_host, nil, :this_channel_is_insecure)
154      blk = proc { ch.destroy }
155      blk.call
156      expect(&blk).to_not raise_error
157    end
158  end
159
160  describe '#connectivity_state' do
161    it 'returns an enum' do
162      ch = GRPC::Core::Channel.new(fake_host, nil, :this_channel_is_insecure)
163      valid_states = [
164        GRPC::Core::ConnectivityStates::IDLE,
165        GRPC::Core::ConnectivityStates::CONNECTING,
166        GRPC::Core::ConnectivityStates::READY,
167        GRPC::Core::ConnectivityStates::TRANSIENT_FAILURE,
168        GRPC::Core::ConnectivityStates::FATAL_FAILURE
169      ]
170
171      expect(valid_states).to include(ch.connectivity_state)
172    end
173
174    it 'returns an enum when trying to connect' do
175      ch = GRPC::Core::Channel.new(fake_host, nil, :this_channel_is_insecure)
176      ch.connectivity_state(true)
177      valid_states = [
178        GRPC::Core::ConnectivityStates::IDLE,
179        GRPC::Core::ConnectivityStates::CONNECTING,
180        GRPC::Core::ConnectivityStates::READY,
181        GRPC::Core::ConnectivityStates::TRANSIENT_FAILURE,
182        GRPC::Core::ConnectivityStates::FATAL_FAILURE
183      ]
184
185      expect(valid_states).to include(ch.connectivity_state)
186    end
187  end
188
189  describe '::SSL_TARGET' do
190    it 'is a symbol' do
191      expect(GRPC::Core::Channel::SSL_TARGET).to be_a(Symbol)
192    end
193  end
194
195  describe '#close' do
196    it 'closes a channel ok' do
197      ch = GRPC::Core::Channel.new(fake_host, nil, :this_channel_is_insecure)
198      blk = proc { ch.close }
199      expect(&blk).to_not raise_error
200    end
201
202    it 'can be called more than once without error' do
203      ch = GRPC::Core::Channel.new(fake_host, nil, :this_channel_is_insecure)
204      blk = proc { ch.close }
205      blk.call
206      expect(&blk).to_not raise_error
207    end
208  end
209end
210