Debian Codegraph @

src/ruby/spec/call_spec.rb

raw
1 # Copyright 2015 gRPC authors.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 require 'spec_helper'
16
17 include GRPC::Core::StatusCodes
18
19 describe GRPC::Core::WriteFlags do
20 it 'should define the known write flag values' do
21 m = GRPC::Core::WriteFlags
22 expect(m.const_get(:BUFFER_HINT)).to_not be_nil
23 expect(m.const_get(:NO_COMPRESS)).to_not be_nil
24 end
25 end
26
27 describe GRPC::Core::RpcErrors do
28 before(:each) do
29 @known_types = {
30 OK: 0,
31 ERROR: 1,
32 NOT_ON_SERVER: 2,
33 NOT_ON_CLIENT: 3,
34 ALREADY_ACCEPTED: 4,
35 ALREADY_INVOKED: 5,
36 NOT_INVOKED: 6,
37 ALREADY_FINISHED: 7,
38 TOO_MANY_OPERATIONS: 8,
39 INVALID_FLAGS: 9,
40 ErrorMessages: {
41 0 => 'ok',
42 1 => 'unknown error',
43 2 => 'not available on a server',
44 3 => 'not available on a client',
45 4 => 'call is already accepted',
46 5 => 'call is already invoked',
47 6 => 'call is not yet invoked',
48 7 => 'call is already finished',
49 8 => 'outstanding read or write present',
50 9 => 'a bad flag was given'
51 }
52 }
53 end
54
55 it 'should have symbols for all the known error codes' do
56 m = GRPC::Core::RpcErrors
57 syms_and_codes = m.constants.collect { |c| [c, m.const_get(c)] }
58 expect(Hash[syms_and_codes]).to eq(@known_types)
59 end
60 end
61
62 describe GRPC::Core::CallOps do
63 before(:each) do
64 @known_types = {
65 SEND_INITIAL_METADATA: 0,
66 SEND_MESSAGE: 1,
67 SEND_CLOSE_FROM_CLIENT: 2,
68 SEND_STATUS_FROM_SERVER: 3,
69 RECV_INITIAL_METADATA: 4,
70 RECV_MESSAGE: 5,
71 RECV_STATUS_ON_CLIENT: 6,
72 RECV_CLOSE_ON_SERVER: 7
73 }
74 end
75
76 it 'should have symbols for all the known operation types' do
77 m = GRPC::Core::CallOps
78 syms_and_codes = m.constants.collect { |c| [c, m.const_get(c)] }
79 expect(Hash[syms_and_codes]).to eq(@known_types)
80 end
81 end
82
83 describe GRPC::Core::Call do
84 let(:test_tag) { Object.new }
85 let(:fake_host) { 'localhost:10101' }
86
87 before(:each) do
88 @ch = GRPC::Core::Channel.new(fake_host, nil, :this_channel_is_insecure)
89 end
90
91 describe '#status' do
92 it 'can save the status and read it back' do
93 call = make_test_call
94 sts = Struct::Status.new(OK, 'OK')
95 expect { call.status = sts }.not_to raise_error
96 expect(call.status).to eq(sts)
97 end
98
99 it 'must be set to a status' do
100 call = make_test_call
101 bad_sts = Object.new
102 expect { call.status = bad_sts }.to raise_error(TypeError)
103 end
104
105 it 'can be set to nil' do
106 call = make_test_call
107 expect { call.status = nil }.not_to raise_error
108 end
109 end
110
111 describe '#metadata' do
112 it 'can save the metadata hash and read it back' do
113 call = make_test_call
114 md = { 'k1' => 'v1', 'k2' => 'v2' }
115 expect { call.metadata = md }.not_to raise_error
116 expect(call.metadata).to be(md)
117 end
118
119 it 'must be set with a hash' do
120 call = make_test_call
121 bad_md = Object.new
122 expect { call.metadata = bad_md }.to raise_error(TypeError)
123 end
124
125 it 'can be set to nil' do
126 call = make_test_call
127 expect { call.metadata = nil }.not_to raise_error
128 end
129 end
130
131 describe '#set_credentials!' do
132 it 'can set a valid CallCredentials object' do
133 call = make_test_call
134 auth_proc = proc { { 'plugin_key' => 'plugin_value' } }
135 creds = GRPC::Core::CallCredentials.new auth_proc
136 expect { call.set_credentials! creds }.not_to raise_error
137 end
138 end
139
140 describe '#cancel' do
141 it 'completes ok' do
142 call = make_test_call
143 expect { call.cancel }.not_to raise_error
144 end
145
146 it 'completes ok when the call is closed' do
147 call = make_test_call
148 call.close
149 expect { call.cancel }.not_to raise_error
150 end
151 end
152
153 describe '#cancel_with_status' do
154 it 'completes ok' do
155 call = make_test_call
156 expect do
157 call.cancel_with_status(0, 'test status')
158 end.not_to raise_error
159 expect do
160 call.cancel_with_status(0, nil)
161 end.to raise_error(TypeError)
162 end
163
164 it 'completes ok when the call is closed' do
165 call = make_test_call
166 call.close
167 expect do
168 call.cancel_with_status(0, 'test status')
169 end.not_to raise_error
170 end
171 end
172
173 def make_test_call
174 @ch.create_call(nil, nil, 'phony_method', nil, deadline)
175 end
176
177 def deadline
178 Time.now + 2 # in 2 seconds; arbitrary
179 end
180 end