2
0

test.tcl 5.2 KB

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