2022-11-02 00:33:17 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
describe FinalDestination::Resolver do
|
|
|
|
let(:mock_response) { [Addrinfo.ip("1.1.1.1"), Addrinfo.ip("2.2.2.2")] }
|
|
|
|
|
|
|
|
before do
|
|
|
|
# No DNS lookups in tests
|
|
|
|
Addrinfo.stubs(:getaddrinfo).never
|
|
|
|
end
|
|
|
|
|
|
|
|
def alive_thread_count
|
|
|
|
Thread.list.filter(&:alive?).count
|
|
|
|
end
|
|
|
|
|
|
|
|
it "handles timeouts correctly" do
|
|
|
|
Addrinfo.stubs(:getaddrinfo).with { |addr| sleep if addr == "sleep.example.com" } # timeout
|
|
|
|
Addrinfo.stubs(:getaddrinfo).with { |addr| addr == "example.com" }.returns(mock_response)
|
|
|
|
|
|
|
|
expect {
|
2022-12-21 22:40:18 +08:00
|
|
|
result = FinalDestination::Resolver.lookup("sleep.example.com", timeout: 0.001)
|
2022-11-02 00:33:17 +08:00
|
|
|
}.to raise_error(Timeout::Error)
|
|
|
|
|
|
|
|
start_thread_count = alive_thread_count
|
|
|
|
|
|
|
|
expect {
|
2022-12-21 22:40:18 +08:00
|
|
|
result = FinalDestination::Resolver.lookup("sleep.example.com", timeout: 0.001)
|
2022-11-02 00:33:17 +08:00
|
|
|
}.to raise_error(Timeout::Error)
|
|
|
|
|
|
|
|
expect(alive_thread_count).to eq(start_thread_count)
|
|
|
|
|
|
|
|
expect(FinalDestination::Resolver.lookup("example.com")).to eq(%w[1.1.1.1 2.2.2.2])
|
|
|
|
|
|
|
|
# Thread available for reuse after successful lookup
|
|
|
|
expect(alive_thread_count).to eq(start_thread_count + 1)
|
|
|
|
end
|
|
|
|
|
2023-02-21 06:54:29 +08:00
|
|
|
it "reads default query timeout from configuration" do
|
|
|
|
GlobalSetting.stubs(:dns_query_timeout_secs).returns(123)
|
|
|
|
expect(FinalDestination::Resolver.send(:default_dns_query_timeout)).to eq(123)
|
|
|
|
end
|
|
|
|
|
2022-11-02 00:33:17 +08:00
|
|
|
it "can lookup correctly" do
|
|
|
|
Addrinfo.stubs(:getaddrinfo).with { |addr| addr == "example.com" }.returns(mock_response)
|
|
|
|
|
|
|
|
expect(FinalDestination::Resolver.lookup("example.com")).to eq(%w[1.1.1.1 2.2.2.2])
|
|
|
|
end
|
|
|
|
end
|