Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/tcl8.5.2/tests/async.test @ 47

Last change on this file since 47 was 25, checked in by landauf, 17 years ago

added tcl to libs

File size: 5.9 KB
Line 
1# Commands covered:  none
2#
3# This file contains a collection of tests for Tcl_AsyncCreate and related
4# library procedures.  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) 1993 The Regents of the University of California.
8# Copyright (c) 1994-1996 Sun Microsystems, Inc.
9# Copyright (c) 1998-1999 by Scriptics Corporation.
10#
11# See the file "license.terms" for information on usage and redistribution
12# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
13#
14# RCS: @(#) $Id: async.test,v 1.9 2006/03/21 11:12:27 dkf Exp $
15
16if {[lsearch [namespace children] ::tcltest] == -1} {
17    package require tcltest
18    namespace import -force ::tcltest::*
19}
20
21testConstraint testasync [llength [info commands testasync]]
22testConstraint threaded [expr {
23    [info exists ::tcl_platform(threaded)] && $::tcl_platform(threaded)
24}]
25
26proc async1 {result code} {
27    global aresult acode
28    set aresult $result
29    set acode $code
30    return "new result"
31}
32proc async2 {result code} {
33    global aresult acode
34    set aresult $result
35    set acode $code
36    return -code error "xyzzy"
37}
38proc async3 {result code} {
39    global aresult
40    set aresult "test pattern"
41    return -code $code $result
42}
43proc \# {result code} {
44    global aresult acode
45    set aresult $result
46    set acode $code
47    return "comment quoting"
48}
49
50if {[testConstraint testasync]} {
51    set handler1 [testasync create async1]
52    set handler2 [testasync create async2]
53    set handler3 [testasync create async3]
54    set handler4 [testasync create \#]
55}
56test async-1.1 {basic async handlers} testasync {
57    set aresult xxx
58    set acode yyy
59    list [catch {testasync mark $handler1 "original" 0} msg] $msg \
60           $acode $aresult
61} {0 {new result} 0 original}
62test async-1.2 {basic async handlers} testasync {
63    set aresult xxx
64    set acode yyy
65    list [catch {testasync mark $handler1 "original" 1} msg] $msg \
66           $acode $aresult
67} {0 {new result} 1 original}
68test async-1.3 {basic async handlers} testasync {
69    set aresult xxx
70    set acode yyy
71    list [catch {testasync mark $handler2 "old" 0} msg] $msg \
72           $acode $aresult
73} {1 xyzzy 0 old}
74test async-1.4 {basic async handlers} testasync {
75    set aresult xxx
76    set acode yyy
77    list [catch {testasync mark $handler2 "old" 3} msg] $msg \
78           $acode $aresult
79} {1 xyzzy 3 old}
80test async-1.5 {basic async handlers} testasync {
81    set aresult xxx
82    list [catch {testasync mark $handler3 "foobar" 0} msg] $msg $aresult
83} {0 foobar {test pattern}}
84test async-1.6 {basic async handlers} testasync {
85    set aresult xxx
86    list [catch {testasync mark $handler3 "foobar" 1} msg] $msg $aresult
87} {1 foobar {test pattern}}
88test async-1.7 {basic async handlers} testasync {
89    set aresult xxx
90    set acode yyy
91    list [catch {testasync mark $handler4 "original" 0} msg] $msg \
92           $acode $aresult
93} {0 {comment quoting} 0 original}
94
95proc mult1 {result code} {
96    global x
97    lappend x mult1
98    return -code 7 mult1
99}
100proc mult2 {result code} {
101    global x
102    lappend x mult2
103    return -code 9 mult2
104}
105proc mult3 {result code} {
106    global x hm1 hm2
107    lappend x [catch {testasync mark $hm2 serial2 0}]
108    lappend x [catch {testasync mark $hm1 serial1 0}]
109    lappend x mult3
110    return -code 11 mult3
111}
112if {[testConstraint testasync]} {
113    set hm1 [testasync create mult1]
114    set hm2 [testasync create mult2]
115    set hm3 [testasync create mult3]
116}
117test async-2.1 {multiple handlers} testasync {
118    set x {}
119    list [catch {testasync mark $hm3 "foobar" 5} msg] $msg $x
120} {9 mult2 {0 0 mult3 mult1 mult2}}
121
122proc del1 {result code} {
123    global x hm1 hm2 hm3 hm4
124    lappend x [catch {testasync mark $hm3 serial2 0}]
125    lappend x [catch {testasync mark $hm1 serial1 0}]
126    lappend x [catch {testasync mark $hm4 serial1 0}]
127    testasync delete $hm1
128    testasync delete $hm2
129    testasync delete $hm3
130    lappend x del1
131    return -code 13 del1
132}
133proc del2 {result code} {
134    global x
135    lappend x del2
136    return -code 3 del2
137}
138if {[testConstraint testasync]} {
139    testasync delete $handler1
140    testasync delete $hm2
141    testasync delete $hm3
142    set hm2 [testasync create del1]
143    set hm3 [testasync create mult2]
144    set hm4 [testasync create del2]
145}
146
147test async-3.1 {deleting handlers} testasync {
148    set x {}
149    list [catch {testasync mark $hm2 "foobar" 5} msg] $msg $x
150} {3 del2 {0 0 0 del1 del2}}
151
152proc nothing {} {
153    # empty proc
154}
155proc hang1 {handle} {
156    global aresult
157    set aresult {Async event not delivered}
158    testasync marklater $handle
159    for {set i 0} {
160        $i < 2500000  &&  $aresult eq "Async event not delivered"
161    } {incr i} {
162        nothing
163    }
164    return $aresult
165}
166proc hang2 {handle} {
167    global aresult
168    set aresult {Async event not delivered}
169    testasync marklater $handle
170    for {set i 0} {
171        $i < 2500000  &&  $aresult eq "Async event not delivered"
172    } {incr i} {}
173    return $aresult
174}
175proc hang3 {handle} [concat {
176    global aresult
177    set aresult {Async event not delivered}
178    testasync marklater $handle
179    set i 0
180} [string repeat {;incr i;} 1500000] {
181    return $aresult
182}]
183
184test async-4.1 {async interrupting bytecode sequence} -constraints {
185    testasync threaded
186} -setup {
187    set hm [testasync create async3]
188} -body {
189    hang1 $hm
190} -result {test pattern} -cleanup {
191    testasync delete $hm
192}
193test async-4.2 {async interrupting straight bytecode sequence} -constraints {
194    testasync threaded
195} -setup {
196    set hm [testasync create async3]
197} -body {
198    hang2 $hm
199} -result {test pattern} -cleanup {
200    testasync delete $hm
201}
202test async-4.3 {async interrupting loop-less bytecode sequence} -constraints {
203    testasync threaded
204} -setup {
205    set hm [testasync create async3]
206} -body {
207    hang3 $hm
208} -result {test pattern} -cleanup {
209    testasync delete $hm
210}
211
212# cleanup
213if {[testConstraint testasync]} {
214    testasync delete
215}
216::tcltest::cleanupTests
217return
218
219# Local Variables:
220# mode: tcl
221# End:
Note: See TracBrowser for help on using the repository browser.