aof.tcl 5.1 KB

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