]> git.saurik.com Git - redis.git/blob - tests/unit/dump.tcl
Test: more MIGRATE tests.
[redis.git] / tests / unit / dump.tcl
1 start_server {tags {"dump"}} {
2 test {DUMP / RESTORE are able to serialize / unserialize a simple key} {
3 r set foo bar
4 set encoded [r dump foo]
5 r del foo
6 list [r exists foo] [r restore foo 0 $encoded] [r ttl foo] [r get foo]
7 } {0 OK -1 bar}
8
9 test {RESTORE can set an arbitrary expire to the materialized key} {
10 r set foo bar
11 set encoded [r dump foo]
12 r del foo
13 r restore foo 5000 $encoded
14 set ttl [r pttl foo]
15 assert {$ttl >= 3000 && $ttl <= 5000}
16 r get foo
17 } {bar}
18
19 test {RESTORE returns an error of the key already exists} {
20 r set foo bar
21 set e {}
22 catch {r restore foo 0 "..."} e
23 set e
24 } {*is busy*}
25
26 test {RESTORE can overwrite an existing key with REPLACE} {
27 r set foo bar1
28 set encoded1 [r dump foo]
29 r set foo bar2
30 set encoded2 [r dump foo]
31 r del foo
32 r restore foo 0 $encoded1
33 r restore foo 0 $encoded2 replace
34 r get foo
35 } {bar2}
36
37 test {RESTORE can detect a syntax error for unrecongized options} {
38 catch {r restore foo 0 "..." invalid-option} e
39 set e
40 } {*syntax*}
41
42 test {DUMP of non existing key returns nil} {
43 r dump nonexisting_key
44 } {}
45
46 test {MIGRATE is caching connections} {
47 # Note, we run this as first test so that the connection cache
48 # is empty.
49 set first [srv 0 client]
50 r set key "Some Value"
51 start_server {tags {"repl"}} {
52 set second [srv 0 client]
53 set second_host [srv 0 host]
54 set second_port [srv 0 port]
55
56 assert_match {*migrate_cached_sockets:0*} [r -1 info]
57 r -1 migrate $second_host $second_port key 9 1000
58 assert_match {*migrate_cached_sockets:1*} [r -1 info]
59 }
60 }
61
62 test {MIGRATE cached connections are released after some time} {
63 after 15000
64 assert_match {*migrate_cached_sockets:0*} [r info]
65 }
66
67 test {MIGRATE is able to migrate a key between two instances} {
68 set first [srv 0 client]
69 r set key "Some Value"
70 start_server {tags {"repl"}} {
71 set second [srv 0 client]
72 set second_host [srv 0 host]
73 set second_port [srv 0 port]
74
75 assert {[$first exists key] == 1}
76 assert {[$second exists key] == 0}
77 set ret [r -1 migrate $second_host $second_port key 9 5000]
78 assert {$ret eq {OK}}
79 assert {[$first exists key] == 0}
80 assert {[$second exists key] == 1}
81 assert {[$second get key] eq {Some Value}}
82 assert {[$second ttl key] == -1}
83 }
84 }
85
86 test {MIGRATE is able to copy a key between two instances} {
87 set first [srv 0 client]
88 r del list
89 r lpush list a b c d
90 start_server {tags {"repl"}} {
91 set second [srv 0 client]
92 set second_host [srv 0 host]
93 set second_port [srv 0 port]
94
95 assert {[$first exists list] == 1}
96 assert {[$second exists list] == 0}
97 set ret [r -1 migrate $second_host $second_port list 9 5000 copy]
98 assert {$ret eq {OK}}
99 assert {[$first exists list] == 1}
100 assert {[$second exists list] == 1}
101 assert {[$first lrange list 0 -1] eq [$second lrange list 0 -1]}
102 }
103 }
104
105 test {MIGRATE will not overwrite existing keys, unless REPLACE is used} {
106 set first [srv 0 client]
107 r del list
108 r lpush list a b c d
109 start_server {tags {"repl"}} {
110 set second [srv 0 client]
111 set second_host [srv 0 host]
112 set second_port [srv 0 port]
113
114 assert {[$first exists list] == 1}
115 assert {[$second exists list] == 0}
116 $second set list somevalue
117 catch {r -1 migrate $second_host $second_port list 9 5000 copy} e
118 assert_match {ERR*} $e
119 set res [r -1 migrate $second_host $second_port list 9 5000 copy replace]
120 assert {$ret eq {OK}}
121 assert {[$first exists list] == 1}
122 assert {[$second exists list] == 1}
123 assert {[$first lrange list 0 -1] eq [$second lrange list 0 -1]}
124 }
125 }
126
127 test {MIGRATE propagates TTL correctly} {
128 set first [srv 0 client]
129 r set key "Some Value"
130 start_server {tags {"repl"}} {
131 set second [srv 0 client]
132 set second_host [srv 0 host]
133 set second_port [srv 0 port]
134
135 assert {[$first exists key] == 1}
136 assert {[$second exists key] == 0}
137 $first expire key 10
138 set ret [r -1 migrate $second_host $second_port key 9 5000]
139 assert {$ret eq {OK}}
140 assert {[$first exists key] == 0}
141 assert {[$second exists key] == 1}
142 assert {[$second get key] eq {Some Value}}
143 assert {[$second ttl key] >= 7 && [$second ttl key] <= 10}
144 }
145 }
146
147 test {MIGRATE can correctly transfer large values} {
148 set first [srv 0 client]
149 r del key
150 for {set j 0} {$j < 5000} {incr j} {
151 r rpush key 1 2 3 4 5 6 7 8 9 10
152 r rpush key "item 1" "item 2" "item 3" "item 4" "item 5" \
153 "item 6" "item 7" "item 8" "item 9" "item 10"
154 }
155 assert {[string length [r dump key]] > (1024*64)}
156 start_server {tags {"repl"}} {
157 set second [srv 0 client]
158 set second_host [srv 0 host]
159 set second_port [srv 0 port]
160
161 assert {[$first exists key] == 1}
162 assert {[$second exists key] == 0}
163 set ret [r -1 migrate $second_host $second_port key 9 10000]
164 assert {$ret eq {OK}}
165 assert {[$first exists key] == 0}
166 assert {[$second exists key] == 1}
167 assert {[$second ttl key] == -1}
168 assert {[$second llen key] == 5000*20}
169 }
170 }
171
172 test {MIGRATE can correctly transfer hashes} {
173 set first [srv 0 client]
174 r del key
175 r hmset key field1 "item 1" field2 "item 2" field3 "item 3" \
176 field4 "item 4" field5 "item 5" field6 "item 6"
177 start_server {tags {"repl"}} {
178 set second [srv 0 client]
179 set second_host [srv 0 host]
180 set second_port [srv 0 port]
181
182 assert {[$first exists key] == 1}
183 assert {[$second exists key] == 0}
184 set ret [r -1 migrate $second_host $second_port key 9 10000]
185 assert {$ret eq {OK}}
186 assert {[$first exists key] == 0}
187 assert {[$second exists key] == 1}
188 assert {[$second ttl key] == -1}
189 }
190 }
191
192 test {MIGRATE timeout actually works} {
193 set first [srv 0 client]
194 r set key "Some Value"
195 start_server {tags {"repl"}} {
196 set second [srv 0 client]
197 set second_host [srv 0 host]
198 set second_port [srv 0 port]
199
200 assert {[$first exists key] == 1}
201 assert {[$second exists key] == 0}
202
203 set rd [redis_deferring_client]
204 $rd debug sleep 1.0 ; # Make second server unable to reply.
205 set e {}
206 catch {r -1 migrate $second_host $second_port key 9 500} e
207 assert_match {IOERR*} $e
208 }
209 }
210 }