1
0

hooks.tcl 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. set testmodule [file normalize tests/modules/hooks.so]
  2. tags "modules" {
  3. start_server [list overrides [list loadmodule "$testmodule" appendonly yes]] {
  4. test {Test clients connection / disconnection hooks} {
  5. for {set j 0} {$j < 2} {incr j} {
  6. set rd1 [redis_deferring_client]
  7. $rd1 close
  8. }
  9. assert {[r hooks.event_count client-connected] > 1}
  10. assert {[r hooks.event_count client-disconnected] > 1}
  11. }
  12. test {Test module cron hook} {
  13. after 100
  14. assert {[r hooks.event_count cron-loop] > 0}
  15. set hz [r hooks.event_last cron-loop]
  16. assert_equal $hz 10
  17. }
  18. test {Test module loaded / unloaded hooks} {
  19. set othermodule [file normalize tests/modules/infotest.so]
  20. r module load $othermodule
  21. r module unload infotest
  22. assert_equal [r hooks.event_last module-loaded] "infotest"
  23. assert_equal [r hooks.event_last module-unloaded] "infotest"
  24. }
  25. test {Test module aofrw hook} {
  26. r debug populate 1000 foo 10000 ;# 10mb worth of data
  27. r config set rdbcompression no ;# rdb progress is only checked once in 2mb
  28. r BGREWRITEAOF
  29. waitForBgrewriteaof r
  30. assert_equal [string match {*module-event-persistence-aof-start*} [exec tail -20 < [srv 0 stdout]]] 1
  31. assert_equal [string match {*module-event-persistence-end*} [exec tail -20 < [srv 0 stdout]]] 1
  32. }
  33. test {Test module aof load and rdb/aof progress hooks} {
  34. # create some aof tail (progress is checked only once in 1000 commands)
  35. for {set j 0} {$j < 4000} {incr j} {
  36. r set "bar$j" x
  37. }
  38. # set some configs that will cause many loading progress events during aof loading
  39. r config set key-load-delay 500
  40. r config set dynamic-hz no
  41. r config set hz 500
  42. r DEBUG LOADAOF
  43. assert_equal [r hooks.event_last loading-aof-start] 0
  44. assert_equal [r hooks.event_last loading-end] 0
  45. assert {[r hooks.event_count loading-rdb-start] == 0}
  46. assert_lessthan 2 [r hooks.event_count loading-progress-rdb] ;# comes from the preamble section
  47. assert_lessthan 2 [r hooks.event_count loading-progress-aof]
  48. if {$::verbose} {
  49. puts "rdb progress events [r hooks.event_count loading-progress-rdb]"
  50. puts "aof progress events [r hooks.event_count loading-progress-aof]"
  51. }
  52. }
  53. # undo configs before next test
  54. r config set dynamic-hz yes
  55. r config set key-load-delay 0
  56. test {Test module rdb save hook} {
  57. # debug reload does: save, flush, load:
  58. assert {[r hooks.event_count persistence-syncrdb-start] == 0}
  59. assert {[r hooks.event_count loading-rdb-start] == 0}
  60. r debug reload
  61. assert {[r hooks.event_count persistence-syncrdb-start] == 1}
  62. assert {[r hooks.event_count loading-rdb-start] == 1}
  63. }
  64. test {Test flushdb hooks} {
  65. r flushdb
  66. assert_equal [r hooks.event_last flush-start] 9
  67. assert_equal [r hooks.event_last flush-end] 9
  68. r flushall
  69. assert_equal [r hooks.event_last flush-start] -1
  70. assert_equal [r hooks.event_last flush-end] -1
  71. }
  72. # replication related tests
  73. set master [srv 0 client]
  74. set master_host [srv 0 host]
  75. set master_port [srv 0 port]
  76. start_server {} {
  77. r module load $testmodule
  78. set replica [srv 0 client]
  79. set replica_host [srv 0 host]
  80. set replica_port [srv 0 port]
  81. $replica replicaof $master_host $master_port
  82. wait_for_condition 50 100 {
  83. [string match {*master_link_status:up*} [r info replication]]
  84. } else {
  85. fail "Can't turn the instance into a replica"
  86. }
  87. test {Test master link up hook} {
  88. assert_equal [r hooks.event_count masterlink-up] 1
  89. assert_equal [r hooks.event_count masterlink-down] 0
  90. }
  91. test {Test role-replica hook} {
  92. assert_equal [r hooks.event_count role-replica] 1
  93. assert_equal [r hooks.event_count role-master] 0
  94. assert_equal [r hooks.event_last role-replica] [s 0 master_host]
  95. }
  96. test {Test replica-online hook} {
  97. assert_equal [r -1 hooks.event_count replica-online] 1
  98. assert_equal [r -1 hooks.event_count replica-offline] 0
  99. }
  100. test {Test master link down hook} {
  101. r client kill type master
  102. assert_equal [r hooks.event_count masterlink-down] 1
  103. wait_for_condition 50 100 {
  104. [string match {*master_link_status:up*} [r info replication]]
  105. } else {
  106. fail "Replica didn't reconnect"
  107. }
  108. assert_equal [r hooks.event_count masterlink-down] 1
  109. assert_equal [r hooks.event_count masterlink-up] 2
  110. }
  111. wait_for_condition 50 10 {
  112. [string match {*master_link_status:up*} [r info replication]]
  113. } else {
  114. fail "Can't turn the instance into a replica"
  115. }
  116. $replica replicaof no one
  117. test {Test role-master hook} {
  118. assert_equal [r hooks.event_count role-replica] 1
  119. assert_equal [r hooks.event_count role-master] 1
  120. assert_equal [r hooks.event_last role-master] {}
  121. }
  122. test {Test replica-offline hook} {
  123. assert_equal [r -1 hooks.event_count replica-online] 2
  124. assert_equal [r -1 hooks.event_count replica-offline] 2
  125. }
  126. # get the replica stdout, to be used by the next test
  127. set replica_stdout [srv 0 stdout]
  128. }
  129. test {Test swapdb hooks} {
  130. r swapdb 0 10
  131. assert_equal [r hooks.event_last swapdb-first] 0
  132. assert_equal [r hooks.event_last swapdb-second] 10
  133. }
  134. # look into the log file of the server that just exited
  135. test {Test shutdown hook} {
  136. assert_equal [string match {*module-event-shutdown*} [exec tail -5 < $replica_stdout]] 1
  137. }
  138. }
  139. }