hooks.tcl 5.4 KB

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