fling_test.cc 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 <stdio.h>
  19. #include <string.h>
  20. #include <string>
  21. #include "absl/strings/str_cat.h"
  22. #include <grpc/support/alloc.h>
  23. #include "src/core/lib/gprpp/host_port.h"
  24. #include "test/core/util/port.h"
  25. #include "test/core/util/subprocess.h"
  26. int main(int /*argc*/, const char** argv) {
  27. const char* me = argv[0];
  28. const char* lslash = strrchr(me, '/');
  29. char root[1024];
  30. int port = grpc_pick_unused_port_or_die();
  31. char* args[10];
  32. int status;
  33. gpr_subprocess *svr, *cli;
  34. /* figure out where we are */
  35. if (lslash) {
  36. memcpy(root, me, static_cast<size_t>(lslash - me));
  37. root[lslash - me] = 0;
  38. } else {
  39. strcpy(root, ".");
  40. }
  41. /* start the server */
  42. std::string command =
  43. absl::StrCat(root, "/fling_server", gpr_subprocess_binary_extension());
  44. args[0] = const_cast<char*>(command.c_str());
  45. args[1] = const_cast<char*>("--bind");
  46. std::string joined = grpc_core::JoinHostPort("::", port);
  47. args[2] = const_cast<char*>(joined.c_str());
  48. args[3] = const_cast<char*>("--no-secure");
  49. svr = gpr_subprocess_create(4, const_cast<const char**>(args));
  50. /* start the client */
  51. command =
  52. absl::StrCat(root, "/fling_client", gpr_subprocess_binary_extension());
  53. args[0] = const_cast<char*>(command.c_str());
  54. args[1] = const_cast<char*>("--target");
  55. joined = grpc_core::JoinHostPort("127.0.0.1", port);
  56. args[2] = const_cast<char*>(joined.c_str());
  57. args[3] = const_cast<char*>("--scenario=ping-pong-request");
  58. args[4] = const_cast<char*>("--no-secure");
  59. args[5] = nullptr;
  60. cli = gpr_subprocess_create(6, const_cast<const char**>(args));
  61. /* wait for completion */
  62. printf("waiting for client\n");
  63. if ((status = gpr_subprocess_join(cli))) {
  64. gpr_subprocess_destroy(cli);
  65. gpr_subprocess_destroy(svr);
  66. return status;
  67. }
  68. gpr_subprocess_destroy(cli);
  69. gpr_subprocess_interrupt(svr);
  70. status = gpr_subprocess_join(svr);
  71. gpr_subprocess_destroy(svr);
  72. return status;
  73. }