test-same-pending-query.cc 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*************************************************************************
  2. *
  3. * Copyright (C) 2018-2023 Ruilin Peng (Nick) <[email protected]>.
  4. *
  5. * smartdns is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * smartdns is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "client.h"
  19. #include "dns.h"
  20. #include "include/utils.h"
  21. #include "server.h"
  22. #include "util.h"
  23. #include "gtest/gtest.h"
  24. #include <fstream>
  25. class SamePending : public ::testing::Test
  26. {
  27. protected:
  28. virtual void SetUp() {}
  29. virtual void TearDown() {}
  30. };
  31. TEST_F(SamePending, pending)
  32. {
  33. smartdns::MockServer server_upstream;
  34. smartdns::Server server;
  35. std::map<int, int> qid_map;
  36. server_upstream.Start("udp://0.0.0.0:61053", [&](struct smartdns::ServerRequestContext *request) {
  37. std::string domain = request->domain;
  38. if (qid_map.find(request->packet->head.id) != qid_map.end()) {
  39. qid_map[request->packet->head.id]++;
  40. usleep(5000);
  41. } else {
  42. qid_map[request->packet->head.id] = 1;
  43. usleep(20000);
  44. }
  45. if (request->domain.length() == 0) {
  46. return smartdns::SERVER_REQUEST_ERROR;
  47. }
  48. if (request->qtype == DNS_T_A) {
  49. unsigned char addr[4] = {1, 2, 3, 4};
  50. dns_add_A(request->response_packet, DNS_RRS_AN, domain.c_str(), 61, addr);
  51. } else if (request->qtype == DNS_T_AAAA) {
  52. unsigned char addr[16] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
  53. dns_add_AAAA(request->response_packet, DNS_RRS_AN, domain.c_str(), 61, addr);
  54. } else {
  55. return smartdns::SERVER_REQUEST_ERROR;
  56. }
  57. request->response_packet->head.rcode = DNS_RC_NOERROR;
  58. return smartdns::SERVER_REQUEST_OK;
  59. });
  60. server.Start(R"""(bind [::]:60053
  61. server 127.0.0.1:61053
  62. cache-size 0
  63. speed-check-mode none
  64. log-level error
  65. )""");
  66. std::vector<std::thread> threads;
  67. uint64_t tick = get_tick_count();
  68. for (int i = 0; i < 5; i++) {
  69. auto t = std::thread([&]() {
  70. for (int j = 0; j < 10; j++) {
  71. smartdns::Client client;
  72. ASSERT_TRUE(client.Query("a.com", 60053));
  73. ASSERT_EQ(client.GetAnswerNum(), 1);
  74. EXPECT_EQ(client.GetStatus(), "NOERROR");
  75. EXPECT_EQ(client.GetAnswer()[0].GetName(), "a.com");
  76. EXPECT_EQ(client.GetAnswer()[0].GetData(), "1.2.3.4");
  77. }
  78. });
  79. threads.push_back(std::move(t));
  80. }
  81. for (auto &t : threads) {
  82. t.join();
  83. }
  84. EXPECT_LT(qid_map.size(), 80);
  85. }