]> git.saurik.com Git - redis.git/blob - tests/integration/aof.tcl
Merge pull request #74 from kmerenkov/issue_620
[redis.git] / tests / integration / aof.tcl
1 set defaults { appendonly {yes} appendfilename {appendonly.aof} }
2 set server_path [tmpdir server.aof]
3 set aof_path "$server_path/appendonly.aof"
4
5 proc append_to_aof {str} {
6 upvar fp fp
7 puts -nonewline $fp $str
8 }
9
10 proc create_aof {code} {
11 upvar fp fp aof_path aof_path
12 set fp [open $aof_path w+]
13 uplevel 1 $code
14 close $fp
15 }
16
17 proc start_server_aof {overrides code} {
18 upvar defaults defaults srv srv server_path server_path
19 set config [concat $defaults $overrides]
20 set srv [start_server [list overrides $config]]
21 uplevel 1 $code
22 kill_server $srv
23 }
24
25 tags {"aof"} {
26 ## Test the server doesn't start when the AOF contains an unfinished MULTI
27 create_aof {
28 append_to_aof [formatCommand set foo hello]
29 append_to_aof [formatCommand multi]
30 append_to_aof [formatCommand set bar world]
31 }
32
33 start_server_aof [list dir $server_path] {
34 test "Unfinished MULTI: Server should not have been started" {
35 if {$::valgrind} {after 2000}
36 assert_equal 0 [is_alive $srv]
37 }
38
39 test "Unfinished MULTI: Server should have logged an error" {
40 set result [exec cat [dict get $srv stdout] | tail -n1]
41 assert_match "*Unexpected end of file reading the append only file*" $result
42 }
43 }
44
45 ## Test that the server exits when the AOF contains a short read
46 create_aof {
47 append_to_aof [formatCommand set foo hello]
48 append_to_aof [string range [formatCommand set bar world] 0 end-1]
49 }
50
51 start_server_aof [list dir $server_path] {
52 test "Short read: Server should not have been started" {
53 if {$::valgrind} {after 2000}
54 assert_equal 0 [is_alive $srv]
55 }
56
57 test "Short read: Server should have logged an error" {
58 set result [exec cat [dict get $srv stdout] | tail -n1]
59 assert_match "*Bad file format reading the append only file*" $result
60 }
61 }
62
63 ## Test that redis-check-aof indeed sees this AOF is not valid
64 test "Short read: Utility should confirm the AOF is not valid" {
65 catch {
66 exec src/redis-check-aof $aof_path
67 } result
68 assert_match "*not valid*" $result
69 }
70
71 test "Short read: Utility should be able to fix the AOF" {
72 set result [exec echo y | src/redis-check-aof --fix $aof_path]
73 assert_match "*Successfully truncated AOF*" $result
74 }
75
76 ## Test that the server can be started using the truncated AOF
77 start_server_aof [list dir $server_path] {
78 test "Fixed AOF: Server should have been started" {
79 assert_equal 1 [is_alive $srv]
80 }
81
82 test "Fixed AOF: Keyspace should contain values that were parsable" {
83 set client [redis [dict get $srv host] [dict get $srv port]]
84 assert_equal "hello" [$client get foo]
85 assert_equal "" [$client get bar]
86 }
87 }
88
89 ## Test that SPOP (that modifies the client its argc/argv) is correctly free'd
90 create_aof {
91 append_to_aof [formatCommand sadd set foo]
92 append_to_aof [formatCommand sadd set bar]
93 append_to_aof [formatCommand spop set]
94 }
95
96 start_server_aof [list dir $server_path] {
97 test "AOF+SPOP: Server should have been started" {
98 assert_equal 1 [is_alive $srv]
99 }
100
101 test "AOF+SPOP: Set should have 1 member" {
102 set client [redis [dict get $srv host] [dict get $srv port]]
103 assert_equal 1 [$client scard set]
104 }
105 }
106
107 ## Test that EXPIREAT is loaded correctly
108 create_aof {
109 append_to_aof [formatCommand rpush list foo]
110 append_to_aof [formatCommand expireat list 1000]
111 append_to_aof [formatCommand rpush list bar]
112 }
113
114 start_server_aof [list dir $server_path] {
115 test "AOF+EXPIRE: Server should have been started" {
116 assert_equal 1 [is_alive $srv]
117 }
118
119 test "AOF+EXPIRE: List should be empty" {
120 set client [redis [dict get $srv host] [dict get $srv port]]
121 assert_equal 0 [$client llen list]
122 }
123 }
124
125 start_server {overrides {appendonly {yes} appendfilename {appendonly.aof}}} {
126 test {Redis should not try to convert DEL into EXPIREAT for EXPIRE -1} {
127 r set x 10
128 r expire x -1
129 }
130 }
131 }