2
0

test.tcl 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. set dbg [r debug object $key]
  35. assert_match "* encoding:$enc *" $dbg
  36. }
  37. proc assert_type {type key} {
  38. assert_equal $type [r type $key]
  39. }
  40. # Wait for the specified condition to be true, with the specified number of
  41. # max retries and delay between retries. Otherwise the 'elsescript' is
  42. # executed.
  43. proc wait_for_condition {maxtries delay e _else_ elsescript} {
  44. while {[incr maxtries -1] >= 0} {
  45. set errcode [catch {uplevel 1 [list expr $e]} result]
  46. if {$errcode == 0} {
  47. if {$result} break
  48. } else {
  49. return -code $errcode $result
  50. }
  51. after $delay
  52. }
  53. if {$maxtries == -1} {
  54. set errcode [catch [uplevel 1 $elsescript] result]
  55. return -code $errcode $result
  56. }
  57. }
  58. proc test {name code {okpattern undefined}} {
  59. # abort if tagged with a tag to deny
  60. foreach tag $::denytags {
  61. if {[lsearch $::tags $tag] >= 0} {
  62. return
  63. }
  64. }
  65. # check if tagged with at least 1 tag to allow when there *is* a list
  66. # of tags to allow, because default policy is to run everything
  67. if {[llength $::allowtags] > 0} {
  68. set matched 0
  69. foreach tag $::allowtags {
  70. if {[lsearch $::tags $tag] >= 0} {
  71. incr matched
  72. }
  73. }
  74. if {$matched < 1} {
  75. return
  76. }
  77. }
  78. incr ::num_tests
  79. set details {}
  80. lappend details "$name in $::curfile"
  81. send_data_packet $::test_server_fd testing $name
  82. if {[catch {set retval [uplevel 1 $code]} error]} {
  83. if {[string match "assertion:*" $error]} {
  84. set msg [string range $error 10 end]
  85. lappend details $msg
  86. lappend ::tests_failed $details
  87. incr ::num_failed
  88. send_data_packet $::test_server_fd err [join $details "\n"]
  89. } else {
  90. # Re-raise, let handler up the stack take care of this.
  91. error $error $::errorInfo
  92. }
  93. } else {
  94. if {$okpattern eq "undefined" || $okpattern eq $retval || [string match $okpattern $retval]} {
  95. incr ::num_passed
  96. send_data_packet $::test_server_fd ok $name
  97. } else {
  98. set msg "Expected '$okpattern' to equal or match '$retval'"
  99. lappend details $msg
  100. lappend ::tests_failed $details
  101. incr ::num_failed
  102. send_data_packet $::test_server_fd err [join $details "\n"]
  103. }
  104. }
  105. if {$::traceleaks} {
  106. set output [exec leaks redis-server]
  107. if {![string match {*0 leaks*} $output]} {
  108. send_data_packet $::test_server_fd err "Detected a memory leak in test '$name': $output"
  109. }
  110. }
  111. }