test.tcl 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. set ::num_tests 0
  2. set ::num_passed 0
  3. set ::num_failed 0
  4. set ::tests_failed {}
  5. proc fail {msg} {
  6. error "assertion:$msg"
  7. }
  8. proc assert {condition} {
  9. if {![uplevel 1 [list expr $condition]]} {
  10. error "assertion:Expected condition '$condition' to be true ([uplevel 1 [list subst -nocommands $condition]])"
  11. }
  12. }
  13. proc assert_match {pattern value} {
  14. if {![string match $pattern $value]} {
  15. error "assertion:Expected '$value' to match '$pattern'"
  16. }
  17. }
  18. proc assert_equal {expected value {detail ""}} {
  19. if {$expected ne $value} {
  20. if {$detail ne ""} {
  21. set detail " (detail: $detail)"
  22. }
  23. error "assertion:Expected '$value' to be equal to '$expected'$detail"
  24. }
  25. }
  26. proc assert_error {pattern code} {
  27. if {[catch {uplevel 1 $code} error]} {
  28. assert_match $pattern $error
  29. } else {
  30. error "assertion:Expected an error but nothing was caught"
  31. }
  32. }
  33. proc assert_encoding {enc key} {
  34. # Swapped out values don't have an encoding, so make sure that
  35. # the value is swapped in before checking the encoding.
  36. set dbg [r debug object $key]
  37. while {[string match "* swapped at:*" $dbg]} {
  38. r debug swapin $key
  39. set dbg [r debug object $key]
  40. }
  41. assert_match "* encoding:$enc *" $dbg
  42. }
  43. proc assert_type {type key} {
  44. assert_equal $type [r type $key]
  45. }
  46. # Wait for the specified condition to be true, with the specified number of
  47. # max retries and delay between retries. Otherwise the 'elsescript' is
  48. # executed.
  49. proc wait_for_condition {maxtries delay e _else_ elsescript} {
  50. while {[incr maxtries -1] >= 0} {
  51. set errcode [catch {uplevel 1 [list expr $e]} result]
  52. if {$errcode == 0} {
  53. if {$result} break
  54. } else {
  55. return -code $errcode $result
  56. }
  57. after $delay
  58. }
  59. if {$maxtries == -1} {
  60. set errcode [catch [uplevel 1 $elsescript] result]
  61. return -code $errcode $result
  62. }
  63. }
  64. proc test {name code {okpattern undefined}} {
  65. # abort if tagged with a tag to deny
  66. foreach tag $::denytags {
  67. if {[lsearch $::tags $tag] >= 0} {
  68. return
  69. }
  70. }
  71. # check if tagged with at least 1 tag to allow when there *is* a list
  72. # of tags to allow, because default policy is to run everything
  73. if {[llength $::allowtags] > 0} {
  74. set matched 0
  75. foreach tag $::allowtags {
  76. if {[lsearch $::tags $tag] >= 0} {
  77. incr matched
  78. }
  79. }
  80. if {$matched < 1} {
  81. return
  82. }
  83. }
  84. incr ::num_tests
  85. set details {}
  86. lappend details "$name in $::curfile"
  87. send_data_packet $::test_server_fd testing $name
  88. if {[catch {set retval [uplevel 1 $code]} error]} {
  89. if {[string match "assertion:*" $error]} {
  90. set msg [string range $error 10 end]
  91. lappend details $msg
  92. lappend ::tests_failed $details
  93. incr ::num_failed
  94. send_data_packet $::test_server_fd err [join $details "\n"]
  95. } else {
  96. # Re-raise, let handler up the stack take care of this.
  97. error $error $::errorInfo
  98. }
  99. } else {
  100. if {$okpattern eq "undefined" || $okpattern eq $retval || [string match $okpattern $retval]} {
  101. incr ::num_passed
  102. send_data_packet $::test_server_fd ok $name
  103. } else {
  104. set msg "Expected '$okpattern' to equal or match '$retval'"
  105. lappend details $msg
  106. lappend ::tests_failed $details
  107. incr ::num_failed
  108. send_data_packet $::test_server_fd err [join $details "\n"]
  109. }
  110. }
  111. if {$::traceleaks} {
  112. set output [exec leaks redis-server]
  113. if {![string match {*0 leaks*} $output]} {
  114. send_data_packet $::test_server_fd err "Detected a memory leak in test '$name': $output"
  115. }
  116. }
  117. }