File tree Expand file tree Collapse file tree 4 files changed +51
-3
lines changed Expand file tree Collapse file tree 4 files changed +51
-3
lines changed Original file line number Diff line number Diff line change @@ -27,6 +27,7 @@ def self.current=(redis)
27
27
include MonitorMixin
28
28
29
29
def initialize ( options = { } )
30
+ @options = options . dup
30
31
@original_client = @client = Client . new ( options )
31
32
32
33
super ( ) # Monitor#initialize
@@ -2422,6 +2423,10 @@ def inspect
2422
2423
"#<Redis client v#{ Redis ::VERSION } for #{ id } >"
2423
2424
end
2424
2425
2426
+ def dup
2427
+ self . class . new ( @options )
2428
+ end
2429
+
2425
2430
def method_missing ( command , *args )
2426
2431
synchronize do |client |
2427
2432
client . call ( [ command ] + args )
Original file line number Diff line number Diff line change @@ -16,9 +16,10 @@ def message
16
16
attr_reader :ring
17
17
18
18
def initialize ( node_configs , options = { } )
19
- @tag = options . delete ( :tag ) || /^\{ (.+?)\} /
20
- @ring = options . delete ( :ring ) || HashRing . new
21
- @default_options = options
19
+ @tag = options [ :tag ] || /^\{ (.+?)\} /
20
+ @ring = options [ :ring ] || HashRing . new
21
+ @node_configs = node_configs . dup
22
+ @default_options = options . dup
22
23
node_configs . each { |node_config | add_node ( node_config ) }
23
24
@subscribed_node = nil
24
25
end
@@ -807,6 +808,10 @@ def inspect
807
808
"#<Redis client v#{ Redis ::VERSION } for #{ nodes . map ( &:id ) . join ( ', ' ) } >"
808
809
end
809
810
811
+ def dup
812
+ self . class . new ( @node_configs , @default_options )
813
+ end
814
+
810
815
protected
811
816
812
817
def on_each_node ( command , *args )
Original file line number Diff line number Diff line change @@ -38,4 +38,33 @@ def test_override_id
38
38
assert_equal redis . nodes . last . client . id , "test1"
39
39
assert_equal "#<Redis client v#{ Redis ::VERSION } for #{ redis . nodes . map ( &:id ) . join ( ', ' ) } >" , redis . inspect
40
40
end
41
+
42
+ def test_can_be_duped_to_create_a_new_connection
43
+ redis = Redis ::Distributed . new ( NODES )
44
+
45
+ clients = redis . info [ 0 ] [ "connected_clients" ] . to_i
46
+
47
+ r2 = redis . dup
48
+ r2 . ping
49
+
50
+ assert_equal clients + 1 , redis . info [ 0 ] [ "connected_clients" ] . to_i
51
+ end
52
+
53
+ def test_keeps_options_after_dup
54
+ r1 = Redis ::Distributed . new ( NODES , :tag => /^(\w +):/ )
55
+
56
+ assert_raise ( Redis ::Distributed ::CannotDistribute ) do
57
+ r1 . sinter ( "foo" , "bar" )
58
+ end
59
+
60
+ assert_equal [ ] , r1 . sinter ( "baz:foo" , "baz:bar" )
61
+
62
+ r2 = r1 . dup
63
+
64
+ assert_raise ( Redis ::Distributed ::CannotDistribute ) do
65
+ r2 . sinter ( "foo" , "bar" )
66
+ end
67
+
68
+ assert_equal [ ] , r2 . sinter ( "baz:foo" , "baz:bar" )
69
+ end
41
70
end
Original file line number Diff line number Diff line change @@ -342,4 +342,13 @@ def test_does_not_change_self_client_options
342
342
assert_equal 1 , redis . client . options [ :db ]
343
343
assert_equal "foo" , redis . client . options [ :scheme ]
344
344
end
345
+
346
+ def test_can_be_duped_to_create_a_new_connection
347
+ clients = r . info [ "connected_clients" ] . to_i
348
+
349
+ r2 = r . dup
350
+ r2 . ping
351
+
352
+ assert_equal clients + 1 , r . info [ "connected_clients" ] . to_i
353
+ end
345
354
end
You can’t perform that action at this time.
0 commit comments