client_streaming.cc 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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 streaming test where the client sends a bunch of messages and the
  74. // server reads them. After reading some messages, the server sends the status.
  75. // Client writes fail after that due to the end of stream and the client
  76. // subsequently requests and receives the status.
  77. static void test_client_streaming(grpc_end2end_test_config config,
  78. int messages) {
  79. grpc_end2end_test_fixture f =
  80. begin_test(config, "test_client_streaming", nullptr, nullptr);
  81. grpc_call* c;
  82. grpc_call* s;
  83. cq_verifier* cqv = cq_verifier_create(f.cq);
  84. grpc_op ops[6];
  85. grpc_op* op;
  86. grpc_metadata_array initial_metadata_recv;
  87. grpc_metadata_array trailing_metadata_recv;
  88. grpc_metadata_array request_metadata_recv;
  89. grpc_call_details call_details;
  90. grpc_status_code status;
  91. grpc_call_error error;
  92. grpc_slice details;
  93. grpc_byte_buffer* request_payload_recv = nullptr;
  94. grpc_byte_buffer* request_payload = nullptr;
  95. int i;
  96. grpc_slice request_payload_slice =
  97. grpc_slice_from_copied_string("hello world");
  98. gpr_timespec deadline = five_seconds_from_now();
  99. c = grpc_channel_create_call(f.client, nullptr, GRPC_PROPAGATE_DEFAULTS, f.cq,
  100. grpc_slice_from_static_string("/foo"), nullptr,
  101. deadline, nullptr);
  102. GPR_ASSERT(c);
  103. grpc_metadata_array_init(&initial_metadata_recv);
  104. grpc_metadata_array_init(&trailing_metadata_recv);
  105. grpc_metadata_array_init(&request_metadata_recv);
  106. grpc_call_details_init(&call_details);
  107. memset(ops, 0, sizeof(ops));
  108. op = ops;
  109. op->op = GRPC_OP_SEND_INITIAL_METADATA;
  110. op->data.send_initial_metadata.count = 0;
  111. op->flags = 0;
  112. op->reserved = nullptr;
  113. op++;
  114. op->op = GRPC_OP_RECV_INITIAL_METADATA;
  115. op->data.recv_initial_metadata.recv_initial_metadata = &initial_metadata_recv;
  116. op->flags = 0;
  117. op->reserved = nullptr;
  118. op++;
  119. error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(1),
  120. nullptr);
  121. GPR_ASSERT(GRPC_CALL_OK == error);
  122. error =
  123. grpc_server_request_call(f.server, &s, &call_details,
  124. &request_metadata_recv, f.cq, f.cq, tag(100));
  125. GPR_ASSERT(GRPC_CALL_OK == error);
  126. CQ_EXPECT_COMPLETION(cqv, tag(100), 1);
  127. cq_verify(cqv);
  128. memset(ops, 0, sizeof(ops));
  129. op = ops;
  130. op->op = GRPC_OP_SEND_INITIAL_METADATA;
  131. op->data.send_initial_metadata.count = 0;
  132. op->flags = 0;
  133. op->reserved = nullptr;
  134. op++;
  135. error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(101),
  136. nullptr);
  137. GPR_ASSERT(GRPC_CALL_OK == error);
  138. CQ_EXPECT_COMPLETION(cqv, tag(101), 1);
  139. CQ_EXPECT_COMPLETION(cqv, tag(1), 1);
  140. cq_verify(cqv);
  141. // Client writes bunch of messages and server reads them
  142. for (i = 0; i < messages; i++) {
  143. request_payload = grpc_raw_byte_buffer_create(&request_payload_slice, 1);
  144. memset(ops, 0, sizeof(ops));
  145. op = ops;
  146. op->op = GRPC_OP_SEND_MESSAGE;
  147. op->data.send_message.send_message = request_payload;
  148. op->flags = 0;
  149. op->reserved = nullptr;
  150. op++;
  151. error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops),
  152. tag(103), nullptr);
  153. GPR_ASSERT(GRPC_CALL_OK == error);
  154. grpc_byte_buffer_destroy(request_payload);
  155. memset(ops, 0, sizeof(ops));
  156. op = ops;
  157. op->op = GRPC_OP_RECV_MESSAGE;
  158. op->data.recv_message.recv_message = &request_payload_recv;
  159. op->flags = 0;
  160. op->reserved = nullptr;
  161. op++;
  162. error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops),
  163. tag(102), nullptr);
  164. GPR_ASSERT(GRPC_CALL_OK == error);
  165. CQ_EXPECT_COMPLETION(cqv, tag(102), 1);
  166. CQ_EXPECT_COMPLETION(cqv, tag(103), 1);
  167. cq_verify(cqv);
  168. GPR_ASSERT(byte_buffer_eq_string(request_payload_recv, "hello world"));
  169. grpc_byte_buffer_destroy(request_payload_recv);
  170. }
  171. // Server sends status denoting end of stream
  172. memset(ops, 0, sizeof(ops));
  173. op = ops;
  174. op->op = GRPC_OP_SEND_STATUS_FROM_SERVER;
  175. op->data.send_status_from_server.trailing_metadata_count = 0;
  176. op->data.send_status_from_server.status = GRPC_STATUS_UNIMPLEMENTED;
  177. grpc_slice status_details = grpc_slice_from_static_string("xyz");
  178. op->data.send_status_from_server.status_details = &status_details;
  179. op->flags = 0;
  180. op->reserved = nullptr;
  181. op++;
  182. error = grpc_call_start_batch(s, ops, static_cast<size_t>(op - ops), tag(104),
  183. nullptr);
  184. GPR_ASSERT(GRPC_CALL_OK == error);
  185. CQ_EXPECT_COMPLETION(cqv, tag(104), 1);
  186. cq_verify(cqv);
  187. // Do an empty verify to make sure that the client receives the status
  188. cq_verify_empty(cqv);
  189. // Client tries sending another message which should fail
  190. request_payload = grpc_raw_byte_buffer_create(&request_payload_slice, 1);
  191. memset(ops, 0, sizeof(ops));
  192. op = ops;
  193. op->op = GRPC_OP_SEND_MESSAGE;
  194. op->data.send_message.send_message = request_payload;
  195. op->flags = 0;
  196. op->reserved = nullptr;
  197. op++;
  198. error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(103),
  199. nullptr);
  200. GPR_ASSERT(GRPC_CALL_OK == error);
  201. grpc_byte_buffer_destroy(request_payload);
  202. CQ_EXPECT_COMPLETION(cqv, tag(103), 0);
  203. cq_verify(cqv);
  204. // Client sends close and requests status
  205. memset(ops, 0, sizeof(ops));
  206. op = ops;
  207. op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
  208. op->flags = 0;
  209. op->reserved = nullptr;
  210. op++;
  211. op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
  212. op->data.recv_status_on_client.trailing_metadata = &trailing_metadata_recv;
  213. op->data.recv_status_on_client.status = &status;
  214. op->data.recv_status_on_client.status_details = &details;
  215. op->flags = 0;
  216. op->reserved = nullptr;
  217. op++;
  218. error = grpc_call_start_batch(c, ops, static_cast<size_t>(op - ops), tag(3),
  219. nullptr);
  220. GPR_ASSERT(GRPC_CALL_OK == error);
  221. CQ_EXPECT_COMPLETION(cqv, tag(3), 1);
  222. cq_verify(cqv);
  223. GPR_ASSERT(status == GRPC_STATUS_UNIMPLEMENTED);
  224. GPR_ASSERT(0 == grpc_slice_str_cmp(details, "xyz"));
  225. grpc_slice_unref(request_payload_slice);
  226. grpc_call_unref(c);
  227. grpc_call_unref(s);
  228. cq_verifier_destroy(cqv);
  229. grpc_metadata_array_destroy(&initial_metadata_recv);
  230. grpc_metadata_array_destroy(&trailing_metadata_recv);
  231. grpc_metadata_array_destroy(&request_metadata_recv);
  232. grpc_call_details_destroy(&call_details);
  233. grpc_slice_unref(details);
  234. end_test(&f);
  235. config.tear_down_data(&f);
  236. }
  237. void client_streaming(grpc_end2end_test_config config) {
  238. for (int i = 0; i < 10; i++) {
  239. test_client_streaming(config, i);
  240. }
  241. }
  242. void client_streaming_pre_init(void) {}