]> git.saurik.com Git - redis.git/blob - tests/unit/dump.tcl
Copyright date fixed in COPYING file.
[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 able to migrate a key between two instances} {
47 set first [srv 0 client]
48 r set key "Some Value"
49 start_server {tags {"repl"}} {
50 set second [srv 0 client]
51 set second_host [srv 0 host]
52 set second_port [srv 0 port]
53
54 assert {[$first exists key] == 1}
55 assert {[$second exists key] == 0}
56 set ret [r -1 migrate $second_host $second_port key 9 5000]
57 assert {$ret eq {OK}}
58 assert {[$first exists key] == 0}
59 assert {[$second exists key] == 1}
60 assert {[$second get key] eq {Some Value}}
61 assert {[$second ttl key] == -1}
62 }
63 }
64
65 test {MIGRATE is able to copy a key between two instances} {
66 set first [srv 0 client]
67 r del list
68 r lpush list a b c d
69 start_server {tags {"repl"}} {
70 set second [srv 0 client]
71 set second_host [srv 0 host]
72 set second_port [srv 0 port]
73
74 assert {[$first exists list] == 1}
75 assert {[$second exists list] == 0}
76 set ret [r -1 migrate $second_host $second_port list 9 5000 copy]
77 assert {$ret eq {OK}}
78 assert {[$first exists list] == 1}
79 assert {[$second exists list] == 1}
80 assert {[$first lrange list 0 -1] eq [$second lrange list 0 -1]}
81 }
82 }
83
84 test {MIGRATE will not overwrite existing keys, unless REPLACE is used} {
85 set first [srv 0 client]
86 r del list
87 r lpush list a b c d
88 start_server {tags {"repl"}} {
89 set second [srv 0 client]
90 set second_host [srv 0 host]
91 set second_port [srv 0 port]
92
93 assert {[$first exists list] == 1}
94 assert {[$second exists list] == 0}
95 $second set list somevalue
96 catch {r -1 migrate $second_host $second_port list 9 5000 copy} e
97 assert_match {ERR*} $e
98 set res [r -1 migrate $second_host $second_port list 9 5000 copy replace]
99 assert {$ret eq {OK}}
100 assert {[$first exists list] == 1}
101 assert {[$second exists list] == 1}
102 assert {[$first lrange list 0 -1] eq [$second lrange list 0 -1]}
103 }
104 }
105
106 test {MIGRATE propagates TTL correctly} {
107 set first [srv 0 client]
108 r set key "Some Value"
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 key] == 1}
115 assert {[$second exists key] == 0}
116 $first expire key 10
117 set ret [r -1 migrate $second_host $second_port key 9 5000]
118 assert {$ret eq {OK}}
119 assert {[$first exists key] == 0}
120 assert {[$second exists key] == 1}
121 assert {[$second get key] eq {Some Value}}
122 assert {[$second ttl key] >= 7 && [$second ttl key] <= 10}
123 }
124 }
125
126 test {MIGRATE can correctly transfer large values} {
127 set first [srv 0 client]
128 r del key
129 for {set j 0} {$j < 5000} {incr j} {
130 r rpush key 1 2 3 4 5 6 7 8 9 10
131 r rpush key "item 1" "item 2" "item 3" "item 4" "item 5" \
132 "item 6" "item 7" "item 8" "item 9" "item 10"
133 }
134 assert {[string length [r dump key]] > (1024*64)}
135 start_server {tags {"repl"}} {
136 set second [srv 0 client]
137 set second_host [srv 0 host]
138 set second_port [srv 0 port]
139
140 assert {[$first exists key] == 1}
141 assert {[$second exists key] == 0}
142 set ret [r -1 migrate $second_host $second_port key 9 10000]
143 assert {$ret eq {OK}}
144 assert {[$first exists key] == 0}
145 assert {[$second exists key] == 1}
146 assert {[$second ttl key] == -1}
147 assert {[$second llen key] == 5000*20}
148 }
149 }
150
151 test {MIGRATE can correctly transfer hashes} {
152 set first [srv 0 client]
153 r del key
154 r hmset key field1 "item 1" field2 "item 2" field3 "item 3" \
155 field4 "item 4" field5 "item 5" field6 "item 6"
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 }
169 }
170
171 test {MIGRATE timeout actually works} {
172 set first [srv 0 client]
173 r set key "Some Value"
174 start_server {tags {"repl"}} {
175 set second [srv 0 client]
176 set second_host [srv 0 host]
177 set second_port [srv 0 port]
178
179 assert {[$first exists key] == 1}
180 assert {[$second exists key] == 0}
181
182 set rd [redis_deferring_client]
183 $rd debug sleep 5.0 ; # Make second server unable to reply.
184 set e {}
185 catch {r -1 migrate $second_host $second_port key 9 1000} e
186 assert_match {IOERR*} $e
187 }
188 }
189 }