interop_client.cc 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  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. request->set_response_type(PayloadType::COMPRESSABLE);
  90. request->set_response_size(kLargeResponseSize);
  91. grpc::string payload(kLargeRequestSize, '\0');
  92. request->mutable_payload()->set_body(payload.c_str(), kLargeRequestSize);
  93. Status s = stub->UnaryCall(&context, *request, response);
  94. // Compression related checks.
  95. GPR_ASSERT(request->response_compression() ==
  96. GetInteropCompressionTypeFromCompressionAlgorithm(
  97. inspector.GetCallCompressionAlgorithm()));
  98. if (request->response_compression() == NONE) {
  99. GPR_ASSERT(!(inspector.GetMessageFlags() & GRPC_WRITE_INTERNAL_COMPRESS));
  100. } else if (request->response_type() == PayloadType::COMPRESSABLE) {
  101. // requested compression and compressable response => results should always
  102. // be compressed.
  103. GPR_ASSERT(inspector.GetMessageFlags() & GRPC_WRITE_INTERNAL_COMPRESS);
  104. }
  105. AssertOkOrPrintErrorStatus(s);
  106. // Payload related checks.
  107. if (request->response_type() != PayloadType::RANDOM) {
  108. GPR_ASSERT(response->payload().type() == request->response_type());
  109. }
  110. switch (response->payload().type()) {
  111. case PayloadType::COMPRESSABLE:
  112. GPR_ASSERT(response->payload().body() ==
  113. grpc::string(kLargeResponseSize, '\0'));
  114. break;
  115. case PayloadType::UNCOMPRESSABLE: {
  116. std::ifstream rnd_file(kRandomFile);
  117. GPR_ASSERT(rnd_file.good());
  118. for (int i = 0; i < kLargeResponseSize; i++) {
  119. GPR_ASSERT(response->payload().body()[i] == (char)rnd_file.get());
  120. }
  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. PerformLargeUnary(&request, &response);
  221. gpr_log(GPR_INFO, "Large unary done.");
  222. }
  223. void InteropClient::DoLargeCompressedUnary() {
  224. const CompressionType compression_types[] = {NONE, GZIP, DEFLATE};
  225. const PayloadType payload_types[] = {COMPRESSABLE, UNCOMPRESSABLE, RANDOM};
  226. for (const auto payload_type : payload_types) {
  227. for (const auto compression_type : compression_types) {
  228. char* log_suffix;
  229. gpr_asprintf(&log_suffix, "(compression=%s; payload=%s)",
  230. CompressionType_Name(compression_type).c_str(),
  231. PayloadType_Name(payload_type).c_str());
  232. gpr_log(GPR_INFO, "Sending a large compressed unary rpc %s.", log_suffix);
  233. SimpleRequest request;
  234. SimpleResponse response;
  235. request.set_response_type(payload_type);
  236. request.set_response_compression(compression_type);
  237. PerformLargeUnary(&request, &response);
  238. gpr_log(GPR_INFO, "Large compressed unary done %s.", log_suffix);
  239. gpr_free(log_suffix);
  240. }
  241. }
  242. }
  243. void InteropClient::DoRequestStreaming() {
  244. gpr_log(GPR_INFO, "Sending request steaming rpc ...");
  245. std::unique_ptr<TestService::Stub> stub(TestService::NewStub(channel_));
  246. ClientContext context;
  247. StreamingInputCallRequest request;
  248. StreamingInputCallResponse response;
  249. std::unique_ptr<ClientWriter<StreamingInputCallRequest>> stream(
  250. stub->StreamingInputCall(&context, &response));
  251. int aggregated_payload_size = 0;
  252. for (unsigned int i = 0; i < request_stream_sizes.size(); ++i) {
  253. Payload* payload = request.mutable_payload();
  254. payload->set_body(grpc::string(request_stream_sizes[i], '\0'));
  255. GPR_ASSERT(stream->Write(request));
  256. aggregated_payload_size += request_stream_sizes[i];
  257. }
  258. stream->WritesDone();
  259. Status s = stream->Finish();
  260. GPR_ASSERT(response.aggregated_payload_size() == aggregated_payload_size);
  261. AssertOkOrPrintErrorStatus(s);
  262. gpr_log(GPR_INFO, "Request streaming done.");
  263. }
  264. void InteropClient::DoResponseStreaming() {
  265. gpr_log(GPR_INFO, "Receiving response steaming rpc ...");
  266. std::unique_ptr<TestService::Stub> stub(TestService::NewStub(channel_));
  267. ClientContext context;
  268. StreamingOutputCallRequest request;
  269. for (unsigned int i = 0; i < response_stream_sizes.size(); ++i) {
  270. ResponseParameters* response_parameter = request.add_response_parameters();
  271. response_parameter->set_size(response_stream_sizes[i]);
  272. }
  273. StreamingOutputCallResponse response;
  274. std::unique_ptr<ClientReader<StreamingOutputCallResponse>> stream(
  275. stub->StreamingOutputCall(&context, request));
  276. unsigned int i = 0;
  277. while (stream->Read(&response)) {
  278. GPR_ASSERT(response.payload().body() ==
  279. grpc::string(response_stream_sizes[i], '\0'));
  280. ++i;
  281. }
  282. GPR_ASSERT(response_stream_sizes.size() == i);
  283. Status s = stream->Finish();
  284. AssertOkOrPrintErrorStatus(s);
  285. gpr_log(GPR_INFO, "Response streaming done.");
  286. }
  287. void InteropClient::DoResponseCompressedStreaming() {
  288. std::unique_ptr<TestService::Stub> stub(TestService::NewStub(channel_));
  289. const CompressionType compression_types[] = {NONE, GZIP, DEFLATE};
  290. const PayloadType payload_types[] = {COMPRESSABLE, UNCOMPRESSABLE, RANDOM};
  291. for (const auto payload_type : payload_types) {
  292. for (const auto compression_type : compression_types) {
  293. ClientContext context;
  294. InteropClientContextInspector inspector(context);
  295. StreamingOutputCallRequest request;
  296. char* log_suffix;
  297. gpr_asprintf(&log_suffix, "(compression=%s; payload=%s)",
  298. CompressionType_Name(compression_type).c_str(),
  299. PayloadType_Name(payload_type).c_str());
  300. gpr_log(GPR_INFO, "Receiving response steaming rpc %s.", log_suffix);
  301. request.set_response_type(payload_type);
  302. request.set_response_compression(compression_type);
  303. for (unsigned int i = 0; i < response_stream_sizes.size(); ++i) {
  304. ResponseParameters* response_parameter =
  305. request.add_response_parameters();
  306. response_parameter->set_size(response_stream_sizes[i]);
  307. }
  308. StreamingOutputCallResponse response;
  309. std::unique_ptr<ClientReader<StreamingOutputCallResponse>> stream(
  310. stub->StreamingOutputCall(&context, request));
  311. unsigned int i = 0;
  312. while (stream->Read(&response)) {
  313. GPR_ASSERT(response.payload().body() ==
  314. grpc::string(response_stream_sizes[i], '\0'));
  315. // Compression related checks.
  316. GPR_ASSERT(request.response_compression() ==
  317. GetInteropCompressionTypeFromCompressionAlgorithm(
  318. inspector.GetCallCompressionAlgorithm()));
  319. if (request.response_compression() == NONE) {
  320. GPR_ASSERT(
  321. !(inspector.GetMessageFlags() & GRPC_WRITE_INTERNAL_COMPRESS));
  322. } else if (request.response_type() == PayloadType::COMPRESSABLE) {
  323. // requested compression and compressable response => results should
  324. // always be compressed.
  325. GPR_ASSERT(inspector.GetMessageFlags() &
  326. GRPC_WRITE_INTERNAL_COMPRESS);
  327. }
  328. ++i;
  329. }
  330. GPR_ASSERT(response_stream_sizes.size() == i);
  331. Status s = stream->Finish();
  332. AssertOkOrPrintErrorStatus(s);
  333. gpr_log(GPR_INFO, "Response streaming done %s.", log_suffix);
  334. gpr_free(log_suffix);
  335. }
  336. }
  337. }
  338. void InteropClient::DoResponseStreamingWithSlowConsumer() {
  339. gpr_log(GPR_INFO, "Receiving response steaming rpc with slow consumer ...");
  340. std::unique_ptr<TestService::Stub> stub(TestService::NewStub(channel_));
  341. ClientContext context;
  342. StreamingOutputCallRequest request;
  343. for (int i = 0; i < kNumResponseMessages; ++i) {
  344. ResponseParameters* response_parameter = request.add_response_parameters();
  345. response_parameter->set_size(kResponseMessageSize);
  346. }
  347. StreamingOutputCallResponse response;
  348. std::unique_ptr<ClientReader<StreamingOutputCallResponse>> stream(
  349. stub->StreamingOutputCall(&context, request));
  350. int i = 0;
  351. while (stream->Read(&response)) {
  352. GPR_ASSERT(response.payload().body() ==
  353. grpc::string(kResponseMessageSize, '\0'));
  354. gpr_log(GPR_INFO, "received message %d", i);
  355. usleep(kReceiveDelayMilliSeconds * 1000);
  356. ++i;
  357. }
  358. GPR_ASSERT(kNumResponseMessages == i);
  359. Status s = stream->Finish();
  360. AssertOkOrPrintErrorStatus(s);
  361. gpr_log(GPR_INFO, "Response streaming done.");
  362. }
  363. void InteropClient::DoHalfDuplex() {
  364. gpr_log(GPR_INFO, "Sending half-duplex streaming rpc ...");
  365. std::unique_ptr<TestService::Stub> stub(TestService::NewStub(channel_));
  366. ClientContext context;
  367. std::unique_ptr<ClientReaderWriter<StreamingOutputCallRequest,
  368. StreamingOutputCallResponse>>
  369. stream(stub->HalfDuplexCall(&context));
  370. StreamingOutputCallRequest request;
  371. ResponseParameters* response_parameter = request.add_response_parameters();
  372. for (unsigned int i = 0; i < response_stream_sizes.size(); ++i) {
  373. response_parameter->set_size(response_stream_sizes[i]);
  374. GPR_ASSERT(stream->Write(request));
  375. }
  376. stream->WritesDone();
  377. unsigned int i = 0;
  378. StreamingOutputCallResponse response;
  379. while (stream->Read(&response)) {
  380. GPR_ASSERT(response.payload().has_body());
  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().has_body());
  408. GPR_ASSERT(response.payload().body() ==
  409. grpc::string(response_stream_sizes[i], '\0'));
  410. }
  411. stream->WritesDone();
  412. GPR_ASSERT(!stream->Read(&response));
  413. Status s = stream->Finish();
  414. AssertOkOrPrintErrorStatus(s);
  415. gpr_log(GPR_INFO, "Ping pong streaming done.");
  416. }
  417. void InteropClient::DoCancelAfterBegin() {
  418. gpr_log(GPR_INFO, "Sending request steaming rpc ...");
  419. std::unique_ptr<TestService::Stub> stub(TestService::NewStub(channel_));
  420. ClientContext context;
  421. StreamingInputCallRequest request;
  422. StreamingInputCallResponse response;
  423. std::unique_ptr<ClientWriter<StreamingInputCallRequest>> stream(
  424. stub->StreamingInputCall(&context, &response));
  425. gpr_log(GPR_INFO, "Trying to cancel...");
  426. context.TryCancel();
  427. Status s = stream->Finish();
  428. GPR_ASSERT(s.error_code() == StatusCode::CANCELLED);
  429. gpr_log(GPR_INFO, "Canceling streaming done.");
  430. }
  431. void InteropClient::DoCancelAfterFirstResponse() {
  432. gpr_log(GPR_INFO, "Sending Ping Pong streaming rpc ...");
  433. std::unique_ptr<TestService::Stub> stub(TestService::NewStub(channel_));
  434. ClientContext context;
  435. std::unique_ptr<ClientReaderWriter<StreamingOutputCallRequest,
  436. StreamingOutputCallResponse>>
  437. stream(stub->FullDuplexCall(&context));
  438. StreamingOutputCallRequest request;
  439. request.set_response_type(PayloadType::COMPRESSABLE);
  440. ResponseParameters* response_parameter = request.add_response_parameters();
  441. response_parameter->set_size(31415);
  442. request.mutable_payload()->set_body(grpc::string(27182, '\0'));
  443. StreamingOutputCallResponse response;
  444. GPR_ASSERT(stream->Write(request));
  445. GPR_ASSERT(stream->Read(&response));
  446. GPR_ASSERT(response.payload().has_body());
  447. GPR_ASSERT(response.payload().body() == grpc::string(31415, '\0'));
  448. gpr_log(GPR_INFO, "Trying to cancel...");
  449. context.TryCancel();
  450. Status s = stream->Finish();
  451. gpr_log(GPR_INFO, "Canceling pingpong streaming done.");
  452. }
  453. void InteropClient::DoTimeoutOnSleepingServer() {
  454. gpr_log(GPR_INFO, "Sending Ping Pong streaming rpc with a short deadline...");
  455. std::unique_ptr<TestService::Stub> stub(TestService::NewStub(channel_));
  456. ClientContext context;
  457. std::chrono::system_clock::time_point deadline =
  458. std::chrono::system_clock::now() + std::chrono::milliseconds(1);
  459. context.set_deadline(deadline);
  460. std::unique_ptr<ClientReaderWriter<StreamingOutputCallRequest,
  461. StreamingOutputCallResponse>>
  462. stream(stub->FullDuplexCall(&context));
  463. StreamingOutputCallRequest request;
  464. request.mutable_payload()->set_body(grpc::string(27182, '\0'));
  465. stream->Write(request);
  466. Status s = stream->Finish();
  467. GPR_ASSERT(s.error_code() == StatusCode::DEADLINE_EXCEEDED);
  468. gpr_log(GPR_INFO, "Pingpong streaming timeout done.");
  469. }
  470. void InteropClient::DoStatusWithMessage() {
  471. gpr_log(GPR_INFO, "Sending RPC with a request for status code 2 and message");
  472. std::unique_ptr<TestService::Stub> stub(TestService::NewStub(channel_));
  473. ClientContext context;
  474. SimpleRequest request;
  475. SimpleResponse response;
  476. EchoStatus *requested_status = request.mutable_response_status();
  477. requested_status->set_code(grpc::StatusCode::UNKNOWN);
  478. grpc::string test_msg = "This is a test message";
  479. requested_status->set_message(test_msg);
  480. Status s = stub->UnaryCall(&context, request, &response);
  481. GPR_ASSERT(s.error_code() == grpc::StatusCode::UNKNOWN);
  482. GPR_ASSERT(s.error_message() == test_msg);
  483. gpr_log(GPR_INFO, "Done testing Status and Message");
  484. }
  485. } // namespace testing
  486. } // namespace grpc