blob: 1332b3c3e85a9853d3496fc4faf566bbb2a9888f [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
nnoble0c475f02014-12-05 15:37:39 -080091 expect { make_test_call.start_read(@tag) }.to raise_error GRPC::Core::CallError
nnoble097ef9b2014-12-01 17:06:10 -080092 end
93 end
94
95 describe '#start_write' do
96 it 'should fail if called immediately' do
nnoble0c475f02014-12-05 15:37:39 -080097 bytes = GRPC::Core::ByteBuffer.new('test string')
nnoble097ef9b2014-12-01 17:06:10 -080098 expect { make_test_call.start_write(bytes, @tag) }
nnoble0c475f02014-12-05 15:37:39 -080099 .to raise_error GRPC::Core::CallError
nnoble097ef9b2014-12-01 17:06:10 -0800100 end
101 end
102
103 describe '#start_write_status' do
104 it 'should fail if called immediately' do
nnoble0c475f02014-12-05 15:37:39 -0800105 sts = GRPC::Core::Status.new(153, 'test detail')
nnoble097ef9b2014-12-01 17:06:10 -0800106 expect { make_test_call.start_write_status(sts, @tag) }
nnoble0c475f02014-12-05 15:37:39 -0800107 .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
nnoble0c475f02014-12-05 15:37:39 -0800113 expect { make_test_call.writes_done(@tag) }.to raise_error GRPC::Core::CallError
nnoble097ef9b2014-12-01 17:06:10 -0800114 end
115 end
116
117 describe '#add_metadata' do
118 it 'adds metadata to a call without fail' do
119 call = make_test_call
120 n = 37
121 metadata = Hash[n.times.collect { |i| ["key%d" % i, "value%d" %i] } ]
122 expect { call.add_metadata(metadata) }.to_not raise_error
123 end
124 end
125
126 describe '#start_invoke' do
127 it 'should cause the INVOKE_ACCEPTED event' do
128 call = make_test_call
129 expect(call.start_invoke(@client_queue, @tag, @tag, @tag)).to be_nil
130 ev = @client_queue.next(deadline)
nnoble0c475f02014-12-05 15:37:39 -0800131 expect(ev.call).to be_a(GRPC::Core::Call)
nnoble097ef9b2014-12-01 17:06:10 -0800132 expect(ev.tag).to be(@tag)
nnoble0c475f02014-12-05 15:37:39 -0800133 expect(ev.type).to be(GRPC::Core::CompletionType::INVOKE_ACCEPTED)
nnoble097ef9b2014-12-01 17:06:10 -0800134 expect(ev.call).to_not be(call)
135 end
136 end
137
138 describe '#start_write' do
139 it 'should cause the WRITE_ACCEPTED event' do
140 call = make_test_call
141 call.start_invoke(@client_queue, @tag, @tag, @tag)
142 ev = @client_queue.next(deadline)
nnoble0c475f02014-12-05 15:37:39 -0800143 expect(ev.type).to be(GRPC::Core::CompletionType::INVOKE_ACCEPTED)
144 expect(call.start_write(GRPC::Core::ByteBuffer.new('test_start_write'),
nnoble097ef9b2014-12-01 17:06:10 -0800145 @tag)).to be_nil
146 ev = @client_queue.next(deadline)
nnoble0c475f02014-12-05 15:37:39 -0800147 expect(ev.call).to be_a(GRPC::Core::Call)
148 expect(ev.type).to be(GRPC::Core::CompletionType::WRITE_ACCEPTED)
nnoble097ef9b2014-12-01 17:06:10 -0800149 expect(ev.tag).to be(@tag)
150 end
151 end
152
153 describe '#status' do
154 it 'can save the status and read it back' do
155 call = make_test_call
nnoble0c475f02014-12-05 15:37:39 -0800156 sts = GRPC::Core::Status.new(OK, 'OK')
nnoble097ef9b2014-12-01 17:06:10 -0800157 expect { call.status = sts }.not_to raise_error
158 expect(call.status).to be(sts)
159 end
160
161 it 'must be set to a status' do
162 call = make_test_call
163 bad_sts = Object.new
164 expect { call.status = bad_sts }.to raise_error(TypeError)
165 end
166
167 it 'can be set to nil' do
168 call = make_test_call
169 expect { call.status = nil }.not_to raise_error
170 end
171 end
172
173 describe '#metadata' do
174 it 'can save the metadata hash and read it back' do
175 call = make_test_call
176 md = {'k1' => 'v1', 'k2' => 'v2'}
177 expect { call.metadata = md }.not_to raise_error
178 expect(call.metadata).to be(md)
179 end
180
181 it 'must be set with a hash' do
182 call = make_test_call
183 bad_md = Object.new
184 expect { call.metadata = bad_md }.to raise_error(TypeError)
185 end
186
187 it 'can be set to nil' do
188 call = make_test_call
189 expect { call.metadata = nil }.not_to raise_error
190 end
191 end
192
193
194 def make_test_call
195 @ch.create_call('dummy_method', 'dummy_host', deadline)
196 end
197
198 def deadline
199 Time.now + 2 # in 2 seconds; arbitrary
200 end
201
202end