Skip to content

Commit 2e60e56

Browse files
committed
Add tests for connecting to IPv4 and IPv6
1 parent c6e000b commit 2e60e56

File tree

3 files changed

+54
-8
lines changed

3 files changed

+54
-8
lines changed

test/helper.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,13 +148,13 @@ def teardown
148148
end
149149

150150
def redis_mock(commands, options = {}, &blk)
151-
RedisMock.start(commands) do |port|
151+
RedisMock.start(commands, options) do |port|
152152
yield _new_client(options.merge(:port => port))
153153
end
154154
end
155155

156156
def redis_mock_with_handler(handler, options = {}, &blk)
157-
RedisMock.start_with_handler(handler) do |port|
157+
RedisMock.start_with_handler(handler, options) do |port|
158158
yield _new_client(options.merge(:port => port))
159159
end
160160
end

test/internals_test.rb

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,4 +348,50 @@ def test_resolves_localhost
348348
Redis.new(OPTIONS.merge(:host => 'localhost')).ping
349349
end
350350
end
351+
352+
class << self
353+
def af_family_supported(af)
354+
hosts = {
355+
Socket::AF_INET => "127.0.0.1",
356+
Socket::AF_INET6 => "::1",
357+
}
358+
359+
begin
360+
s = Socket.new(af, Socket::SOCK_STREAM, 0)
361+
begin
362+
sa = Socket.pack_sockaddr_in(9999, hosts[af])
363+
s.bind(sa)
364+
yield
365+
rescue Errno::EADDRNOTAVAIL
366+
ensure
367+
s.close
368+
end
369+
rescue Errno::ESOCKTNOSUPPORT
370+
end
371+
end
372+
end
373+
374+
def af_test(host)
375+
commands = {
376+
:ping => lambda { |*_| "+pong" },
377+
}
378+
379+
redis_mock(commands, :host => host) do |redis|
380+
assert_nothing_raised do
381+
redis.ping
382+
end
383+
end
384+
end
385+
386+
af_family_supported(Socket::AF_INET) do
387+
def test_connect_ipv4
388+
af_test("127.0.0.1")
389+
end
390+
end
391+
392+
af_family_supported(Socket::AF_INET6) do
393+
def test_connect_ipv6
394+
af_test("::1")
395+
end
396+
end
351397
end

test/support/redis_mock.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ module RedisMock
44
class Server
55
VERBOSE = false
66

7-
def initialize(port, &block)
8-
@server = TCPServer.new("127.0.0.1", port)
7+
def initialize(port, options = {}, &block)
8+
@server = TCPServer.new(options[:host] || "127.0.0.1", port)
99
@server.setsockopt(Socket::SOL_SOCKET,Socket::SO_REUSEADDR, true)
1010
end
1111

@@ -53,8 +53,8 @@ def run
5353
# # Every connection will be closed immediately
5454
# end
5555
#
56-
def self.start_with_handler(blk)
57-
server = Server.new(MOCK_PORT)
56+
def self.start_with_handler(blk, options = {})
57+
server = Server.new(MOCK_PORT, options)
5858

5959
begin
6060
server.start(&blk)
@@ -75,7 +75,7 @@ def self.start_with_handler(blk)
7575
# assert_equal "PONG", Redis.new(:port => MOCK_PORT).ping
7676
# end
7777
#
78-
def self.start(commands = {}, &blk)
78+
def self.start(commands, options = {}, &blk)
7979
handler = lambda do |session|
8080
while line = session.gets
8181
argv = Array.new(line[1..-3].to_i) do
@@ -110,6 +110,6 @@ def self.start(commands = {}, &blk)
110110
end
111111
end
112112

113-
start_with_handler(handler, &blk)
113+
start_with_handler(handler, options, &blk)
114114
end
115115
end

0 commit comments

Comments
 (0)