blob: 2a47eac23a3cf231564839917314ec3031132be3 [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'
nnoble097ef9b2014-12-01 17:06:10 -080031
nnoble0c475f02014-12-05 15:37:39 -080032include GRPC::Core::StatusCodes
nnoble097ef9b2014-12-01 17:06:10 -080033
nnoble0c475f02014-12-05 15:37:39 -080034describe GRPC::Core::RpcErrors do
nnoble097ef9b2014-12-01 17:06:10 -080035 before(:each) do
36 @known_types = {
Tim Emiolae2860c52015-01-16 02:58:41 -080037 OK: 0,
38 ERROR: 1,
39 NOT_ON_SERVER: 2,
40 NOT_ON_CLIENT: 3,
41 ALREADY_ACCEPTED: 4,
42 ALREADY_INVOKED: 5,
43 NOT_INVOKED: 6,
44 ALREADY_FINISHED: 7,
45 TOO_MANY_OPERATIONS: 8,
46 INVALID_FLAGS: 9,
47 ErrorMessages: {
48 0 => 'ok',
49 1 => 'unknown error',
50 2 => 'not available on a server',
51 3 => 'not available on a client',
52 4 => 'call is already accepted',
53 5 => 'call is already invoked',
54 6 => 'call is not yet invoked',
55 7 => 'call is already finished',
56 8 => 'outstanding read or write present',
57 9 => 'a bad flag was given'
nnoble097ef9b2014-12-01 17:06:10 -080058 }
59 }
60 end
61
62 it 'should have symbols for all the known error codes' do
nnoble0c475f02014-12-05 15:37:39 -080063 m = GRPC::Core::RpcErrors
nnoble097ef9b2014-12-01 17:06:10 -080064 syms_and_codes = m.constants.collect { |c| [c, m.const_get(c)] }
65 expect(Hash[syms_and_codes]).to eq(@known_types)
66 end
nnoble097ef9b2014-12-01 17:06:10 -080067end
68
nnoble0c475f02014-12-05 15:37:39 -080069describe GRPC::Core::Call do
nnoble097ef9b2014-12-01 17:06:10 -080070 before(:each) do
71 @tag = Object.new
nnoble0c475f02014-12-05 15:37:39 -080072 @client_queue = GRPC::Core::CompletionQueue.new
Tim Emiolad02d1d52015-01-26 18:36:17 -080073 fake_host = 'localhost:10101'
74 @ch = GRPC::Core::Channel.new(fake_host, nil)
nnoble097ef9b2014-12-01 17:06:10 -080075 end
76
77 describe '#start_read' do
Tim Emiolab24054a2015-02-09 12:34:16 -080078 xit 'should fail if called immediately' do
Tim Emiolae2860c52015-01-16 02:58:41 -080079 blk = proc { make_test_call.start_read(@tag) }
temiola58327912014-12-15 17:51:16 -080080 expect(&blk).to raise_error GRPC::Core::CallError
nnoble097ef9b2014-12-01 17:06:10 -080081 end
82 end
83
84 describe '#start_write' do
Tim Emiolab24054a2015-02-09 12:34:16 -080085 xit 'should fail if called immediately' do
nnoble0c475f02014-12-05 15:37:39 -080086 bytes = GRPC::Core::ByteBuffer.new('test string')
Tim Emiolae2860c52015-01-16 02:58:41 -080087 blk = proc { make_test_call.start_write(bytes, @tag) }
temiola58327912014-12-15 17:51:16 -080088 expect(&blk).to raise_error GRPC::Core::CallError
nnoble097ef9b2014-12-01 17:06:10 -080089 end
90 end
91
92 describe '#start_write_status' do
Tim Emiolab24054a2015-02-09 12:34:16 -080093 xit 'should fail if called immediately' do
Tim Emiolae2860c52015-01-16 02:58:41 -080094 blk = proc { make_test_call.start_write_status(153, 'x', @tag) }
temiola58327912014-12-15 17:51:16 -080095 expect(&blk).to raise_error GRPC::Core::CallError
nnoble097ef9b2014-12-01 17:06:10 -080096 end
97 end
98
99 describe '#writes_done' do
Tim Emiolab24054a2015-02-09 12:34:16 -0800100 xit 'should fail if called immediately' do
Tim Emiolae2860c52015-01-16 02:58:41 -0800101 blk = proc { make_test_call.writes_done(Object.new) }
temiola58327912014-12-15 17:51:16 -0800102 expect(&blk).to raise_error GRPC::Core::CallError
nnoble097ef9b2014-12-01 17:06:10 -0800103 end
104 end
105
106 describe '#add_metadata' do
107 it 'adds metadata to a call without fail' do
108 call = make_test_call
109 n = 37
Tim Emiolae2860c52015-01-16 02:58:41 -0800110 one_md = proc { |x| [sprintf('key%d', x), sprintf('value%d', x)] }
111 metadata = Hash[n.times.collect { |i| one_md.call i }]
nnoble097ef9b2014-12-01 17:06:10 -0800112 expect { call.add_metadata(metadata) }.to_not raise_error
113 end
114 end
115
nnoble097ef9b2014-12-01 17:06:10 -0800116 describe '#status' do
117 it 'can save the status and read it back' do
118 call = make_test_call
temiola58327912014-12-15 17:51:16 -0800119 sts = Struct::Status.new(OK, 'OK')
nnoble097ef9b2014-12-01 17:06:10 -0800120 expect { call.status = sts }.not_to raise_error
temiola58327912014-12-15 17:51:16 -0800121 expect(call.status).to eq(sts)
nnoble097ef9b2014-12-01 17:06:10 -0800122 end
123
124 it 'must be set to a status' do
125 call = make_test_call
126 bad_sts = Object.new
127 expect { call.status = bad_sts }.to raise_error(TypeError)
128 end
129
130 it 'can be set to nil' do
131 call = make_test_call
132 expect { call.status = nil }.not_to raise_error
133 end
134 end
135
136 describe '#metadata' do
137 it 'can save the metadata hash and read it back' do
138 call = make_test_call
Tim Emiolae2860c52015-01-16 02:58:41 -0800139 md = { 'k1' => 'v1', 'k2' => 'v2' }
nnoble097ef9b2014-12-01 17:06:10 -0800140 expect { call.metadata = md }.not_to raise_error
141 expect(call.metadata).to be(md)
142 end
143
144 it 'must be set with a hash' do
145 call = make_test_call
146 bad_md = Object.new
147 expect { call.metadata = bad_md }.to raise_error(TypeError)
148 end
149
150 it 'can be set to nil' do
151 call = make_test_call
152 expect { call.metadata = nil }.not_to raise_error
153 end
154 end
155
nnoble097ef9b2014-12-01 17:06:10 -0800156 def make_test_call
157 @ch.create_call('dummy_method', 'dummy_host', deadline)
158 end
159
160 def deadline
161 Time.now + 2 # in 2 seconds; arbitrary
162 end
Craig Tiller06059952015-02-18 08:34:56 -0800163end