active_call_spec.rb 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  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. require 'spec_helper'
  15. include GRPC::Core::StatusCodes
  16. describe GRPC::ActiveCall do
  17. ActiveCall = GRPC::ActiveCall
  18. Call = GRPC::Core::Call
  19. CallOps = GRPC::Core::CallOps
  20. WriteFlags = GRPC::Core::WriteFlags
  21. def ok_status
  22. Struct::Status.new(OK, 'OK')
  23. end
  24. def send_and_receive_close_and_status(client_call, server_call)
  25. client_call.run_batch(CallOps::SEND_CLOSE_FROM_CLIENT => nil)
  26. server_call.run_batch(CallOps::RECV_CLOSE_ON_SERVER => nil,
  27. CallOps::SEND_STATUS_FROM_SERVER => ok_status)
  28. client_call.run_batch(CallOps::RECV_STATUS_ON_CLIENT => nil)
  29. end
  30. def inner_call_of_active_call(active_call)
  31. active_call.instance_variable_get(:@call)
  32. end
  33. before(:each) do
  34. @pass_through = proc { |x| x }
  35. host = '0.0.0.0:0'
  36. @server = new_core_server_for_testing(nil)
  37. server_port = @server.add_http2_port(host, :this_port_is_insecure)
  38. @server.start
  39. @received_rpcs_queue = Queue.new
  40. @server_thread = Thread.new do
  41. begin
  42. received_rpc = @server.request_call
  43. rescue GRPC::Core::CallError, StandardError => e
  44. # enqueue the exception in this case as a way to indicate the error
  45. received_rpc = e
  46. end
  47. @received_rpcs_queue.push(received_rpc)
  48. end
  49. @ch = GRPC::Core::Channel.new("0.0.0.0:#{server_port}", nil,
  50. :this_channel_is_insecure)
  51. end
  52. after(:each) do
  53. @server.shutdown_and_notify(deadline)
  54. @server.close
  55. @server_thread.join
  56. end
  57. describe 'restricted view methods' do
  58. before(:each) do
  59. call = make_test_call
  60. ActiveCall.client_invoke(call)
  61. @client_call = ActiveCall.new(call, @pass_through,
  62. @pass_through, deadline)
  63. end
  64. describe '#multi_req_view' do
  65. it 'exposes a fixed subset of the ActiveCall.methods' do
  66. want = %w(cancelled?, deadline, each_remote_read, metadata, \
  67. shutdown, peer, peer_cert, send_initial_metadata, \
  68. initial_metadata_sent)
  69. v = @client_call.multi_req_view
  70. want.each do |w|
  71. expect(v.methods.include?(w))
  72. end
  73. end
  74. end
  75. describe '#single_req_view' do
  76. it 'exposes a fixed subset of the ActiveCall.methods' do
  77. want = %w(cancelled?, deadline, metadata, shutdown, \
  78. send_initial_metadata, metadata_to_send, \
  79. merge_metadata_to_send, initial_metadata_sent)
  80. v = @client_call.single_req_view
  81. want.each do |w|
  82. expect(v.methods.include?(w))
  83. end
  84. end
  85. end
  86. describe '#interceptable' do
  87. it 'exposes a fixed subset of the ActiveCall.methods' do
  88. want = %w(deadline)
  89. v = @client_call.interceptable
  90. want.each do |w|
  91. expect(v.methods.include?(w))
  92. end
  93. end
  94. end
  95. end
  96. describe '#remote_send' do
  97. it 'allows a client to send a payload to the server', test: true do
  98. call = make_test_call
  99. ActiveCall.client_invoke(call)
  100. client_call = ActiveCall.new(call, @pass_through,
  101. @pass_through, deadline)
  102. msg = 'message is a string'
  103. client_call.remote_send(msg)
  104. # check that server rpc new was received
  105. recvd_rpc = @received_rpcs_queue.pop
  106. expect(recvd_rpc).to_not eq nil
  107. recvd_call = recvd_rpc.call
  108. # Accept the call, and verify that the server reads the response ok.
  109. server_call = ActiveCall.new(recvd_call, @pass_through,
  110. @pass_through, deadline,
  111. metadata_received: true,
  112. started: false)
  113. expect(server_call.remote_read).to eq(msg)
  114. # finish the call
  115. server_call.send_initial_metadata
  116. call.run_batch(CallOps::RECV_INITIAL_METADATA => nil)
  117. send_and_receive_close_and_status(call, recvd_call)
  118. end
  119. it 'marshals the payload using the marshal func' do
  120. call = make_test_call
  121. ActiveCall.client_invoke(call)
  122. marshal = proc { |x| 'marshalled:' + x }
  123. client_call = ActiveCall.new(call, marshal, @pass_through, deadline)
  124. msg = 'message is a string'
  125. client_call.remote_send(msg)
  126. # confirm that the message was marshalled
  127. recvd_rpc = @received_rpcs_queue.pop
  128. recvd_call = recvd_rpc.call
  129. server_ops = {
  130. CallOps::SEND_INITIAL_METADATA => nil
  131. }
  132. recvd_call.run_batch(server_ops)
  133. server_call = ActiveCall.new(recvd_call, @pass_through,
  134. @pass_through, deadline,
  135. metadata_received: true)
  136. expect(server_call.remote_read).to eq('marshalled:' + msg)
  137. # finish the call
  138. call.run_batch(CallOps::RECV_INITIAL_METADATA => nil)
  139. send_and_receive_close_and_status(call, recvd_call)
  140. end
  141. TEST_WRITE_FLAGS = [WriteFlags::BUFFER_HINT, WriteFlags::NO_COMPRESS]
  142. TEST_WRITE_FLAGS.each do |f|
  143. it "successfully makes calls with write_flag set to #{f}" do
  144. call = make_test_call
  145. ActiveCall.client_invoke(call)
  146. marshal = proc { |x| 'marshalled:' + x }
  147. client_call = ActiveCall.new(call, marshal,
  148. @pass_through, deadline)
  149. msg = 'message is a string'
  150. client_call.write_flag = f
  151. client_call.remote_send(msg)
  152. # flush the message in case writes are set to buffered
  153. call.run_batch(CallOps::SEND_CLOSE_FROM_CLIENT => nil) if f == 1
  154. # confirm that the message was marshalled
  155. recvd_rpc = @received_rpcs_queue.pop
  156. recvd_call = recvd_rpc.call
  157. server_ops = {
  158. CallOps::SEND_INITIAL_METADATA => nil
  159. }
  160. recvd_call.run_batch(server_ops)
  161. server_call = ActiveCall.new(recvd_call, @pass_through,
  162. @pass_through, deadline,
  163. metadata_received: true)
  164. expect(server_call.remote_read).to eq('marshalled:' + msg)
  165. # finish the call
  166. server_call.send_status(OK, '', true)
  167. client_call.receive_and_check_status
  168. end
  169. end
  170. end
  171. describe 'sending initial metadata', send_initial_metadata: true do
  172. it 'sends metadata before sending a message if it hasnt been sent yet' do
  173. call = make_test_call
  174. @client_call = ActiveCall.new(
  175. call,
  176. @pass_through,
  177. @pass_through,
  178. deadline,
  179. started: false)
  180. metadata = { key: 'dummy_val', other: 'other_val' }
  181. expect(@client_call.metadata_sent).to eq(false)
  182. @client_call.merge_metadata_to_send(metadata)
  183. message = 'dummy message'
  184. expect(call).to(
  185. receive(:run_batch)
  186. .with(
  187. hash_including(
  188. CallOps::SEND_INITIAL_METADATA => metadata)).once)
  189. expect(call).to(
  190. receive(:run_batch).with(hash_including(
  191. CallOps::SEND_MESSAGE => message)).once)
  192. @client_call.remote_send(message)
  193. expect(@client_call.metadata_sent).to eq(true)
  194. end
  195. it 'doesnt send metadata if it thinks its already been sent' do
  196. call = make_test_call
  197. @client_call = ActiveCall.new(call,
  198. @pass_through,
  199. @pass_through,
  200. deadline)
  201. expect(@client_call.metadata_sent).to eql(true)
  202. expect(call).to(
  203. receive(:run_batch).with(hash_including(
  204. CallOps::SEND_INITIAL_METADATA)).never)
  205. @client_call.remote_send('test message')
  206. end
  207. it 'sends metadata if it is explicitly sent and ok to do so' do
  208. call = make_test_call
  209. @client_call = ActiveCall.new(call,
  210. @pass_through,
  211. @pass_through,
  212. deadline,
  213. started: false)
  214. expect(@client_call.metadata_sent).to eql(false)
  215. metadata = { test_key: 'val' }
  216. @client_call.merge_metadata_to_send(metadata)
  217. expect(@client_call.metadata_to_send).to eq(metadata)
  218. expect(call).to(
  219. receive(:run_batch).with(hash_including(
  220. CallOps::SEND_INITIAL_METADATA =>
  221. metadata)).once)
  222. @client_call.send_initial_metadata
  223. end
  224. it 'explicit sending does nothing if metadata has already been sent' do
  225. call = make_test_call
  226. @client_call = ActiveCall.new(call,
  227. @pass_through,
  228. @pass_through,
  229. deadline)
  230. expect(@client_call.metadata_sent).to eql(true)
  231. blk = proc do
  232. @client_call.send_initial_metadata
  233. end
  234. expect { blk.call }.to_not raise_error
  235. end
  236. end
  237. describe '#merge_metadata_to_send', merge_metadata_to_send: true do
  238. it 'adds to existing metadata when there is existing metadata to send' do
  239. call = make_test_call
  240. starting_metadata = {
  241. k1: 'key1_val',
  242. k2: 'key2_val',
  243. k3: 'key3_val'
  244. }
  245. @client_call = ActiveCall.new(
  246. call,
  247. @pass_through, @pass_through,
  248. deadline,
  249. started: false,
  250. metadata_to_send: starting_metadata)
  251. expect(@client_call.metadata_to_send).to eq(starting_metadata)
  252. @client_call.merge_metadata_to_send(
  253. k3: 'key3_new_val',
  254. k4: 'key4_val')
  255. expected_md_to_send = {
  256. k1: 'key1_val',
  257. k2: 'key2_val',
  258. k3: 'key3_new_val',
  259. k4: 'key4_val' }
  260. expect(@client_call.metadata_to_send).to eq(expected_md_to_send)
  261. @client_call.merge_metadata_to_send(k5: 'key5_val')
  262. expected_md_to_send.merge!(k5: 'key5_val')
  263. expect(@client_call.metadata_to_send).to eq(expected_md_to_send)
  264. end
  265. it 'fails when initial metadata has already been sent' do
  266. call = make_test_call
  267. @client_call = ActiveCall.new(
  268. call,
  269. @pass_through,
  270. @pass_through,
  271. deadline,
  272. started: true)
  273. expect(@client_call.metadata_sent).to eq(true)
  274. blk = proc do
  275. @client_call.merge_metadata_to_send(k1: 'key1_val')
  276. end
  277. expect { blk.call }.to raise_error
  278. end
  279. end
  280. describe '#client_invoke' do
  281. it 'sends metadata to the server when present' do
  282. call = make_test_call
  283. metadata = { k1: 'v1', k2: 'v2' }
  284. ActiveCall.client_invoke(call, metadata)
  285. recvd_rpc = @received_rpcs_queue.pop
  286. recvd_call = recvd_rpc.call
  287. expect(recvd_call).to_not be_nil
  288. expect(recvd_rpc.metadata).to_not be_nil
  289. expect(recvd_rpc.metadata['k1']).to eq('v1')
  290. expect(recvd_rpc.metadata['k2']).to eq('v2')
  291. # finish the call
  292. recvd_call.run_batch(CallOps::SEND_INITIAL_METADATA => {})
  293. call.run_batch(CallOps::RECV_INITIAL_METADATA => nil)
  294. send_and_receive_close_and_status(call, recvd_call)
  295. end
  296. end
  297. describe '#send_status', send_status: true do
  298. it 'works when no metadata or messages have been sent yet' do
  299. call = make_test_call
  300. ActiveCall.client_invoke(call)
  301. recvd_rpc = @received_rpcs_queue.pop
  302. server_call = ActiveCall.new(
  303. recvd_rpc.call,
  304. @pass_through,
  305. @pass_through,
  306. deadline,
  307. started: false)
  308. expect(server_call.metadata_sent).to eq(false)
  309. blk = proc { server_call.send_status(OK) }
  310. expect { blk.call }.to_not raise_error
  311. end
  312. end
  313. describe '#remote_read', remote_read: true do
  314. it 'reads the response sent by a server' do
  315. call = make_test_call
  316. ActiveCall.client_invoke(call)
  317. client_call = ActiveCall.new(call, @pass_through,
  318. @pass_through, deadline)
  319. msg = 'message is a string'
  320. client_call.remote_send(msg)
  321. server_call = expect_server_to_receive(msg)
  322. server_call.remote_send('server_response')
  323. expect(client_call.remote_read).to eq('server_response')
  324. send_and_receive_close_and_status(
  325. call, inner_call_of_active_call(server_call))
  326. end
  327. it 'saves no metadata when the server adds no metadata' do
  328. call = make_test_call
  329. ActiveCall.client_invoke(call)
  330. client_call = ActiveCall.new(call, @pass_through,
  331. @pass_through, deadline)
  332. msg = 'message is a string'
  333. client_call.remote_send(msg)
  334. server_call = expect_server_to_receive(msg)
  335. server_call.remote_send('ignore me')
  336. expect(client_call.metadata).to be_nil
  337. client_call.remote_read
  338. expect(client_call.metadata).to eq({})
  339. send_and_receive_close_and_status(
  340. call, inner_call_of_active_call(server_call))
  341. end
  342. it 'saves metadata add by the server' do
  343. call = make_test_call
  344. ActiveCall.client_invoke(call)
  345. client_call = ActiveCall.new(call, @pass_through,
  346. @pass_through, deadline)
  347. msg = 'message is a string'
  348. client_call.remote_send(msg)
  349. server_call = expect_server_to_receive(msg, k1: 'v1', k2: 'v2')
  350. server_call.remote_send('ignore me')
  351. expect(client_call.metadata).to be_nil
  352. client_call.remote_read
  353. expected = { 'k1' => 'v1', 'k2' => 'v2' }
  354. expect(client_call.metadata).to eq(expected)
  355. send_and_receive_close_and_status(
  356. call, inner_call_of_active_call(server_call))
  357. end
  358. it 'get a status from server when nothing else sent from server' do
  359. client_call = make_test_call
  360. ActiveCall.client_invoke(client_call)
  361. recvd_rpc = @received_rpcs_queue.pop
  362. recvd_call = recvd_rpc.call
  363. server_call = ActiveCall.new(
  364. recvd_call,
  365. @pass_through,
  366. @pass_through,
  367. deadline,
  368. started: false)
  369. server_call.send_status(OK, 'OK')
  370. # Check that we can receive initial metadata and a status
  371. client_call.run_batch(
  372. CallOps::RECV_INITIAL_METADATA => nil)
  373. batch_result = client_call.run_batch(
  374. CallOps::RECV_STATUS_ON_CLIENT => nil)
  375. expect(batch_result.status.code).to eq(OK)
  376. end
  377. it 'get a nil msg before a status when an OK status is sent' do
  378. call = make_test_call
  379. ActiveCall.client_invoke(call)
  380. client_call = ActiveCall.new(call, @pass_through,
  381. @pass_through, deadline)
  382. msg = 'message is a string'
  383. client_call.remote_send(msg)
  384. call.run_batch(CallOps::SEND_CLOSE_FROM_CLIENT => nil)
  385. server_call = expect_server_to_receive(msg)
  386. server_call.remote_send('server_response')
  387. server_call.send_status(OK, 'OK')
  388. expect(client_call.remote_read).to eq('server_response')
  389. res = client_call.remote_read
  390. expect(res).to be_nil
  391. end
  392. it 'unmarshals the response using the unmarshal func' do
  393. call = make_test_call
  394. ActiveCall.client_invoke(call)
  395. unmarshal = proc { |x| 'unmarshalled:' + x }
  396. client_call = ActiveCall.new(call, @pass_through,
  397. unmarshal, deadline)
  398. # confirm the client receives the unmarshalled message
  399. msg = 'message is a string'
  400. client_call.remote_send(msg)
  401. server_call = expect_server_to_receive(msg)
  402. server_call.remote_send('server_response')
  403. expect(client_call.remote_read).to eq('unmarshalled:server_response')
  404. send_and_receive_close_and_status(
  405. call, inner_call_of_active_call(server_call))
  406. end
  407. end
  408. describe '#each_remote_read' do
  409. it 'creates an Enumerator' do
  410. call = make_test_call
  411. client_call = ActiveCall.new(call, @pass_through,
  412. @pass_through, deadline)
  413. expect(client_call.each_remote_read).to be_a(Enumerator)
  414. # finish the call
  415. client_call.cancel
  416. end
  417. it 'the returned enumerator can read n responses' do
  418. call = make_test_call
  419. ActiveCall.client_invoke(call)
  420. client_call = ActiveCall.new(call, @pass_through,
  421. @pass_through, deadline)
  422. msg = 'message is a string'
  423. reply = 'server_response'
  424. client_call.remote_send(msg)
  425. server_call = expect_server_to_receive(msg)
  426. e = client_call.each_remote_read
  427. n = 3 # arbitrary value > 1
  428. n.times do
  429. server_call.remote_send(reply)
  430. expect(e.next).to eq(reply)
  431. end
  432. send_and_receive_close_and_status(
  433. call, inner_call_of_active_call(server_call))
  434. end
  435. it 'the returns an enumerator that stops after an OK Status' do
  436. call = make_test_call
  437. ActiveCall.client_invoke(call)
  438. client_call = ActiveCall.new(call, @pass_through,
  439. @pass_through, deadline)
  440. msg = 'message is a string'
  441. reply = 'server_response'
  442. client_call.remote_send(msg)
  443. call.run_batch(CallOps::SEND_CLOSE_FROM_CLIENT => nil)
  444. server_call = expect_server_to_receive(msg)
  445. e = client_call.each_remote_read
  446. n = 3 # arbitrary value > 1
  447. n.times do
  448. server_call.remote_send(reply)
  449. expect(e.next).to eq(reply)
  450. end
  451. server_call.send_status(OK, 'OK', true)
  452. expect { e.next }.to raise_error(StopIteration)
  453. end
  454. end
  455. describe '#closing the call from the client' do
  456. it 'finishes ok if the server sends a status response' do
  457. call = make_test_call
  458. ActiveCall.client_invoke(call)
  459. client_call = ActiveCall.new(call, @pass_through,
  460. @pass_through, deadline)
  461. msg = 'message is a string'
  462. client_call.remote_send(msg)
  463. expect do
  464. call.run_batch(CallOps::SEND_CLOSE_FROM_CLIENT => nil)
  465. end.to_not raise_error
  466. server_call = expect_server_to_receive(msg)
  467. server_call.remote_send('server_response')
  468. expect(client_call.remote_read).to eq('server_response')
  469. server_call.send_status(OK, 'status code is OK')
  470. expect { client_call.receive_and_check_status }.to_not raise_error
  471. end
  472. it 'finishes ok if the server sends an early status response' do
  473. call = make_test_call
  474. ActiveCall.client_invoke(call)
  475. client_call = ActiveCall.new(call, @pass_through,
  476. @pass_through, deadline)
  477. msg = 'message is a string'
  478. client_call.remote_send(msg)
  479. server_call = expect_server_to_receive(msg)
  480. server_call.remote_send('server_response')
  481. server_call.send_status(OK, 'status code is OK')
  482. expect(client_call.remote_read).to eq('server_response')
  483. expect do
  484. call.run_batch(CallOps::SEND_CLOSE_FROM_CLIENT => nil)
  485. end.to_not raise_error
  486. expect { client_call.receive_and_check_status }.to_not raise_error
  487. end
  488. it 'finishes ok if SEND_CLOSE and RECV_STATUS has been sent' do
  489. call = make_test_call
  490. ActiveCall.client_invoke(call)
  491. client_call = ActiveCall.new(call, @pass_through,
  492. @pass_through, deadline)
  493. msg = 'message is a string'
  494. client_call.remote_send(msg)
  495. server_call = expect_server_to_receive(msg)
  496. server_call.remote_send('server_response')
  497. server_call.send_status(OK, 'status code is OK')
  498. expect(client_call.remote_read).to eq('server_response')
  499. expect do
  500. call.run_batch(
  501. CallOps::SEND_CLOSE_FROM_CLIENT => nil,
  502. CallOps::RECV_STATUS_ON_CLIENT => nil)
  503. end.to_not raise_error
  504. end
  505. end
  506. # Test sending of the initial metadata in #run_server_bidi
  507. # from the server handler both implicitly and explicitly.
  508. describe '#run_server_bidi metadata sending tests', run_server_bidi: true do
  509. before(:each) do
  510. @requests = ['first message', 'second message']
  511. @server_to_client_metadata = { 'test_key' => 'test_val' }
  512. @server_status = OK
  513. @client_call = make_test_call
  514. @client_call.run_batch(CallOps::SEND_INITIAL_METADATA => {})
  515. recvd_rpc = @received_rpcs_queue.pop
  516. recvd_call = recvd_rpc.call
  517. @server_call = ActiveCall.new(
  518. recvd_call,
  519. @pass_through,
  520. @pass_through,
  521. deadline,
  522. metadata_received: true,
  523. started: false,
  524. metadata_to_send: @server_to_client_metadata)
  525. end
  526. after(:each) do
  527. # Send the requests and send a close so the server can send a status
  528. @requests.each do |message|
  529. @client_call.run_batch(CallOps::SEND_MESSAGE => message)
  530. end
  531. @client_call.run_batch(CallOps::SEND_CLOSE_FROM_CLIENT => nil)
  532. @server_thread.join
  533. # Expect that initial metadata was sent,
  534. # the requests were echoed, and a status was sent
  535. batch_result = @client_call.run_batch(
  536. CallOps::RECV_INITIAL_METADATA => nil)
  537. expect(batch_result.metadata).to eq(@server_to_client_metadata)
  538. @requests.each do |message|
  539. batch_result = @client_call.run_batch(
  540. CallOps::RECV_MESSAGE => nil)
  541. expect(batch_result.message).to eq(message)
  542. end
  543. batch_result = @client_call.run_batch(
  544. CallOps::RECV_STATUS_ON_CLIENT => nil)
  545. expect(batch_result.status.code).to eq(@server_status)
  546. end
  547. it 'sends the initial metadata implicitly if not already sent' do
  548. # Server handler that doesn't have access to a "call"
  549. # It echoes the requests
  550. fake_gen_each_reply_with_no_call_param = proc do |msgs|
  551. msgs
  552. end
  553. int_ctx = GRPC::InterceptionContext.new
  554. @server_thread = Thread.new do
  555. @server_call.run_server_bidi(
  556. fake_gen_each_reply_with_no_call_param, int_ctx)
  557. @server_call.send_status(@server_status)
  558. end
  559. end
  560. it 'sends the metadata when sent explicitly and not already sent' do
  561. # Fake server handler that has access to a "call" object and
  562. # uses it to explicitly update and send the initial metadata
  563. fake_gen_each_reply_with_call_param = proc do |msgs, call_param|
  564. call_param.merge_metadata_to_send(@server_to_client_metadata)
  565. call_param.send_initial_metadata
  566. msgs
  567. end
  568. int_ctx = GRPC::InterceptionContext.new
  569. @server_thread = Thread.new do
  570. @server_call.run_server_bidi(
  571. fake_gen_each_reply_with_call_param, int_ctx)
  572. @server_call.send_status(@server_status)
  573. end
  574. end
  575. end
  576. def expect_server_to_receive(sent_text, **kw)
  577. c = expect_server_to_be_invoked(**kw)
  578. expect(c.remote_read).to eq(sent_text)
  579. c
  580. end
  581. def expect_server_to_be_invoked(**kw)
  582. recvd_rpc = @received_rpcs_queue.pop
  583. expect(recvd_rpc).to_not eq nil
  584. recvd_call = recvd_rpc.call
  585. recvd_call.run_batch(CallOps::SEND_INITIAL_METADATA => kw)
  586. ActiveCall.new(recvd_call, @pass_through, @pass_through, deadline,
  587. metadata_received: true, started: true)
  588. end
  589. def make_test_call
  590. @ch.create_call(nil, nil, '/method', nil, deadline)
  591. end
  592. def deadline
  593. Time.now + 2 # in 2 seconds; arbitrary
  594. end
  595. end