server_crash_test_client.cc 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. #include <iostream>
  19. #include <memory>
  20. #include <sstream>
  21. #include <string>
  22. #include "absl/flags/flag.h"
  23. #include <grpc/support/log.h>
  24. #include <grpcpp/channel.h>
  25. #include <grpcpp/client_context.h>
  26. #include <grpcpp/create_channel.h>
  27. #include "src/proto/grpc/testing/echo.grpc.pb.h"
  28. #include "test/cpp/util/test_config.h"
  29. ABSL_FLAG(std::string, address, "", "Address to connect to");
  30. ABSL_FLAG(std::string, mode, "", "Test mode to use");
  31. using grpc::testing::EchoRequest;
  32. using grpc::testing::EchoResponse;
  33. int main(int argc, char** argv) {
  34. grpc::testing::InitTest(&argc, &argv, true);
  35. auto stub = grpc::testing::EchoTestService::NewStub(grpc::CreateChannel(
  36. absl::GetFlag(FLAGS_address), grpc::InsecureChannelCredentials()));
  37. EchoRequest request;
  38. EchoResponse response;
  39. grpc::ClientContext context;
  40. context.set_wait_for_ready(true);
  41. if (absl::GetFlag(FLAGS_mode) == "bidi") {
  42. auto stream = stub->BidiStream(&context);
  43. for (int i = 0;; i++) {
  44. std::ostringstream msg;
  45. msg << "Hello " << i;
  46. request.set_message(msg.str());
  47. GPR_ASSERT(stream->Write(request));
  48. GPR_ASSERT(stream->Read(&response));
  49. GPR_ASSERT(response.message() == request.message());
  50. }
  51. } else if (absl::GetFlag(FLAGS_mode) == "response") {
  52. EchoRequest request;
  53. request.set_message("Hello");
  54. auto stream = stub->ResponseStream(&context, request);
  55. for (;;) {
  56. GPR_ASSERT(stream->Read(&response));
  57. }
  58. } else {
  59. gpr_log(GPR_ERROR, "invalid test mode '%s'",
  60. absl::GetFlag(FLAGS_mode).c_str());
  61. return 1;
  62. }
  63. }