socket_test.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Copyright 2019 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "socket.h"
  15. #include "gmock/gmock.h"
  16. #include "gtest/gtest.h"
  17. #include <chrono>
  18. #include <thread>
  19. #include <vector>
  20. // Basic socket send & receive test
  21. TEST(Socket, SendRecv) {
  22. const char* port = "19021";
  23. auto server = dap::Socket("localhost", port);
  24. auto client = dap::Socket::connect("localhost", port, 0);
  25. ASSERT_TRUE(client != nullptr);
  26. const std::string expect = "Hello World!";
  27. std::string read;
  28. auto thread = std::thread([&] {
  29. auto conn = server.accept();
  30. ASSERT_TRUE(conn != nullptr);
  31. char c;
  32. while (conn->read(&c, 1) != 0) {
  33. read += c;
  34. }
  35. });
  36. ASSERT_TRUE(client->write(expect.data(), expect.size()));
  37. client->close();
  38. thread.join();
  39. ASSERT_EQ(read, expect);
  40. }
  41. // See https://github.com/google/cppdap/issues/37
  42. TEST(Socket, CloseOnDifferentThread) {
  43. const char* port = "19021";
  44. auto server = dap::Socket("localhost", port);
  45. auto client = dap::Socket::connect("localhost", port, 0);
  46. ASSERT_TRUE(client != nullptr);
  47. auto conn = server.accept();
  48. auto thread = std::thread([&] {
  49. // Closing client on different thread should unblock client->read().
  50. client->close();
  51. });
  52. char c;
  53. while (client->read(&c, 1) != 0) {
  54. }
  55. thread.join();
  56. }
  57. TEST(Socket, ConnectTimeout) {
  58. const char* port = "19021";
  59. const int timeoutMillis = 200;
  60. const int maxAttempts = 1024;
  61. using namespace std::chrono;
  62. auto server = dap::Socket("localhost", port);
  63. std::vector<std::shared_ptr<dap::ReaderWriter>> connections;
  64. for (int i = 0; i < maxAttempts; i++) {
  65. auto start = system_clock::now();
  66. auto connection = dap::Socket::connect("localhost", port, timeoutMillis);
  67. auto end = system_clock::now();
  68. if (!connection) {
  69. auto timeTakenMillis = duration_cast<milliseconds>(end - start).count();
  70. ASSERT_GE(timeTakenMillis + 20, // +20ms for a bit of timing wiggle room
  71. timeoutMillis);
  72. return;
  73. }
  74. // Keep hold of the connections to saturate any incoming socket buffers.
  75. connections.emplace_back(std::move(connection));
  76. }
  77. FAIL() << "Failed to test timeout after " << maxAttempts << " attempts";
  78. }