2
0

aof.tcl 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 cat [dict get $srv stdout] | tail -n1]
  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 cat [dict get $srv stdout] | tail -n1]
  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 echo y | src/redis-check-aof --fix $aof_path]
  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. assert_equal "hello" [$client get foo]
  86. assert_equal "" [$client get bar]
  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. start_server_aof [list dir $server_path] {
  96. test "AOF+SPOP: Server should have been started" {
  97. assert_equal 1 [is_alive $srv]
  98. }
  99. test "AOF+SPOP: Set should have 1 member" {
  100. set client [redis [dict get $srv host] [dict get $srv port]]
  101. assert_equal 1 [$client scard set]
  102. }
  103. }
  104. ## Test that EXPIREAT is loaded correctly
  105. create_aof {
  106. append_to_aof [formatCommand rpush list foo]
  107. append_to_aof [formatCommand expireat list 1000]
  108. append_to_aof [formatCommand rpush list bar]
  109. }
  110. start_server_aof [list dir $server_path] {
  111. test "AOF+EXPIRE: Server should have been started" {
  112. assert_equal 1 [is_alive $srv]
  113. }
  114. test "AOF+EXPIRE: List should be empty" {
  115. set client [redis [dict get $srv host] [dict get $srv port]]
  116. assert_equal 0 [$client llen list]
  117. }
  118. }
  119. start_server {overrides {appendonly {yes} appendfilename {appendonly.aof}}} {
  120. test {Redis should not try to convert DEL into EXPIREAT for EXPIRE -1} {
  121. r set x 10
  122. r expire x -1
  123. }
  124. }
  125. }