blob: 3c5d33ffcdfc098dceaa55b788986c87d41bbe3b [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
Tim Emiola37b09f42015-03-27 13:39:16 -070069describe GRPC::Core::CallOps do
70 before(:each) do
71 @known_types = {
72 SEND_INITIAL_METADATA: 0,
73 SEND_MESSAGE: 1,
74 SEND_CLOSE_FROM_CLIENT: 2,
75 SEND_STATUS_FROM_SERVER: 3,
76 RECV_INITIAL_METADATA: 4,
77 RECV_MESSAGE: 5,
78 RECV_STATUS_ON_CLIENT: 6,
Tim Emiola98a32d32015-03-28 01:48:44 -070079 RECV_CLOSE_ON_SERVER: 7
Tim Emiola37b09f42015-03-27 13:39:16 -070080 }
81 end
82
83 it 'should have symbols for all the known operation types' do
84 m = GRPC::Core::CallOps
85 syms_and_codes = m.constants.collect { |c| [c, m.const_get(c)] }
86 expect(Hash[syms_and_codes]).to eq(@known_types)
87 end
88end
89
nnoble0c475f02014-12-05 15:37:39 -080090describe GRPC::Core::Call do
Tim Emiola98a32d32015-03-28 01:48:44 -070091 let(:client_queue) { GRPC::Core::CompletionQueue.new }
92 let(:test_tag) { Object.new }
93 let(:fake_host) { 'localhost:10101' }
Tim Emiola564719d2015-03-27 13:07:34 -070094
nnoble097ef9b2014-12-01 17:06:10 -080095 before(:each) do
Tim Emiolad02d1d52015-01-26 18:36:17 -080096 @ch = GRPC::Core::Channel.new(fake_host, nil)
nnoble097ef9b2014-12-01 17:06:10 -080097 end
98
nnoble097ef9b2014-12-01 17:06:10 -080099 describe '#status' do
100 it 'can save the status and read it back' do
101 call = make_test_call
temiola58327912014-12-15 17:51:16 -0800102 sts = Struct::Status.new(OK, 'OK')
nnoble097ef9b2014-12-01 17:06:10 -0800103 expect { call.status = sts }.not_to raise_error
temiola58327912014-12-15 17:51:16 -0800104 expect(call.status).to eq(sts)
nnoble097ef9b2014-12-01 17:06:10 -0800105 end
106
107 it 'must be set to a status' do
108 call = make_test_call
109 bad_sts = Object.new
110 expect { call.status = bad_sts }.to raise_error(TypeError)
111 end
112
113 it 'can be set to nil' do
114 call = make_test_call
115 expect { call.status = nil }.not_to raise_error
116 end
117 end
118
119 describe '#metadata' do
120 it 'can save the metadata hash and read it back' do
121 call = make_test_call
Tim Emiolae2860c52015-01-16 02:58:41 -0800122 md = { 'k1' => 'v1', 'k2' => 'v2' }
nnoble097ef9b2014-12-01 17:06:10 -0800123 expect { call.metadata = md }.not_to raise_error
124 expect(call.metadata).to be(md)
125 end
126
127 it 'must be set with a hash' do
128 call = make_test_call
129 bad_md = Object.new
130 expect { call.metadata = bad_md }.to raise_error(TypeError)
131 end
132
133 it 'can be set to nil' do
134 call = make_test_call
135 expect { call.metadata = nil }.not_to raise_error
136 end
137 end
138
nnoble097ef9b2014-12-01 17:06:10 -0800139 def make_test_call
Tim Emiolafde3dbf2015-08-11 16:35:41 -0700140 @ch.create_call(client_queue, nil, nil, 'dummy_method', nil, deadline)
nnoble097ef9b2014-12-01 17:06:10 -0800141 end
142
143 def deadline
144 Time.now + 2 # in 2 seconds; arbitrary
145 end
Craig Tiller190d3602015-02-18 09:23:38 -0800146end