grpc 1.66.0-x86_64-darwin → 1.67.0-x86_64-darwin

Sign up to get free protection for your applications and to get access to all the features.
@@ -16,36 +16,8 @@ require 'spec_helper'
16
16
 
17
17
  include GRPC::Core
18
18
 
19
- shared_context 'setup: tags' do
20
- let(:sent_message) { 'sent message' }
21
- let(:reply_text) { 'the reply' }
22
-
23
- def deadline
24
- Time.now + 5
25
- end
26
-
27
- def server_allows_client_to_proceed(metadata = {})
28
- recvd_rpc = @server.request_call
29
- expect(recvd_rpc).to_not eq nil
30
- server_call = recvd_rpc.call
31
- ops = { CallOps::SEND_INITIAL_METADATA => metadata }
32
- server_batch = server_call.run_batch(ops)
33
- expect(server_batch.send_metadata).to be true
34
- server_call
35
- end
36
-
37
- def new_client_call
38
- @ch.create_call(nil, nil, '/method', nil, deadline)
39
- end
40
-
41
- def ok_status
42
- Struct::Status.new(StatusCodes::OK, 'OK')
43
- end
44
- end
45
-
46
19
  shared_examples 'basic GRPC message delivery is OK' do
47
20
  include GRPC::Core
48
- include_context 'setup: tags'
49
21
 
50
22
  context 'the test channel' do
51
23
  it 'should have a target' do
@@ -53,272 +25,45 @@ shared_examples 'basic GRPC message delivery is OK' do
53
25
  end
54
26
  end
55
27
 
56
- context 'a client call' do
57
- it 'should have a peer' do
58
- expect(new_client_call.peer).to be_a(String)
59
- end
60
- end
61
-
62
- it 'calls have peer info' do
63
- call = new_client_call
64
- expect(call.peer).to be_a(String)
65
- end
66
-
67
- it 'servers receive requests from clients and can respond' do
68
- call = new_client_call
69
- server_call = nil
70
-
71
- server_thread = Thread.new do
72
- server_call = server_allows_client_to_proceed
73
- end
74
-
75
- client_ops = {
76
- CallOps::SEND_INITIAL_METADATA => {},
77
- CallOps::SEND_MESSAGE => sent_message,
78
- CallOps::SEND_CLOSE_FROM_CLIENT => nil
79
- }
80
- client_batch = call.run_batch(client_ops)
81
- expect(client_batch.send_metadata).to be true
82
- expect(client_batch.send_message).to be true
83
- expect(client_batch.send_close).to be true
84
-
85
- # confirm the server can read the inbound message
86
- server_thread.join
87
- server_ops = {
88
- CallOps::RECV_MESSAGE => nil
89
- }
90
- server_batch = server_call.run_batch(server_ops)
91
- expect(server_batch.message).to eq(sent_message)
92
- server_ops = {
93
- CallOps::RECV_CLOSE_ON_SERVER => nil,
94
- CallOps::SEND_STATUS_FROM_SERVER => ok_status
95
- }
96
- server_batch = server_call.run_batch(server_ops)
97
- expect(server_batch.send_close).to be true
98
- expect(server_batch.send_status).to be true
99
-
100
- # finish the call
101
- final_client_batch = call.run_batch(
102
- CallOps::RECV_INITIAL_METADATA => nil,
103
- CallOps::RECV_STATUS_ON_CLIENT => nil)
104
- expect(final_client_batch.metadata).to eq({})
105
- expect(final_client_batch.status.code).to eq(0)
106
- end
107
-
108
- it 'responses written by servers are received by the client' do
109
- call = new_client_call
110
- server_call = nil
111
-
112
- server_thread = Thread.new do
113
- server_call = server_allows_client_to_proceed
114
- end
115
-
116
- client_ops = {
117
- CallOps::SEND_INITIAL_METADATA => {},
118
- CallOps::SEND_MESSAGE => sent_message,
119
- CallOps::SEND_CLOSE_FROM_CLIENT => nil
120
- }
121
- client_batch = call.run_batch(client_ops)
122
- expect(client_batch.send_metadata).to be true
123
- expect(client_batch.send_message).to be true
124
- expect(client_batch.send_close).to be true
125
-
126
- # confirm the server can read the inbound message
127
- server_thread.join
128
- server_ops = {
129
- CallOps::RECV_MESSAGE => nil
130
- }
131
- server_batch = server_call.run_batch(server_ops)
132
- expect(server_batch.message).to eq(sent_message)
133
- server_ops = {
134
- CallOps::RECV_CLOSE_ON_SERVER => nil,
135
- CallOps::SEND_MESSAGE => reply_text,
136
- CallOps::SEND_STATUS_FROM_SERVER => ok_status
137
- }
138
- server_batch = server_call.run_batch(server_ops)
139
- expect(server_batch.send_close).to be true
140
- expect(server_batch.send_message).to be true
141
- expect(server_batch.send_status).to be true
142
-
143
- # finish the call
144
- final_client_batch = call.run_batch(
145
- CallOps::RECV_INITIAL_METADATA => nil,
146
- CallOps::RECV_MESSAGE => nil,
147
- CallOps::RECV_STATUS_ON_CLIENT => nil)
148
- expect(final_client_batch.metadata).to eq({})
149
- expect(final_client_batch.message).to eq(reply_text)
150
- expect(final_client_batch.status.code).to eq(0)
151
- end
152
-
153
- it 'compressed messages can be sent and received' do
154
- call = new_client_call
155
- server_call = nil
156
- long_request_str = '0' * 2000
157
- long_response_str = '1' * 2000
158
- md = { 'grpc-internal-encoding-request' => 'gzip' }
159
-
160
- server_thread = Thread.new do
161
- server_call = server_allows_client_to_proceed(md)
28
+ it 'unary calls work' do
29
+ run_services_on_server(@server, services: [EchoService]) do
30
+ call = @stub.an_rpc(EchoMsg.new, return_op: true)
31
+ expect(call.execute).to be_a(EchoMsg)
162
32
  end
163
-
164
- client_ops = {
165
- CallOps::SEND_INITIAL_METADATA => md,
166
- CallOps::SEND_MESSAGE => long_request_str,
167
- CallOps::SEND_CLOSE_FROM_CLIENT => nil
168
- }
169
- client_batch = call.run_batch(client_ops)
170
- expect(client_batch.send_metadata).to be true
171
- expect(client_batch.send_message).to be true
172
- expect(client_batch.send_close).to be true
173
-
174
- # confirm the server can read the inbound message
175
- server_thread.join
176
- server_ops = {
177
- CallOps::RECV_MESSAGE => nil
178
- }
179
- server_batch = server_call.run_batch(server_ops)
180
- expect(server_batch.message).to eq(long_request_str)
181
- server_ops = {
182
- CallOps::RECV_CLOSE_ON_SERVER => nil,
183
- CallOps::SEND_MESSAGE => long_response_str,
184
- CallOps::SEND_STATUS_FROM_SERVER => ok_status
185
- }
186
- server_batch = server_call.run_batch(server_ops)
187
- expect(server_batch.send_close).to be true
188
- expect(server_batch.send_message).to be true
189
- expect(server_batch.send_status).to be true
190
-
191
- client_ops = {
192
- CallOps::RECV_INITIAL_METADATA => nil,
193
- CallOps::RECV_MESSAGE => nil,
194
- CallOps::RECV_STATUS_ON_CLIENT => nil
195
- }
196
- final_client_batch = call.run_batch(client_ops)
197
- expect(final_client_batch.metadata).to eq({})
198
- expect(final_client_batch.message).to eq long_response_str
199
- expect(final_client_batch.status.code).to eq(0)
200
33
  end
201
34
 
202
- it 'servers can ignore a client write and send a status' do
203
- call = new_client_call
204
- server_call = nil
205
-
206
- server_thread = Thread.new do
207
- server_call = server_allows_client_to_proceed
35
+ it 'unary calls work when enabling compression' do
36
+ run_services_on_server(@server, services: [EchoService]) do
37
+ long_request_str = '0' * 2000
38
+ md = { 'grpc-internal-encoding-request' => 'gzip' }
39
+ call = @stub.an_rpc(EchoMsg.new(msg: long_request_str),
40
+ return_op: true,
41
+ metadata: md)
42
+ response = call.execute
43
+ expect(response).to be_a(EchoMsg)
44
+ expect(response.msg).to eq(long_request_str)
208
45
  end
209
-
210
- client_ops = {
211
- CallOps::SEND_INITIAL_METADATA => {},
212
- CallOps::SEND_MESSAGE => sent_message,
213
- CallOps::SEND_CLOSE_FROM_CLIENT => nil
214
- }
215
- client_batch = call.run_batch(client_ops)
216
- expect(client_batch.send_metadata).to be true
217
- expect(client_batch.send_message).to be true
218
- expect(client_batch.send_close).to be true
219
-
220
- # confirm the server can read the inbound message
221
- the_status = Struct::Status.new(StatusCodes::OK, 'OK')
222
- server_thread.join
223
- server_ops = {
224
- CallOps::SEND_STATUS_FROM_SERVER => the_status
225
- }
226
- server_batch = server_call.run_batch(server_ops)
227
- expect(server_batch.message).to eq nil
228
- expect(server_batch.send_status).to be true
229
-
230
- final_client_batch = call.run_batch(
231
- CallOps::RECV_INITIAL_METADATA => nil,
232
- CallOps::RECV_STATUS_ON_CLIENT => nil)
233
- expect(final_client_batch.metadata).to eq({})
234
- expect(final_client_batch.status.code).to eq(0)
235
- end
236
-
237
- it 'completes calls by sending status to client and server' do
238
- call = new_client_call
239
- server_call = nil
240
-
241
- server_thread = Thread.new do
242
- server_call = server_allows_client_to_proceed
243
- end
244
-
245
- client_ops = {
246
- CallOps::SEND_INITIAL_METADATA => {},
247
- CallOps::SEND_MESSAGE => sent_message
248
- }
249
- client_batch = call.run_batch(client_ops)
250
- expect(client_batch.send_metadata).to be true
251
- expect(client_batch.send_message).to be true
252
-
253
- # confirm the server can read the inbound message and respond
254
- the_status = Struct::Status.new(StatusCodes::OK, 'OK', {})
255
- server_thread.join
256
- server_ops = {
257
- CallOps::RECV_MESSAGE => nil
258
- }
259
- server_batch = server_call.run_batch(server_ops)
260
- expect(server_batch.message).to eq sent_message
261
- server_ops = {
262
- CallOps::SEND_MESSAGE => reply_text,
263
- CallOps::SEND_STATUS_FROM_SERVER => the_status
264
- }
265
- server_batch = server_call.run_batch(server_ops)
266
- expect(server_batch.send_status).to be true
267
- expect(server_batch.send_message).to be true
268
-
269
- # confirm the client can receive the server response and status.
270
- client_ops = {
271
- CallOps::SEND_CLOSE_FROM_CLIENT => nil,
272
- CallOps::RECV_INITIAL_METADATA => nil,
273
- CallOps::RECV_MESSAGE => nil,
274
- CallOps::RECV_STATUS_ON_CLIENT => nil
275
- }
276
- final_client_batch = call.run_batch(client_ops)
277
- expect(final_client_batch.send_close).to be true
278
- expect(final_client_batch.message).to eq reply_text
279
- expect(final_client_batch.status).to eq the_status
280
-
281
- # confirm the server can receive the client close.
282
- server_ops = {
283
- CallOps::RECV_CLOSE_ON_SERVER => nil
284
- }
285
- final_server_batch = server_call.run_batch(server_ops)
286
- expect(final_server_batch.send_close).to be true
287
46
  end
288
47
 
289
48
  def client_cancel_test(cancel_proc, expected_code,
290
49
  expected_details)
291
- call = new_client_call
292
- server_call = nil
293
-
294
- server_thread = Thread.new do
295
- server_call = server_allows_client_to_proceed
50
+ call = @stub.an_rpc(EchoMsg.new, return_op: true)
51
+ run_services_on_server(@server, services: [EchoService]) do
52
+ # start the call, but don't send a message yet
53
+ call.start_call
54
+ # cancel the call
55
+ cancel_proc.call(call)
56
+ # check the client's status
57
+ failed = false
58
+ begin
59
+ call.execute
60
+ rescue GRPC::BadStatus => e
61
+ failed = true
62
+ expect(e.code).to be expected_code
63
+ expect(e.details).to eq expected_details
64
+ end
65
+ expect(failed).to be(true)
296
66
  end
297
-
298
- client_ops = {
299
- CallOps::SEND_INITIAL_METADATA => {},
300
- CallOps::RECV_INITIAL_METADATA => nil
301
- }
302
- client_batch = call.run_batch(client_ops)
303
- expect(client_batch.send_metadata).to be true
304
- expect(client_batch.metadata).to eq({})
305
-
306
- cancel_proc.call(call)
307
-
308
- server_thread.join
309
- server_ops = {
310
- CallOps::RECV_CLOSE_ON_SERVER => nil
311
- }
312
- server_batch = server_call.run_batch(server_ops)
313
- expect(server_batch.send_close).to be true
314
-
315
- client_ops = {
316
- CallOps::RECV_STATUS_ON_CLIENT => {}
317
- }
318
- client_batch = call.run_batch(client_ops)
319
-
320
- expect(client_batch.status.code).to be expected_code
321
- expect(client_batch.status.details).to eq expected_details
322
67
  end
323
68
 
324
69
  it 'clients can cancel a call on the server' do
@@ -344,8 +89,6 @@ shared_examples 'basic GRPC message delivery is OK' do
344
89
  end
345
90
 
346
91
  shared_examples 'GRPC metadata delivery works OK' do
347
- include_context 'setup: tags'
348
-
349
92
  describe 'from client => server' do
350
93
  before(:example) do
351
94
  n = 7 # arbitrary number of metadata
@@ -364,53 +107,31 @@ shared_examples 'GRPC metadata delivery works OK' do
364
107
 
365
108
  it 'raises an exception if a metadata key is invalid' do
366
109
  @bad_keys.each do |md|
367
- call = new_client_call
368
- client_ops = {
369
- CallOps::SEND_INITIAL_METADATA => md
370
- }
371
- blk = proc do
372
- call.run_batch(client_ops)
110
+ # NOTE: no need to run a server in this test b/c the failure
111
+ # happens while validating metadata to send.
112
+ failed = false
113
+ begin
114
+ @stub.an_rpc(EchoMsg.new, metadata: md)
115
+ rescue TypeError => e
116
+ failed = true
117
+ expect(e.message).to eq('grpc_rb_md_ary_fill_hash_cb: bad type for key parameter')
373
118
  end
374
- expect(&blk).to raise_error
119
+ expect(failed).to be(true)
375
120
  end
376
121
  end
377
122
 
378
123
  it 'sends all the metadata pairs when keys and values are valid' do
379
- @valid_metadata.each do |md|
380
- recvd_rpc = nil
381
- rcv_thread = Thread.new do
382
- recvd_rpc = @server.request_call
124
+ service = EchoService.new
125
+ run_services_on_server(@server, services: [service]) do
126
+ @valid_metadata.each_with_index do |md, i|
127
+ expect(@stub.an_rpc(EchoMsg.new, metadata: md)).to be_a(EchoMsg)
128
+ # confirm the server can receive the client metadata
129
+ # finish the call
130
+ expect(service.received_md.length).to eq(i + 1)
131
+ md.each do |k, v|
132
+ expect(service.received_md[i][k.to_s]).to eq(v)
133
+ end
383
134
  end
384
-
385
- call = new_client_call
386
- client_ops = {
387
- CallOps::SEND_INITIAL_METADATA => md,
388
- CallOps::SEND_CLOSE_FROM_CLIENT => nil
389
- }
390
- client_batch = call.run_batch(client_ops)
391
- expect(client_batch.send_metadata).to be true
392
-
393
- # confirm the server can receive the client metadata
394
- rcv_thread.join
395
- expect(recvd_rpc).to_not eq nil
396
- recvd_md = recvd_rpc.metadata
397
- replace_symbols = Hash[md.each_pair.collect { |x, y| [x.to_s, y] }]
398
- expect(recvd_md).to eq(recvd_md.merge(replace_symbols))
399
-
400
- # finish the call
401
- final_server_batch = recvd_rpc.call.run_batch(
402
- CallOps::RECV_CLOSE_ON_SERVER => nil,
403
- CallOps::SEND_INITIAL_METADATA => nil,
404
- CallOps::SEND_STATUS_FROM_SERVER => ok_status)
405
- expect(final_server_batch.send_close).to be(true)
406
- expect(final_server_batch.send_metadata).to be(true)
407
- expect(final_server_batch.send_status).to be(true)
408
-
409
- final_client_batch = call.run_batch(
410
- CallOps::RECV_INITIAL_METADATA => nil,
411
- CallOps::RECV_STATUS_ON_CLIENT => nil)
412
- expect(final_client_batch.metadata).to eq({})
413
- expect(final_client_batch.status.code).to eq(0)
414
135
  end
415
136
  end
416
137
  end
@@ -432,120 +153,61 @@ shared_examples 'GRPC metadata delivery works OK' do
432
153
  end
433
154
 
434
155
  it 'raises an exception if a metadata key is invalid' do
435
- @bad_keys.each do |md|
436
- recvd_rpc = nil
437
- rcv_thread = Thread.new do
438
- recvd_rpc = @server.request_call
439
- end
440
-
441
- call = new_client_call
442
- # client signals that it's done sending metadata to allow server to
443
- # respond
444
- client_ops = {
445
- CallOps::SEND_INITIAL_METADATA => nil
446
- }
447
- call.run_batch(client_ops)
448
-
449
- # server gets the invocation
450
- rcv_thread.join
451
- expect(recvd_rpc).to_not eq nil
452
- server_ops = {
453
- CallOps::SEND_INITIAL_METADATA => md
454
- }
455
- blk = proc do
456
- recvd_rpc.call.run_batch(server_ops)
156
+ service = EchoService.new
157
+ run_services_on_server(@server, services: [service]) do
158
+ @bad_keys.each do |md|
159
+ proceed = Queue.new
160
+ server_exception = nil
161
+ service.on_call_started = proc do |call|
162
+ call.send_initial_metadata(md)
163
+ rescue TypeError => e
164
+ server_exception = e
165
+ ensure
166
+ proceed.push(1)
167
+ end
168
+ client_exception = nil
169
+ client_call = @stub.an_rpc(EchoMsg.new, return_op: true)
170
+ thr = Thread.new do
171
+ client_call.execute
172
+ rescue GRPC::BadStatus => e
173
+ client_exception = e
174
+ end
175
+ proceed.pop
176
+ # TODO(apolcyn): we shouldn't need this cancel here. It's
177
+ # only currently needed b/c the server does not seem to properly
178
+ # terminate the RPC if it fails to send initial metadata. That
179
+ # should be fixed, in which case this cancellation can be removed.
180
+ client_call.cancel
181
+ thr.join
182
+ p client_exception
183
+ expect(client_exception.nil?).to be(false)
184
+ expect(server_exception.nil?).to be(false)
185
+ expect(server_exception.message).to eq(
186
+ 'grpc_rb_md_ary_fill_hash_cb: bad type for key parameter')
457
187
  end
458
- expect(&blk).to raise_error
459
-
460
- # cancel the call so the server can shut down immediately
461
- call.cancel
462
188
  end
463
189
  end
464
190
 
465
191
  it 'sends an empty hash if no metadata is added' do
466
- recvd_rpc = nil
467
- rcv_thread = Thread.new do
468
- recvd_rpc = @server.request_call
192
+ run_services_on_server(@server, services: [EchoService]) do
193
+ call = @stub.an_rpc(EchoMsg.new, return_op: true)
194
+ expect(call.execute).to be_a(EchoMsg)
195
+ expect(call.metadata).to eq({})
469
196
  end
470
-
471
- call = new_client_call
472
- # client signals that it's done sending metadata to allow server to
473
- # respond
474
- client_ops = {
475
- CallOps::SEND_INITIAL_METADATA => nil,
476
- CallOps::SEND_CLOSE_FROM_CLIENT => nil
477
- }
478
- client_batch = call.run_batch(client_ops)
479
- expect(client_batch.send_metadata).to be true
480
- expect(client_batch.send_close).to be true
481
-
482
- # server gets the invocation but sends no metadata back
483
- rcv_thread.join
484
- expect(recvd_rpc).to_not eq nil
485
- server_call = recvd_rpc.call
486
- server_ops = {
487
- # receive close and send status to finish the call
488
- CallOps::RECV_CLOSE_ON_SERVER => nil,
489
- CallOps::SEND_INITIAL_METADATA => nil,
490
- CallOps::SEND_STATUS_FROM_SERVER => ok_status
491
- }
492
- srv_batch = server_call.run_batch(server_ops)
493
- expect(srv_batch.send_close).to be true
494
- expect(srv_batch.send_metadata).to be true
495
- expect(srv_batch.send_status).to be true
496
-
497
- # client receives nothing as expected
498
- client_ops = {
499
- CallOps::RECV_INITIAL_METADATA => nil,
500
- # receive status to finish the call
501
- CallOps::RECV_STATUS_ON_CLIENT => nil
502
- }
503
- final_client_batch = call.run_batch(client_ops)
504
- expect(final_client_batch.metadata).to eq({})
505
- expect(final_client_batch.status.code).to eq(0)
506
197
  end
507
198
 
508
199
  it 'sends all the pairs when keys and values are valid' do
509
- @valid_metadata.each do |md|
510
- recvd_rpc = nil
511
- rcv_thread = Thread.new do
512
- recvd_rpc = @server.request_call
200
+ service = EchoService.new
201
+ run_services_on_server(@server, services: [service]) do
202
+ @valid_metadata.each do |md|
203
+ service.on_call_started = proc do |call|
204
+ call.send_initial_metadata(md)
205
+ end
206
+ call = @stub.an_rpc(EchoMsg.new, return_op: true)
207
+ call.execute
208
+ replace_symbols = Hash[md.each_pair.collect { |x, y| [x.to_s, y] }]
209
+ expect(call.metadata).to eq(replace_symbols)
513
210
  end
514
-
515
- call = new_client_call
516
- # client signals that it's done sending metadata to allow server to
517
- # respond
518
- client_ops = {
519
- CallOps::SEND_INITIAL_METADATA => nil,
520
- CallOps::SEND_CLOSE_FROM_CLIENT => nil
521
- }
522
- client_batch = call.run_batch(client_ops)
523
- expect(client_batch.send_metadata).to be true
524
- expect(client_batch.send_close).to be true
525
-
526
- # server gets the invocation but sends no metadata back
527
- rcv_thread.join
528
- expect(recvd_rpc).to_not eq nil
529
- server_call = recvd_rpc.call
530
- server_ops = {
531
- CallOps::RECV_CLOSE_ON_SERVER => nil,
532
- CallOps::SEND_INITIAL_METADATA => md,
533
- CallOps::SEND_STATUS_FROM_SERVER => ok_status
534
- }
535
- srv_batch = server_call.run_batch(server_ops)
536
- expect(srv_batch.send_close).to be true
537
- expect(srv_batch.send_metadata).to be true
538
- expect(srv_batch.send_status).to be true
539
-
540
- # client receives nothing as expected
541
- client_ops = {
542
- CallOps::RECV_INITIAL_METADATA => nil,
543
- CallOps::RECV_STATUS_ON_CLIENT => nil
544
- }
545
- final_client_batch = call.run_batch(client_ops)
546
- replace_symbols = Hash[md.each_pair.collect { |x, y| [x.to_s, y] }]
547
- expect(final_client_batch.metadata).to eq(replace_symbols)
548
- expect(final_client_batch.status.code).to eq(0)
549
211
  end
550
212
  end
551
213
  end
@@ -554,16 +216,11 @@ end
554
216
  describe 'the http client/server' do
555
217
  before(:example) do
556
218
  server_host = '0.0.0.0:0'
557
- @server = new_core_server_for_testing(nil)
219
+ @server = new_rpc_server_for_testing
558
220
  server_port = @server.add_http2_port(server_host, :this_port_is_insecure)
559
- @server.start
560
221
  @ch = Channel.new("0.0.0.0:#{server_port}", nil, :this_channel_is_insecure)
561
- end
562
-
563
- after(:example) do
564
- @ch.close
565
- @server.shutdown_and_notify(deadline)
566
- @server.close
222
+ @stub = EchoStub.new(
223
+ "0.0.0.0:#{server_port}", nil, channel_override: @ch)
567
224
  end
568
225
 
569
226
  it_behaves_like 'basic GRPC message delivery is OK' do
@@ -574,8 +231,6 @@ describe 'the http client/server' do
574
231
  end
575
232
 
576
233
  describe 'the secure http client/server' do
577
- include_context 'setup: tags'
578
-
579
234
  def load_test_certs
580
235
  test_root = File.join(File.dirname(__FILE__), 'testdata')
581
236
  files = ['ca.pem', 'server1.key', 'server1.pem']
@@ -587,17 +242,14 @@ describe 'the secure http client/server' do
587
242
  server_host = '0.0.0.0:0'
588
243
  server_creds = GRPC::Core::ServerCredentials.new(
589
244
  nil, [{ private_key: certs[1], cert_chain: certs[2] }], false)
590
- @server = new_core_server_for_testing(nil)
245
+ @server = new_rpc_server_for_testing
591
246
  server_port = @server.add_http2_port(server_host, server_creds)
592
- @server.start
593
247
  args = { Channel::SSL_TARGET => 'foo.test.google.fr' }
594
- @ch = Channel.new("0.0.0.0:#{server_port}", args,
595
- GRPC::Core::ChannelCredentials.new(certs[0], nil, nil))
596
- end
597
-
598
- after(:example) do
599
- @server.shutdown_and_notify(deadline)
600
- @server.close
248
+ @ch = Channel.new(
249
+ "0.0.0.0:#{server_port}", args,
250
+ GRPC::Core::ChannelCredentials.new(certs[0], nil, nil))
251
+ @stub = EchoStub.new(
252
+ "0.0.0.0:#{server_port}", nil, channel_override: @ch)
601
253
  end
602
254
 
603
255
  it_behaves_like 'basic GRPC message delivery is OK' do
@@ -606,59 +258,25 @@ describe 'the secure http client/server' do
606
258
  it_behaves_like 'GRPC metadata delivery works OK' do
607
259
  end
608
260
 
609
- def credentials_update_test(creds_update_md)
610
- auth_proc = proc { creds_update_md }
261
+ it 'modifies metadata with CallCredentials' do
262
+ # create call creds
263
+ auth_proc = proc { { 'k1' => 'v1' } }
611
264
  call_creds = GRPC::Core::CallCredentials.new(auth_proc)
612
-
613
- initial_md_key = 'k2'
614
- initial_md_val = 'v2'
615
- initial_md = { initial_md_key => initial_md_val }
616
- expected_md = creds_update_md.clone
617
- fail 'bad test param' unless expected_md[initial_md_key].nil?
618
- expected_md[initial_md_key] = initial_md_val
619
-
620
- recvd_rpc = nil
621
- rcv_thread = Thread.new do
622
- recvd_rpc = @server.request_call
265
+ # create arbitrary custom metadata
266
+ custom_md = { 'k2' => 'v2' }
267
+ # perform an RPC
268
+ echo_service = EchoService.new
269
+ run_services_on_server(@server, services: [echo_service]) do
270
+ expect(@stub.an_rpc(EchoMsg.new,
271
+ credentials: call_creds,
272
+ metadata: custom_md)).to be_a(EchoMsg)
273
+ end
274
+ # call creds metadata should be merged with custom MD
275
+ expect(echo_service.received_md.length).to eq(1)
276
+ expected_md = { 'k1' => 'v1', 'k2' => 'v2' }
277
+ expected_md.each do |k, v|
278
+ expect(echo_service.received_md[0][k]).to eq(v)
623
279
  end
624
-
625
- call = new_client_call
626
- call.set_credentials! call_creds
627
-
628
- client_batch = call.run_batch(
629
- CallOps::SEND_INITIAL_METADATA => initial_md,
630
- CallOps::SEND_CLOSE_FROM_CLIENT => nil)
631
- expect(client_batch.send_metadata).to be true
632
- expect(client_batch.send_close).to be true
633
-
634
- # confirm the server can receive the client metadata
635
- rcv_thread.join
636
- expect(recvd_rpc).to_not eq nil
637
- recvd_md = recvd_rpc.metadata
638
- replace_symbols = Hash[expected_md.each_pair.collect { |x, y| [x.to_s, y] }]
639
- expect(recvd_md).to eq(recvd_md.merge(replace_symbols))
640
-
641
- credentials_update_test_finish_call(call, recvd_rpc.call)
642
- end
643
-
644
- def credentials_update_test_finish_call(client_call, server_call)
645
- final_server_batch = server_call.run_batch(
646
- CallOps::RECV_CLOSE_ON_SERVER => nil,
647
- CallOps::SEND_INITIAL_METADATA => nil,
648
- CallOps::SEND_STATUS_FROM_SERVER => ok_status)
649
- expect(final_server_batch.send_close).to be(true)
650
- expect(final_server_batch.send_metadata).to be(true)
651
- expect(final_server_batch.send_status).to be(true)
652
-
653
- final_client_batch = client_call.run_batch(
654
- CallOps::RECV_INITIAL_METADATA => nil,
655
- CallOps::RECV_STATUS_ON_CLIENT => nil)
656
- expect(final_client_batch.metadata).to eq({})
657
- expect(final_client_batch.status.code).to eq(0)
658
- end
659
-
660
- it 'modifies metadata with CallCredentials' do
661
- credentials_update_test('k1' => 'updated-v1')
662
280
  end
663
281
 
664
282
  it 'modifies large metadata with CallCredentials' do
@@ -666,11 +284,34 @@ describe 'the secure http client/server' do
666
284
  '00000000000000000000000000000000000000000000000000000000000000',
667
285
  '11111111111111111111111111111111111111111111111111111111111111',
668
286
  )
669
- md = {
670
- k3: val_array,
671
- k4: '0000000000000000000000000000000000000000000000000000000000',
672
- keeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeey5: 'v1'
287
+ # create call creds
288
+ auth_proc = proc do
289
+ {
290
+ k2: val_array,
291
+ k3: '0000000000000000000000000000000000000000000000000000000000',
292
+ keeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeey4: 'v4'
293
+ }
294
+ end
295
+ call_creds = GRPC::Core::CallCredentials.new(auth_proc)
296
+ # create arbitrary custom metadata
297
+ custom_md = { k1: 'v1' }
298
+ # perform an RPC
299
+ echo_service = EchoService.new
300
+ run_services_on_server(@server, services: [echo_service]) do
301
+ expect(@stub.an_rpc(EchoMsg.new,
302
+ credentials: call_creds,
303
+ metadata: custom_md)).to be_a(EchoMsg)
304
+ end
305
+ # call creds metadata should be merged with custom MD
306
+ expect(echo_service.received_md.length).to eq(1)
307
+ expected_md = {
308
+ k1: 'v1',
309
+ k2: val_array,
310
+ k3: '0000000000000000000000000000000000000000000000000000000000',
311
+ keeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeey4: 'v4'
673
312
  }
674
- credentials_update_test(md)
313
+ expected_md.each do |k, v|
314
+ expect(echo_service.received_md[0][k.to_s]).to eq(v)
315
+ end
675
316
  end
676
317
  end