| [25] | 1 | # Commands covered: (test)thread |
|---|
| 2 | # |
|---|
| 3 | # This file contains a collection of tests for one or more of the Tcl |
|---|
| 4 | # built-in commands. Sourcing this file into Tcl runs the tests and |
|---|
| 5 | # generates output for errors. No output means no errors were found. |
|---|
| 6 | # |
|---|
| 7 | # Copyright (c) 1996 Sun Microsystems, Inc. |
|---|
| 8 | # Copyright (c) 1998-1999 by Scriptics Corporation. |
|---|
| 9 | # |
|---|
| 10 | # See the file "license.terms" for information on usage and redistribution |
|---|
| 11 | # of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
|---|
| 12 | # |
|---|
| 13 | # RCS: @(#) $Id: thread.test,v 1.18 2007/12/13 15:26:07 dgp Exp $ |
|---|
| 14 | |
|---|
| 15 | if {[lsearch [namespace children] ::tcltest] == -1} { |
|---|
| 16 | package require tcltest |
|---|
| 17 | namespace import -force ::tcltest::* |
|---|
| 18 | } |
|---|
| 19 | |
|---|
| 20 | # Some tests require the testthread command |
|---|
| 21 | |
|---|
| 22 | testConstraint testthread [expr {[info commands testthread] != {}}] |
|---|
| 23 | |
|---|
| 24 | if {[testConstraint testthread]} { |
|---|
| 25 | testthread errorproc ThreadError |
|---|
| 26 | |
|---|
| 27 | proc ThreadError {id info} { |
|---|
| 28 | global threadError |
|---|
| 29 | set threadError $info |
|---|
| 30 | } |
|---|
| 31 | |
|---|
| 32 | proc ThreadNullError {id info} { |
|---|
| 33 | # ignore |
|---|
| 34 | } |
|---|
| 35 | } |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | test thread-1.1 {Tcl_ThreadObjCmd: no args} {testthread} { |
|---|
| 39 | list [catch {testthread} msg] $msg |
|---|
| 40 | } {1 {wrong # args: should be "testthread option ?args?"}} |
|---|
| 41 | test thread-1.2 {Tcl_ThreadObjCmd: bad option} {testthread} { |
|---|
| 42 | list [catch {testthread foo} msg] $msg |
|---|
| 43 | } {1 {bad option "foo": must be create, exit, id, join, names, send, wait, or errorproc}} |
|---|
| 44 | test thread-1.3 {Tcl_ThreadObjCmd: initial thread list} {testthread} { |
|---|
| 45 | list [threadReap] [llength [testthread names]] |
|---|
| 46 | } {1 1} |
|---|
| 47 | test thread-1.4 {Tcl_ThreadObjCmd: thread create } {testthread} { |
|---|
| 48 | threadReap |
|---|
| 49 | set serverthread [testthread create] |
|---|
| 50 | update |
|---|
| 51 | set numthreads [llength [testthread names]] |
|---|
| 52 | threadReap |
|---|
| 53 | set numthreads |
|---|
| 54 | } {2} |
|---|
| 55 | test thread-1.5 {Tcl_ThreadObjCmd: thread create one shot} {testthread} { |
|---|
| 56 | threadReap |
|---|
| 57 | testthread create {set x 5} |
|---|
| 58 | foreach try {0 1 2 4 5 6} { |
|---|
| 59 | # Try various ways to yield |
|---|
| 60 | update |
|---|
| 61 | after 10 |
|---|
| 62 | set l [llength [testthread names]] |
|---|
| 63 | if {$l == 1} { |
|---|
| 64 | break |
|---|
| 65 | } |
|---|
| 66 | } |
|---|
| 67 | threadReap |
|---|
| 68 | set l |
|---|
| 69 | } {1} |
|---|
| 70 | test thread-1.6 {Tcl_ThreadObjCmd: thread exit} {testthread} { |
|---|
| 71 | threadReap |
|---|
| 72 | testthread create {testthread exit} |
|---|
| 73 | update |
|---|
| 74 | after 10 |
|---|
| 75 | set result [llength [testthread names]] |
|---|
| 76 | threadReap |
|---|
| 77 | set result |
|---|
| 78 | } {1} |
|---|
| 79 | test thread-1.7 {Tcl_ThreadObjCmd: thread id args} {testthread} { |
|---|
| 80 | set x [catch {testthread id x} msg] |
|---|
| 81 | list $x $msg |
|---|
| 82 | } {1 {wrong # args: should be "testthread id"}} |
|---|
| 83 | test thread-1.8 {Tcl_ThreadObjCmd: thread id} {testthread} { |
|---|
| 84 | string compare [testthread id] $::tcltest::mainThread |
|---|
| 85 | } {0} |
|---|
| 86 | test thread-1.9 {Tcl_ThreadObjCmd: thread names args} {testthread} { |
|---|
| 87 | set x [catch {testthread names x} msg] |
|---|
| 88 | list $x $msg |
|---|
| 89 | } {1 {wrong # args: should be "testthread names"}} |
|---|
| 90 | test thread-1.10 {Tcl_ThreadObjCmd: thread id} {testthread} { |
|---|
| 91 | string compare [testthread names] $::tcltest::mainThread |
|---|
| 92 | } {0} |
|---|
| 93 | test thread-1.11 {Tcl_ThreadObjCmd: send args} {testthread} { |
|---|
| 94 | set x [catch {testthread send} msg] |
|---|
| 95 | list $x $msg |
|---|
| 96 | } {1 {wrong # args: should be "testthread send ?-async? id script"}} |
|---|
| 97 | test thread-1.12 {Tcl_ThreadObjCmd: send nonint} {testthread} { |
|---|
| 98 | set x [catch {testthread send abc command} msg] |
|---|
| 99 | list $x $msg |
|---|
| 100 | } {1 {expected integer but got "abc"}} |
|---|
| 101 | test thread-1.13 {Tcl_ThreadObjCmd: send args} {testthread} { |
|---|
| 102 | threadReap |
|---|
| 103 | set serverthread [testthread create] |
|---|
| 104 | set five [testthread send $serverthread {set x 5}] |
|---|
| 105 | threadReap |
|---|
| 106 | set five |
|---|
| 107 | } 5 |
|---|
| 108 | test thread-1.14 {Tcl_ThreadObjCmd: send bad id} {testthread} { |
|---|
| 109 | set tid [expr $::tcltest::mainThread + 10] |
|---|
| 110 | set x [catch {testthread send $tid {set x 5}} msg] |
|---|
| 111 | list $x $msg |
|---|
| 112 | } {1 {invalid thread id}} |
|---|
| 113 | test thread-1.15 {Tcl_ThreadObjCmd: wait} {testthread} { |
|---|
| 114 | threadReap |
|---|
| 115 | set serverthread [testthread create {set z 5 ; testthread wait}] |
|---|
| 116 | set five [testthread send $serverthread {set z}] |
|---|
| 117 | threadReap |
|---|
| 118 | set five |
|---|
| 119 | } 5 |
|---|
| 120 | test thread-1.16 {Tcl_ThreadObjCmd: errorproc args} {testthread} { |
|---|
| 121 | set x [catch {testthread errorproc foo bar} msg] |
|---|
| 122 | list $x $msg |
|---|
| 123 | } {1 {wrong # args: should be "testthread errorproc proc"}} |
|---|
| 124 | test thread-1.17 {Tcl_ThreadObjCmd: errorproc change} {testthread} { |
|---|
| 125 | testthread errorproc foo |
|---|
| 126 | testthread errorproc ThreadError |
|---|
| 127 | } {} |
|---|
| 128 | |
|---|
| 129 | # The tests above also cover: |
|---|
| 130 | # TclCreateThread, except when pthread_create fails |
|---|
| 131 | # NewThread, safe and regular |
|---|
| 132 | # ThreadErrorProc, except for printing to standard error |
|---|
| 133 | |
|---|
| 134 | test thread-2.1 {ListUpdateInner and ListRemove} {testthread} { |
|---|
| 135 | threadReap |
|---|
| 136 | catch {unset tid} |
|---|
| 137 | foreach t {0 1 2} { |
|---|
| 138 | upvar #0 t$t tid |
|---|
| 139 | set tid [testthread create] |
|---|
| 140 | } |
|---|
| 141 | threadReap |
|---|
| 142 | } 1 |
|---|
| 143 | |
|---|
| 144 | test thread-3.1 {TclThreadList} {testthread} { |
|---|
| 145 | threadReap |
|---|
| 146 | catch {unset tid} |
|---|
| 147 | set len [llength [testthread names]] |
|---|
| 148 | set l1 {} |
|---|
| 149 | foreach t {0 1 2} { |
|---|
| 150 | lappend l1 [testthread create] |
|---|
| 151 | } |
|---|
| 152 | set l2 [testthread names] |
|---|
| 153 | list $l1 $l2 |
|---|
| 154 | set c [string compare \ |
|---|
| 155 | [lsort -integer [concat $::tcltest::mainThread $l1]] \ |
|---|
| 156 | [lsort -integer $l2]] |
|---|
| 157 | threadReap |
|---|
| 158 | list $len $c |
|---|
| 159 | } {1 0} |
|---|
| 160 | |
|---|
| 161 | test thread-4.1 {TclThreadSend to self} {testthread} { |
|---|
| 162 | catch {unset x} |
|---|
| 163 | testthread send [testthread id] { |
|---|
| 164 | set x 4 |
|---|
| 165 | } |
|---|
| 166 | set x |
|---|
| 167 | } {4} |
|---|
| 168 | test thread-4.2 {TclThreadSend -async} {testthread} { |
|---|
| 169 | threadReap |
|---|
| 170 | set len [llength [testthread names]] |
|---|
| 171 | set serverthread [testthread create] |
|---|
| 172 | testthread send -async $serverthread { |
|---|
| 173 | after 1000 |
|---|
| 174 | testthread exit |
|---|
| 175 | } |
|---|
| 176 | set two [llength [testthread names]] |
|---|
| 177 | after 1500 {set done 1} |
|---|
| 178 | vwait done |
|---|
| 179 | threadReap |
|---|
| 180 | list $len [llength [testthread names]] $two |
|---|
| 181 | } {1 1 2} |
|---|
| 182 | test thread-4.3 {TclThreadSend preserve errorInfo} {testthread} { |
|---|
| 183 | threadReap |
|---|
| 184 | set len [llength [testthread names]] |
|---|
| 185 | set serverthread [testthread create] |
|---|
| 186 | set x [catch {testthread send $serverthread {set undef}} msg] |
|---|
| 187 | threadReap |
|---|
| 188 | list $len $x $msg $::errorInfo |
|---|
| 189 | } {1 1 {can't read "undef": no such variable} {can't read "undef": no such variable |
|---|
| 190 | while executing |
|---|
| 191 | "set undef" |
|---|
| 192 | invoked from within |
|---|
| 193 | "testthread send $serverthread {set undef}"}} |
|---|
| 194 | test thread-4.4 {TclThreadSend preserve code} {testthread} { |
|---|
| 195 | threadReap |
|---|
| 196 | set len [llength [testthread names]] |
|---|
| 197 | set serverthread [testthread create] |
|---|
| 198 | set ::errorInfo {} |
|---|
| 199 | set x [catch {testthread send $serverthread {set ::errorInfo {}; break}} msg] |
|---|
| 200 | threadReap |
|---|
| 201 | list $len $x $msg $::errorInfo |
|---|
| 202 | } {1 3 {} {}} |
|---|
| 203 | test thread-4.5 {TclThreadSend preserve errorCode} {testthread} { |
|---|
| 204 | threadReap |
|---|
| 205 | set ::tcltest::mainThread [testthread names] |
|---|
| 206 | set serverthread [testthread create] |
|---|
| 207 | set x [catch {testthread send $serverthread {error ERR INFO CODE}} msg] |
|---|
| 208 | threadReap |
|---|
| 209 | list $x $msg $::errorCode |
|---|
| 210 | } {1 ERR CODE} |
|---|
| 211 | |
|---|
| 212 | |
|---|
| 213 | test thread-5.0 {Joining threads} {testthread} { |
|---|
| 214 | threadReap |
|---|
| 215 | set serverthread [testthread create -joinable] |
|---|
| 216 | testthread send -async $serverthread {after 1000 ; testthread exit} |
|---|
| 217 | set res [testthread join $serverthread] |
|---|
| 218 | threadReap |
|---|
| 219 | set res |
|---|
| 220 | } {0} |
|---|
| 221 | test thread-5.1 {Joining threads after the fact} {testthread} { |
|---|
| 222 | threadReap |
|---|
| 223 | set serverthread [testthread create -joinable] |
|---|
| 224 | testthread send -async $serverthread {testthread exit} |
|---|
| 225 | after 2000 |
|---|
| 226 | set res [testthread join $serverthread] |
|---|
| 227 | threadReap |
|---|
| 228 | set res |
|---|
| 229 | } {0} |
|---|
| 230 | test thread-5.2 {Try to join a detached thread} {testthread} { |
|---|
| 231 | threadReap |
|---|
| 232 | set serverthread [testthread create] |
|---|
| 233 | testthread send -async $serverthread {after 1000 ; testthread exit} |
|---|
| 234 | catch {set res [testthread join $serverthread]} msg |
|---|
| 235 | threadReap |
|---|
| 236 | lrange $msg 0 2 |
|---|
| 237 | } {cannot join thread} |
|---|
| 238 | |
|---|
| 239 | test thread-6.1 {freeing very large object trees in a thread} testthread { |
|---|
| 240 | # conceptual duplicate of obj-32.1 |
|---|
| 241 | threadReap |
|---|
| 242 | set serverthread [testthread create -joinable] |
|---|
| 243 | testthread send -async $serverthread { |
|---|
| 244 | set x {} |
|---|
| 245 | for {set i 0} {$i<100000} {incr i} { |
|---|
| 246 | set x [list $x {}] |
|---|
| 247 | } |
|---|
| 248 | unset x |
|---|
| 249 | testthread exit |
|---|
| 250 | } |
|---|
| 251 | catch {set res [testthread join $serverthread]} msg |
|---|
| 252 | threadReap |
|---|
| 253 | set res |
|---|
| 254 | } {0} |
|---|
| 255 | |
|---|
| 256 | # cleanup |
|---|
| 257 | ::tcltest::cleanupTests |
|---|
| 258 | return |
|---|