Debian Codegraph @

tests/async.test

raw
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 if {"::tcltest" ni [namespace children]} {
15 package require tcltest 2.5
16 namespace import -force ::tcltest::*
17 }
18
19 ::tcltest::loadTestedCommands
20 catch [list package require -exact Tcltest [info patchlevel]]
21
22 testConstraint testasync [llength [info commands testasync]]
23 testConstraint threaded [::tcl::pkgconfig get threaded]
24 testConstraint notWinCI [expr {$::tcl_platform(platform) != "windows" || ![info exists ::env(CI)]}]
25
26 proc async1 {result code} {
27 global aresult acode
28 set aresult $result
29 set acode $code
30 return "new result"
31 }
32 proc async2 {result code} {
33 global aresult acode
34 set aresult $result
35 set acode $code
36 return -code error "xyzzy"
37 }
38 proc async3 {result code} {
39 global aresult
40 set aresult "test pattern"
41 return -code $code $result
42 }
43 proc \# {result code} {
44 global aresult acode
45 set aresult $result
46 set acode $code
47 return "comment quoting"
48 }
49
50 if {[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 }
56 test 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}
62 test 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}
68 test 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}
74 test 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}
80 test 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}}
84 test 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}}
88 test 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
95 proc mult1 {result code} {
96 global x
97 lappend x mult1
98 return -code 7 mult1
99 }
100 proc mult2 {result code} {
101 global x
102 lappend x mult2
103 return -code 9 mult2
104 }
105 proc 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 }
112 if {[testConstraint testasync]} {
113 set hm1 [testasync create mult1]
114 set hm2 [testasync create mult2]
115 set hm3 [testasync create mult3]
116 }
117 test 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
122 proc 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 }
133 proc del2 {result code} {
134 global x
135 lappend x del2
136 return -code 3 del2
137 }
138 if {[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
147 test 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
152 test async-4.1 {async interrupting bytecode sequence} -constraints {
153 testasync threaded
154 } -setup {
155 set hm [testasync create async3]
156 proc nothing {} {
157 # empty proc
158 }
159 } -body {
160 apply {{handle} {
161 global aresult
162 set aresult {Async event not delivered}
163 testasync marklater $handle
164 # allow plenty of time to pass in case valgrind is running
165 set start [clock seconds]
166 while {
167 [clock seconds] - $start < 180 && $aresult eq "Async event not delivered"
168 } {
169 # be less busy
170 after 100
171 nothing
172 }
173 return $aresult
174 }} $hm
175 } -result {test pattern} -cleanup {
176 # give other threads some time to go way so that valgrind doesn't pick up
177 # "still reachable" cases from early thread termination
178 after 100
179 testasync delete $hm
180 }
181 test async-4.2 {async interrupting straight bytecode sequence} -constraints {
182 testasync threaded
183 } -setup {
184 set hm [testasync create async3]
185 } -body {
186 apply {{handle} {
187 global aresult
188 set aresult {Async event not delivered}
189 testasync marklater $handle
190 # allow plenty of time to pass in case valgrind is running
191 set start [clock seconds]
192 while {
193 [clock seconds] - $start < 180 && $aresult eq "Async event not delivered"
194 } {
195 # be less busy
196 after 100
197 }
198 return $aresult
199 }} $hm
200 } -result {test pattern} -cleanup {
201 # give other threads some time to go way so that valgrind doesn't pick up
202 # "still reachable" cases from early thread termination
203 after 100
204 testasync delete $hm
205 }
206 test async-4.3 {async interrupting loop-less bytecode sequence} -constraints {
207 testasync threaded notWinCI
208 } -setup {
209 set hm [testasync create async3]
210 } -body {
211 apply [list {handle} [concat {
212 global aresult
213 set aresult {Async event not delivered}
214 testasync marklater $handle
215 set i 0
216 } "[string repeat {;incr i;} 1500000]after 10;" {
217 return $aresult
218 }]] $hm
219 } -result {test pattern} -cleanup {
220 # give other threads some time to go way so that valgrind doesn't pick up
221 # "still reachable" cases from early thread termination
222 after 100
223 testasync delete $hm
224 }
225
226 # cleanup
227 if {[testConstraint testasync]} {
228 testasync delete
229 }
230 ::tcltest::cleanupTests
231 return
232
233 # Local Variables:
234 # mode: tcl
235 # End: