os/persistentdata/persistentstorage/sqlite3api/TEST/TCL/tcldistribution/tests/thread.test
First public contribution.
1 # Commands covered: (test)thread
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.
7 # Copyright (c) 1996 Sun Microsystems, Inc.
8 # Copyright (c) 1998-1999 by Scriptics Corporation.
10 # See the file "license.terms" for information on usage and redistribution
11 # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
13 # RCS: @(#) $Id: thread.test,v 1.10 2000/05/02 22:02:36 kupries Exp $
15 if {[lsearch [namespace children] ::tcltest] == -1} {
16 package require tcltest
17 namespace import -force ::tcltest::*
20 # Some tests require the testthread command
22 set ::tcltest::testConstraints(testthread) \
23 [expr {[info commands testthread] != {}}]
25 if {$::tcltest::testConstraints(testthread)} {
27 testthread errorproc ThreadError
29 proc ThreadError {id info} {
34 proc ThreadNullError {id info} {
40 test thread-1.1 {Tcl_ThreadObjCmd: no args} {testthread} {
41 list [catch {testthread} msg] $msg
42 } {1 {wrong # args: should be "testthread option ?args?"}}
44 test thread-1.2 {Tcl_ThreadObjCmd: bad option} {testthread} {
45 list [catch {testthread foo} msg] $msg
46 } {1 {bad option "foo": must be create, exit, id, join, names, send, wait, or errorproc}}
48 test thread-1.3 {Tcl_ThreadObjCmd: initial thread list} {testthread} {
49 list [threadReap] [llength [testthread names]]
52 test thread-1.4 {Tcl_ThreadObjCmd: thread create } {testthread} {
54 set serverthread [testthread create]
56 set numthreads [llength [testthread names]]
61 test thread-1.5 {Tcl_ThreadObjCmd: thread create one shot} {testthread} {
63 testthread create {set x 5}
64 foreach try {0 1 2 4 5 6} {
65 # Try various ways to yield
68 set l [llength [testthread names]]
77 test thread-1.6 {Tcl_ThreadObjCmd: thread exit} {testthread} {
79 testthread create {testthread exit}
82 set result [llength [testthread names]]
87 test thread-1.7 {Tcl_ThreadObjCmd: thread id args} {testthread} {
88 set x [catch {testthread id x} msg]
90 } {1 {wrong # args: should be "testthread id"}}
92 test thread-1.8 {Tcl_ThreadObjCmd: thread id} {testthread} {
93 string compare [testthread id] $::tcltest::mainThread
96 test thread-1.9 {Tcl_ThreadObjCmd: thread names args} {testthread} {
97 set x [catch {testthread names x} msg]
99 } {1 {wrong # args: should be "testthread names"}}
101 test thread-1.10 {Tcl_ThreadObjCmd: thread id} {testthread} {
102 string compare [testthread names] $::tcltest::mainThread
105 test thread-1.11 {Tcl_ThreadObjCmd: send args} {testthread} {
106 set x [catch {testthread send} msg]
108 } {1 {wrong # args: should be "testthread send ?-async? id script"}}
110 test thread-1.12 {Tcl_ThreadObjCmd: send nonint} {testthread} {
111 set x [catch {testthread send abc command} msg]
113 } {1 {expected integer but got "abc"}}
115 test thread-1.13 {Tcl_ThreadObjCmd: send args} {testthread} {
117 set serverthread [testthread create]
118 set five [testthread send $serverthread {set x 5}]
123 test thread-1.14 {Tcl_ThreadObjCmd: send bad id} {testthread} {
124 set tid [expr $::tcltest::mainThread + 10]
125 set x [catch {testthread send $tid {set x 5}} msg]
127 } {1 {invalid thread id}}
129 test thread-1.15 {Tcl_ThreadObjCmd: wait} {testthread} {
131 set serverthread [testthread create {set z 5 ; testthread wait}]
132 set five [testthread send $serverthread {set z}]
137 test thread-1.16 {Tcl_ThreadObjCmd: errorproc args} {testthread} {
138 set x [catch {testthread errorproc foo bar} msg]
140 } {1 {wrong # args: should be "testthread errorproc proc"}}
142 test thread-1.17 {Tcl_ThreadObjCmd: errorproc change} {testthread} {
143 testthread errorproc foo
144 testthread errorproc ThreadError
147 # The tests above also cover:
148 # TclCreateThread, except when pthread_create fails
149 # NewThread, safe and regular
150 # ThreadErrorProc, except for printing to standard error
152 test thread-2.1 {ListUpdateInner and ListRemove} {testthread} {
157 set tid [testthread create]
162 test thread-3.1 {TclThreadList} {testthread} {
165 set len [llength [testthread names]]
168 lappend l1 [testthread create]
170 set l2 [testthread names]
172 set c [string compare \
173 [lsort -integer [concat $::tcltest::mainThread $l1]] \
174 [lsort -integer $l2]]
179 test thread-4.1 {TclThreadSend to self} {testthread} {
181 testthread send [testthread id] {
187 test thread-4.2 {TclThreadSend -async} {testthread} {
189 set len [llength [testthread names]]
190 set serverthread [testthread create]
191 testthread send -async $serverthread {
195 set two [llength [testthread names]]
196 after 1500 {set done 1}
199 list $len [llength [testthread names]] $two
202 test thread-4.3 {TclThreadSend preserve errorInfo} {testthread} {
204 set len [llength [testthread names]]
205 set serverthread [testthread create]
206 set x [catch {testthread send $serverthread {set undef}} msg]
208 list $len $x $msg $errorInfo
209 } {1 1 {can't read "undef": no such variable} {can't read "undef": no such variable
213 "testthread send $serverthread {set undef}"}}
215 test thread-4.4 {TclThreadSend preserve code} {testthread} {
217 set len [llength [testthread names]]
218 set serverthread [testthread create]
219 set x [catch {testthread send $serverthread {break}} msg]
221 list $len $x $msg $errorInfo
224 test thread-4.5 {TclThreadSend preserve errorCode} {testthread} {
226 set ::tcltest::mainThread [testthread names]
227 set serverthread [testthread create]
228 set x [catch {testthread send $serverthread {error ERR INFO CODE}} msg]
230 list $x $msg $errorCode
234 test thread-5.0 {Joining threads} {testthread} {
236 set serverthread [testthread create -joinable]
237 testthread send -async $serverthread {after 1000 ; testthread exit}
238 set res [testthread join $serverthread]
243 test thread-5.1 {Joining threads after the fact} {testthread} {
245 set serverthread [testthread create -joinable]
246 testthread send -async $serverthread {testthread exit}
248 set res [testthread join $serverthread]
253 test thread-5.2 {Try to join a detached thread} {testthread} {
255 set serverthread [testthread create]
256 testthread send -async $serverthread {after 1000 ; testthread exit}
257 catch {set res [testthread join $serverthread]} msg
260 } {cannot join thread}
263 ::tcltest::cleanupTests