2
0

test.tcl 4.3 KB

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