interop_client.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. *
  3. * Copyright 2015 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. #ifndef GRPC_TEST_CPP_INTEROP_INTEROP_CLIENT_H
  19. #define GRPC_TEST_CPP_INTEROP_INTEROP_CLIENT_H
  20. #include <memory>
  21. #include <grpc/grpc.h>
  22. #include <grpcpp/channel.h>
  23. #include "src/proto/grpc/testing/messages.pb.h"
  24. #include "src/proto/grpc/testing/test.grpc.pb.h"
  25. namespace grpc {
  26. namespace testing {
  27. // Function pointer for custom checks.
  28. typedef std::function<void(const InteropClientContextInspector&,
  29. const SimpleRequest*, const SimpleResponse*)>
  30. CheckerFn;
  31. typedef std::function<std::shared_ptr<Channel>(void)> ChannelCreationFunc;
  32. class InteropClient {
  33. public:
  34. /// If new_stub_every_test_case is true, a new TestService::Stub object is
  35. /// created for every test case
  36. /// If do_not_abort_on_transient_failures is true, abort() is not called in
  37. /// case of transient failures (like connection failures)
  38. explicit InteropClient(ChannelCreationFunc channel_creation_func,
  39. bool new_stub_every_test_case,
  40. bool do_not_abort_on_transient_failures);
  41. ~InteropClient() {}
  42. void Reset(const std::shared_ptr<Channel>& channel);
  43. bool DoEmpty();
  44. bool DoLargeUnary();
  45. bool DoServerCompressedUnary();
  46. bool DoClientCompressedUnary();
  47. bool DoPingPong();
  48. bool DoHalfDuplex();
  49. bool DoRequestStreaming();
  50. bool DoResponseStreaming();
  51. bool DoServerCompressedStreaming();
  52. bool DoClientCompressedStreaming();
  53. bool DoResponseStreamingWithSlowConsumer();
  54. bool DoCancelAfterBegin();
  55. bool DoCancelAfterFirstResponse();
  56. bool DoTimeoutOnSleepingServer();
  57. bool DoEmptyStream();
  58. bool DoStatusWithMessage();
  59. // Verifies Unicode and Whitespace is correctly processed in status message.
  60. bool DoSpecialStatusMessage();
  61. bool DoCustomMetadata();
  62. bool DoUnimplementedMethod();
  63. bool DoUnimplementedService();
  64. bool DoCacheableUnary();
  65. // all requests are sent to one server despite multiple servers are resolved
  66. bool DoPickFirstUnary();
  67. // The following interop test are not yet part of the interop spec, and are
  68. // not implemented cross-language. They are considered experimental for now,
  69. // but at some point in the future, might be codified and implemented in all
  70. // languages
  71. bool DoChannelSoakTest(int32_t soak_iterations, int32_t max_failures,
  72. int64_t max_acceptable_per_iteration_latency_ms,
  73. int32_t overall_timeout_seconds);
  74. bool DoRpcSoakTest(int32_t soak_iterations, int32_t max_failures,
  75. int64_t max_acceptable_per_iteration_latency_ms,
  76. int32_t overall_timeout_seconds);
  77. bool DoLongLivedChannelTest(int32_t soak_iterations,
  78. int32_t iteration_interval);
  79. // Auth tests.
  80. // username is a string containing the user email
  81. bool DoJwtTokenCreds(const std::string& username);
  82. bool DoComputeEngineCreds(const std::string& default_service_account,
  83. const std::string& oauth_scope);
  84. // username the GCE default service account email
  85. bool DoOauth2AuthToken(const std::string& username,
  86. const std::string& oauth_scope);
  87. // username is a string containing the user email
  88. bool DoPerRpcCreds(const std::string& json_key);
  89. // default_service_account is the GCE default service account email
  90. bool DoGoogleDefaultCredentials(const std::string& default_service_account);
  91. private:
  92. class ServiceStub {
  93. public:
  94. // If new_stub_every_call = true, pointer to a new instance of
  95. // TestServce::Stub is returned by Get() everytime it is called
  96. ServiceStub(ChannelCreationFunc channel_creation_func,
  97. bool new_stub_every_call);
  98. TestService::Stub* Get();
  99. UnimplementedService::Stub* GetUnimplementedServiceStub();
  100. // forces channel to be recreated.
  101. void ResetChannel();
  102. private:
  103. ChannelCreationFunc channel_creation_func_;
  104. std::unique_ptr<TestService::Stub> stub_;
  105. std::unique_ptr<UnimplementedService::Stub> unimplemented_service_stub_;
  106. std::shared_ptr<Channel> channel_;
  107. bool new_stub_every_call_; // If true, a new stub is returned by every
  108. // Get() call
  109. };
  110. bool PerformLargeUnary(SimpleRequest* request, SimpleResponse* response);
  111. /// Run \a custom_check_fn as an additional check.
  112. bool PerformLargeUnary(SimpleRequest* request, SimpleResponse* response,
  113. const CheckerFn& custom_checks_fn);
  114. bool AssertStatusOk(const Status& s,
  115. const std::string& optional_debug_string);
  116. bool AssertStatusCode(const Status& s, StatusCode expected_code,
  117. const std::string& optional_debug_string);
  118. bool TransientFailureOrAbort();
  119. std::tuple<bool, int32_t, std::string> PerformOneSoakTestIteration(
  120. const bool reset_channel,
  121. const int32_t max_acceptable_per_iteration_latency_ms);
  122. void PerformSoakTest(const bool reset_channel_per_iteration,
  123. const int32_t soak_iterations,
  124. const int32_t max_failures,
  125. const int32_t max_acceptable_per_iteration_latency_ms,
  126. const int32_t overall_timeout_seconds);
  127. ServiceStub serviceStub_;
  128. /// If true, abort() is not called for transient failures
  129. bool do_not_abort_on_transient_failures_;
  130. };
  131. } // namespace testing
  132. } // namespace grpc
  133. #endif // GRPC_TEST_CPP_INTEROP_INTEROP_CLIENT_H