Skip to content

Commit 497ca8f

Browse files
committed
Redis#connection is the official way to get connection details.
1 parent 55f2578 commit 497ca8f

File tree

4 files changed

+70
-40
lines changed

4 files changed

+70
-40
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
* `Redis::Distributed` now supports `sscan` and `sscan_each`. See #572.
66

7+
* `Redis#connection` returns a hash with connection information.
8+
You shouldn't need to call `Redis#_client`, ever.
9+
710

811
# 4.0
912

lib/redis.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2749,6 +2749,16 @@ def dup
27492749
self.class.new(@options)
27502750
end
27512751

2752+
def connection
2753+
{
2754+
host: @original_client.host,
2755+
port: @original_client.port,
2756+
db: @original_client.db,
2757+
id: @original_client.id,
2758+
location: @original_client.location
2759+
}
2760+
end
2761+
27522762
def method_missing(command, *args)
27532763
synchronize do |client|
27542764
client.call([command] + args)

test/connection_test.rb

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
require_relative "helper"
2+
3+
class TestConnection < Test::Unit::TestCase
4+
5+
include Helper::Client
6+
7+
def test_provides_a_meaningful_inspect
8+
assert_equal "#<Redis client v#{Redis::VERSION} for redis://127.0.0.1:#{PORT}/15>", r.inspect
9+
end
10+
11+
def test_connection_information
12+
assert_equal "127.0.0.1", r.connection.fetch(:host)
13+
assert_equal 6381, r.connection.fetch(:port)
14+
assert_equal 15, r.connection.fetch(:db)
15+
assert_equal "127.0.0.1:6381", r.connection.fetch(:location)
16+
assert_equal "redis://127.0.0.1:6381/15", r.connection.fetch(:id)
17+
end
18+
19+
def test_default_id_with_host_and_port
20+
redis = Redis.new(OPTIONS.merge(:host => "host", :port => "1234", :db => 0))
21+
assert_equal "redis://host:1234/0", redis.connection.fetch(:id)
22+
end
23+
24+
def test_default_id_with_host_and_port_and_explicit_scheme
25+
redis = Redis.new(OPTIONS.merge(:host => "host", :port => "1234", :db => 0, :scheme => "foo"))
26+
assert_equal "redis://host:1234/0", redis.connection.fetch(:id)
27+
end
28+
29+
def test_default_id_with_path
30+
redis = Redis.new(OPTIONS.merge(:path => "/tmp/redis.sock", :db => 0))
31+
assert_equal "redis:///tmp/redis.sock/0", redis.connection.fetch(:id)
32+
end
33+
34+
def test_default_id_with_path_and_explicit_scheme
35+
redis = Redis.new(OPTIONS.merge(:path => "/tmp/redis.sock", :db => 0, :scheme => "foo"))
36+
assert_equal "redis:///tmp/redis.sock/0", redis.connection.fetch(:id)
37+
end
38+
39+
def test_override_id
40+
redis = Redis.new(OPTIONS.merge(:id => "test"))
41+
assert_equal "test", redis.connection.fetch(:id)
42+
end
43+
44+
def test_id_inside_multi
45+
redis = Redis.new(OPTIONS)
46+
id = nil
47+
connection_id = nil
48+
49+
redis.multi do
50+
id = redis.id
51+
connection_id = redis.connection.fetch(:id)
52+
end
53+
54+
assert_equal "redis://127.0.0.1:6381/15", id
55+
assert_equal "redis://127.0.0.1:6381/15", connection_id
56+
end
57+
end

test/internals_test.rb

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,6 @@ def test_raises_on_protocol_errors
4141
end
4242
end
4343

44-
def test_provides_a_meaningful_inspect
45-
assert_equal "#<Redis client v#{Redis::VERSION} for redis://127.0.0.1:#{PORT}/15>", r.inspect
46-
end
47-
4844
def test_redis_current
4945
assert_equal "127.0.0.1", Redis.current._client.host
5046
assert_equal 6379, Redis.current._client.port
@@ -76,48 +72,12 @@ def test_redis_connected?
7672
assert !fresh_client.connected?
7773
end
7874

79-
def test_default_id_with_host_and_port
80-
redis = Redis.new(OPTIONS.merge(:host => "host", :port => "1234", :db => 0))
81-
assert_equal "redis://host:1234/0", redis._client.id
82-
end
83-
84-
def test_default_id_with_host_and_port_and_explicit_scheme
85-
redis = Redis.new(OPTIONS.merge(:host => "host", :port => "1234", :db => 0, :scheme => "foo"))
86-
assert_equal "redis://host:1234/0", redis._client.id
87-
end
88-
89-
def test_default_id_with_path
90-
redis = Redis.new(OPTIONS.merge(:path => "/tmp/redis.sock", :db => 0))
91-
assert_equal "redis:///tmp/redis.sock/0", redis._client.id
92-
end
93-
94-
def test_default_id_with_path_and_explicit_scheme
95-
redis = Redis.new(OPTIONS.merge(:path => "/tmp/redis.sock", :db => 0, :scheme => "foo"))
96-
assert_equal "redis:///tmp/redis.sock/0", redis._client.id
97-
end
98-
99-
def test_override_id
100-
redis = Redis.new(OPTIONS.merge(:id => "test"))
101-
assert_equal redis._client.id, "test"
102-
end
103-
10475
def test_timeout
10576
assert_nothing_raised do
10677
Redis.new(OPTIONS.merge(:timeout => 0))
10778
end
10879
end
10980

110-
def test_id_inside_multi
111-
redis = Redis.new(OPTIONS)
112-
id = nil
113-
114-
redis.multi do
115-
id = redis.id
116-
end
117-
118-
assert_equal id, "redis://127.0.0.1:6381/15"
119-
end
120-
12181
driver(:ruby) do
12282
def test_tcp_keepalive
12383
keepalive = {:time => 20, :intvl => 10, :probes => 5}

0 commit comments

Comments
 (0)