os/persistentdata/persistentstorage/sqlite3api/TEST/TCL/tcldistribution/tests/thread.test
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     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.10 2000/05/02 22:02:36 kupries 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 set ::tcltest::testConstraints(testthread) \
    23 	[expr {[info commands testthread] != {}}]
    24 
    25 if {$::tcltest::testConstraints(testthread)} {
    26 
    27     testthread errorproc ThreadError
    28 
    29     proc ThreadError {id info} {
    30 	global threadError
    31 	set threadError $info
    32     }
    33 
    34     proc ThreadNullError {id info} {
    35 	# ignore
    36     }
    37 }
    38 
    39 
    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?"}}
    43 
    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}}
    47 
    48 test thread-1.3 {Tcl_ThreadObjCmd: initial thread list} {testthread} {
    49     list [threadReap] [llength [testthread names]]
    50 } {1 1}
    51 
    52 test thread-1.4 {Tcl_ThreadObjCmd: thread create } {testthread} {
    53     threadReap
    54     set serverthread [testthread create]
    55     update
    56     set numthreads [llength [testthread names]]
    57     threadReap
    58     set numthreads
    59 } {2}
    60 
    61 test thread-1.5 {Tcl_ThreadObjCmd: thread create one shot} {testthread} {
    62     threadReap
    63     testthread create {set x 5}
    64     foreach try {0 1 2 4 5 6} {
    65 	# Try various ways to yield
    66 	update
    67 	after 10
    68 	set l [llength [testthread names]]
    69 	if {$l == 1} {
    70 	    break
    71 	}
    72     }
    73     threadReap
    74     set l
    75 } {1}
    76 
    77 test thread-1.6 {Tcl_ThreadObjCmd: thread exit} {testthread} {
    78     threadReap
    79     testthread create {testthread exit}
    80     update
    81     after 10
    82     set result [llength [testthread names]]
    83     threadReap
    84     set result
    85 } {1}
    86 
    87 test thread-1.7 {Tcl_ThreadObjCmd: thread id args} {testthread} {
    88     set x [catch {testthread id x} msg]
    89     list $x $msg
    90 } {1 {wrong # args: should be "testthread id"}}
    91 
    92 test thread-1.8 {Tcl_ThreadObjCmd: thread id} {testthread} {
    93     string compare [testthread id] $::tcltest::mainThread
    94 } {0}
    95 
    96 test thread-1.9 {Tcl_ThreadObjCmd: thread names args} {testthread} {
    97     set x [catch {testthread names x} msg]
    98     list $x $msg
    99 } {1 {wrong # args: should be "testthread names"}}
   100 
   101 test thread-1.10 {Tcl_ThreadObjCmd: thread id} {testthread} {
   102     string compare [testthread names] $::tcltest::mainThread
   103 } {0}
   104 
   105 test thread-1.11 {Tcl_ThreadObjCmd: send args} {testthread} {
   106     set x [catch {testthread send} msg]
   107     list $x $msg
   108 } {1 {wrong # args: should be "testthread send ?-async? id script"}}
   109 
   110 test thread-1.12 {Tcl_ThreadObjCmd: send nonint} {testthread} {
   111     set x [catch {testthread send abc command} msg]
   112     list $x $msg
   113 } {1 {expected integer but got "abc"}}
   114 
   115 test thread-1.13 {Tcl_ThreadObjCmd: send args} {testthread} {
   116     threadReap
   117     set serverthread [testthread create]
   118     set five [testthread send $serverthread {set x 5}]
   119     threadReap
   120     set five
   121 } 5
   122 
   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]
   126     list $x $msg
   127 } {1 {invalid thread id}}
   128 
   129 test thread-1.15 {Tcl_ThreadObjCmd: wait} {testthread} {
   130     threadReap
   131     set serverthread [testthread create {set z 5 ; testthread wait}]
   132     set five [testthread send $serverthread {set z}]
   133     threadReap
   134     set five
   135 } 5
   136 
   137 test thread-1.16 {Tcl_ThreadObjCmd: errorproc args} {testthread} {
   138     set x [catch {testthread errorproc foo bar} msg]
   139     list $x $msg
   140 } {1 {wrong # args: should be "testthread errorproc proc"}}
   141 
   142 test thread-1.17 {Tcl_ThreadObjCmd: errorproc change} {testthread} {
   143     testthread errorproc foo
   144     testthread errorproc ThreadError
   145 } {}
   146 
   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
   151 
   152 test thread-2.1 {ListUpdateInner and ListRemove} {testthread} {
   153     threadReap
   154     catch {unset tid}
   155     foreach t {0 1 2} {
   156 	upvar #0 t$t tid
   157 	set tid [testthread create]
   158     }
   159     threadReap
   160 } 1
   161 
   162 test thread-3.1 {TclThreadList} {testthread} {
   163     threadReap
   164     catch {unset tid}
   165     set len [llength [testthread names]]
   166     set l1  {}
   167     foreach t {0 1 2} {
   168 	lappend l1 [testthread create]
   169     }
   170     set l2 [testthread names]
   171     list $l1 $l2
   172     set c [string compare \
   173 	    [lsort -integer [concat $::tcltest::mainThread $l1]] \
   174 	    [lsort -integer $l2]]
   175     threadReap
   176     list $len $c
   177 } {1 0}
   178 
   179 test thread-4.1 {TclThreadSend to self} {testthread} {
   180     catch {unset x}
   181     testthread send [testthread id] {
   182 	set x 4
   183     }
   184     set x
   185 } {4}
   186 
   187 test thread-4.2 {TclThreadSend -async} {testthread} {
   188     threadReap
   189     set len [llength [testthread names]]
   190     set serverthread [testthread create]
   191     testthread send -async $serverthread {
   192 	after 1000
   193 	testthread exit
   194     }
   195     set two [llength [testthread names]]
   196     after 1500 {set done 1}
   197     vwait done
   198     threadReap
   199     list $len [llength [testthread names]] $two
   200 } {1 1 2}
   201 
   202 test thread-4.3 {TclThreadSend preserve errorInfo} {testthread} {
   203     threadReap
   204     set len [llength [testthread names]]
   205     set serverthread [testthread create]
   206     set x [catch {testthread send $serverthread {set undef}} msg]
   207     threadReap
   208     list $len $x $msg $errorInfo
   209 } {1 1 {can't read "undef": no such variable} {can't read "undef": no such variable
   210     while executing
   211 "set undef"
   212     invoked from within
   213 "testthread send $serverthread {set undef}"}}
   214 
   215 test thread-4.4 {TclThreadSend preserve code} {testthread} {
   216     threadReap
   217     set len [llength [testthread names]]
   218     set serverthread [testthread create]
   219     set x [catch {testthread send $serverthread {break}} msg]
   220     threadReap
   221     list $len $x $msg $errorInfo
   222 } {1 3 {} {}}
   223 
   224 test thread-4.5 {TclThreadSend preserve errorCode} {testthread} {
   225     threadReap
   226     set ::tcltest::mainThread [testthread names]
   227     set serverthread [testthread create]
   228     set x [catch {testthread send $serverthread {error ERR INFO CODE}} msg]
   229     threadReap
   230     list $x $msg $errorCode
   231 } {1 ERR CODE}
   232 
   233 
   234 test thread-5.0 {Joining threads} {testthread} {
   235     threadReap
   236     set serverthread [testthread create -joinable]
   237     testthread send -async $serverthread {after 1000 ; testthread exit}
   238     set res [testthread join $serverthread]
   239     threadReap
   240     set res
   241 } {0}
   242 
   243 test thread-5.1 {Joining threads after the fact} {testthread} {
   244     threadReap
   245     set serverthread [testthread create -joinable]
   246     testthread send -async $serverthread {testthread exit}
   247     after 2000
   248     set res [testthread join $serverthread]
   249     threadReap
   250     set res
   251 } {0}
   252 
   253 test thread-5.2 {Try to join a detached thread} {testthread} {
   254     threadReap
   255     set serverthread [testthread create]
   256     testthread send -async $serverthread {after 1000 ; testthread exit}
   257     catch {set res [testthread join $serverthread]} msg
   258     threadReap
   259     lrange $msg 0 2
   260 } {cannot join thread}
   261 
   262 # cleanup
   263 ::tcltest::cleanupTests
   264 return