2
0

aof.tcl 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 not have been started" {
  30. assert_equal 0 [is_alive $srv]
  31. }
  32. test "Unfinished MULTI: Server should have logged an error" {
  33. set result [exec cat [dict get $srv stdout] | tail -n1]
  34. assert_match "*Unexpected end of file reading the append only file*" $result
  35. }
  36. }
  37. ## Test that the server exits when the AOF contains a short read
  38. create_aof {
  39. append_to_aof [formatCommand set foo hello]
  40. append_to_aof [string range [formatCommand set bar world] 0 end-1]
  41. }
  42. start_server_aof [list dir $server_path] {
  43. test "Short read: Server should not have been started" {
  44. assert_equal 0 [is_alive $srv]
  45. }
  46. test "Short read: Server should have logged an error" {
  47. set result [exec cat [dict get $srv stdout] | tail -n1]
  48. assert_match "*Bad file format reading the append only file*" $result
  49. }
  50. }
  51. ## Test that redis-check-aof indeed sees this AOF is not valid
  52. test "Short read: Utility should confirm the AOF is not valid" {
  53. catch {
  54. exec src/redis-check-aof $aof_path
  55. } result
  56. assert_match "*not valid*" $result
  57. }
  58. test "Short read: Utility should be able to fix the AOF" {
  59. set result [exec echo y | src/redis-check-aof --fix $aof_path]
  60. assert_match "*Successfully truncated AOF*" $result
  61. }
  62. ## Test that the server can be started using the truncated AOF
  63. start_server_aof [list dir $server_path] {
  64. test "Fixed AOF: Server should have been started" {
  65. assert_equal 1 [is_alive $srv]
  66. }
  67. test "Fixed AOF: Keyspace should contain values that were parsable" {
  68. set client [redis [dict get $srv host] [dict get $srv port]]
  69. assert_equal "hello" [$client get foo]
  70. assert_equal "" [$client get bar]
  71. }
  72. }
  73. ## Test that SPOP (that modifies the client its argc/argv) is correctly free'd
  74. create_aof {
  75. append_to_aof [formatCommand sadd set foo]
  76. append_to_aof [formatCommand sadd set bar]
  77. append_to_aof [formatCommand spop set]
  78. }
  79. start_server_aof [list dir $server_path] {
  80. test "AOF+SPOP: Server should have been started" {
  81. assert_equal 1 [is_alive $srv]
  82. }
  83. test "AOF+SPOP: Set should have 1 member" {
  84. set client [redis [dict get $srv host] [dict get $srv port]]
  85. assert_equal 1 [$client scard set]
  86. }
  87. }
  88. ## Test that EXPIREAT is loaded correctly
  89. create_aof {
  90. append_to_aof [formatCommand rpush list foo]
  91. append_to_aof [formatCommand expireat list 1000]
  92. append_to_aof [formatCommand rpush list bar]
  93. }
  94. start_server_aof [list dir $server_path] {
  95. test "AOF+EXPIRE: Server should have been started" {
  96. assert_equal 1 [is_alive $srv]
  97. }
  98. test "AOF+EXPIRE: List should be empty" {
  99. set client [redis [dict get $srv host] [dict get $srv port]]
  100. assert_equal 0 [$client llen list]
  101. }
  102. }
  103. }