server_streaming.cc 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*
  2. *
  3. * Copyright 2020 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <grpc/byte_buffer.h>
  21. #include <grpc/support/alloc.h>
  22. #include <grpc/support/log.h>
  23. #include <grpc/support/time.h>
  24. #include "test/core/end2end/cq_verifier.h"
  25. #include "test/core/end2end/end2end_tests.h"
  26. static void* tag(intptr_t t) { return reinterpret_cast<void*>(t); }
  27. static grpc_end2end_test_fixture begin_test(grpc_end2end_test_config config,
  28. const char* test_name,
  29. grpc_channel_args* client_args,
  30. grpc_channel_args* server_args) {
  31. grpc_end2end_test_fixture f;
  32. gpr_log(GPR_INFO, "Running test: %s/%s", test_name, config.name);
  33. f = config.create_fixture(client_args, server_args);
  34. config.init_server(&f, server_args);
  35. config.init_client(&f, client_args);
  36. return f;
  37. }
  38. static gpr_timespec n_seconds_from_now(int n) {
  39. return grpc_timeout_seconds_to_deadline(n);
  40. }
  41. static gpr_timespec five_seconds_from_now(void) {
  42. return n_seconds_from_now(5);
  43. }
  44. static void drain_cq(grpc_completion_queue* cq) {
  45. grpc_event ev;
  46. do {
  47. ev = grpc_completion_queue_next(cq, five_seconds_from_now(), nullptr);
  48. } while (ev.type != GRPC_QUEUE_SHUTDOWN);
  49. }
  50. static void shutdown_server(grpc_end2end_test_fixture* f) {
  51. if (!f->server) return;
  52. grpc_server_shutdown_and_notify(f->server, f->shutdown_cq, tag(1000));
  53. GPR_ASSERT(grpc_completion_queue_pluck(f->shutdown_cq, tag(1000),
  54. grpc_timeout_seconds_to_deadline(5),
  55. nullptr)
  56. .type == GRPC_OP_COMPLETE);
  57. grpc_server_destroy(f->server);
  58. f->server = nullptr;
  59. }
  60. static void shutdown_client(grpc_end2end_test_fixture* f) {
  61. if (!f->client) return;
  62. grpc_channel_destroy(f->client);
  63. f->client = nullptr;
  64. }
  65. static void end_test(grpc_end2end_test_fixture* f) {
  66. shutdown_server(f);
  67. shutdown_client(f);
  68. grpc_completion_queue_shutdown(f->cq);
  69. drain_cq(f->cq);
  70. grpc_completion_queue_destroy(f->cq);
  71. grpc_completion_queue_destroy(f->shutdown_cq);
  72. }
  73. /* Client requests status along with the initial metadata. Server streams
  74. * messages and ends with a non-OK status. Client reads after server is done
  75. * writing, and expects to get the status after the messages. */
  76. static void test_server_streaming(grpc_end2end_test_config config,
  77. int num_messages) {
  78. grpc_end2end_test_fixture f =
  79. begin_test(config, "test_server_streaming", nullptr, nullptr);
  80. grpc_call* c;
  81. grpc_call* s;
  82. cq_verifier* cqv = cq_verifier_create(f.cq);
  83. grpc_op ops[6];
  84. grpc_op* op;
  85. grpc_metadata_array initial_metadata_recv;
  86. grpc_metadata_array trailing_metadata_recv;
  87. grpc_metadata_array request_metadata_recv;
  88. grpc_call_details call_details;
  89. grpc_status_code status;
  90. grpc_call_error error;
  91. grpc_slice details;
  92. int was_cancelled = 2;
  93. grpc_byte_buffer* request_payload_recv;
  94. grpc_byte_buffer* response_payload;
  95. grpc_slice response_payload_slice =
  96. grpc_slice_from_copied_string("hello world");
  97. gpr_timespec deadline = five_seconds_from_now();
  98. c = grpc_channel_create_call(f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq,
  99. grpc_slice_from_static_string("/foo"), nullptr,
  100. deadline, nullptr);
  101. GPR_ASSERT(c);
  102. grpc_metadata_array_init(&initial_metadata_recv);
  103. grpc_metadata_array_init(&trailing_metadata_recv);
  104. grpc_metadata_array_init(&request_metadata_recv);
  105. grpc_call_details_init(&call_details);
  106. memset(ops, 0, sizeof(ops));
  107. op = ops;
  108. op->op = GRPC_OP_SEND_INITIAL_METADATA;
  109. op->data.send_initial_metadata.count = 0;
  110. op->flags = 0;
  111. op->reserved = nullptr;
  112. op++;
  113. op->op = GRPC_OP_RECV_INITIAL_METADATA;
  114. op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
  115. op->flags = 0;
  116. op->reserved = nullptr;
  117. op++;
  118. // Client requests status early but should not receive status till all the
  119. // messages are received.
  120. op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
  121. op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
  122. op->data.recv_status_on_client.status = &status;
  123. op->data.recv_status_on_client.status_details = &details;
  124. op->flags = 0;
  125. op->reserved = nullptr;
  126. op++;
  127. error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(1),
  128. nullptr);
  129. GPR_ASSERT(GRPC_CALL_OK == error);
  130. // Client sends close early
  131. memset(ops, 0, sizeof(ops));
  132. op = ops;
  133. op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
  134. op->flags = 0;
  135. op->reserved = nullptr;
  136. op++;
  137. error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(3),
  138. nullptr);
  139. GPR_ASSERT(GRPC_CALL_OK == error);
  140. CQ_EXPECT_COMPLETION(cqv, tag(3), 1);
  141. cq_verify(cqv);
  142. error =
  143. grpc_server_request_call(f.server, &s, &call_details,
  144. &request_metadata_recv, f.cq, f.cq, tag(100));
  145. GPR_ASSERT(GRPC_CALL_OK == error);
  146. CQ_EXPECT_COMPLETION(cqv, tag(100), 1);
  147. cq_verify(cqv);
  148. memset(ops, 0, sizeof(ops));
  149. op = ops;
  150. op->op = GRPC_OP_SEND_INITIAL_METADATA;
  151. op->data.send_initial_metadata.count = 0;
  152. op->flags = 0;
  153. op->reserved = nullptr;
  154. op++;
  155. error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(101),
  156. nullptr);
  157. GPR_ASSERT(GRPC_CALL_OK == error);
  158. CQ_EXPECT_COMPLETION(cqv, tag(101), 1);
  159. cq_verify(cqv);
  160. // Server writes bunch of messages
  161. for (int i = 0; i < num_messages; i++) {
  162. response_payload = grpc_raw_byte_buffer_create(&response_payload_slice, 1);
  163. memset(ops, 0, sizeof(ops));
  164. op = ops;
  165. op->op = GRPC_OP_SEND_MESSAGE;
  166. op->data.send_message.send_message = response_payload;
  167. op->flags = 0;
  168. op->reserved = nullptr;
  169. op++;
  170. error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops),
  171. tag(103), nullptr);
  172. GPR_ASSERT(GRPC_CALL_OK == error);
  173. CQ_EXPECT_COMPLETION(cqv, tag(103), 1);
  174. cq_verify(cqv);
  175. grpc_byte_buffer_destroy(response_payload);
  176. }
  177. // Server sends status
  178. memset(ops, 0, sizeof(ops));
  179. op = ops;
  180. op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
  181. op->data.send_status_from_server.trailing_metadata_count = 0;
  182. op->data.send_status_from_server.status = GRPC_STATUS_UNIMPLEMENTED;
  183. grpc_slice status_details = grpc_slice_from_static_string("xyz");
  184. op->data.send_status_from_server.status_details = &status_details;
  185. op->flags = 0;
  186. op->reserved = nullptr;
  187. op++;
  188. op->op = GRPC_OP_RECV_CLOSE_ON_SERVER;
  189. op->data.recv_close_on_server.cancelled = &was_cancelled;
  190. op->flags = 0;
  191. op->reserved = nullptr;
  192. op++;
  193. error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(104),
  194. nullptr);
  195. GPR_ASSERT(GRPC_CALL_OK == error);
  196. bool seen_status = false;
  197. CQ_MAYBE_EXPECT_COMPLETION(cqv, tag(1), true, &seen_status);
  198. CQ_EXPECT_COMPLETION(cqv, tag(104), 1);
  199. cq_verify(cqv);
  200. // Client keeps reading messages till it gets the status
  201. int num_messages_received = 0;
  202. while (true) {
  203. memset(ops, 0, sizeof(ops));
  204. op = ops;
  205. op->op = GRPC_OP_RECV_MESSAGE;
  206. op->data.recv_message.recv_message = &request_payload_recv;
  207. op->flags = 0;
  208. op->reserved = nullptr;
  209. op++;
  210. error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops),
  211. tag(102), nullptr);
  212. GPR_ASSERT(GRPC_CALL_OK == error);
  213. CQ_MAYBE_EXPECT_COMPLETION(cqv, tag(1), true, &seen_status);
  214. CQ_EXPECT_COMPLETION(cqv, tag(102), true);
  215. cq_verify(cqv);
  216. if (request_payload_recv == nullptr) {
  217. // The transport has received the trailing metadata.
  218. break;
  219. }
  220. GPR_ASSERT(byte_buffer_eq_string(request_payload_recv, "hello world"));
  221. grpc_byte_buffer_destroy(request_payload_recv);
  222. num_messages_received++;
  223. }
  224. GPR_ASSERT(num_messages_received == num_messages);
  225. if (!seen_status) {
  226. CQ_EXPECT_COMPLETION(cqv, tag(1), true);
  227. cq_verify(cqv);
  228. }
  229. GPR_ASSERT(status == GRPC_STATUS_UNIMPLEMENTED);
  230. GPR_ASSERT(0 == grpc_slice_str_cmp(details, "xyz"));
  231. grpc_slice_unref(response_payload_slice);
  232. grpc_call_unref(c);
  233. grpc_call_unref(s);
  234. cq_verifier_destroy(cqv);
  235. grpc_metadata_array_destroy(&initial_metadata_recv);
  236. grpc_metadata_array_destroy(&trailing_metadata_recv);
  237. grpc_metadata_array_destroy(&request_metadata_recv);
  238. grpc_call_details_destroy(&call_details);
  239. grpc_slice_unref(details);
  240. end_test(&f);
  241. config.tear_down_data(&f);
  242. }
  243. void server_streaming(grpc_end2end_test_config config) {
  244. test_server_streaming(config, 0);
  245. test_server_streaming(config, 1);
  246. test_server_streaming(config, 10);
  247. }
  248. void server_streaming_pre_init(void) {}