blob: b8ecd64f393cb9af31b6d25b1ec7e6a7896b7f6b [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 -080033include GRPC::Core::StatusCodes
nnoble097ef9b2014-12-01 17:06:10 -080034
nnoble0c475f02014-12-05 15:37:39 -080035describe GRPC::Core::RpcErrors do
nnoble097ef9b2014-12-01 17:06:10 -080036 before(:each) do
37 @known_types = {
Tim Emiolae2860c52015-01-16 02:58:41 -080038 OK: 0,
39 ERROR: 1,
40 NOT_ON_SERVER: 2,
41 NOT_ON_CLIENT: 3,
42 ALREADY_ACCEPTED: 4,
43 ALREADY_INVOKED: 5,
44 NOT_INVOKED: 6,
45 ALREADY_FINISHED: 7,
46 TOO_MANY_OPERATIONS: 8,
47 INVALID_FLAGS: 9,
48 ErrorMessages: {
49 0 => 'ok',
50 1 => 'unknown error',
51 2 => 'not available on a server',
52 3 => 'not available on a client',
53 4 => 'call is already accepted',
54 5 => 'call is already invoked',
55 6 => 'call is not yet invoked',
56 7 => 'call is already finished',
57 8 => 'outstanding read or write present',
58 9 => 'a bad flag was given'
nnoble097ef9b2014-12-01 17:06:10 -080059 }
60 }
61 end
62
63 it 'should have symbols for all the known error codes' do
nnoble0c475f02014-12-05 15:37:39 -080064 m = GRPC::Core::RpcErrors
nnoble097ef9b2014-12-01 17:06:10 -080065 syms_and_codes = m.constants.collect { |c| [c, m.const_get(c)] }
66 expect(Hash[syms_and_codes]).to eq(@known_types)
67 end
nnoble097ef9b2014-12-01 17:06:10 -080068end
69
nnoble0c475f02014-12-05 15:37:39 -080070describe GRPC::Core::Call do
nnoble097ef9b2014-12-01 17:06:10 -080071 before(:each) do
72 @tag = Object.new
nnoble0c475f02014-12-05 15:37:39 -080073 @client_queue = GRPC::Core::CompletionQueue.new
74 @server_queue = GRPC::Core::CompletionQueue.new
nnoble097ef9b2014-12-01 17:06:10 -080075 port = find_unused_tcp_port
76 host = "localhost:#{port}"
nnoble0c475f02014-12-05 15:37:39 -080077 @server = GRPC::Core::Server.new(@server_queue, nil)
nnoble097ef9b2014-12-01 17:06:10 -080078 @server.add_http2_port(host)
nnoble0c475f02014-12-05 15:37:39 -080079 @ch = GRPC::Core::Channel.new(host, nil)
nnoble097ef9b2014-12-01 17:06:10 -080080 end
81
82 after(:each) do
83 @server.close
84 end
85
86 describe '#start_read' do
87 it 'should fail if called immediately' do
Tim Emiolae2860c52015-01-16 02:58:41 -080088 blk = proc { make_test_call.start_read(@tag) }
temiola58327912014-12-15 17:51:16 -080089 expect(&blk).to raise_error GRPC::Core::CallError
nnoble097ef9b2014-12-01 17:06:10 -080090 end
91 end
92
93 describe '#start_write' do
94 it 'should fail if called immediately' do
nnoble0c475f02014-12-05 15:37:39 -080095 bytes = GRPC::Core::ByteBuffer.new('test string')
Tim Emiolae2860c52015-01-16 02:58:41 -080096 blk = proc { make_test_call.start_write(bytes, @tag) }
temiola58327912014-12-15 17:51:16 -080097 expect(&blk).to raise_error GRPC::Core::CallError
nnoble097ef9b2014-12-01 17:06:10 -080098 end
99 end
100
101 describe '#start_write_status' do
102 it 'should fail if called immediately' do
Tim Emiolae2860c52015-01-16 02:58:41 -0800103 blk = proc { make_test_call.start_write_status(153, 'x', @tag) }
temiola58327912014-12-15 17:51:16 -0800104 expect(&blk).to raise_error GRPC::Core::CallError
nnoble097ef9b2014-12-01 17:06:10 -0800105 end
106 end
107
108 describe '#writes_done' do
109 it 'should fail if called immediately' do
Tim Emiolae2860c52015-01-16 02:58:41 -0800110 blk = proc { make_test_call.writes_done(Object.new) }
temiola58327912014-12-15 17:51:16 -0800111 expect(&blk).to raise_error GRPC::Core::CallError
nnoble097ef9b2014-12-01 17:06:10 -0800112 end
113 end
114
115 describe '#add_metadata' do
116 it 'adds metadata to a call without fail' do
117 call = make_test_call
118 n = 37
Tim Emiolae2860c52015-01-16 02:58:41 -0800119 one_md = proc { |x| [sprintf('key%d', x), sprintf('value%d', x)] }
120 metadata = Hash[n.times.collect { |i| one_md.call i }]
nnoble097ef9b2014-12-01 17:06:10 -0800121 expect { call.add_metadata(metadata) }.to_not raise_error
122 end
123 end
124
125 describe '#start_invoke' do
126 it 'should cause the INVOKE_ACCEPTED event' do
127 call = make_test_call
128 expect(call.start_invoke(@client_queue, @tag, @tag, @tag)).to be_nil
129 ev = @client_queue.next(deadline)
nnoble0c475f02014-12-05 15:37:39 -0800130 expect(ev.call).to be_a(GRPC::Core::Call)
nnoble097ef9b2014-12-01 17:06:10 -0800131 expect(ev.tag).to be(@tag)
nnoble0c475f02014-12-05 15:37:39 -0800132 expect(ev.type).to be(GRPC::Core::CompletionType::INVOKE_ACCEPTED)
nnoble097ef9b2014-12-01 17:06:10 -0800133 expect(ev.call).to_not be(call)
134 end
135 end
136
137 describe '#start_write' do
138 it 'should cause the WRITE_ACCEPTED event' do
139 call = make_test_call
140 call.start_invoke(@client_queue, @tag, @tag, @tag)
141 ev = @client_queue.next(deadline)
nnoble0c475f02014-12-05 15:37:39 -0800142 expect(ev.type).to be(GRPC::Core::CompletionType::INVOKE_ACCEPTED)
143 expect(call.start_write(GRPC::Core::ByteBuffer.new('test_start_write'),
nnoble097ef9b2014-12-01 17:06:10 -0800144 @tag)).to be_nil
145 ev = @client_queue.next(deadline)
nnoble0c475f02014-12-05 15:37:39 -0800146 expect(ev.call).to be_a(GRPC::Core::Call)
147 expect(ev.type).to be(GRPC::Core::CompletionType::WRITE_ACCEPTED)
nnoble097ef9b2014-12-01 17:06:10 -0800148 expect(ev.tag).to be(@tag)
149 end
150 end
151
152 describe '#status' do
153 it 'can save the status and read it back' do
154 call = make_test_call
temiola58327912014-12-15 17:51:16 -0800155 sts = Struct::Status.new(OK, 'OK')
nnoble097ef9b2014-12-01 17:06:10 -0800156 expect { call.status = sts }.not_to raise_error
temiola58327912014-12-15 17:51:16 -0800157 expect(call.status).to eq(sts)
nnoble097ef9b2014-12-01 17:06:10 -0800158 end
159
160 it 'must be set to a status' do
161 call = make_test_call
162 bad_sts = Object.new
163 expect { call.status = bad_sts }.to raise_error(TypeError)
164 end
165
166 it 'can be set to nil' do
167 call = make_test_call
168 expect { call.status = nil }.not_to raise_error
169 end
170 end
171
172 describe '#metadata' do
173 it 'can save the metadata hash and read it back' do
174 call = make_test_call
Tim Emiolae2860c52015-01-16 02:58:41 -0800175 md = { 'k1' => 'v1', 'k2' => 'v2' }
nnoble097ef9b2014-12-01 17:06:10 -0800176 expect { call.metadata = md }.not_to raise_error
177 expect(call.metadata).to be(md)
178 end
179
180 it 'must be set with a hash' do
181 call = make_test_call
182 bad_md = Object.new
183 expect { call.metadata = bad_md }.to raise_error(TypeError)
184 end
185
186 it 'can be set to nil' do
187 call = make_test_call
188 expect { call.metadata = nil }.not_to raise_error
189 end
190 end
191
nnoble097ef9b2014-12-01 17:06:10 -0800192 def make_test_call
193 @ch.create_call('dummy_method', 'dummy_host', deadline)
194 end
195
196 def deadline
197 Time.now + 2 # in 2 seconds; arbitrary
198 end
nnoble097ef9b2014-12-01 17:06:10 -0800199end