interop_client.cc 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. /*
  2. *
  3. * Copyright 2015, Google Inc.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following disclaimer
  14. * in the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the name of Google Inc. nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. */
  33. #include "test/cpp/interop/interop_client.h"
  34. #include <fstream>
  35. #include <memory>
  36. #include <unistd.h>
  37. #include <grpc/grpc.h>
  38. #include <grpc/support/log.h>
  39. #include <grpc/support/string_util.h>
  40. #include <grpc++/channel_interface.h>
  41. #include <grpc++/client_context.h>
  42. #include <grpc++/credentials.h>
  43. #include <grpc++/status.h>
  44. #include <grpc++/stream.h>
  45. #include "test/cpp/interop/client_helper.h"
  46. #include "test/proto/test.grpc.pb.h"
  47. #include "test/proto/empty.grpc.pb.h"
  48. #include "test/proto/messages.grpc.pb.h"
  49. #include "src/core/transport/stream_op.h"
  50. namespace grpc {
  51. namespace testing {
  52. static const char* kRandomFile = "test/cpp/interop/rnd.dat";
  53. namespace {
  54. // The same value is defined by the Java client.
  55. const std::vector<int> request_stream_sizes = {27182, 8, 1828, 45904};
  56. const std::vector<int> response_stream_sizes = {31415, 9, 2653, 58979};
  57. const int kNumResponseMessages = 2000;
  58. const int kResponseMessageSize = 1030;
  59. const int kReceiveDelayMilliSeconds = 20;
  60. const int kLargeRequestSize = 271828;
  61. const int kLargeResponseSize = 314159;
  62. } // namespace
  63. InteropClient::InteropClient(std::shared_ptr<ChannelInterface> channel)
  64. : channel_(channel) {}
  65. void InteropClient::AssertOkOrPrintErrorStatus(const Status& s) {
  66. if (s.ok()) {
  67. return;
  68. }
  69. gpr_log(GPR_INFO, "Error status code: %d, message: %s", s.error_code(),
  70. s.error_message().c_str());
  71. GPR_ASSERT(0);
  72. }
  73. void InteropClient::DoEmpty() {
  74. gpr_log(GPR_INFO, "Sending an empty rpc...");
  75. std::unique_ptr<TestService::Stub> stub(TestService::NewStub(channel_));
  76. Empty request = Empty::default_instance();
  77. Empty response = Empty::default_instance();
  78. ClientContext context;
  79. Status s = stub->EmptyCall(&context, request, &response);
  80. AssertOkOrPrintErrorStatus(s);
  81. gpr_log(GPR_INFO, "Empty rpc done.");
  82. }
  83. // Shared code to set large payload, make rpc and check response payload.
  84. void InteropClient::PerformLargeUnary(SimpleRequest* request,
  85. SimpleResponse* response) {
  86. std::unique_ptr<TestService::Stub> stub(TestService::NewStub(channel_));
  87. ClientContext context;
  88. InteropClientContextInspector inspector(context);
  89. // If the request doesn't already specify the response type, default to
  90. // COMPRESSABLE.
  91. request->set_response_size(kLargeResponseSize);
  92. grpc::string payload(kLargeRequestSize, '\0');
  93. request->mutable_payload()->set_body(payload.c_str(), kLargeRequestSize);
  94. Status s = stub->UnaryCall(&context, *request, response);
  95. // Compression related checks.
  96. GPR_ASSERT(request->response_compression() ==
  97. GetInteropCompressionTypeFromCompressionAlgorithm(
  98. inspector.GetCallCompressionAlgorithm()));
  99. if (request->response_compression() == NONE) {
  100. GPR_ASSERT(!(inspector.GetMessageFlags() & GRPC_WRITE_INTERNAL_COMPRESS));
  101. } else if (request->response_type() == PayloadType::COMPRESSABLE) {
  102. // requested compression and compressable response => results should always
  103. // be compressed.
  104. GPR_ASSERT(inspector.GetMessageFlags() & GRPC_WRITE_INTERNAL_COMPRESS);
  105. }
  106. AssertOkOrPrintErrorStatus(s);
  107. // Payload related checks.
  108. if (request->response_type() != PayloadType::RANDOM) {
  109. GPR_ASSERT(response->payload().type() == request->response_type());
  110. }
  111. switch (response->payload().type()) {
  112. case PayloadType::COMPRESSABLE:
  113. GPR_ASSERT(response->payload().body() ==
  114. grpc::string(kLargeResponseSize, '\0'));
  115. break;
  116. case PayloadType::UNCOMPRESSABLE: {
  117. std::ifstream rnd_file(kRandomFile);
  118. GPR_ASSERT(rnd_file.good());
  119. for (int i = 0; i < kLargeResponseSize; i++) {
  120. GPR_ASSERT(response->payload().body()[i] == (char)rnd_file.get());
  121. }
  122. } break;
  123. default:
  124. GPR_ASSERT(false);
  125. }
  126. }
  127. void InteropClient::DoComputeEngineCreds(
  128. const grpc::string& default_service_account,
  129. const grpc::string& oauth_scope) {
  130. gpr_log(GPR_INFO,
  131. "Sending a large unary rpc with compute engine credentials ...");
  132. SimpleRequest request;
  133. SimpleResponse response;
  134. request.set_fill_username(true);
  135. request.set_fill_oauth_scope(true);
  136. request.set_response_type(PayloadType::COMPRESSABLE);
  137. PerformLargeUnary(&request, &response);
  138. gpr_log(GPR_INFO, "Got username %s", response.username().c_str());
  139. gpr_log(GPR_INFO, "Got oauth_scope %s", response.oauth_scope().c_str());
  140. GPR_ASSERT(!response.username().empty());
  141. GPR_ASSERT(response.username().c_str() == default_service_account);
  142. GPR_ASSERT(!response.oauth_scope().empty());
  143. const char* oauth_scope_str = response.oauth_scope().c_str();
  144. GPR_ASSERT(oauth_scope.find(oauth_scope_str) != grpc::string::npos);
  145. gpr_log(GPR_INFO, "Large unary with compute engine creds done.");
  146. }
  147. void InteropClient::DoServiceAccountCreds(const grpc::string& username,
  148. const grpc::string& oauth_scope) {
  149. gpr_log(GPR_INFO,
  150. "Sending a large unary rpc with service account credentials ...");
  151. SimpleRequest request;
  152. SimpleResponse response;
  153. request.set_fill_username(true);
  154. request.set_fill_oauth_scope(true);
  155. request.set_response_type(PayloadType::COMPRESSABLE);
  156. PerformLargeUnary(&request, &response);
  157. GPR_ASSERT(!response.username().empty());
  158. GPR_ASSERT(!response.oauth_scope().empty());
  159. GPR_ASSERT(username.find(response.username()) != grpc::string::npos);
  160. const char* oauth_scope_str = response.oauth_scope().c_str();
  161. GPR_ASSERT(oauth_scope.find(oauth_scope_str) != grpc::string::npos);
  162. gpr_log(GPR_INFO, "Large unary with service account creds done.");
  163. }
  164. void InteropClient::DoOauth2AuthToken(const grpc::string& username,
  165. const grpc::string& oauth_scope) {
  166. gpr_log(GPR_INFO,
  167. "Sending a unary rpc with raw oauth2 access token credentials ...");
  168. SimpleRequest request;
  169. SimpleResponse response;
  170. request.set_fill_username(true);
  171. request.set_fill_oauth_scope(true);
  172. std::unique_ptr<TestService::Stub> stub(TestService::NewStub(channel_));
  173. ClientContext context;
  174. Status s = stub->UnaryCall(&context, request, &response);
  175. AssertOkOrPrintErrorStatus(s);
  176. GPR_ASSERT(!response.username().empty());
  177. GPR_ASSERT(!response.oauth_scope().empty());
  178. GPR_ASSERT(username.find(response.username()) != grpc::string::npos);
  179. const char* oauth_scope_str = response.oauth_scope().c_str();
  180. GPR_ASSERT(oauth_scope.find(oauth_scope_str) != grpc::string::npos);
  181. gpr_log(GPR_INFO, "Unary with oauth2 access token credentials done.");
  182. }
  183. void InteropClient::DoPerRpcCreds(const grpc::string& username,
  184. const grpc::string& oauth_scope) {
  185. gpr_log(GPR_INFO,
  186. "Sending a unary rpc with per-rpc raw oauth2 access token ...");
  187. SimpleRequest request;
  188. SimpleResponse response;
  189. request.set_fill_username(true);
  190. request.set_fill_oauth_scope(true);
  191. std::unique_ptr<TestService::Stub> stub(TestService::NewStub(channel_));
  192. ClientContext context;
  193. grpc::string access_token = GetOauth2AccessToken();
  194. std::shared_ptr<Credentials> creds = AccessTokenCredentials(access_token);
  195. context.set_credentials(creds);
  196. Status s = stub->UnaryCall(&context, request, &response);
  197. AssertOkOrPrintErrorStatus(s);
  198. GPR_ASSERT(!response.username().empty());
  199. GPR_ASSERT(!response.oauth_scope().empty());
  200. GPR_ASSERT(username.find(response.username()) != grpc::string::npos);
  201. const char* oauth_scope_str = response.oauth_scope().c_str();
  202. GPR_ASSERT(oauth_scope.find(oauth_scope_str) != grpc::string::npos);
  203. gpr_log(GPR_INFO, "Unary with per-rpc oauth2 access token done.");
  204. }
  205. void InteropClient::DoJwtTokenCreds(const grpc::string& username) {
  206. gpr_log(GPR_INFO, "Sending a large unary rpc with JWT token credentials ...");
  207. SimpleRequest request;
  208. SimpleResponse response;
  209. request.set_fill_username(true);
  210. request.set_response_type(PayloadType::COMPRESSABLE);
  211. PerformLargeUnary(&request, &response);
  212. GPR_ASSERT(!response.username().empty());
  213. GPR_ASSERT(username.find(response.username()) != grpc::string::npos);
  214. gpr_log(GPR_INFO, "Large unary with JWT token creds done.");
  215. }
  216. void InteropClient::DoLargeUnary() {
  217. gpr_log(GPR_INFO, "Sending a large unary rpc...");
  218. SimpleRequest request;
  219. SimpleResponse response;
  220. request.set_response_type(PayloadType::COMPRESSABLE);
  221. PerformLargeUnary(&request, &response);
  222. gpr_log(GPR_INFO, "Large unary done.");
  223. }
  224. void InteropClient::DoLargeCompressedUnary() {
  225. const CompressionType compression_types[] = {NONE, GZIP, DEFLATE};
  226. const PayloadType payload_types[] = {COMPRESSABLE, UNCOMPRESSABLE, RANDOM};
  227. for (const auto payload_type : payload_types) {
  228. for (const auto compression_type : compression_types) {
  229. char* log_suffix;
  230. gpr_asprintf(&log_suffix, "(compression=%s; payload=%s)",
  231. CompressionType_Name(compression_type).c_str(),
  232. PayloadType_Name(payload_type).c_str());
  233. gpr_log(GPR_INFO, "Sending a large compressed unary rpc %s.", log_suffix);
  234. SimpleRequest request;
  235. SimpleResponse response;
  236. request.set_response_type(payload_type);
  237. request.set_response_compression(compression_type);
  238. PerformLargeUnary(&request, &response);
  239. gpr_log(GPR_INFO, "Large compressed unary done %s.", log_suffix);
  240. gpr_free(log_suffix);
  241. }
  242. }
  243. }
  244. void InteropClient::DoRequestStreaming() {
  245. gpr_log(GPR_INFO, "Sending request steaming rpc ...");
  246. std::unique_ptr<TestService::Stub> stub(TestService::NewStub(channel_));
  247. ClientContext context;
  248. StreamingInputCallRequest request;
  249. StreamingInputCallResponse response;
  250. std::unique_ptr<ClientWriter<StreamingInputCallRequest>> stream(
  251. stub->StreamingInputCall(&context, &response));
  252. int aggregated_payload_size = 0;
  253. for (unsigned int i = 0; i < request_stream_sizes.size(); ++i) {
  254. Payload* payload = request.mutable_payload();
  255. payload->set_body(grpc::string(request_stream_sizes[i], '\0'));
  256. GPR_ASSERT(stream->Write(request));
  257. aggregated_payload_size += request_stream_sizes[i];
  258. }
  259. stream->WritesDone();
  260. Status s = stream->Finish();
  261. GPR_ASSERT(response.aggregated_payload_size() == aggregated_payload_size);
  262. AssertOkOrPrintErrorStatus(s);
  263. gpr_log(GPR_INFO, "Request streaming done.");
  264. }
  265. void InteropClient::DoResponseStreaming() {
  266. gpr_log(GPR_INFO, "Receiving response steaming rpc ...");
  267. std::unique_ptr<TestService::Stub> stub(TestService::NewStub(channel_));
  268. ClientContext context;
  269. StreamingOutputCallRequest request;
  270. for (unsigned int i = 0; i < response_stream_sizes.size(); ++i) {
  271. ResponseParameters* response_parameter = request.add_response_parameters();
  272. response_parameter->set_size(response_stream_sizes[i]);
  273. }
  274. StreamingOutputCallResponse response;
  275. std::unique_ptr<ClientReader<StreamingOutputCallResponse>> stream(
  276. stub->StreamingOutputCall(&context, request));
  277. unsigned int i = 0;
  278. while (stream->Read(&response)) {
  279. GPR_ASSERT(response.payload().body() ==
  280. grpc::string(response_stream_sizes[i], '\0'));
  281. ++i;
  282. }
  283. GPR_ASSERT(response_stream_sizes.size() == i);
  284. Status s = stream->Finish();
  285. AssertOkOrPrintErrorStatus(s);
  286. gpr_log(GPR_INFO, "Response streaming done.");
  287. }
  288. void InteropClient::DoResponseCompressedStreaming() {
  289. std::unique_ptr<TestService::Stub> stub(TestService::NewStub(channel_));
  290. const CompressionType compression_types[] = {NONE, GZIP, DEFLATE};
  291. const PayloadType payload_types[] = {COMPRESSABLE, UNCOMPRESSABLE, RANDOM};
  292. for (const auto payload_type : payload_types) {
  293. for (const auto compression_type : compression_types) {
  294. ClientContext context;
  295. InteropClientContextInspector inspector(context);
  296. StreamingOutputCallRequest request;
  297. char* log_suffix;
  298. gpr_asprintf(&log_suffix, "(compression=%s; payload=%s)",
  299. CompressionType_Name(compression_type).c_str(),
  300. PayloadType_Name(payload_type).c_str());
  301. gpr_log(GPR_INFO, "Receiving response steaming rpc %s.", log_suffix);
  302. request.set_response_type(payload_type);
  303. request.set_response_compression(compression_type);
  304. for (unsigned int i = 0; i < response_stream_sizes.size(); ++i) {
  305. ResponseParameters* response_parameter =
  306. request.add_response_parameters();
  307. response_parameter->set_size(response_stream_sizes[i]);
  308. }
  309. StreamingOutputCallResponse response;
  310. std::unique_ptr<ClientReader<StreamingOutputCallResponse>> stream(
  311. stub->StreamingOutputCall(&context, request));
  312. unsigned int i = 0;
  313. while (stream->Read(&response)) {
  314. GPR_ASSERT(response.payload().body() ==
  315. grpc::string(response_stream_sizes[i], '\0'));
  316. // Compression related checks.
  317. GPR_ASSERT(request.response_compression() ==
  318. GetInteropCompressionTypeFromCompressionAlgorithm(
  319. inspector.GetCallCompressionAlgorithm()));
  320. if (request.response_compression() == NONE) {
  321. GPR_ASSERT(
  322. !(inspector.GetMessageFlags() & GRPC_WRITE_INTERNAL_COMPRESS));
  323. } else if (request.response_type() == PayloadType::COMPRESSABLE) {
  324. // requested compression and compressable response => results should
  325. // always be compressed.
  326. GPR_ASSERT(inspector.GetMessageFlags() &
  327. GRPC_WRITE_INTERNAL_COMPRESS);
  328. }
  329. ++i;
  330. }
  331. GPR_ASSERT(response_stream_sizes.size() == i);
  332. Status s = stream->Finish();
  333. AssertOkOrPrintErrorStatus(s);
  334. gpr_log(GPR_INFO, "Response streaming done %s.", log_suffix);
  335. gpr_free(log_suffix);
  336. }
  337. }
  338. }
  339. void InteropClient::DoResponseStreamingWithSlowConsumer() {
  340. gpr_log(GPR_INFO, "Receiving response steaming rpc with slow consumer ...");
  341. std::unique_ptr<TestService::Stub> stub(TestService::NewStub(channel_));
  342. ClientContext context;
  343. StreamingOutputCallRequest request;
  344. for (int i = 0; i < kNumResponseMessages; ++i) {
  345. ResponseParameters* response_parameter = request.add_response_parameters();
  346. response_parameter->set_size(kResponseMessageSize);
  347. }
  348. StreamingOutputCallResponse response;
  349. std::unique_ptr<ClientReader<StreamingOutputCallResponse>> stream(
  350. stub->StreamingOutputCall(&context, request));
  351. int i = 0;
  352. while (stream->Read(&response)) {
  353. GPR_ASSERT(response.payload().body() ==
  354. grpc::string(kResponseMessageSize, '\0'));
  355. gpr_log(GPR_INFO, "received message %d", i);
  356. usleep(kReceiveDelayMilliSeconds * 1000);
  357. ++i;
  358. }
  359. GPR_ASSERT(kNumResponseMessages == i);
  360. Status s = stream->Finish();
  361. AssertOkOrPrintErrorStatus(s);
  362. gpr_log(GPR_INFO, "Response streaming done.");
  363. }
  364. void InteropClient::DoHalfDuplex() {
  365. gpr_log(GPR_INFO, "Sending half-duplex streaming rpc ...");
  366. std::unique_ptr<TestService::Stub> stub(TestService::NewStub(channel_));
  367. ClientContext context;
  368. std::unique_ptr<ClientReaderWriter<StreamingOutputCallRequest,
  369. StreamingOutputCallResponse>>
  370. stream(stub->HalfDuplexCall(&context));
  371. StreamingOutputCallRequest request;
  372. ResponseParameters* response_parameter = request.add_response_parameters();
  373. for (unsigned int i = 0; i < response_stream_sizes.size(); ++i) {
  374. response_parameter->set_size(response_stream_sizes[i]);
  375. GPR_ASSERT(stream->Write(request));
  376. }
  377. stream->WritesDone();
  378. unsigned int i = 0;
  379. StreamingOutputCallResponse response;
  380. while (stream->Read(&response)) {
  381. GPR_ASSERT(response.payload().body() ==
  382. grpc::string(response_stream_sizes[i], '\0'));
  383. ++i;
  384. }
  385. GPR_ASSERT(response_stream_sizes.size() == i);
  386. Status s = stream->Finish();
  387. AssertOkOrPrintErrorStatus(s);
  388. gpr_log(GPR_INFO, "Half-duplex streaming rpc done.");
  389. }
  390. void InteropClient::DoPingPong() {
  391. gpr_log(GPR_INFO, "Sending Ping Pong streaming rpc ...");
  392. std::unique_ptr<TestService::Stub> stub(TestService::NewStub(channel_));
  393. ClientContext context;
  394. std::unique_ptr<ClientReaderWriter<StreamingOutputCallRequest,
  395. StreamingOutputCallResponse>>
  396. stream(stub->FullDuplexCall(&context));
  397. StreamingOutputCallRequest request;
  398. request.set_response_type(PayloadType::COMPRESSABLE);
  399. ResponseParameters* response_parameter = request.add_response_parameters();
  400. Payload* payload = request.mutable_payload();
  401. StreamingOutputCallResponse response;
  402. for (unsigned int i = 0; i < request_stream_sizes.size(); ++i) {
  403. response_parameter->set_size(response_stream_sizes[i]);
  404. payload->set_body(grpc::string(request_stream_sizes[i], '\0'));
  405. GPR_ASSERT(stream->Write(request));
  406. GPR_ASSERT(stream->Read(&response));
  407. GPR_ASSERT(response.payload().body() ==
  408. grpc::string(response_stream_sizes[i], '\0'));
  409. }
  410. stream->WritesDone();
  411. GPR_ASSERT(!stream->Read(&response));
  412. Status s = stream->Finish();
  413. AssertOkOrPrintErrorStatus(s);
  414. gpr_log(GPR_INFO, "Ping pong streaming done.");
  415. }
  416. void InteropClient::DoCancelAfterBegin() {
  417. gpr_log(GPR_INFO, "Sending request steaming rpc ...");
  418. std::unique_ptr<TestService::Stub> stub(TestService::NewStub(channel_));
  419. ClientContext context;
  420. StreamingInputCallRequest request;
  421. StreamingInputCallResponse response;
  422. std::unique_ptr<ClientWriter<StreamingInputCallRequest>> stream(
  423. stub->StreamingInputCall(&context, &response));
  424. gpr_log(GPR_INFO, "Trying to cancel...");
  425. context.TryCancel();
  426. Status s = stream->Finish();
  427. GPR_ASSERT(s.error_code() == StatusCode::CANCELLED);
  428. gpr_log(GPR_INFO, "Canceling streaming done.");
  429. }
  430. void InteropClient::DoCancelAfterFirstResponse() {
  431. gpr_log(GPR_INFO, "Sending Ping Pong streaming rpc ...");
  432. std::unique_ptr<TestService::Stub> stub(TestService::NewStub(channel_));
  433. ClientContext context;
  434. std::unique_ptr<ClientReaderWriter<StreamingOutputCallRequest,
  435. StreamingOutputCallResponse>>
  436. stream(stub->FullDuplexCall(&context));
  437. StreamingOutputCallRequest request;
  438. request.set_response_type(PayloadType::COMPRESSABLE);
  439. ResponseParameters* response_parameter = request.add_response_parameters();
  440. response_parameter->set_size(31415);
  441. request.mutable_payload()->set_body(grpc::string(27182, '\0'));
  442. StreamingOutputCallResponse response;
  443. GPR_ASSERT(stream->Write(request));
  444. GPR_ASSERT(stream->Read(&response));
  445. GPR_ASSERT(response.payload().body() == grpc::string(31415, '\0'));
  446. gpr_log(GPR_INFO, "Trying to cancel...");
  447. context.TryCancel();
  448. Status s = stream->Finish();
  449. gpr_log(GPR_INFO, "Canceling pingpong streaming done.");
  450. }
  451. void InteropClient::DoTimeoutOnSleepingServer() {
  452. gpr_log(GPR_INFO, "Sending Ping Pong streaming rpc with a short deadline...");
  453. std::unique_ptr<TestService::Stub> stub(TestService::NewStub(channel_));
  454. ClientContext context;
  455. std::chrono::system_clock::time_point deadline =
  456. std::chrono::system_clock::now() + std::chrono::milliseconds(1);
  457. context.set_deadline(deadline);
  458. std::unique_ptr<ClientReaderWriter<StreamingOutputCallRequest,
  459. StreamingOutputCallResponse>>
  460. stream(stub->FullDuplexCall(&context));
  461. StreamingOutputCallRequest request;
  462. request.mutable_payload()->set_body(grpc::string(27182, '\0'));
  463. stream->Write(request);
  464. Status s = stream->Finish();
  465. GPR_ASSERT(s.error_code() == StatusCode::DEADLINE_EXCEEDED);
  466. gpr_log(GPR_INFO, "Pingpong streaming timeout done.");
  467. }
  468. void InteropClient::DoStatusWithMessage() {
  469. gpr_log(GPR_INFO, "Sending RPC with a request for status code 2 and message");
  470. std::unique_ptr<TestService::Stub> stub(TestService::NewStub(channel_));
  471. ClientContext context;
  472. SimpleRequest request;
  473. SimpleResponse response;
  474. EchoStatus* requested_status = request.mutable_response_status();
  475. requested_status->set_code(grpc::StatusCode::UNKNOWN);
  476. grpc::string test_msg = "This is a test message";
  477. requested_status->set_message(test_msg);
  478. Status s = stub->UnaryCall(&context, request, &response);
  479. GPR_ASSERT(s.error_code() == grpc::StatusCode::UNKNOWN);
  480. GPR_ASSERT(s.error_message() == test_msg);
  481. gpr_log(GPR_INFO, "Done testing Status and Message");
  482. }
  483. } // namespace testing
  484. } // namespace grpc