Skip to content

Commit c6e000b

Browse files
committed
Add test for resolving a host to an address
The IPv6 change in b146d74 made a bunch of tests fail because they could not connect to 'localhost'. They could not connect because only the first result from getaddrinfo(3) was used, which happened to be the IPv6 localhost address. This commit changes those tests to use 127.0.0.1 and adds a test that explicitly tests that the client can resolve localhost.
1 parent 90ab29b commit c6e000b

7 files changed

+29
-23
lines changed

test/commands_on_value_types_test.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def test_flushall
9999

100100
def test_migrate
101101
redis_mock(:migrate => lambda { |*args| args }) do |redis|
102-
options = { :host => "localhost", :port => 1234 }
102+
options = { :host => "127.0.0.1", :port => 1234 }
103103

104104
assert_raise(RuntimeError, /host not specified/) do
105105
redis.migrate("foo", options.reject { |key, _| key == :host })
@@ -114,17 +114,17 @@ def test_migrate
114114

115115
# Test defaults
116116
actual = redis.migrate("foo", options)
117-
expected = ["localhost", "1234", "foo", default_db.to_s, default_timeout.to_s]
117+
expected = ["127.0.0.1", "1234", "foo", default_db.to_s, default_timeout.to_s]
118118
assert_equal expected, actual
119119

120120
# Test db override
121121
actual = redis.migrate("foo", options.merge(:db => default_db + 1))
122-
expected = ["localhost", "1234", "foo", (default_db + 1).to_s, default_timeout.to_s]
122+
expected = ["127.0.0.1", "1234", "foo", (default_db + 1).to_s, default_timeout.to_s]
123123
assert_equal expected, actual
124124

125125
# Test timeout override
126126
actual = redis.migrate("foo", options.merge(:timeout => default_timeout + 1))
127-
expected = ["localhost", "1234", "foo", default_db.to_s, (default_timeout + 1).to_s]
127+
expected = ["127.0.0.1", "1234", "foo", default_db.to_s, (default_timeout + 1).to_s]
128128
assert_equal expected, actual
129129
end
130130
end

test/connection_handling_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ def test_shutdown_with_error_from_multi_exec
157157

158158
def test_slaveof
159159
redis_mock(:slaveof => lambda { |host, port| "+SLAVEOF #{host} #{port}" }) do |redis|
160-
assert_equal "SLAVEOF localhost 6381", redis.slaveof("localhost", 6381)
160+
assert_equal "SLAVEOF somehost 6381", redis.slaveof("somehost", 6381)
161161
end
162162
end
163163

test/distributed_internals_test.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,32 @@ class TestDistributedInternals < Test::Unit::TestCase
77
include Helper::Distributed
88

99
def test_provides_a_meaningful_inspect
10-
nodes = ["redis://localhost:#{PORT}/15", *NODES]
10+
nodes = ["redis://127.0.0.1:#{PORT}/15", *NODES]
1111
redis = Redis::Distributed.new nodes
1212

1313
assert_equal "#<Redis client v#{Redis::VERSION} for #{redis.nodes.map(&:id).join(', ')}>", redis.inspect
1414
end
1515

1616
def test_default_as_urls
17-
nodes = ["redis://localhost:#{PORT}/15", *NODES]
17+
nodes = ["redis://127.0.0.1:#{PORT}/15", *NODES]
1818
redis = Redis::Distributed.new nodes
19-
assert_equal ["redis://localhost:#{PORT}/15", *NODES], redis.nodes.map { |node| node.client.id}
19+
assert_equal ["redis://127.0.0.1:#{PORT}/15", *NODES], redis.nodes.map { |node| node.client.id}
2020
end
2121

2222
def test_default_as_config_hashes
23-
nodes = [OPTIONS.merge(:host => 'localhost'), OPTIONS.merge(:host => 'localhost', :port => PORT.next)]
23+
nodes = [OPTIONS.merge(:host => '127.0.0.1'), OPTIONS.merge(:host => 'somehost', :port => PORT.next)]
2424
redis = Redis::Distributed.new nodes
25-
assert_equal ["redis://localhost:#{PORT}/15","redis://localhost:#{PORT.next}/15"], redis.nodes.map { |node| node.client.id }
25+
assert_equal ["redis://127.0.0.1:#{PORT}/15","redis://somehost:#{PORT.next}/15"], redis.nodes.map { |node| node.client.id }
2626
end
2727

2828
def test_as_mix_and_match
29-
nodes = ["redis://localhost:7389/15", OPTIONS.merge(:host => 'localhost'), OPTIONS.merge(:host => 'localhost', :port => PORT.next)]
29+
nodes = ["redis://127.0.0.1:7389/15", OPTIONS.merge(:host => 'somehost'), OPTIONS.merge(:host => 'somehost', :port => PORT.next)]
3030
redis = Redis::Distributed.new nodes
31-
assert_equal ["redis://localhost:7389/15", "redis://localhost:#{PORT}/15", "redis://localhost:#{PORT.next}/15"], redis.nodes.map { |node| node.client.id }
31+
assert_equal ["redis://127.0.0.1:7389/15", "redis://somehost:#{PORT}/15", "redis://somehost:#{PORT.next}/15"], redis.nodes.map { |node| node.client.id }
3232
end
3333

3434
def test_override_id
35-
nodes = [OPTIONS.merge(:host => 'localhost', :id => "test"), OPTIONS.merge( :host => 'localhost', :port => PORT.next, :id => "test1")]
35+
nodes = [OPTIONS.merge(:host => '127.0.0.1', :id => "test"), OPTIONS.merge( :host => 'somehost', :port => PORT.next, :id => "test1")]
3636
redis = Redis::Distributed.new nodes
3737
assert_equal redis.nodes.first.client.id, "test"
3838
assert_equal redis.nodes.last.client.id, "test1"

test/distributed_key_tags_test.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@ class TestDistributedKeyTags < Test::Unit::TestCase
88
include Helper::Distributed
99

1010
def test_hashes_consistently
11-
r1 = Redis::Distributed.new ["redis://localhost:#{PORT}/15", *NODES]
12-
r2 = Redis::Distributed.new ["redis://localhost:#{PORT}/15", *NODES]
13-
r3 = Redis::Distributed.new ["redis://localhost:#{PORT}/15", *NODES]
11+
r1 = Redis::Distributed.new ["redis://127.0.0.1:#{PORT}/15", *NODES]
12+
r2 = Redis::Distributed.new ["redis://127.0.0.1:#{PORT}/15", *NODES]
13+
r3 = Redis::Distributed.new ["redis://127.0.0.1:#{PORT}/15", *NODES]
1414

1515
assert_equal r1.node_for("foo").id, r2.node_for("foo").id
1616
assert_equal r1.node_for("foo").id, r3.node_for("foo").id
1717
end
1818

1919
def test_allows_clustering_of_keys
2020
r = Redis::Distributed.new(NODES)
21-
r.add_node("redis://localhost:#{PORT}/14")
21+
r.add_node("redis://127.0.0.1:#{PORT}/14")
2222
r.flushdb
2323

2424
100.times do |i|
@@ -29,7 +29,7 @@ def test_allows_clustering_of_keys
2929
end
3030

3131
def test_distributes_keys_if_no_clustering_is_used
32-
r.add_node("redis://localhost:#{PORT}/14")
32+
r.add_node("redis://127.0.0.1:#{PORT}/14")
3333
r.flushdb
3434

3535
r.set "users:1", 1
@@ -40,7 +40,7 @@ def test_distributes_keys_if_no_clustering_is_used
4040

4141
def test_allows_passing_a_custom_tag_extractor
4242
r = Redis::Distributed.new(NODES, :tag => /^(.+?):/)
43-
r.add_node("redis://localhost:#{PORT}/14")
43+
r.add_node("redis://127.0.0.1:#{PORT}/14")
4444
r.flushdb
4545

4646
100.times do |i|

test/distributed_test.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class TestDistributed < Test::Unit::TestCase
77
include Helper::Distributed
88

99
def test_handle_multiple_servers
10-
@r = Redis::Distributed.new ["redis://localhost:#{PORT}/15", *NODES]
10+
@r = Redis::Distributed.new ["redis://127.0.0.1:#{PORT}/15", *NODES]
1111

1212
100.times do |idx|
1313
@r.set(idx.to_s, "foo#{idx}")
@@ -32,9 +32,9 @@ def test_add_nodes
3232
assert_equal 10, @r.nodes[0].client.timeout
3333
assert_equal logger, @r.nodes[0].client.logger
3434

35-
@r.add_node("redis://localhost:6380/14")
35+
@r.add_node("redis://127.0.0.1:6380/14")
3636

37-
assert_equal "localhost", @r.nodes[1].client.host
37+
assert_equal "127.0.0.1", @r.nodes[1].client.host
3838
assert_equal 6380, @r.nodes[1].client.port
3939
assert_equal 14, @r.nodes[1].client.db
4040
assert_equal 10, @r.nodes[1].client.timeout

test/internals_test.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,4 +342,10 @@ def test_does_not_change_self_client_options
342342
assert_equal 1, redis.client.options[:db]
343343
assert_equal "foo", redis.client.options[:scheme]
344344
end
345+
346+
def test_resolves_localhost
347+
assert_nothing_raised do
348+
Redis.new(OPTIONS.merge(:host => 'localhost')).ping
349+
end
350+
end
345351
end

test/publish_subscribe_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ def test_unsubscribe_without_a_subscribe
191191
def test_subscribe_past_a_timeout
192192
# For some reason, a thread here doesn't reproduce the issue.
193193
sleep = %{sleep #{OPTIONS[:timeout] * 2}}
194-
publish = %{echo "publish foo bar\r\n" | nc localhost #{OPTIONS[:port]}}
194+
publish = %{echo "publish foo bar\r\n" | nc 127.0.0.1 #{OPTIONS[:port]}}
195195
cmd = [sleep, publish].join("; ")
196196

197197
IO.popen(cmd, "r+") do |pipe|

0 commit comments

Comments
 (0)