blob: 8e8e3d3ae2f48889c98064002727e61158a930cc [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
37 before(:each) do
38 @known_types = {
39 :OK => 0,
40 :ERROR => 1,
41 :NOT_ON_SERVER => 2,
42 :NOT_ON_CLIENT => 3,
temiola71bb1372014-12-11 11:27:25 -080043 :ALREADY_ACCEPTED => 4,
44 :ALREADY_INVOKED => 5,
45 :NOT_INVOKED => 6,
46 :ALREADY_FINISHED => 7,
47 :TOO_MANY_OPERATIONS => 8,
48 :INVALID_FLAGS => 9,
nnoble097ef9b2014-12-01 17:06:10 -080049 :ErrorMessages => {
50 0=>'ok',
51 1=>'unknown error',
52 2=>'not available on a server',
53 3=>'not available on a client',
temiola71bb1372014-12-11 11:27:25 -080054 4=>'call is already accepted',
55 5=>'call is already invoked',
56 6=>'call is not yet invoked',
57 7=>'call is already finished',
58 8=>'outstanding read or write present',
59 9=>'a bad flag was given',
nnoble097ef9b2014-12-01 17:06:10 -080060 }
61 }
62 end
63
64 it 'should have symbols for all the known error codes' do
nnoble0c475f02014-12-05 15:37:39 -080065 m = GRPC::Core::RpcErrors
nnoble097ef9b2014-12-01 17:06:10 -080066 syms_and_codes = m.constants.collect { |c| [c, m.const_get(c)] }
67 expect(Hash[syms_and_codes]).to eq(@known_types)
68 end
69
70end
71
nnoble0c475f02014-12-05 15:37:39 -080072describe GRPC::Core::Call do
nnoble097ef9b2014-12-01 17:06:10 -080073
74 before(:each) do
75 @tag = Object.new
nnoble0c475f02014-12-05 15:37:39 -080076 @client_queue = GRPC::Core::CompletionQueue.new
77 @server_queue = GRPC::Core::CompletionQueue.new
nnoble097ef9b2014-12-01 17:06:10 -080078 port = find_unused_tcp_port
79 host = "localhost:#{port}"
nnoble0c475f02014-12-05 15:37:39 -080080 @server = GRPC::Core::Server.new(@server_queue, nil)
nnoble097ef9b2014-12-01 17:06:10 -080081 @server.add_http2_port(host)
nnoble0c475f02014-12-05 15:37:39 -080082 @ch = GRPC::Core::Channel.new(host, nil)
nnoble097ef9b2014-12-01 17:06:10 -080083 end
84
85 after(:each) do
86 @server.close
87 end
88
89 describe '#start_read' do
90 it 'should fail if called immediately' do
temiola58327912014-12-15 17:51:16 -080091 blk = Proc.new { make_test_call.start_read(@tag) }
92 expect(&blk).to raise_error GRPC::Core::CallError
nnoble097ef9b2014-12-01 17:06:10 -080093 end
94 end
95
96 describe '#start_write' do
97 it 'should fail if called immediately' do
nnoble0c475f02014-12-05 15:37:39 -080098 bytes = GRPC::Core::ByteBuffer.new('test string')
temiola58327912014-12-15 17:51:16 -080099 blk = Proc.new { make_test_call.start_write(bytes, @tag) }
100 expect(&blk).to raise_error GRPC::Core::CallError
nnoble097ef9b2014-12-01 17:06:10 -0800101 end
102 end
103
104 describe '#start_write_status' do
105 it 'should fail if called immediately' do
temiola58327912014-12-15 17:51:16 -0800106 blk = Proc.new { make_test_call.start_write_status(153, 'x', @tag) }
107 expect(&blk).to raise_error GRPC::Core::CallError
nnoble097ef9b2014-12-01 17:06:10 -0800108 end
109 end
110
111 describe '#writes_done' do
112 it 'should fail if called immediately' do
temiola58327912014-12-15 17:51:16 -0800113 blk = Proc.new { make_test_call.writes_done(Object.new) }
114 expect(&blk).to raise_error GRPC::Core::CallError
nnoble097ef9b2014-12-01 17:06:10 -0800115 end
116 end
117
118 describe '#add_metadata' do
119 it 'adds metadata to a call without fail' do
120 call = make_test_call
121 n = 37
122 metadata = Hash[n.times.collect { |i| ["key%d" % i, "value%d" %i] } ]
123 expect { call.add_metadata(metadata) }.to_not raise_error
124 end
125 end
126
127 describe '#start_invoke' do
128 it 'should cause the INVOKE_ACCEPTED event' do
129 call = make_test_call
130 expect(call.start_invoke(@client_queue, @tag, @tag, @tag)).to be_nil
131 ev = @client_queue.next(deadline)
nnoble0c475f02014-12-05 15:37:39 -0800132 expect(ev.call).to be_a(GRPC::Core::Call)
nnoble097ef9b2014-12-01 17:06:10 -0800133 expect(ev.tag).to be(@tag)
nnoble0c475f02014-12-05 15:37:39 -0800134 expect(ev.type).to be(GRPC::Core::CompletionType::INVOKE_ACCEPTED)
nnoble097ef9b2014-12-01 17:06:10 -0800135 expect(ev.call).to_not be(call)
136 end
137 end
138
139 describe '#start_write' do
140 it 'should cause the WRITE_ACCEPTED event' do
141 call = make_test_call
142 call.start_invoke(@client_queue, @tag, @tag, @tag)
143 ev = @client_queue.next(deadline)
nnoble0c475f02014-12-05 15:37:39 -0800144 expect(ev.type).to be(GRPC::Core::CompletionType::INVOKE_ACCEPTED)
145 expect(call.start_write(GRPC::Core::ByteBuffer.new('test_start_write'),
nnoble097ef9b2014-12-01 17:06:10 -0800146 @tag)).to be_nil
147 ev = @client_queue.next(deadline)
nnoble0c475f02014-12-05 15:37:39 -0800148 expect(ev.call).to be_a(GRPC::Core::Call)
149 expect(ev.type).to be(GRPC::Core::CompletionType::WRITE_ACCEPTED)
nnoble097ef9b2014-12-01 17:06:10 -0800150 expect(ev.tag).to be(@tag)
151 end
152 end
153
154 describe '#status' do
155 it 'can save the status and read it back' do
156 call = make_test_call
temiola58327912014-12-15 17:51:16 -0800157 sts = Struct::Status.new(OK, 'OK')
nnoble097ef9b2014-12-01 17:06:10 -0800158 expect { call.status = sts }.not_to raise_error
temiola58327912014-12-15 17:51:16 -0800159 expect(call.status).to eq(sts)
nnoble097ef9b2014-12-01 17:06:10 -0800160 end
161
162 it 'must be set to a status' do
163 call = make_test_call
164 bad_sts = Object.new
165 expect { call.status = bad_sts }.to raise_error(TypeError)
166 end
167
168 it 'can be set to nil' do
169 call = make_test_call
170 expect { call.status = nil }.not_to raise_error
171 end
172 end
173
174 describe '#metadata' do
175 it 'can save the metadata hash and read it back' do
176 call = make_test_call
177 md = {'k1' => 'v1', 'k2' => 'v2'}
178 expect { call.metadata = md }.not_to raise_error
179 expect(call.metadata).to be(md)
180 end
181
182 it 'must be set with a hash' do
183 call = make_test_call
184 bad_md = Object.new
185 expect { call.metadata = bad_md }.to raise_error(TypeError)
186 end
187
188 it 'can be set to nil' do
189 call = make_test_call
190 expect { call.metadata = nil }.not_to raise_error
191 end
192 end
193
194
195 def make_test_call
196 @ch.create_call('dummy_method', 'dummy_host', deadline)
197 end
198
199 def deadline
200 Time.now + 2 # in 2 seconds; arbitrary
201 end
202
203end