Created
January 3, 2020 13:28
-
-
Save kirs/00c02ef92e0418578135fe0a6cbd3d7d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'net/http' | |
require 'socket' | |
# to run on Ubuntu/Debian with sudo access | |
def with_unresponsive_dns | |
fake_dns_ip = "127.0.0.1" | |
th = Thread.new do | |
server = UDPSocket.new | |
server.bind(fake_dns_ip, 53) | |
server.recvfrom(16) | |
sleep | |
end | |
original_dns = %x(cat /etc/resolv.conf |grep -i '^nameserver'|head -n1|cut -d ' ' -f2).strip | |
`echo "$(sed 's/#{original_dns}/#{fake_dns_ip}/g' /etc/resolv.conf)" > /etc/resolv.conf` | |
yield | |
ensure | |
`echo "$(sed 's/#{fake_dns_ip}/#{original_dns}/g' /etc/resolv.conf)" > /etc/resolv.conf` | |
th.kill | |
th.join | |
end | |
now = Time.now | |
begin | |
with_unresponsive_dns do | |
uri = URI('http://example.com/some_path?query=string') | |
puts "Started getaddrinfo. This should timeout after 1s" | |
Timeout.timeout(1) do | |
Socket.getaddrinfo("www.ruby-lang.org", "http") | |
end | |
end | |
ensure | |
puts "elapsed: #{Time.now - now}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment