2
0

timer.tcl 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. set testmodule [file normalize tests/modules/timer.so]
  2. start_server {tags {"modules"}} {
  3. r module load $testmodule
  4. test {RM_CreateTimer: a sequence of timers work} {
  5. # We can't guarantee same-ms but we try using MULTI/EXEC
  6. r multi
  7. for {set i 0} {$i < 20} {incr i} {
  8. r test.createtimer 10 timer-incr-key
  9. }
  10. r exec
  11. after 500
  12. assert_equal 20 [r get timer-incr-key]
  13. }
  14. test {RM_GetTimer: basic sanity} {
  15. # Getting non-existing timer
  16. assert_equal {} [r test.gettimer 0]
  17. # Getting a real timer
  18. set id [r test.createtimer 10000 timer-incr-key]
  19. set info [r test.gettimer $id]
  20. assert_equal "timer-incr-key" [lindex $info 0]
  21. set remaining [lindex $info 1]
  22. assert {$remaining < 10000 && $remaining > 1}
  23. }
  24. test {RM_StopTimer: basic sanity} {
  25. r set "timer-incr-key" 0
  26. set id [r test.createtimer 1000 timer-incr-key]
  27. assert_equal 1 [r test.stoptimer $id]
  28. # Wait to be sure timer doesn't execute
  29. after 2000
  30. assert_equal 0 [r get timer-incr-key]
  31. # Stop non-existing timer
  32. assert_equal 0 [r test.stoptimer $id]
  33. }
  34. test {Timer appears non-existing after it fires} {
  35. r set "timer-incr-key" 0
  36. set id [r test.createtimer 10 timer-incr-key]
  37. # verify timer fired
  38. after 500
  39. assert_equal 1 [r get timer-incr-key]
  40. # verify id does not exist
  41. assert_equal {} [r test.gettimer $id]
  42. }
  43. }