YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/util/net/dns_resolver-test.cc
Line
Count
Source (jump to first uncovered line)
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  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,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
//
18
// The following only applies to changes made to this file as part of YugaByte development.
19
//
20
// Portions Copyright (c) YugaByte, Inc.
21
//
22
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
23
// in compliance with the License.  You may obtain a copy of the License at
24
//
25
// http://www.apache.org/licenses/LICENSE-2.0
26
//
27
// Unless required by applicable law or agreed to in writing, software distributed under the License
28
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
29
// or implied.  See the License for the specific language governing permissions and limitations
30
// under the License.
31
//
32
33
#include "yb/util/net/dns_resolver.h"
34
35
#include <vector>
36
37
#include <boost/optional/optional.hpp>
38
39
#include <gtest/gtest.h>
40
41
#include "yb/gutil/strings/util.h"
42
#include "yb/util/countdown_latch.h"
43
#include "yb/util/net/sockaddr.h"
44
#include "yb/util/status_log.h"
45
#include "yb/util/test_macros.h"
46
#include "yb/util/test_util.h"
47
#include "yb/util/thread.h"
48
49
using namespace std::literals;
50
51
namespace yb {
52
53
class DnsResolverTest : public YBTest {
54
 protected:
55
1
  void SetUp() override {
56
1
    io_thread_ = CHECK_RESULT(Thread::Make("io_thread", "io_thread", [this] {
57
1
      boost::system::error_code ec;
58
1
      io_service_.run(ec);
59
1
      LOG_IF(ERROR, ec) << "Failed to run io service: " << ec;
60
1
    }));
61
1
  }
62
63
1
  void TearDown() override {
64
1
    work_.reset();
65
1
    Join();
66
1
  }
67
68
 protected:
69
1
  void Join() {
70
1
    auto deadline = std::chrono::steady_clock::now() + 15s;
71
1
    while (!io_service_.stopped()) {
72
0
      if (std::chrono::steady_clock::now() >= deadline) {
73
0
        LOG(ERROR) << "Io service failed to stop";
74
0
        io_service_.stop();
75
0
        break;
76
0
      }
77
0
      std::this_thread::sleep_for(10ms);
78
0
    }
79
1
    io_thread_->Join();
80
1
  }
81
82
  ThreadPtr io_thread_;
83
  IoService io_service_;
84
  boost::optional<IoService::work> work_{io_service_};
85
86
  DnsResolver resolver_{&io_service_};
87
};
88
89
1
TEST_F(DnsResolverTest, TestResolution) {
90
1
  auto future = resolver_.ResolveFuture("localhost");
91
1
  ASSERT_EQ(future.wait_for(1s), std::future_status::ready);
92
1
  auto addr = ASSERT_RESULT(Copy(future.get()));
93
1
  LOG(INFO) << "Address: " << addr;
94
1
  if (addr.is_v4()) {
95
1
    EXPECT_TRUE(HasPrefixString(ToString(addr), "127."));
96
0
  } else if (addr.is_v6()) {
97
0
    EXPECT_TRUE(HasPrefixString(ToString(addr), "[::1]"));
98
0
  }
99
1
}
100
101
} // namespace yb