diff options
author | eregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2018-08-03 16:19:40 +0000 |
---|---|---|
committer | eregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2018-08-03 16:19:40 +0000 |
commit | b53cf149ad8d7c46572e4567ca949b4f82ebb22c (patch) | |
tree | ee5032bcb38573dade8ba2c46acbcc0d5f3ddfe3 /spec/ruby/library/socket/fixtures/classes.rb | |
parent | aeeaadaad08038217440c1e9e7c5ca126d7dc633 (diff) |
Update to ruby/spec@9be7c7e
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64180 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'spec/ruby/library/socket/fixtures/classes.rb')
-rw-r--r-- | spec/ruby/library/socket/fixtures/classes.rb | 65 |
1 files changed, 60 insertions, 5 deletions
diff --git a/spec/ruby/library/socket/fixtures/classes.rb b/spec/ruby/library/socket/fixtures/classes.rb index b8e5d2a38d..1098b04a5e 100644 --- a/spec/ruby/library/socket/fixtures/classes.rb +++ b/spec/ruby/library/socket/fixtures/classes.rb @@ -1,16 +1,16 @@ require 'socket' module SocketSpecs - # helper to get the hostname associated to 127.0.0.1 - def self.hostname + # helper to get the hostname associated to 127.0.0.1 or the given ip + def self.hostname(ip = "127.0.0.1") # Calculate each time, without caching, since the result might # depend on things like do_not_reverse_lookup mode, which is # changing from test to test - Socket.getaddrinfo("127.0.0.1", nil)[0][2] + Socket.getaddrinfo(ip, nil)[0][2] end - def self.hostnamev6 - Socket.getaddrinfo("::1", nil)[0][2] + def self.hostname_reverse_lookup(ip = "127.0.0.1") + Socket.getaddrinfo(ip, nil, 0, 0, 0, 0, true)[0][2] end def self.addr(which=:ipv4) @@ -47,6 +47,61 @@ module SocketSpecs File.delete(path) if File.exist?(path) end + def self.ipv6_available? + @ipv6_available ||= begin + server = TCPServer.new('::1', 0) + rescue Errno::EADDRNOTAVAIL + :no + else + server.close + :yes + end + @ipv6_available == :yes + end + + def self.each_ip_protocol + describe 'using IPv4' do + yield Socket::AF_INET, '127.0.0.1', 'AF_INET' + end + + guard -> { SocketSpecs.ipv6_available? } do + describe 'using IPv6' do + yield Socket::AF_INET6, '::1', 'AF_INET6' + end + end + end + + def self.loop_with_timeout(timeout = 5) + require 'timeout' + time = Time.now + + loop do + if Time.now - time >= timeout + raise TimeoutError, "Did not succeed within #{timeout} seconds" + end + + sleep 0.01 # necessary on OSX; don't know why + yield + end + end + + def self.wait_until_success(timeout = 5) + loop_with_timeout(timeout) do + begin + return yield + rescue + end + end + end + + def self.dest_addr_req_error + error = Errno::EDESTADDRREQ + platform_is :windows do + error = Errno::ENOTCONN + end + error + end + # TCPServer echo server accepting one connection class SpecTCPServer attr_reader :hostname, :port |