Debian Codegraph @

tests/ioCmd.test

raw
1 # -*- tcl -*-
2 # Commands covered: open, close, gets, read, puts, seek, tell, eof, flush,
3 # fblocked, fconfigure, open, channel, fcopy
4 #
5 # This file contains a collection of tests for one or more of the Tcl
6 # built-in commands. Sourcing this file into Tcl runs the tests and
7 # generates output for errors. No output means no errors were found.
8 #
9 # Copyright (c) 1991-1994 The Regents of the University of California.
10 # Copyright (c) 1994-1996 Sun Microsystems, Inc.
11 # Copyright (c) 1998-1999 Scriptics Corporation.
12 #
13 # See the file "license.terms" for information on usage and redistribution
14 # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
15
16 if {"::tcltest" ni [namespace children]} {
17 package require tcltest 2.5
18 namespace import -force ::tcltest::*
19 }
20 source [file join [file dirname [info script]] tcltests.tcl]
21
22 ::tcltest::loadTestedCommands
23 catch [list package require -exact Tcltest [info patchlevel]]
24
25 # Custom constraints used in this file
26 testConstraint testchannel [llength [info commands testchannel]]
27
28 #----------------------------------------------------------------------
29
30 test iocmd-1.1 {puts command} {
31 list [catch {puts} msg] $msg
32 } {1 {wrong # args: should be "puts ?-nonewline? ?channelId? string"}}
33 test iocmd-1.2 {puts command} {
34 list [catch {puts a b c d e f g} msg] $msg
35 } {1 {wrong # args: should be "puts ?-nonewline? ?channelId? string"}}
36 test iocmd-1.3 {puts command} {
37 list [catch {puts froboz -nonewline kablooie} msg] $msg
38 } {1 {wrong # args: should be "puts ?-nonewline? ?channelId? string"}}
39 test iocmd-1.4 {puts command} {
40 list [catch {puts froboz hello} msg] $msg
41 } {1 {can not find channel named "froboz"}}
42 test iocmd-1.5 {puts command} {
43 list [catch {puts stdin hello} msg] $msg
44 } {1 {channel "stdin" wasn't opened for writing}}
45
46 set path(test1) [makeFile {} test1]
47
48 test iocmd-1.6 {puts command} {
49 set f [open $path(test1) w]
50 fconfigure $f -translation lf -eofchar {}
51 puts -nonewline $f foobar
52 close $f
53 file size $path(test1)
54 } 6
55 test iocmd-1.7 {puts command} {
56 set f [open $path(test1) w]
57 fconfigure $f -translation lf -eofchar {}
58 puts $f foobar
59 close $f
60 file size $path(test1)
61 } 7
62 test iocmd-1.8 {puts command} {
63 set f [open $path(test1) w]
64 fconfigure $f -translation binary
65 puts -nonewline $f [binary format a4a5 foo bar]
66 close $f
67 file size $path(test1)
68 } 9
69
70 test iocmd-2.1 {flush command} {
71 list [catch {flush} msg] $msg
72 } {1 {wrong # args: should be "flush channelId"}}
73 test iocmd-2.2 {flush command} {
74 list [catch {flush a b c d e} msg] $msg
75 } {1 {wrong # args: should be "flush channelId"}}
76 test iocmd-2.3 {flush command} {
77 list [catch {flush foo} msg] $msg
78 } {1 {can not find channel named "foo"}}
79 test iocmd-2.4 {flush command} {
80 list [catch {flush stdin} msg] $msg
81 } {1 {channel "stdin" wasn't opened for writing}}
82
83 test iocmd-3.1 {gets command} {
84 list [catch {gets} msg] $msg
85 } {1 {wrong # args: should be "gets channelId ?varName?"}}
86 test iocmd-3.2 {gets command} {
87 list [catch {gets a b c d e f g} msg] $msg
88 } {1 {wrong # args: should be "gets channelId ?varName?"}}
89 test iocmd-3.3 {gets command} {
90 list [catch {gets aaa} msg] $msg
91 } {1 {can not find channel named "aaa"}}
92 test iocmd-3.4 {gets command} {
93 list [catch {gets stdout} msg] $msg
94 } {1 {channel "stdout" wasn't opened for reading}}
95 test iocmd-3.5 {gets command} {
96 set f [open $path(test1) w]
97 puts $f [binary format a4a5 foo bar]
98 close $f
99 set f [open $path(test1) r]
100 set result [gets $f]
101 close $f
102 set x foo\x00
103 set x "${x}bar\x00\x00"
104 string compare $x $result
105 } 0
106
107 test iocmd-4.1 {read command} {
108 list [catch {read} msg] $msg
109 } {1 {wrong # args: should be "read channelId ?numChars?" or "read ?-nonewline? channelId"}}
110 test iocmd-4.2 {read command} {
111 list [catch {read a b c d e f g h} msg] $msg
112 } {1 {wrong # args: should be "read channelId ?numChars?" or "read ?-nonewline? channelId"}}
113 test iocmd-4.3 {read command} {
114 list [catch {read aaa} msg] $msg
115 } {1 {can not find channel named "aaa"}}
116 test iocmd-4.4 {read command} {
117 list [catch {read -nonewline} msg] $msg
118 } {1 {wrong # args: should be "read channelId ?numChars?" or "read ?-nonewline? channelId"}}
119 test iocmd-4.5 {read command} {
120 list [catch {read -nonew file4} msg] $msg $::errorCode
121 } {1 {can not find channel named "-nonew"} {TCL LOOKUP CHANNEL -nonew}}
122 test iocmd-4.6 {read command} {
123 list [catch {read stdout} msg] $msg
124 } {1 {channel "stdout" wasn't opened for reading}}
125 test iocmd-4.7 {read command} {
126 list [catch {read -nonewline stdout} msg] $msg
127 } {1 {channel "stdout" wasn't opened for reading}}
128 test iocmd-4.8 {read command with incorrect combination of arguments} {
129 file delete $path(test1)
130 set f [open $path(test1) w]
131 puts $f "Two lines: this one"
132 puts $f "and this one"
133 close $f
134 set f [open $path(test1)]
135 set x [list [catch {read -nonewline $f 20 z} msg] $msg $::errorCode]
136 close $f
137 set x
138 } {1 {wrong # args: should be "read channelId ?numChars?" or "read ?-nonewline? channelId"} {TCL WRONGARGS}}
139 test iocmd-4.9 {read command} {
140 list [catch {read stdin foo} msg] $msg $::errorCode
141 } {1 {expected non-negative integer but got "foo"} {TCL VALUE NUMBER}}
142 test iocmd-4.10 {read command} {
143 list [catch {read file107} msg] $msg $::errorCode
144 } {1 {can not find channel named "file107"} {TCL LOOKUP CHANNEL file107}}
145 set path(test3) [makeFile {} test3]
146 test iocmd-4.11 {read command} {
147 set f [open $path(test3) w]
148 set x [list [catch {read $f} msg] $msg $::errorCode]
149 close $f
150 string compare [string tolower $x] \
151 [list 1 [format "channel \"%s\" wasn't opened for reading" $f] none]
152 } 0
153 test iocmd-4.12 {read command} -setup {
154 set f [open $path(test1)]
155 } -body {
156 read $f 12z
157 } -cleanup {
158 close $f
159 } -result {expected non-negative integer but got "12z"} -errorCode {TCL VALUE NUMBER}
160
161 test iocmd-5.1 {seek command} -returnCodes error -body {
162 seek
163 } -result {wrong # args: should be "seek channelId offset ?origin?"}
164 test iocmd-5.2 {seek command} -returnCodes error -body {
165 seek a b c d e f g
166 } -result {wrong # args: should be "seek channelId offset ?origin?"}
167 test iocmd-5.3 {seek command} -returnCodes error -body {
168 seek stdin gugu
169 } -result {expected integer but got "gugu"}
170 test iocmd-5.4 {seek command} -returnCodes error -body {
171 seek stdin 100 gugu
172 } -result {bad origin "gugu": must be start, current, or end}
173
174 test iocmd-6.1 {tell command} {
175 list [catch {tell} msg] $msg
176 } {1 {wrong # args: should be "tell channelId"}}
177 test iocmd-6.2 {tell command} {
178 list [catch {tell a b c d e} msg] $msg
179 } {1 {wrong # args: should be "tell channelId"}}
180 test iocmd-6.3 {tell command} {
181 list [catch {tell aaa} msg] $msg
182 } {1 {can not find channel named "aaa"}}
183
184 test iocmd-7.1 {close command} {
185 list [catch {close} msg] $msg
186 } {1 {wrong # args: should be "close channelId ?direction?"}}
187 test iocmd-7.2 {close command} {
188 list [catch {close a b c d e} msg] $msg
189 } {1 {wrong # args: should be "close channelId ?direction?"}}
190 test iocmd-7.3 {close command} {
191 list [catch {close aaa} msg] $msg
192 } {1 {can not find channel named "aaa"}}
193 test iocmd-7.4 {close command} -setup {
194 set chan [open [info script] r]
195 } -body {
196 chan close $chan bar
197 } -cleanup {
198 close $chan
199 } -returnCodes error -result "bad direction \"bar\": must be read or write"
200 test iocmd-7.5 {close command} -setup {
201 set chan [open [info script] r]
202 } -body {
203 chan close $chan write
204 } -cleanup {
205 close $chan
206 } -returnCodes error -result "Half-close of write-side not possible, side not opened or already closed"
207
208 test iocmd-8.1 {fconfigure command} {
209 list [catch {fconfigure} msg] $msg
210 } {1 {wrong # args: should be "fconfigure channelId ?-option value ...?"}}
211 test iocmd-8.2 {fconfigure command} {
212 list [catch {fconfigure a b c d e f} msg] $msg
213 } {1 {wrong # args: should be "fconfigure channelId ?-option value ...?"}}
214 test iocmd-8.3 {fconfigure command} {
215 list [catch {fconfigure a b} msg] $msg
216 } {1 {can not find channel named "a"}}
217 test iocmd-8.4 {fconfigure command} {
218 file delete $path(test1)
219 set f1 [open $path(test1) w]
220 set x [list [catch {fconfigure $f1 froboz} msg] $msg]
221 close $f1
222 set x
223 } {1 {bad option "froboz": should be one of -blocking, -buffering, -buffersize, -encoding, -eofchar, or -translation}}
224 test iocmd-8.5 {fconfigure command} {
225 list [catch {fconfigure stdin -buffering froboz} msg] $msg
226 } {1 {bad value for -buffering: must be one of full, line, or none}}
227 test iocmd-8.6 {fconfigure command} {
228 list [catch {fconfigure stdin -translation froboz} msg] $msg
229 } {1 {bad value for -translation: must be one of auto, binary, cr, lf, crlf, or platform}}
230 test iocmd-8.7 {fconfigure command} {
231 file delete $path(test1)
232 set f1 [open $path(test1) w]
233 fconfigure $f1 -translation lf -eofchar {} -encoding unicode
234 set x [fconfigure $f1]
235 close $f1
236 set x
237 } {-blocking 1 -buffering full -buffersize 4096 -encoding unicode -eofchar {} -translation lf}
238 test iocmd-8.8 {fconfigure command} {
239 file delete $path(test1)
240 set f1 [open $path(test1) w]
241 fconfigure $f1 -translation lf -buffering line -buffersize 3030 \
242 -eofchar {} -encoding unicode
243 set x ""
244 lappend x [fconfigure $f1 -buffering]
245 lappend x [fconfigure $f1]
246 close $f1
247 set x
248 } {line {-blocking 1 -buffering line -buffersize 3030 -encoding unicode -eofchar {} -translation lf}}
249 test iocmd-8.9 {fconfigure command} {
250 file delete $path(test1)
251 set f1 [open $path(test1) w]
252 fconfigure $f1 -translation binary -buffering none -buffersize 4040
253 set x [fconfigure $f1]
254 close $f1
255 set x
256 } {-blocking 1 -buffering none -buffersize 4040 -encoding binary -eofchar {} -translation lf}
257 test iocmd-8.10 {fconfigure command} {
258 list [catch {fconfigure a b} msg] $msg
259 } {1 {can not find channel named "a"}}
260 set path(fconfigure.dummy) [makeFile {} fconfigure.dummy]
261 test iocmd-8.11 {fconfigure command} {
262 set chan [open $path(fconfigure.dummy) r]
263 set res [list [catch {fconfigure $chan -froboz blarfo} msg] $msg]
264 close $chan
265 set res
266 } {1 {bad option "-froboz": should be one of -blocking, -buffering, -buffersize, -encoding, -eofchar, or -translation}}
267 test iocmd-8.12 {fconfigure command} {
268 set chan [open $path(fconfigure.dummy) r]
269 set res [list [catch {fconfigure $chan -b blarfo} msg] $msg]
270 close $chan
271 set res
272 } {1 {bad option "-b": should be one of -blocking, -buffering, -buffersize, -encoding, -eofchar, or -translation}}
273 test iocmd-8.13 {fconfigure command} {
274 set chan [open $path(fconfigure.dummy) r]
275 set res [list [catch {fconfigure $chan -buffer blarfo} msg] $msg]
276 close $chan
277 set res
278 } {1 {bad option "-buffer": should be one of -blocking, -buffering, -buffersize, -encoding, -eofchar, or -translation}}
279 removeFile fconfigure.dummy
280 test iocmd-8.14 {fconfigure command} {
281 fconfigure stdin -buffers
282 } 4096
283 test iocmd-8.15.1 {fconfigure command / tcp channel} -constraints {socket unixOrWin} -setup {
284 set srv [socket -server iocmdSRV -myaddr 127.0.0.1 0]
285 set port [lindex [fconfigure $srv -sockname] 2]
286 proc iocmdSRV {sock ip port} {close $sock}
287 set cli [socket 127.0.0.1 $port]
288 } -body {
289 fconfigure $cli -blah
290 } -cleanup {
291 close $cli
292 close $srv
293 unset cli srv port
294 rename iocmdSRV {}
295 } -returnCodes error -result {bad option "-blah": should be one of -blocking, -buffering, -buffersize, -encoding, -eofchar, -translation, -connecting, -peername, or -sockname}
296 test iocmd-8.16 {fconfigure command / tcp channel} -constraints socket -setup {
297 set srv [socket -server iocmdSRV -myaddr 127.0.0.1 0]
298 set port [lindex [fconfigure $srv -sockname] 2]
299 proc iocmdSRV {sock ip port} {close $sock}
300 set cli [socket 127.0.0.1 $port]
301 } -body {
302 expr {[lindex [fconfigure $cli -peername] 2] == $port}
303 } -cleanup {
304 close $cli
305 close $srv
306 unset cli srv port
307 rename iocmdSRV {}
308 } -result 1
309 test iocmd-8.17 {fconfigure command / tcp channel} -constraints nonPortable -setup {
310 set srv [socket -server iocmdSRV -myaddr 127.0.0.1 0]
311 set port [lindex [fconfigure $srv -sockname] 2]
312 proc iocmdSRV {sock ip port} {close $sock}
313 set cli [socket 127.0.0.1 $port]
314 } -body {
315 # It is possible that you don't get the connection reset by peer
316 # error but rather a valid answer. Depends on the tcp implementation
317 update
318 puts $cli "blah"
319 flush $cli; # that flush could/should fail too
320 update
321 regsub -all {can([^:])+: } [catch {fconfigure $cli -peername} msg] {}
322 } -cleanup {
323 close $cli
324 close $srv
325 unset cli srv port
326 rename iocmdSRV {}
327 } -result 1
328 test iocmd-8.18 {fconfigure command / unix tty channel} -constraints {nonPortable unix} -setup {
329 set tty ""
330 } -body {
331 # might fail if /dev/ttya is unavailable
332 set tty [open /dev/ttya]
333 fconfigure $tty -blah blih
334 } -cleanup {
335 if {$tty ne ""} {
336 close $tty
337 }
338 } -returnCodes error -result {bad option "-blah": should be one of -blocking, -buffering, -buffersize, -encoding, -eofchar, -translation, or -mode}
339 test iocmd-8.19 {fconfigure command / win tty channel} -constraints {nonPortable win} -setup {
340 set tty ""
341 } -body {
342 # might fail early if com1 is unavailable
343 set tty [open com1]
344 fconfigure $tty -blah blih
345 } -cleanup {
346 if {$tty ne ""} {
347 close $tty
348 }
349 } -returnCodes error -result {bad option "-blah": should be one of -blocking, -buffering, -buffersize, -encoding, -eofchar, -translation, -mode, -handshake, -pollinterval, -sysbuffer, -timeout, -ttycontrol, or -xchar}
350 # TODO: Test parsing of serial channel options (nonPortable, since requires an
351 # open channel to work with).
352
353 test iocmd-9.1 {eof command} {
354 list [catch {eof} msg] $msg $::errorCode
355 } {1 {wrong # args: should be "eof channelId"} {TCL WRONGARGS}}
356 test iocmd-9.2 {eof command} {
357 list [catch {eof a b} msg] $msg $::errorCode
358 } {1 {wrong # args: should be "eof channelId"} {TCL WRONGARGS}}
359 test iocmd-9.3 {eof command} {
360 catch {close file100}
361 list [catch {eof file100} msg] $msg $::errorCode
362 } {1 {can not find channel named "file100"} {TCL LOOKUP CHANNEL file100}}
363
364 # The tests for Tcl_ExecObjCmd are in exec.test
365
366 test iocmd-10.1 {fblocked command} {
367 list [catch {fblocked} msg] $msg
368 } {1 {wrong # args: should be "fblocked channelId"}}
369 test iocmd-10.2 {fblocked command} {
370 list [catch {fblocked a b c d e f g} msg] $msg
371 } {1 {wrong # args: should be "fblocked channelId"}}
372 test iocmd-10.3 {fblocked command} {
373 list [catch {fblocked file1000} msg] $msg
374 } {1 {can not find channel named "file1000"}}
375 test iocmd-10.4 {fblocked command} {
376 list [catch {fblocked stdout} msg] $msg
377 } {1 {channel "stdout" wasn't opened for reading}}
378 test iocmd-10.5 {fblocked command} {
379 fblocked stdin
380 } 0
381
382 set path(test4) [makeFile {} test4]
383 set path(test5) [makeFile {} test5]
384
385 test iocmd-11.1 {I/O to command pipelines} {unixOrWin unixExecs} {
386 set f [open $path(test4) w]
387 close $f
388 list [catch {open "| cat < \"$path(test4)\" > \"$path(test5)\"" w} msg] $msg $::errorCode
389 } {1 {can't write input to command: standard input was redirected} {TCL OPERATION EXEC BADREDIRECT}}
390 test iocmd-11.2 {I/O to command pipelines} {unixOrWin unixExecs} {
391 list [catch {open "| echo > \"$path(test5)\"" r} msg] $msg $::errorCode
392 } {1 {can't read output from command: standard output was redirected} {TCL OPERATION EXEC BADREDIRECT}}
393 test iocmd-11.3 {I/O to command pipelines} {unixOrWin unixExecs} {
394 list [catch {open "| echo > \"$path(test5)\"" r+} msg] $msg $::errorCode
395 } {1 {can't read output from command: standard output was redirected} {TCL OPERATION EXEC BADREDIRECT}}
396 test iocmd-11.4 {I/O to command pipelines} {notValgrind unixOrWin} {
397 list [catch {open "| no_such_command_exists" rb} msg] $msg $::errorCode
398 } {1 {couldn't execute "no_such_command_exists": no such file or directory} {POSIX ENOENT {no such file or directory}}}
399
400 test iocmd-12.1 {POSIX open access modes: RDONLY} {
401 file delete $path(test1)
402 set f [open $path(test1) w]
403 puts $f "Two lines: this one"
404 puts $f "and this one"
405 close $f
406 set f [open $path(test1) RDONLY]
407 set x [list [gets $f] [catch {puts $f Test} msg] $msg]
408 close $f
409 string compare $x \
410 "{Two lines: this one} 1 [list [format "channel \"%s\" wasn't opened for writing" $f]]"
411 } 0
412 test iocmd-12.2 {POSIX open access modes: RDONLY} -match regexp -body {
413 file delete $path(test3)
414 open $path(test3) RDONLY
415 } -returnCodes error -result {(?i)couldn't open ".*test3": no such file or directory}
416 test iocmd-12.3 {POSIX open access modes: WRONLY} -match regexp -body {
417 file delete $path(test3)
418 open $path(test3) WRONLY
419 } -returnCodes error -result {(?i)couldn't open ".*test3": no such file or directory}
420 #
421 # Test 13.4 relies on assigning the same channel name twice.
422 #
423 test iocmd-12.4 {POSIX open access modes: WRONLY} {unix} {
424 file delete $path(test3)
425 set f [open $path(test3) w]
426 fconfigure $f -eofchar {}
427 puts $f xyzzy
428 close $f
429 set f [open $path(test3) WRONLY]
430 fconfigure $f -eofchar {}
431 puts -nonewline $f "ab"
432 seek $f 0 current
433 set x [list [catch {gets $f} msg] $msg]
434 close $f
435 set f [open $path(test3) r]
436 fconfigure $f -eofchar {}
437 lappend x [gets $f]
438 close $f
439 set y [list 1 [format "channel \"%s\" wasn't opened for reading" $f] abzzy]
440 string compare $x $y
441 } 0
442 test iocmd-12.5 {POSIX open access modes: RDWR} -match regexp -body {
443 file delete $path(test3)
444 open $path(test3) RDWR
445 } -returnCodes error -result {(?i)couldn't open ".*test3": no such file or directory}
446 test iocmd-12.6 {POSIX open access modes: errors} {
447 concat [catch {open $path(test3) "FOO \{BAR BAZ"} msg] $msg\n$::errorInfo
448 } "1 unmatched open brace in list
449 unmatched open brace in list
450 while processing open access modes \"FOO {BAR BAZ\"
451 invoked from within
452 \"open \$path(test3) \"FOO \\{BAR BAZ\"\""
453 test iocmd-12.7 {POSIX open access modes: errors} {
454 list [catch {open $path(test3) {FOO BAR BAZ}} msg] $msg
455 } {1 {invalid access mode "FOO": must be RDONLY, WRONLY, RDWR, APPEND, BINARY, CREAT, EXCL, NOCTTY, NONBLOCK, or TRUNC}}
456 test iocmd-12.8 {POSIX open access modes: errors} {
457 list [catch {open $path(test3) {TRUNC CREAT}} msg] $msg
458 } {1 {access mode must include either RDONLY, WRONLY, or RDWR}}
459 close [open $path(test3) w]
460 test iocmd-12.9 {POSIX open access modes: BINARY} {
461 list [catch {open $path(test1) BINARY} msg] $msg
462 } {1 {access mode must include either RDONLY, WRONLY, or RDWR}}
463 test iocmd-12.10 {POSIX open access modes: BINARY} {
464 set f [open $path(test1) {WRONLY BINARY TRUNC}]
465 puts $f a
466 puts $f b
467 puts -nonewline $f c ;# contents are now 5 bytes: a\nb\nc
468 close $f
469 set f [open $path(test1) r]
470 fconfigure $f -translation binary
471 set result [string length [read $f]]
472 close $f
473 set result
474 } 5
475 test iocmd-12.11 {POSIX open access modes: BINARY} {
476 set f [open $path(test1) {WRONLY BINARY TRUNC}]
477 puts $f \u0248 ;# gets truncated to \u0048
478 close $f
479 set f [open $path(test1) r]
480 fconfigure $f -translation binary
481 set result [read -nonewline $f]
482 close $f
483 set result
484 } H
485
486 test iocmd-13.1 {errors in open command} {
487 list [catch {open} msg] $msg
488 } {1 {wrong # args: should be "open fileName ?access? ?permissions?"}}
489 test iocmd-13.2 {errors in open command} {
490 list [catch {open a b c d} msg] $msg
491 } {1 {wrong # args: should be "open fileName ?access? ?permissions?"}}
492 test iocmd-13.3 {errors in open command} {
493 list [catch {open $path(test1) x} msg] $msg
494 } {1 {illegal access mode "x"}}
495 test iocmd-13.4 {errors in open command} {
496 list [catch {open $path(test1) rw} msg] $msg
497 } {1 {illegal access mode "rw"}}
498 test iocmd-13.5 {errors in open command} {
499 list [catch {open $path(test1) r+1} msg] $msg
500 } {1 {illegal access mode "r+1"}}
501 test iocmd-13.6 {errors in open command} {
502 set msg [list [catch {open _non_existent_} msg] $msg $::errorCode]
503 regsub [file join {} _non_existent_] $msg "_non_existent_" msg
504 string tolower $msg
505 } {1 {couldn't open "_non_existent_": no such file or directory} {posix enoent {no such file or directory}}}
506 test iocmd-13.7 {errors in open command} {
507 list [catch {open $path(test1) b} msg] $msg
508 } {1 {illegal access mode "b"}}
509 test iocmd-13.8 {errors in open command} {
510 list [catch {open $path(test1) rbb} msg] $msg
511 } {1 {illegal access mode "rbb"}}
512 test iocmd-13.9 {errors in open command} {
513 list [catch {open $path(test1) r++} msg] $msg
514 } {1 {illegal access mode "r++"}}
515 test iocmd-13.10.1 {open for append, a mode} -setup {
516 set log [makeFile {} out]
517 set chans {}
518 } -body {
519 foreach i { 0 1 2 3 4 5 6 7 8 9 } {
520 puts [set ch [open $log a]] $i
521 lappend chans $ch
522 }
523 foreach ch $chans {catch {close $ch}}
524 lsort [split [string trim [viewFile out]] \n]
525 } -cleanup {
526 removeFile out
527 # Ensure that channels are gone, even if body failed to do so
528 foreach ch $chans {catch {close $ch}}
529 } -result {0 1 2 3 4 5 6 7 8 9}
530 test iocmd-13.10.2 {open for append, O_APPEND} -setup {
531 set log [makeFile {} out]
532 set chans {}
533 } -body {
534 foreach i { 0 1 2 3 4 5 6 7 8 9 } {
535 puts [set ch [open $log {WRONLY CREAT APPEND}]] $i
536 lappend chans $ch
537 }
538 foreach ch $chans {catch {close $ch}}
539 lsort [split [string trim [viewFile out]] \n]
540 } -cleanup {
541 removeFile out
542 # Ensure that channels are gone, even if body failed to do so
543 foreach ch $chans {catch {close $ch}}
544 } -result {0 1 2 3 4 5 6 7 8 9}
545 test ioCmd-13.11 {open ... a+ must not use O_APPEND: Bug 1773127} -setup {
546 set f [makeFile {} ioutil41.tmp]
547 set fid [open $f wb]
548 puts -nonewline $fid 123
549 close $fid
550 } -body {
551 set fid [open $f ab+]
552 puts -nonewline $fid 456
553 seek $fid 2
554 set d [read $fid 2]
555 seek $fid 4
556 puts -nonewline $fid x
557 close $fid
558 set fid [open $f rb]
559 append d [read $fid]
560 close $fid
561 return $d
562 } -cleanup {
563 removeFile $f
564 } -result 341234x6
565
566 test iocmd-14.1 {file id parsing errors} {
567 list [catch {eof gorp} msg] $msg $::errorCode
568 } {1 {can not find channel named "gorp"} {TCL LOOKUP CHANNEL gorp}}
569 test iocmd-14.2 {file id parsing errors} {
570 list [catch {eof filex} msg] $msg
571 } {1 {can not find channel named "filex"}}
572 test iocmd-14.3 {file id parsing errors} {
573 list [catch {eof file12a} msg] $msg
574 } {1 {can not find channel named "file12a"}}
575 test iocmd-14.4 {file id parsing errors} {
576 list [catch {eof file123} msg] $msg
577 } {1 {can not find channel named "file123"}}
578 test iocmd-14.5 {file id parsing errors} {
579 list [catch {eof stdout} msg] $msg
580 } {0 0}
581 test iocmd-14.6 {file id parsing errors} {
582 list [catch {eof stdin} msg] $msg
583 } {0 0}
584 test iocmd-14.7 {file id parsing errors} {
585 list [catch {eof stdout} msg] $msg
586 } {0 0}
587 test iocmd-14.8 {file id parsing errors} {
588 list [catch {eof stderr} msg] $msg
589 } {0 0}
590 test iocmd-14.9 {file id parsing errors} {
591 list [catch {eof stderr1} msg] $msg
592 } {1 {can not find channel named "stderr1"}}
593
594 set f [open $path(test1) w]
595 close $f
596
597 set expect "1 {can not find channel named \"$f\"}"
598 test iocmd-14.10 {file id parsing errors} {
599 list [catch {eof $f} msg] $msg
600 } $expect
601
602 test iocmd-15.1 {Tcl_FcopyObjCmd} {fcopy} {
603 list [catch {fcopy} msg] $msg
604 } {1 {wrong # args: should be "fcopy input output ?-size size? ?-command callback?"}}
605 test iocmd-15.2 {Tcl_FcopyObjCmd} {fcopy} {
606 list [catch {fcopy 1} msg] $msg
607 } {1 {wrong # args: should be "fcopy input output ?-size size? ?-command callback?"}}
608 test iocmd-15.3 {Tcl_FcopyObjCmd} {fcopy} {
609 list [catch {fcopy 1 2 3 4 5 6 7} msg] $msg
610 } {1 {wrong # args: should be "fcopy input output ?-size size? ?-command callback?"}}
611 test iocmd-15.4 {Tcl_FcopyObjCmd} {fcopy} {
612 list [catch {fcopy 1 2 3} msg] $msg
613 } {1 {wrong # args: should be "fcopy input output ?-size size? ?-command callback?"}}
614 test iocmd-15.5 {Tcl_FcopyObjCmd} {fcopy} {
615 list [catch {fcopy 1 2 3 4 5} msg] $msg
616 } {1 {wrong # args: should be "fcopy input output ?-size size? ?-command callback?"}}
617
618 set path(test2) [makeFile {} test2]
619 set f [open $path(test1) w]
620 close $f
621 set rfile [open $path(test1) r]
622 set wfile [open $path(test2) w]
623
624 test iocmd-15.6 {Tcl_FcopyObjCmd} {fcopy} {
625 list [catch {fcopy foo $wfile} msg] $msg
626 } {1 {can not find channel named "foo"}}
627 test iocmd-15.7 {Tcl_FcopyObjCmd} {fcopy} {
628 list [catch {fcopy $rfile foo} msg] $msg
629 } {1 {can not find channel named "foo"}}
630 test iocmd-15.8 {Tcl_FcopyObjCmd} {fcopy} {
631 list [catch {fcopy $wfile $wfile} msg] $msg
632 } "1 {channel \"$wfile\" wasn't opened for reading}"
633 test iocmd-15.9 {Tcl_FcopyObjCmd} {fcopy} {
634 list [catch {fcopy $rfile $rfile} msg] $msg
635 } "1 {channel \"$rfile\" wasn't opened for writing}"
636 test iocmd-15.10 {Tcl_FcopyObjCmd} {fcopy} {
637 list [catch {fcopy $rfile $wfile foo bar} msg] $msg
638 } {1 {bad option "foo": must be -size or -command}}
639 test iocmd-15.11 {Tcl_FcopyObjCmd} {fcopy} {
640 list [catch {fcopy $rfile $wfile -size foo} msg] $msg
641 } {1 {expected integer but got "foo"}}
642 test iocmd-15.12 {Tcl_FcopyObjCmd} {fcopy} {
643 list [catch {fcopy $rfile $wfile -command bar -size foo} msg] $msg
644 } {1 {expected integer but got "foo"}}
645
646 close $rfile
647 close $wfile
648
649 # ### ### ### ######### ######### #########
650 ## Testing the reflected channel.
651
652 test iocmd-20.0 {chan, wrong#args} {
653 catch {chan} msg
654 set msg
655 } {wrong # args: should be "chan subcommand ?arg ...?"}
656 test iocmd-20.1 {chan, unknown method} -body {
657 chan foo
658 } -returnCodes error -match glob -result {unknown or ambiguous subcommand "foo": must be *}
659
660 # --- --- --- --------- --------- ---------
661 # chan create, and method "initialize"
662
663 test iocmd-21.0 {chan create, wrong#args, not enough} {
664 catch {chan create} msg
665 set msg
666 } {wrong # args: should be "chan create mode cmdprefix"}
667 test iocmd-21.1 {chan create, wrong#args, too many} {
668 catch {chan create a b c} msg
669 set msg
670 } {wrong # args: should be "chan create mode cmdprefix"}
671 test iocmd-21.2 {chan create, r/w mode empty} {
672 proc foo {cmd args} { return {initialize finalize watch} }
673 set chan [chan create {} foo]
674 close $chan
675 rename foo {}
676 } {}
677 test iocmd-21.3 {chan create, invalid r/w mode, bad string} {
678 proc foo {} {}
679 catch {chan create {c} foo} msg
680 rename foo {}
681 set msg
682 } {bad mode "c": must be read or write}
683 test iocmd-21.4 {chan create, bad handler, not a list} {
684 catch {chan create {r w} "foo \{"} msg
685 set msg
686 } {unmatched open brace in list}
687 test iocmd-21.5 {chan create, bad handler, not a command} {
688 catch {chan create {r w} foo} msg
689 set msg
690 } {invalid command name "foo"}
691 test iocmd-21.6 {chan create, initialize failed, bad signature} {
692 proc foo {} {}
693 catch {chan create {r w} foo} msg
694 rename foo {}
695 set msg
696 } {wrong # args: should be "foo"}
697 test iocmd-21.7 {chan create, initialize failed, bad signature} {
698 proc foo {} {}
699 catch {chan create {r w} ::foo} msg
700 rename foo {}
701 set msg
702 } {wrong # args: should be "::foo"}
703 test iocmd-21.8 {chan create, initialize failed, bad result, not a list} -body {
704 proc foo {args} {return "\{"}
705 catch {chan create {r w} foo} msg
706 rename foo {}
707 set ::errorInfo
708 } -match glob -result {chan handler "foo initialize" returned non-list: *}
709 test iocmd-21.9 {chan create, initialize failed, bad result, not a list} -body {
710 proc foo {args} {return \{\{\}}
711 catch {chan create {r w} foo} msg
712 rename foo {}
713 set msg
714 } -match glob -result {chan handler "foo initialize" returned non-list: *}
715 test iocmd-21.10 {chan create, initialize failed, bad result, empty list} -body {
716 proc foo {args} {}
717 catch {chan create {r w} foo} msg
718 rename foo {}
719 set msg
720 } -match glob -result {*all required methods*}
721 test iocmd-21.11 {chan create, initialize failed, bad result, bogus method name} -body {
722 proc foo {args} {return 1}
723 catch {chan create {r w} foo} msg
724 rename foo {}
725 set msg
726 } -match glob -result {*bad method "1": must be *}
727 test iocmd-21.12 {chan create, initialize failed, bad result, bogus method name} -body {
728 proc foo {args} {return {a b c}}
729 catch {chan create {r w} foo} msg
730 rename foo {}
731 set msg
732 } -match glob -result {*bad method "c": must be *}
733 test iocmd-21.13 {chan create, initialize failed, bad result, required methods missing} -body {
734 proc foo {args} {return {initialize finalize}}
735 catch {chan create {r w} foo} msg
736 rename foo {}
737 set msg
738 } -match glob -result {*all required methods*}
739 test iocmd-21.14 {chan create, initialize failed, bad result, mode/handler mismatch} -body {
740 proc foo {args} {return {initialize finalize watch read}}
741 catch {chan create {r w} foo} msg
742 rename foo {}
743 set msg
744 } -match glob -result {*lacks a "write" method}
745 test iocmd-21.15 {chan create, initialize failed, bad result, mode/handler mismatch} -body {
746 proc foo {args} {return {initialize finalize watch write}}
747 catch {chan create {r w} foo} msg
748 rename foo {}
749 set msg
750 } -match glob -result {*lacks a "read" method}
751 test iocmd-21.16 {chan create, initialize failed, bad result, cget(all) mismatch} -body {
752 proc foo {args} {return {initialize finalize watch cget write read}}
753 catch {chan create {r w} foo} msg
754 rename foo {}
755 set msg
756 } -match glob -result {*supports "cget" but not "cgetall"}
757 test iocmd-21.17 {chan create, initialize failed, bad result, cget(all) mismatch} -body {
758 proc foo {args} {return {initialize finalize watch cgetall read write}}
759 catch {chan create {r w} foo} msg
760 rename foo {}
761 set msg
762 } -match glob -result {*supports "cgetall" but not "cget"}
763 test iocmd-21.18 {chan create, initialize ok, creates channel} -match glob -body {
764 proc foo {args} {
765 global res
766 lappend res $args
767 if {[lindex $args 0] ne "initialize"} {return}
768 return {initialize finalize watch read write}
769 }
770 set res {}
771 lappend res [file channel rc*]
772 lappend res [chan create {r w} foo]
773 lappend res [close [lindex $res end]]
774 lappend res [file channel rc*]
775 rename foo {}
776 set res
777 } -result {{} {initialize rc* {read write}} rc* {finalize rc*} {} {}}
778 test iocmd-21.19 {chan create, init failure -> no channel, no finalize} -match glob -body {
779 proc foo {args} {
780 global res
781 lappend res $args
782 return {}
783 }
784 set res {}
785 lappend res [file channel rc*]
786 lappend res [catch {chan create {r w} foo} msg]
787 lappend res $msg
788 lappend res [file channel rc*]
789 rename foo {}
790 set res
791 } -result {{} {initialize rc* {read write}} 1 {*all required methods*} {}}
792 test iocmd-21.20 {Bug 88aef05cda} -setup {
793 proc foo {method chan args} {
794 switch -- $method blocking {
795 chan configure $chan -blocking [lindex $args 0]
796 return
797 } initialize {
798 return {initialize finalize watch blocking read write
799 configure cget cgetall}
800 } finalize {
801 return
802 }
803 }
804 set ch [chan create {read write} foo]
805 } -body {
806 chan configure $ch -blocking 0
807 } -cleanup {
808 close $ch
809 rename foo {}
810 } -match glob -returnCodes 1 -result {*(infinite loop?)*}
811 test iocmd-21.21 {[close] in [read] segfaults} -setup {
812 proc foo {method chan args} {
813 switch -- $method initialize {
814 return {initialize finalize watch read}
815 } finalize {} watch {} read {
816 close $chan
817 return a
818 }
819 }
820 set ch [chan create read foo]
821 } -body {
822 read $ch 0
823 } -cleanup {
824 close $ch
825 rename foo {}
826 } -result {}
827 test iocmd-21.22 {[close] in [read] segfaults} -setup {
828 proc foo {method chan args} {
829 switch -- $method initialize {
830 return {initialize finalize watch read}
831 } finalize {} watch {} read {
832 catch {close $chan}
833 return a
834 }
835 }
836 set ch [chan create read foo]
837 } -body {
838 read $ch 1
839 } -returnCodes error -cleanup {
840 catch {close $ch}
841 rename foo {}
842 } -match glob -result {*invalid argument*}
843 test iocmd-21.23 {[close] in [gets] segfaults} -setup {
844 proc foo {method chan args} {
845 switch -- $method initialize {
846 return {initialize finalize watch read}
847 } finalize {} watch {} read {
848 catch {close $chan}
849 return \n
850 }
851 }
852 set ch [chan create read foo]
853 } -body {
854 gets $ch
855 } -cleanup {
856 catch {close $ch}
857 rename foo {}
858 } -result {}
859 test iocmd-21.24 {[close] in binary [gets] segfaults} -setup {
860 proc foo {method chan args} {
861 switch -- $method initialize {
862 return {initialize finalize watch read}
863 } finalize {} watch {} read {
864 catch {close $chan}
865 return \n
866 }
867 }
868 set ch [chan create read foo]
869 } -body {
870 chan configure $ch -translation binary
871 gets $ch
872 } -cleanup {
873 catch {close $ch}
874 rename foo {}
875 } -result {}
876
877 # --- --- --- --------- --------- ---------
878 # Helper commands to record the arguments to handler methods.
879
880 # Stored in a script so that the threads and interpreters needing this
881 # code do not need their own copy but can access this variable.
882
883 set helperscript {
884
885 proc note {item} {global res; lappend res $item; return}
886 proc track {} {upvar args item; note $item; return}
887 proc notes {items} {foreach i $items {note $i}}
888 # This forces the return options to be in the order that the test expects!
889 proc noteOpts opts {global res; lappend res [dict merge {
890 -code !?! -level !?! -errorcode !?! -errorline !?! -errorinfo !?!
891 } $opts]; return}
892
893 # Helper command, canned result for 'initialize' method.
894 # Gets the optional methods as arguments. Use return features
895 # to post the result higher up.
896
897 proc init {args} {
898 lappend args initialize finalize watch read write
899 return -code return $args
900 }
901 proc oninit {args} {
902 upvar args hargs
903 if {[lindex $hargs 0] ne "initialize"} {return}
904 lappend args initialize finalize watch read write
905 return -code return $args
906 }
907 proc onfinal {} {
908 upvar args hargs
909 if {[lindex $hargs 0] ne "finalize"} {return}
910 return -code return ""
911 }
912 }
913
914 # Set everything up in the main thread.
915 eval $helperscript
916
917 # --- --- --- --------- --------- ---------
918 # method finalize
919
920 test iocmd-22.1 {chan finalize, handler destruction has no effect on channel} -match glob -body {
921 set res {}
922 proc foo {args} {track; oninit; return}
923 note [set c [chan create {r w} foo]]
924 rename foo {}
925 note [file channels rc*]
926 note [catch {close $c} msg]; note $msg
927 note [file channels rc*]
928 set res
929 } -result {{initialize rc* {read write}} rc* rc* 1 {invalid command name "foo"} {}}
930 test iocmd-22.2 {chan finalize, for close} -match glob -body {
931 set res {}
932 proc foo {args} {track; oninit; return {}}
933 note [set c [chan create {r w} foo]]
934 close $c
935 # Close deleted the channel.
936 note [file channels rc*]
937 # Channel destruction does not kill handler command!
938 note [info command foo]
939 rename foo {}
940 set res
941 } -result {{initialize rc* {read write}} rc* {finalize rc*} {} foo}
942 test iocmd-22.3 {chan finalize, for close, error, close error} -match glob -body {
943 set res {}
944 proc foo {args} {track; oninit; return -code error 5}
945 note [set c [chan create {r w} foo]]
946 note [catch {close $c} msg]; note $msg
947 # Channel is gone despite error.
948 note [file channels rc*]
949 rename foo {}
950 set res
951 } -result {{initialize rc* {read write}} rc* {finalize rc*} 1 5 {}}
952 test iocmd-22.4 {chan finalize, for close, error, close error} -match glob -body {
953 set res {}
954 proc foo {args} {track; oninit; error FOO}
955 note [set c [chan create {r w} foo]]
956 note [catch {close $c} msg]; note $msg; note $::errorInfo
957 rename foo {}
958 set res
959 } -result {{initialize rc* {read write}} rc* {finalize rc*} 1 FOO {FOO
960 *"close $c"}}
961 test iocmd-22.5 {chan finalize, for close, arbitrary result, ignored} -match glob -body {
962 set res {}
963 proc foo {args} {track; oninit; return SOMETHING}
964 note [set c [chan create {r w} foo]]
965 note [catch {close $c} msg]; note $msg
966 rename foo {}
967 set res
968 } -result {{initialize rc* {read write}} rc* {finalize rc*} 0 {}}
969 test iocmd-22.6 {chan finalize, for close, break, close error} -match glob -body {
970 set res {}
971 proc foo {args} {track; oninit; return -code 3}
972 note [set c [chan create {r w} foo]]
973 note [catch {close $c} msg]; note $msg
974 rename foo {}
975 set res
976 } -result {{initialize rc* {read write}} rc* {finalize rc*} 1 *bad code*}
977 test iocmd-22.7 {chan finalize, for close, continue, close error} -match glob -body {
978 set res {}
979 proc foo {args} {track; oninit; return -code 4}
980 note [set c [chan create {r w} foo]]
981 note [catch {close $c} msg]; note $msg
982 rename foo {}
983 set res
984 } -result {{initialize rc* {read write}} rc* {finalize rc*} 1 *bad code*}
985 test iocmd-22.8 {chan finalize, for close, custom code, close error} -match glob -body {
986 set res {}
987 proc foo {args} {track; oninit; return -code 777 BANG}
988 note [set c [chan create {r w} foo]]
989 note [catch {close $c} msg]; note $msg
990 rename foo {}
991 set res
992 } -result {{initialize rc* {read write}} rc* {finalize rc*} 1 *bad code*}
993 test iocmd-22.9 {chan finalize, for close, ignore level, close error} -match glob -setup {
994 set res {}
995 } -body {
996 proc foo {args} {track; oninit; return -level 5 -code 777 BANG}
997 note [set c [chan create {r w} foo]]
998 note [catch {close $c} msg opt]; note $msg; noteOpts $opt
999 return $res
1000 } -cleanup {
1001 rename foo {}
1002 } -result {{initialize rc* {read write}} rc* {finalize rc*} 1 *bad code* {-code 1 -level 0 -errorcode NONE -errorline 1 -errorinfo *bad code*subcommand "finalize"*}}
1003
1004 # --- === *** ###########################
1005 # method read
1006
1007 test iocmd-23.1 {chan read, regular data return} -match glob -body {
1008 set res {}
1009 proc foo {args} {
1010 oninit; onfinal; track
1011 return snarf
1012 }
1013 set c [chan create {r w} foo]
1014 note [read $c 10]
1015 close $c
1016 rename foo {}
1017 set res
1018 } -result {{read rc* 4096} {read rc* 4096} snarfsnarf}
1019 test iocmd-23.2 {chan read, bad data return, too much} -match glob -body {
1020 set res {}
1021 proc foo {args} {
1022 oninit; onfinal; track
1023 return [string repeat snarf 1000]
1024 }
1025 set c [chan create {r w} foo]
1026 note [catch {read $c 2} msg]; note $msg
1027 close $c
1028 rename foo {}
1029 set res
1030 } -result {{read rc* 4096} 1 {read delivered more than requested}}
1031 test iocmd-23.3 {chan read, for non-readable channel} -match glob -body {
1032 set res {}
1033 proc foo {args} {
1034 oninit; onfinal; track; note MUST_NOT_HAPPEN
1035 }
1036 set c [chan create {w} foo]
1037 note [catch {read $c 2} msg]; note $msg
1038 close $c
1039 rename foo {}
1040 set res
1041 } -result {1 {channel "rc*" wasn't opened for reading}}
1042 test iocmd-23.4 {chan read, error return} -match glob -body {
1043 set res {}
1044 proc foo {args} {
1045 oninit; onfinal; track
1046 return -code error BOOM!
1047 }
1048 set c [chan create {r w} foo]
1049 note [catch {read $c 2} msg]; note $msg
1050 close $c
1051 rename foo {}
1052 set res
1053 } -result {{read rc* 4096} 1 BOOM!}
1054 test iocmd-23.5 {chan read, break return is error} -match glob -body {
1055 set res {}
1056 proc foo {args} {
1057 oninit; onfinal; track
1058 return -code break BOOM!
1059 }
1060 set c [chan create {r w} foo]
1061 note [catch {read $c 2} msg]; note $msg
1062 close $c
1063 rename foo {}
1064 set res
1065 } -result {{read rc* 4096} 1 *bad code*}
1066 test iocmd-23.6 {chan read, continue return is error} -match glob -body {
1067 set res {}
1068 proc foo {args} {
1069 oninit; onfinal; track
1070 return -code continue BOOM!
1071 }
1072 set c [chan create {r w} foo]
1073 note [catch {read $c 2} msg]; note $msg
1074 close $c
1075 rename foo {}
1076 set res
1077 } -result {{read rc* 4096} 1 *bad code*}
1078 test iocmd-23.7 {chan read, custom return is error} -match glob -body {
1079 set res {}
1080 proc foo {args} {
1081 oninit; onfinal; track
1082 return -code 777 BOOM!
1083 }
1084 set c [chan create {r w} foo]
1085 note [catch {read $c 2} msg]; note $msg
1086 close $c
1087 rename foo {}
1088 set res
1089 } -result {{read rc* 4096} 1 *bad code*}
1090 test iocmd-23.8 {chan read, level is squashed} -match glob -body {
1091 set res {}
1092 proc foo {args} {
1093 oninit; onfinal; track
1094 return -level 55 -code 777 BOOM!
1095 }
1096 set c [chan create {r w} foo]
1097 note [catch {read $c 2} msg opt]; note $msg; noteOpts $opt
1098 close $c
1099 rename foo {}
1100 set res
1101 } -result {{read rc* 4096} 1 *bad code* {-code 1 -level 0 -errorcode NONE -errorline 1 -errorinfo *bad code*subcommand "read"*}}
1102 test iocmd-23.9 {chan read, no data means eof} -match glob -setup {
1103 set res {}
1104 proc foo {args} {
1105 oninit; onfinal; track
1106 return ""
1107 }
1108 set c [chan create {r w} foo]
1109 } -body {
1110 note [read $c 2]
1111 note [eof $c]
1112 set res
1113 } -cleanup {
1114 close $c
1115 rename foo {}
1116 unset res
1117 } -result {{read rc* 4096} {} 1}
1118 test iocmd-23.10 {chan read, EAGAIN means no data, yet no eof either} -match glob -setup {
1119 set res {}
1120 proc foo {args} {
1121 oninit; onfinal; track
1122 error EAGAIN
1123 }
1124 set c [chan create {r w} foo]
1125 } -body {
1126 note [read $c 2]
1127 note [eof $c]
1128 set res
1129 } -cleanup {
1130 close $c
1131 rename foo {}
1132 unset res
1133 } -result {{read rc* 4096} {} 0}
1134 test iocmd-23.11 {chan read, close pulls the rug out} -match glob -body {
1135 set res {}
1136 proc foo {args} {
1137 oninit; onfinal; track
1138 set args [lassign $args sub id]
1139 if {$sub ne "read"} {return}
1140 close $id
1141 return {}
1142 }
1143 set c [chan create {r} foo]
1144 note [read $c]
1145 rename foo {}
1146 set res
1147 } -result {{read rc* 4096} {}}
1148
1149 # --- === *** ###########################
1150 # method write
1151
1152 test iocmd-24.1 {chan write, regular write} -match glob -body {
1153 set res {}
1154 proc foo {args} {
1155 oninit; onfinal; track
1156 set written [string length [lindex $args 2]]
1157 note $written
1158 return $written
1159 }
1160 set c [chan create {r w} foo]
1161 puts -nonewline $c snarf; flush $c
1162 close $c
1163 rename foo {}
1164 set res
1165 } -result {{write rc* snarf} 5}
1166 test iocmd-24.2 {chan write, partial write is ok} -match glob -body {
1167 set res {}
1168 proc foo {args} {
1169 oninit; onfinal; track
1170 set written [string length [lindex $args 2]]
1171 if {$written > 10} {set written [expr {$written / 2}]}
1172 note $written
1173 return $written
1174 }
1175 set c [chan create {r w} foo]
1176 puts -nonewline $c snarfsnarfsnarf; flush $c
1177 close $c
1178 rename foo {}
1179 set res
1180 } -result {{write rc* snarfsnarfsnarf} 7 {write rc* arfsnarf} 8}
1181 test iocmd-24.3 {chan write, failed write} -match glob -body {
1182 set res {}
1183 proc foo {args} {oninit; onfinal; track; note -1; return -1}
1184 set c [chan create {r w} foo]
1185 puts -nonewline $c snarfsnarfsnarf; flush $c
1186 close $c
1187 rename foo {}
1188 set res
1189 } -result {{write rc* snarfsnarfsnarf} -1}
1190 test iocmd-24.4 {chan write, non-writable channel} -match glob -body {
1191 set res {}
1192 proc foo {args} {oninit; onfinal; track; note MUST_NOT_HAPPEN; return}
1193 set c [chan create {r} foo]
1194 note [catch {puts -nonewline $c snarfsnarfsnarf; flush $c} msg]; note $msg
1195 close $c
1196 rename foo {}
1197 set res
1198 } -result {1 {channel "rc*" wasn't opened for writing}}
1199 test iocmd-24.5 {chan write, bad result, more written than data} -match glob -body {
1200 set res {}
1201 proc foo {args} {oninit; onfinal; track; return 10000}
1202 set c [chan create {r w} foo]
1203 note [catch {puts -nonewline $c snarf; flush $c} msg]; note $msg
1204 close $c
1205 rename foo {}
1206 set res
1207 } -result {{write rc* snarf} 1 {write wrote more than requested}}
1208 test iocmd-24.6 {chan write, bad result, zero-length write} -match glob -body {
1209 set res {}
1210 proc foo {args} {oninit; onfinal; track; return 0}
1211 set c [chan create {r w} foo]
1212 note [catch {puts -nonewline $c snarf; flush $c} msg]; note $msg
1213 close $c
1214 rename foo {}
1215 set res
1216 } -result {{write rc* snarf} 1 {write wrote nothing}}
1217 test iocmd-24.7 {chan write, failed write, error return} -match glob -body {
1218 set res {}
1219 proc foo {args} {oninit; onfinal; track; return -code error BOOM!}
1220 set c [chan create {r w} foo]
1221 note [catch {puts -nonewline $c snarfsnarfsnarf; flush $c} msg]
1222 note $msg
1223 close $c
1224 rename foo {}
1225 set res
1226 } -result {{write rc* snarfsnarfsnarf} 1 BOOM!}
1227 test iocmd-24.8 {chan write, failed write, error return} -match glob -body {
1228 set res {}
1229 proc foo {args} {oninit; onfinal; track; error BOOM!}
1230 set c [chan create {r w} foo]
1231 notes [catch {puts -nonewline $c snarfsnarfsnarf; flush $c} msg]
1232 note $msg
1233 close $c
1234 rename foo {}
1235 set res
1236 } -result {{write rc* snarfsnarfsnarf} 1 BOOM!}
1237 test iocmd-24.9 {chan write, failed write, break return is error} -match glob -body {
1238 set res {}
1239 proc foo {args} {oninit; onfinal; track; return -code break BOOM!}
1240 set c [chan create {r w} foo]
1241 note [catch {puts -nonewline $c snarfsnarfsnarf; flush $c} msg]
1242 note $msg
1243 close $c
1244 rename foo {}
1245 set res
1246 } -result {{write rc* snarfsnarfsnarf} 1 *bad code*}
1247 test iocmd-24.10 {chan write, failed write, continue return is error} -match glob -body {
1248 set res {}
1249 proc foo {args} {oninit; onfinal; track; return -code continue BOOM!}
1250 set c [chan create {r w} foo]
1251 note [catch {puts -nonewline $c snarfsnarfsnarf; flush $c} msg]
1252 note $msg
1253 close $c
1254 rename foo {}
1255 set res
1256 } -result {{write rc* snarfsnarfsnarf} 1 *bad code*}
1257 test iocmd-24.11 {chan write, failed write, custom return is error} -match glob -body {
1258 set res {}
1259 proc foo {args} {oninit; onfinal; track; return -code 777 BOOM!}
1260 set c [chan create {r w} foo]
1261 note [catch {puts -nonewline $c snarfsnarfsnarf; flush $c} msg]
1262 note $msg
1263 close $c
1264 rename foo {}
1265 set res
1266 } -result {{write rc* snarfsnarfsnarf} 1 *bad code*}
1267 test iocmd-24.12 {chan write, failed write, non-numeric return is error} -match glob -body {
1268 set res {}
1269 proc foo {args} {oninit; onfinal; track; return BANG}
1270 set c [chan create {r w} foo]
1271 note [catch {puts -nonewline $c snarfsnarfsnarf; flush $c} msg]
1272 note $msg
1273 close $c
1274 rename foo {}
1275 set res
1276 } -result {{write rc* snarfsnarfsnarf} 1 {expected integer but got "BANG"}}
1277 test iocmd-24.13 {chan write, failed write, level is ignored} -match glob -body {
1278 set res {}
1279 proc foo {args} {oninit; onfinal; track; return -level 55 -code 777 BOOM!}
1280 set c [chan create {r w} foo]
1281 note [catch {puts -nonewline $c snarfsnarfsnarf; flush $c} msg opt]
1282 note $msg
1283 noteOpts $opt
1284 close $c
1285 rename foo {}
1286 set res
1287 } -result {{write rc* snarfsnarfsnarf} 1 *bad code* {-code 1 -level 0 -errorcode NONE -errorline 1 -errorinfo *bad code*subcommand "write"*}}
1288 test iocmd-24.14 {chan write, no EAGAIN means that writing is allowed at this time, bug 2936225} -match glob -setup {
1289 set res {}
1290 proc foo {args} {
1291 oninit; onfinal; track
1292 return 3
1293 }
1294 set c [chan create {r w} foo]
1295 } -body {
1296 note [puts -nonewline $c ABC ; flush $c]
1297 set res
1298 } -cleanup {
1299 close $c
1300 rename foo {}
1301 unset res
1302 } -result {{write rc* ABC} {}}
1303 test iocmd-24.15 {chan write, EAGAIN means that writing is not allowed at this time, bug 2936225} -match glob -setup {
1304 set res {}
1305 proc foo {args} {
1306 oninit; onfinal; track
1307 # Note: The EAGAIN signals that the channel cannot accept
1308 # write requests right now, this in turn causes the IO core to
1309 # request the generation of writable events (see expected
1310 # result below, and compare to case 24.14 above).
1311 error EAGAIN
1312 }
1313 set c [chan create {r w} foo]
1314 } -body {
1315 note [puts -nonewline $c ABC ; flush $c]
1316 set res
1317 } -cleanup {
1318 close $c
1319 rename foo {}
1320 unset res
1321 } -result {{write rc* ABC} {watch rc* write} {}}
1322
1323 # --- === *** ###########################
1324 # method cgetall
1325
1326 test iocmd-25.1 {chan configure, cgetall, standard options} -match glob -body {
1327 set res {}
1328 proc foo {args} {oninit; onfinal; track; note MUST_NOT_HAPPEN; return}
1329 set c [chan create {r w} foo]
1330 note [fconfigure $c]
1331 close $c
1332 rename foo {}
1333 set res
1334 } -result {{-blocking 1 -buffering full -buffersize 4096 -encoding * -eofchar {{} {}} -translation {auto *}}}
1335 test iocmd-25.2 {chan configure, cgetall, no options} -match glob -body {
1336 set res {}
1337 proc foo {args} {oninit cget cgetall; onfinal; track; return ""}
1338 set c [chan create {r w} foo]
1339 note [fconfigure $c]
1340 close $c
1341 rename foo {}
1342 set res
1343 } -result {{cgetall rc*} {-blocking 1 -buffering full -buffersize 4096 -encoding * -eofchar {{} {}} -translation {auto *}}}
1344 test iocmd-25.3 {chan configure, cgetall, regular result} -match glob -body {
1345 set res {}
1346 proc foo {args} {
1347 oninit cget cgetall; onfinal; track
1348 return "-bar foo -snarf x"
1349 }
1350 set c [chan create {r w} foo]
1351 note [fconfigure $c]
1352 close $c
1353 rename foo {}
1354 set res
1355 } -result {{cgetall rc*} {-blocking 1 -buffering full -buffersize 4096 -encoding * -eofchar {{} {}} -translation {auto *} -bar foo -snarf x}}
1356 test iocmd-25.4 {chan configure, cgetall, bad result, list of uneven length} -match glob -body {
1357 set res {}
1358 proc foo {args} {
1359 oninit cget cgetall; onfinal; track
1360 return "-bar"
1361 }
1362 set c [chan create {r w} foo]
1363 note [catch {fconfigure $c} msg]; note $msg
1364 close $c
1365 rename foo {}
1366 set res
1367 } -result {{cgetall rc*} 1 {Expected list with even number of elements, got 1 element instead}}
1368 test iocmd-25.5 {chan configure, cgetall, bad result, not a list} -match glob -body {
1369 set res {}
1370 proc foo {args} {
1371 oninit cget cgetall; onfinal; track
1372 return "\{"
1373 }
1374 set c [chan create {r w} foo]
1375 note [catch {fconfigure $c} msg]; note $msg
1376 close $c
1377 rename foo {}
1378 set res
1379 } -result {{cgetall rc*} 1 {unmatched open brace in list}}
1380 test iocmd-25.6 {chan configure, cgetall, error return} -match glob -body {
1381 set res {}
1382 proc foo {args} {
1383 oninit cget cgetall; onfinal; track
1384 return -code error BOOM!
1385 }
1386 set c [chan create {r w} foo]
1387 note [catch {fconfigure $c} msg]; note $msg
1388 close $c
1389 rename foo {}
1390 set res
1391 } -result {{cgetall rc*} 1 BOOM!}
1392 test iocmd-25.7 {chan configure, cgetall, break return is error} -match glob -body {
1393 set res {}
1394 proc foo {args} {
1395 oninit cget cgetall; onfinal; track
1396 return -code break BOOM!
1397 }
1398 set c [chan create {r w} foo]
1399 note [catch {fconfigure $c} msg]; note $msg
1400 close $c
1401 rename foo {}
1402 set res
1403 } -result {{cgetall rc*} 1 *bad code*}
1404 test iocmd-25.8 {chan configure, cgetall, continue return is error} -match glob -body {
1405 set res {}
1406 proc foo {args} {
1407 oninit cget cgetall; onfinal; track
1408 return -code continue BOOM!
1409 }
1410 set c [chan create {r w} foo]
1411 note [catch {fconfigure $c} msg]; note $msg
1412 close $c
1413 rename foo {}
1414 set res
1415 } -result {{cgetall rc*} 1 *bad code*}
1416 test iocmd-25.9 {chan configure, cgetall, custom return is error} -match glob -body {
1417 set res {}
1418 proc foo {args} {
1419 oninit cget cgetall; onfinal; track
1420 return -code 777 BOOM!
1421 }
1422 set c [chan create {r w} foo]
1423 note [catch {fconfigure $c} msg]; note $msg
1424 close $c
1425 rename foo {}
1426 set res
1427 } -result {{cgetall rc*} 1 *bad code*}
1428 test iocmd-25.10 {chan configure, cgetall, level is ignored} -match glob -body {
1429 set res {}
1430 proc foo {args} {
1431 oninit cget cgetall; onfinal; track
1432 return -level 55 -code 777 BANG
1433 }
1434 set c [chan create {r w} foo]
1435 note [catch {fconfigure $c} msg opt]; note $msg; noteOpts $opt
1436 close $c
1437 rename foo {}
1438 set res
1439 } -result {{cgetall rc*} 1 *bad code* {-code 1 -level 0 -errorcode NONE -errorline 1 -errorinfo *bad code*subcommand "cgetall"*}}
1440
1441 # --- === *** ###########################
1442 # method configure
1443
1444 test iocmd-26.1 {chan configure, set standard option} -match glob -body {
1445 set res {}
1446 proc foo {args} {
1447 oninit configure; onfinal; track; note MUST_NOT_HAPPEN; return
1448 }
1449 set c [chan create {r w} foo]
1450 note [fconfigure $c -translation lf]
1451 close $c
1452 rename foo {}
1453 set res
1454 } -result {{}}
1455 test iocmd-26.2 {chan configure, set option, error return} -match glob -body {
1456 set res {}
1457 proc foo {args} {
1458 oninit configure; onfinal; track
1459 return -code error BOOM!
1460 }
1461 set c [chan create {r w} foo]
1462 note [catch {fconfigure $c -rc-foo bar} msg]; note $msg
1463 close $c
1464 rename foo {}
1465 set res
1466 } -result {{configure rc* -rc-foo bar} 1 BOOM!}
1467 test iocmd-26.3 {chan configure, set option, ok return} -match glob -body {
1468 set res {}
1469 proc foo {args} {oninit configure; onfinal; track; return}
1470 set c [chan create {r w} foo]
1471 note [fconfigure $c -rc-foo bar]
1472 close $c
1473 rename foo {}
1474 set res
1475 } -result {{configure rc* -rc-foo bar} {}}
1476 test iocmd-26.4 {chan configure, set option, break return is error} -match glob -body {
1477 set res {}
1478 proc foo {args} {
1479 oninit configure; onfinal; track
1480 return -code break BOOM!
1481 }
1482 set c [chan create {r w} foo]
1483 note [catch {fconfigure $c -rc-foo bar} msg]; note $msg
1484 close $c
1485 rename foo {}
1486 set res
1487 } -result {{configure rc* -rc-foo bar} 1 *bad code*}
1488 test iocmd-26.5 {chan configure, set option, continue return is error} -match glob -body {
1489 set res {}
1490 proc foo {args} {
1491 oninit configure; onfinal; track
1492 return -code continue BOOM!
1493 }
1494 set c [chan create {r w} foo]
1495 note [catch {fconfigure $c -rc-foo bar} msg]; note $msg
1496 close $c
1497 rename foo {}
1498 set res
1499 } -result {{configure rc* -rc-foo bar} 1 *bad code*}
1500 test iocmd-26.6 {chan configure, set option, custom return is error} -match glob -body {
1501 set res {}
1502 proc foo {args} {
1503 oninit configure; onfinal; track
1504 return -code 444 BOOM!
1505 }
1506 set c [chan create {r w} foo]
1507 note [catch {fconfigure $c -rc-foo bar} msg]; note $msg
1508 close $c
1509 rename foo {}
1510 set res
1511 } -result {{configure rc* -rc-foo bar} 1 *bad code*}
1512 test iocmd-26.7 {chan configure, set option, level is ignored} -match glob -body {
1513 set res {}
1514 proc foo {args} {
1515 oninit configure; onfinal; track
1516 return -level 55 -code 444 BANG
1517 }
1518 set c [chan create {r w} foo]
1519 note [catch {fconfigure $c -rc-foo bar} msg opt]; note $msg; noteOpts $opt
1520 close $c
1521 rename foo {}
1522 set res
1523 } -result {{configure rc* -rc-foo bar} 1 *bad code* {-code 1 -level 0 -errorcode NONE -errorline 1 -errorinfo *bad code*subcommand "configure"*}}
1524
1525 # --- === *** ###########################
1526 # method cget
1527
1528 test iocmd-27.1 {chan configure, get option, ok return} -match glob -body {
1529 set res {}
1530 proc foo {args} {oninit cget cgetall; onfinal; track; return foo}
1531 set c [chan create {r w} foo]
1532 note [fconfigure $c -rc-foo]
1533 close $c
1534 rename foo {}
1535 set res
1536 } -result {{cget rc* -rc-foo} foo}
1537 test iocmd-27.2 {chan configure, get option, error return} -match glob -body {
1538 set res {}
1539 proc foo {args} {
1540 oninit cget cgetall; onfinal; track
1541 return -code error BOOM!
1542 }
1543 set c [chan create {r w} foo]
1544 note [catch {fconfigure $c -rc-foo} msg]; note $msg
1545 close $c
1546 rename foo {}
1547 set res
1548 } -result {{cget rc* -rc-foo} 1 BOOM!}
1549 test iocmd-27.3 {chan configure, get option, break return is error} -match glob -body {
1550 set res {}
1551 proc foo {args} {
1552 oninit cget cgetall; onfinal; track
1553 return -code error BOOM!
1554 }
1555 set c [chan create {r w} foo]
1556 note [catch {fconfigure $c -rc-foo} msg]; note $msg
1557 close $c
1558 rename foo {}
1559 set res
1560 } -result {{cget rc* -rc-foo} 1 BOOM!}
1561 test iocmd-27.4 {chan configure, get option, continue return is error} -match glob -body {
1562 set res {}
1563 proc foo {args} {
1564 oninit cget cgetall; onfinal; track
1565 return -code continue BOOM!
1566 }
1567 set c [chan create {r w} foo]
1568 note [catch {fconfigure $c -rc-foo} msg]; note $msg
1569 close $c
1570 rename foo {}
1571 set res
1572 } -result {{cget rc* -rc-foo} 1 *bad code*}
1573 test iocmd-27.5 {chan configure, get option, custom return is error} -match glob -body {
1574 set res {}
1575 proc foo {args} {
1576 oninit cget cgetall; onfinal; track
1577 return -code 333 BOOM!
1578 }
1579 set c [chan create {r w} foo]
1580 note [catch {fconfigure $c -rc-foo} msg]; note $msg
1581 close $c
1582 rename foo {}
1583 set res
1584 } -result {{cget rc* -rc-foo} 1 *bad code*}
1585 test iocmd-27.6 {chan configure, get option, level is ignored} -match glob -body {
1586 set res {}
1587 proc foo {args} {
1588 oninit cget cgetall; onfinal; track
1589 return -level 77 -code 333 BANG
1590 }
1591 set c [chan create {r w} foo]
1592 note [catch {fconfigure $c -rc-foo} msg opt]; note $msg; noteOpts $opt
1593 close $c
1594 rename foo {}
1595 set res
1596 } -result {{cget rc* -rc-foo} 1 *bad code* {-code 1 -level 0 -errorcode NONE -errorline 1 -errorinfo *bad code*subcommand "cget"*}}
1597
1598 # --- === *** ###########################
1599 # method seek
1600
1601 test iocmd-28.1 {chan tell, not supported by handler} -match glob -body {
1602 set res {}
1603 proc foo {args} {oninit; onfinal; track; note MUST_NOT_HAPPEN; return}
1604 set c [chan create {r w} foo]
1605 note [tell $c]
1606 close $c
1607 rename foo {}
1608 set res
1609 } -result {-1}
1610 test iocmd-28.2 {chan tell, error return} -match glob -body {
1611 set res {}
1612 proc foo {args} {oninit seek; onfinal; track; return -code error BOOM!}
1613 set c [chan create {r w} foo]
1614 note [catch {tell $c} msg]; note $msg
1615 close $c
1616 rename foo {}
1617 set res
1618 } -result {{seek rc* 0 current} 1 BOOM!}
1619 test iocmd-28.3 {chan tell, break return is error} -match glob -body {
1620 set res {}
1621 proc foo {args} {oninit seek; onfinal; track; return -code break BOOM!}
1622 set c [chan create {r w} foo]
1623 note [catch {tell $c} msg]; note $msg
1624 close $c
1625 rename foo {}
1626 set res
1627 } -result {{seek rc* 0 current} 1 *bad code*}
1628 test iocmd-28.4 {chan tell, continue return is error} -match glob -body {
1629 set res {}
1630 proc foo {args} {oninit seek; onfinal; track; return -code continue BOOM!}
1631 set c [chan create {r w} foo]
1632 note [catch {tell $c} msg]; note $msg
1633 close $c
1634 rename foo {}
1635 set res
1636 } -result {{seek rc* 0 current} 1 *bad code*}
1637 test iocmd-28.5 {chan tell, custom return is error} -match glob -body {
1638 set res {}
1639 proc foo {args} {oninit seek; onfinal; track; return -code 222 BOOM!}
1640 set c [chan create {r w} foo]
1641 note [catch {tell $c} msg]; note $msg
1642 close $c
1643 rename foo {}
1644 set res
1645 } -result {{seek rc* 0 current} 1 *bad code*}
1646 test iocmd-28.6 {chan tell, level is ignored} -match glob -body {
1647 set res {}
1648 proc foo {args} {oninit seek; onfinal; track; return -level 11 -code 222 BANG}
1649 set c [chan create {r w} foo]
1650 note [catch {tell $c} msg opt]; note $msg; noteOpts $opt
1651 close $c
1652 rename foo {}
1653 set res
1654 } -result {{seek rc* 0 current} 1 *bad code* {-code 1 -level 0 -errorcode NONE -errorline 1 -errorinfo *bad code*subcommand "seek"*}}
1655 test iocmd-28.7 {chan tell, regular return} -match glob -body {
1656 set res {}
1657 proc foo {args} {oninit seek; onfinal; track; return 88}
1658 set c [chan create {r w} foo]
1659 note [tell $c]
1660 close $c
1661 rename foo {}
1662 set res
1663 } -result {{seek rc* 0 current} 88}
1664 test iocmd-28.8 {chan tell, negative return} -match glob -body {
1665 set res {}
1666 proc foo {args} {oninit seek; onfinal; track; return -1}
1667 set c [chan create {r w} foo]
1668 note [catch {tell $c} msg]; note $msg
1669 close $c
1670 rename foo {}
1671 set res
1672 } -result {{seek rc* 0 current} 1 {Tried to seek before origin}}
1673 test iocmd-28.9 {chan tell, string return} -match glob -body {
1674 set res {}
1675 proc foo {args} {oninit seek; onfinal; track; return BOGUS}
1676 set c [chan create {r w} foo]
1677 note [catch {tell $c} msg]; note $msg
1678 close $c
1679 rename foo {}
1680 set res
1681 } -result {{seek rc* 0 current} 1 {expected integer but got "BOGUS"}}
1682 test iocmd-28.10 {chan seek, not supported by handler} -match glob -body {
1683 set res {}
1684 proc foo {args} {oninit; onfinal; track; note MUST_NOT_HAPPEN; return}
1685 set c [chan create {r w} foo]
1686 note [catch {seek $c 0 start} msg]; note $msg
1687 close $c
1688 rename foo {}
1689 set res
1690 } -result {1 {error during seek on "rc*": invalid argument}}
1691 test iocmd-28.11 {chan seek, error return} -match glob -body {
1692 set res {}
1693 proc foo {args} {oninit seek; onfinal; track; return -code error BOOM!}
1694 set c [chan create {r w} foo]
1695 note [catch {seek $c 0 start} msg]; note $msg
1696 close $c
1697 rename foo {}
1698 set res
1699 } -result {{seek rc* 0 start} 1 BOOM!}
1700 test iocmd-28.12 {chan seek, break return is error} -match glob -body {
1701 set res {}
1702 proc foo {args} {oninit seek; onfinal; track; return -code break BOOM!}
1703 set c [chan create {r w} foo]
1704 note [catch {seek $c 0 start} msg]; note $msg
1705 close $c
1706 rename foo {}
1707 set res
1708 } -result {{seek rc* 0 start} 1 *bad code*}
1709 test iocmd-28.13 {chan seek, continue return is error} -match glob -body {
1710 set res {}
1711 proc foo {args} {oninit seek; onfinal; track; return -code continue BOOM!}
1712 set c [chan create {r w} foo]
1713 note [catch {seek $c 0 start} msg]; note $msg
1714 close $c
1715 rename foo {}
1716 set res
1717 } -result {{seek rc* 0 start} 1 *bad code*}
1718 test iocmd-28.14 {chan seek, custom return is error} -match glob -body {
1719 set res {}
1720 proc foo {args} {oninit seek; onfinal; track; return -code 99 BOOM!}
1721 set c [chan create {r w} foo]
1722 note [catch {seek $c 0 start} msg]; note $msg
1723 close $c
1724 rename foo {}
1725 set res
1726 } -result {{seek rc* 0 start} 1 *bad code*}
1727 test iocmd-28.15 {chan seek, level is ignored} -match glob -body {
1728 set res {}
1729 proc foo {args} {oninit seek; onfinal; track; return -level 33 -code 99 BANG}
1730 set c [chan create {r w} foo]
1731 note [catch {seek $c 0 start} msg opt]; note $msg; noteOpts $opt
1732 close $c
1733 rename foo {}
1734 set res
1735 } -result {{seek rc* 0 start} 1 *bad code* {-code 1 -level 0 -errorcode NONE -errorline 1 -errorinfo *bad code*subcommand "seek"*}}
1736 test iocmd-28.16 {chan seek, bogus return, negative location} -match glob -body {
1737 set res {}
1738 proc foo {args} {oninit seek; onfinal; track; return -45}
1739 set c [chan create {r w} foo]
1740 note [catch {seek $c 0 start} msg]; note $msg
1741 close $c
1742 rename foo {}
1743 set res
1744 } -result {{seek rc* 0 start} 1 {Tried to seek before origin}}
1745 test iocmd-28.17 {chan seek, bogus return, string return} -match glob -body {
1746 set res {}
1747 proc foo {args} {oninit seek; onfinal; track; return BOGUS}
1748 set c [chan create {r w} foo]
1749 note [catch {seek $c 0 start} msg]; note $msg
1750 close $c
1751 rename foo {}
1752 set res
1753 } -result {{seek rc* 0 start} 1 {expected integer but got "BOGUS"}}
1754 test iocmd-28.18 {chan seek, ok result} -match glob -body {
1755 set res {}
1756 proc foo {args} {oninit seek; onfinal; track; return 23}
1757 set c [chan create {r w} foo]
1758 note [seek $c 0 current]
1759 close $c
1760 rename foo {}
1761 set res
1762 } -result {{seek rc* 0 current} {}}
1763 foreach {testname code} {
1764 iocmd-28.19.0 start
1765 iocmd-28.19.1 current
1766 iocmd-28.19.2 end
1767 } {
1768 test $testname "chan seek, base conversion, $code" -match glob -body {
1769 set res {}
1770 proc foo {args} {oninit seek; onfinal; track; return 0}
1771 set c [chan create {r w} foo]
1772 note [seek $c 0 $code]
1773 close $c
1774 rename foo {}
1775 set res
1776 } -result [list [list seek rc* 0 $code] {}]
1777 }
1778
1779 # --- === *** ###########################
1780 # method blocking
1781
1782 test iocmd-29.1 {chan blocking, no handler support} -match glob -body {
1783 set res {}
1784 proc foo {args} {oninit; onfinal; track; note MUST_NOT_HAPPEN; return}
1785 set c [chan create {r w} foo]
1786 note [fconfigure $c -blocking]
1787 close $c
1788 rename foo {}
1789 set res
1790 } -result {1}
1791 test iocmd-29.2 {chan blocking, no handler support} -match glob -body {
1792 set res {}
1793 proc foo {args} {oninit; onfinal; track; note MUST_NOT_HAPPEN; return}
1794 set c [chan create {r w} foo]
1795 note [fconfigure $c -blocking 0]
1796 note [fconfigure $c -blocking]
1797 close $c
1798 rename foo {}
1799 set res
1800 } -result {{} 0}
1801 test iocmd-29.3 {chan blocking, retrieval, handler support} -match glob -body {
1802 set res {}
1803 proc foo {args} {oninit blocking; onfinal; track; note MUST_NOT_HAPPEN; return}
1804 set c [chan create {r w} foo]
1805 note [fconfigure $c -blocking]
1806 close $c
1807 rename foo {}
1808 set res
1809 } -result {1}
1810 test iocmd-29.4 {chan blocking, resetting, handler support} -match glob -body {
1811 set res {}
1812 proc foo {args} {oninit blocking; onfinal; track; return}
1813 set c [chan create {r w} foo]
1814 note [fconfigure $c -blocking 0]
1815 note [fconfigure $c -blocking]
1816 close $c
1817 rename foo {}
1818 set res
1819 } -result {{blocking rc* 0} {} 0}
1820 test iocmd-29.5 {chan blocking, setting, handler support} -match glob -body {
1821 set res {}
1822 proc foo {args} {oninit blocking; onfinal; track; return}
1823 set c [chan create {r w} foo]
1824 note [fconfigure $c -blocking 1]
1825 note [fconfigure $c -blocking]
1826 close $c
1827 rename foo {}
1828 set res
1829 } -result {{blocking rc* 1} {} 1}
1830 test iocmd-29.6 {chan blocking, error return} -match glob -body {
1831 set res {}
1832 proc foo {args} {oninit blocking; onfinal; track; error BOOM!}
1833 set c [chan create {r w} foo]
1834 note [catch {fconfigure $c -blocking 0} msg]; note $msg
1835 # Catch the close. It changes blocking mode internally, and runs into the error result.
1836 catch {close $c}
1837 rename foo {}
1838 set res
1839 } -result {{blocking rc* 0} 1 BOOM!}
1840 test iocmd-29.7 {chan blocking, break return is error} -match glob -body {
1841 set res {}
1842 proc foo {args} {oninit blocking; onfinal; track; return -code break BOOM!}
1843 set c [chan create {r w} foo]
1844 note [catch {fconfigure $c -blocking 0} msg]; note $msg
1845 catch {close $c}
1846 rename foo {}
1847 set res
1848 } -result {{blocking rc* 0} 1 *bad code*}
1849 test iocmd-29.8 {chan blocking, continue return is error} -match glob -body {
1850 set res {}
1851 proc foo {args} {oninit blocking; onfinal; track; return -code continue BOOM!}
1852 set c [chan create {r w} foo]
1853 note [catch {fconfigure $c -blocking 0} msg]; note $msg
1854 catch {close $c}
1855 rename foo {}
1856 set res
1857 } -result {{blocking rc* 0} 1 *bad code*}
1858 test iocmd-29.9 {chan blocking, custom return is error} -match glob -body {
1859 set res {}
1860 proc foo {args} {oninit blocking; onfinal; track; return -code 44 BOOM!}
1861 set c [chan create {r w} foo]
1862 note [catch {fconfigure $c -blocking 0} msg]; note $msg
1863 catch {close $c}
1864 rename foo {}
1865 set res
1866 } -result {{blocking rc* 0} 1 *bad code*}
1867 test iocmd-29.10 {chan blocking, level is ignored} -match glob -setup {
1868 set res {}
1869 } -body {
1870 proc foo {args} {oninit blocking; onfinal; track; return -level 99 -code 44 BANG}
1871 set c [chan create {r w} foo]
1872 note [catch {fconfigure $c -blocking 0} msg opt]; note $msg; noteOpts $opt
1873 catch {close $c}
1874 return $res
1875 } -cleanup {
1876 rename foo {}
1877 } -result {{blocking rc* 0} 1 *bad code* {-code 1 -level 0 -errorcode NONE -errorline 1 -errorinfo *bad code*subcommand "blocking"*}}
1878 test iocmd-29.11 {chan blocking, regular return ok, value ignored} -match glob -body {
1879 set res {}
1880 proc foo {args} {oninit blocking; onfinal; track; return BOGUS}
1881 set c [chan create {r w} foo]
1882 note [catch {fconfigure $c -blocking 0} msg]; note $msg
1883 catch {close $c}
1884 rename foo {}
1885 set res
1886 } -result {{blocking rc* 0} 0 {}}
1887
1888 # --- === *** ###########################
1889 # method watch
1890
1891 test iocmd-30.1 {chan watch, read interest, some return} -match glob -body {
1892 set res {}
1893 proc foo {args} {oninit; onfinal; track; return IGNORED}
1894 set c [chan create {r w} foo]
1895 note [fileevent $c readable {set tick $tick}]
1896 close $c ;# 2nd watch, interest zero.
1897 rename foo {}
1898 set res
1899 } -result {{watch rc* read} {} {watch rc* {}}}
1900 test iocmd-30.2 {chan watch, write interest, error return} -match glob -body {
1901 set res {}
1902 proc foo {args} {oninit; onfinal; track; return -code error BOOM!_IGNORED}
1903 set c [chan create {r w} foo]
1904 note [fileevent $c writable {set tick $tick}]
1905 note [fileevent $c writable {}]
1906 close $c
1907 rename foo {}
1908 set res
1909 } -result {{watch rc* write} {} {watch rc* {}} {}}
1910 test iocmd-30.3 {chan watch, accumulated interests} -match glob -body {
1911 set res {}
1912 proc foo {args} {oninit; onfinal; track; return}
1913 set c [chan create {r w} foo]
1914 note [fileevent $c writable {set tick $tick}]
1915 note [fileevent $c readable {set tick $tick}]
1916 note [fileevent $c writable {}]
1917 note [fileevent $c readable {}]
1918 close $c
1919 rename foo {}
1920 set res
1921 } -result {{watch rc* write} {} {watch rc* {read write}} {} {watch rc* read} {} {watch rc* {}} {}}
1922 test iocmd-30.4 {chan watch, unchanged interest not forwarded} -match glob -body {
1923 set res {}
1924 proc foo {args} {oninit; onfinal; track; return}
1925 set c [chan create {r w} foo]
1926 note [fileevent $c writable {set tick $tick}]
1927 note [fileevent $c readable {set tick $tick}] ;# Script is changing,
1928 note [fileevent $c readable {set tock $tock}] ;# interest does not.
1929 close $c ;# 3rd and 4th watch, removing the event handlers.
1930 rename foo {}
1931 set res
1932 } -result {{watch rc* write} {} {watch rc* {read write}} {} {} {watch rc* write} {watch rc* {}}}
1933
1934 # --- === *** ###########################
1935 # chan postevent
1936
1937 test iocmd-31.1 {chan postevent, restricted to reflected channels} -match glob -body {
1938 set c [open [makeFile {} goo] r]
1939 catch {chan postevent $c {r w}} msg
1940 close $c
1941 removeFile goo
1942 set msg
1943 } -result {can not find reflected channel named "file*"}
1944 test iocmd-31.2 {chan postevent, unwanted events} -match glob -body {
1945 set res {}
1946 proc foo {args} {oninit; onfinal; track; return}
1947 set c [chan create {r w} foo]
1948 catch {chan postevent $c {r w}} msg; note $msg
1949 close $c
1950 rename foo {}
1951 set res
1952 } -result {{tried to post events channel "rc*" is not interested in}}
1953 test iocmd-31.3 {chan postevent, bad input, empty list} -match glob -body {
1954 set res {}
1955 proc foo {args} {oninit; onfinal; track; return}
1956 set c [chan create {r w} foo]
1957 catch {chan postevent $c {}} msg; note $msg
1958 close $c
1959 rename foo {}
1960 set res
1961 } -result {{bad event list: is empty}}
1962 test iocmd-31.4 {chan postevent, bad input, illlegal keyword} -match glob -body {
1963 set res {}
1964 proc foo {args} {oninit; onfinal; track; return}
1965 set c [chan create {r w} foo]
1966 catch {chan postevent $c goo} msg; note $msg
1967 close $c
1968 rename foo {}
1969 set res
1970 } -result {{bad event "goo": must be read or write}}
1971 test iocmd-31.5 {chan postevent, bad input, not a list} -match glob -body {
1972 set res {}
1973 proc foo {args} {oninit; onfinal; track; return}
1974 set c [chan create {r w} foo]
1975 catch {chan postevent $c "\{"} msg; note $msg
1976 close $c
1977 rename foo {}
1978 set res
1979 } -result {{unmatched open brace in list}}
1980 test iocmd-31.6 {chan postevent, posted events do happen} -match glob -body {
1981 set res {}
1982 proc foo {args} {oninit; onfinal; track; return}
1983 set c [chan create {r w} foo]
1984 set tock {}
1985 note [fileevent $c readable {lappend res TOCK; set tock 1}]
1986 set stop [after 15000 {lappend res TIMEOUT; set tock 1}]
1987 after 1000 {note [chan postevent $c r]}
1988 vwait ::tock
1989 catch {after cancel $stop}
1990 close $c
1991 rename foo {}
1992 set res
1993 } -result {{watch rc* read} {} TOCK {} {watch rc* {}}}
1994 test iocmd-31.7 {chan postevent, posted events do happen} -match glob -body {
1995 set res {}
1996 proc foo {args} {oninit; onfinal; track; return}
1997 set c [chan create {r w} foo]
1998 note [fileevent $c writable {lappend res TOCK; set tock 1}]
1999 set stop [after 15000 {lappend res TIMEOUT; set tock 1}]
2000 after 1000 {note [chan postevent $c w]}
2001 vwait ::tock
2002 catch {after cancel $stop}
2003 close $c
2004 rename foo {}
2005 set res
2006 } -result {{watch rc* write} {} TOCK {} {watch rc* {}}}
2007 test iocmd-31.8 {chan postevent after close throws error} -match glob -setup {
2008 proc foo {args} {oninit; onfinal; track; return}
2009 proc dummy args { return }
2010 set c [chan create {r w} foo]
2011 fileevent $c readable dummy
2012 } -body {
2013 close $c
2014 chan postevent $c read
2015 } -cleanup {
2016 rename foo {}
2017 rename dummy {}
2018 } -returnCodes error -result {can not find reflected channel named "rc*"}
2019
2020 # --- === *** ###########################
2021 # 'Pull the rug' tests. Create channel in a interpreter A, move to
2022 # other interpreter B, destroy the origin interpreter (A) before or
2023 # during access from B. Must not crash, must return proper errors.
2024
2025 test iocmd-32.0 {origin interpreter of moved channel gone} -match glob -body {
2026
2027 set ida [interp create];#puts <<$ida>>
2028 set idb [interp create];#puts <<$idb>>
2029
2030 # Magic to get the test* commands in the children
2031 load {} Tcltest $ida
2032 load {} Tcltest $idb
2033
2034 # Set up channel in interpreter
2035 interp eval $ida $helperscript
2036 set chan [interp eval $ida {
2037 proc foo {args} {oninit seek; onfinal; track; return}
2038 set chan [chan create {r w} foo]
2039 fconfigure $chan -buffering none
2040 set chan
2041 }]
2042
2043 # Move channel to 2nd interpreter.
2044 interp eval $ida [list testchannel cut $chan]
2045 interp eval $idb [list testchannel splice $chan]
2046
2047 # Kill origin interpreter, then access channel from 2nd interpreter.
2048 interp delete $ida
2049
2050 set res {}
2051 lappend res [catch {interp eval $idb [list puts $chan shoo]} msg] $msg
2052 lappend res [catch {interp eval $idb [list tell $chan]} msg] $msg
2053 lappend res [catch {interp eval $idb [list seek $chan 1]} msg] $msg
2054 lappend res [catch {interp eval $idb [list gets $chan]} msg] $msg
2055 lappend res [catch {interp eval $idb [list close $chan]} msg] $msg
2056 set res
2057
2058 } -cleanup {
2059 interp delete $idb
2060 } -constraints {testchannel} \
2061 -result {1 {Owner lost} 1 {Owner lost} 1 {Owner lost} 1 {Owner lost} 1 {Owner lost}}
2062
2063 test iocmd-32.1 {origin interpreter of moved channel destroyed during access} -match glob -body {
2064
2065 set ida [interp create];#puts <<$ida>>
2066 set idb [interp create];#puts <<$idb>>
2067
2068 # Magic to get the test* commands in the children
2069 load {} Tcltest $ida
2070 load {} Tcltest $idb
2071
2072 # Set up channel in thread
2073 set chan [interp eval $ida $helperscript]
2074 set chan [interp eval $ida {
2075 proc foo {args} {
2076 oninit; onfinal; track;
2077 # destroy interpreter during channel access
2078 suicide
2079 }
2080 set chan [chan create {r w} foo]
2081 fconfigure $chan -buffering none
2082 set chan
2083 }]
2084 interp alias $ida suicide {} interp delete $ida
2085
2086 # Move channel to 2nd thread.
2087 interp eval $ida [list testchannel cut $chan]
2088 interp eval $idb [list testchannel splice $chan]
2089
2090 # Run access from interpreter B, this will give us a synchronous
2091 # response.
2092
2093 interp eval $idb [list set chan $chan]
2094 set res [interp eval $idb {
2095 # wait a bit, give the main thread the time to start its event
2096 # loop to wait for the response from B
2097 after 2000
2098 catch { puts $chan shoo } res
2099 set res
2100 }]
2101 set res
2102 } -cleanup {
2103 interp delete $idb
2104 } -constraints {testchannel} -result {Owner lost}
2105
2106 test iocmd-32.2 {delete interp of reflected chan} {
2107 # Bug 3034840
2108 # Run this test in an interp with memory debugging to panic
2109 # on the double free
2110 interp create child
2111 child eval {
2112 proc no-op args {}
2113 proc driver {sub args} {return {initialize finalize watch read}}
2114 chan event [chan create read driver] readable no-op
2115 }
2116 interp delete child
2117 } {}
2118
2119 # 1st attempt without error in write, another with error in write:
2120 foreach ::writeErr {0 1} {
2121 test iocmd-32.3.$::writeErr {prevent copy-state against segfault by finalize, bug [79474c58800cdf94]} -setup {
2122 proc test_chan {args} {
2123 set rest [lassign $args mode chan]
2124 lappend ::ret $mode
2125 switch -exact $mode {
2126 read {puts $chan "Test" ; close $chan}
2127 write {if {$::writeErr} {return "boom"}; set data [lindex $rest 0]; string length $data}
2128 finalize {after 20 {set ::done done}}
2129 initialize {return "initialize watch finalize read write"}
2130 }
2131 }
2132 set clchlst {}
2133 set toev [after 5000 {set ::done tout}]
2134 } -body {
2135 set ::ret {}
2136 set ch [chan create "read write" test_chan]
2137 lappend clchlst $ch
2138
2139 lassign [chan pipe] in1 out1
2140 lappend clchlst $in1 $out1
2141 lassign [chan pipe] in2 out2
2142 lappend clchlst $in2 $out2
2143 lassign [chan pipe] in3 out3
2144 lappend clchlst $in3 $out3
2145
2146 # simulate exec: echo test >@ $out2 2>@ $out3 <@ $in1 &:
2147 fileevent $out2 writable [list apply {{cho che} {
2148 puts $cho test; close $cho; close $che
2149 }} $out2 $out3]
2150 # recopy to given chans in handler
2151 fileevent $in2 readable [list apply {{in out} {
2152 if {[catch {
2153 chan copy $in $out
2154 } msg]} {
2155 #puts err:$msg
2156 fileevent $in readable {}
2157 }
2158 }} $in2 $ch]
2159 fileevent $in3 readable [list apply {{in out} {
2160 if {[catch {
2161 chan copy $in $out
2162 } msg]} {
2163 #puts err:$msg
2164 fileevent $in readable {}
2165 }
2166 }} $in3 $ch]
2167 fileevent $out1 writable [list apply {{in out} {
2168 if {[catch {
2169 chan copy $in $out
2170 } msg]} {
2171 #puts err:$msg
2172 fileevent $out writable {}
2173 }
2174 }} $ch $out1]
2175
2176 vwait ::done
2177 lappend ::ret $::done
2178 } -cleanup {
2179 foreach ch $clchlst {
2180 catch {close $ch}
2181 }
2182 after cancel $toev
2183 unset -nocomplain ::done ::ret ch in1 in2 in3 out1 out2 out3 toev clchlst
2184 } -result {initialize read write finalize done}
2185 }; unset ::writeErr
2186
2187 # ### ### ### ######### ######### #########
2188 ## Same tests as above, but exercising the code forwarding and
2189 ## receiving driver operations to the originator thread.
2190
2191 # -*- tcl -*-
2192 # ### ### ### ######### ######### #########
2193 ## Testing the reflected channel (Thread forwarding).
2194 #
2195 ## The id numbers refer to the original test without thread
2196 ## forwarding, and gaps due to tests not applicable to forwarding are
2197 ## left to keep this asociation.
2198
2199 # ### ### ### ######### ######### #########
2200 ## Helper command. Runs a script in a separate thread and returns the
2201 ## result. A channel is transfered into the thread as well, and list of
2202 ## configuation variables
2203
2204 proc inthread {chan script args} {
2205 # Test thread.
2206
2207 set tid [thread::create -preserved]
2208 thread::send $tid {load {} Tcltest}
2209
2210 # Init thread configuration.
2211 # - Listed variables
2212 # - Id of main thread
2213 # - A number of helper commands
2214
2215 foreach v $args {
2216 upvar 1 $v x
2217 thread::send $tid [list set $v $x]
2218
2219 }
2220 thread::send $tid [list set mid [thread::id]]
2221 thread::send $tid {
2222 proc note {item} {global notes; lappend notes $item}
2223 proc notes {} {global notes; return $notes}
2224 proc noteOpts opts {global notes; lappend notes [dict merge {
2225 -code !?! -level !?! -errorcode !?! -errorline !?! -errorinfo !?!
2226 } $opts]}
2227 }
2228 thread::send $tid [list proc s {} [list uplevel 1 $script]]; # (*)
2229
2230 # Transfer channel (cut/splice aka detach/attach)
2231
2232 testchannel cut $chan
2233 thread::send $tid [list testchannel splice $chan]
2234
2235 # Run test script, also run local event loop!
2236 # The local event loop waits for the result to come back.
2237 # It is also necessary for the execution of forwarded channel
2238 # operations.
2239
2240 set ::tres ""
2241 thread::send -async $tid {
2242 after 500
2243 catch {s} res; # This runs the script, 's' was defined at (*)
2244 thread::send -async $mid [list set ::tres $res]
2245 }
2246 vwait ::tres
2247 # Remove test thread, and return the captured result.
2248
2249 thread::release $tid
2250 return $::tres
2251 }
2252
2253 # ### ### ### ######### ######### #########
2254
2255 # ### ### ### ######### ######### #########
2256
2257 test iocmd.tf-22.2 {chan finalize, for close} -match glob -body {
2258 set res {}
2259 proc foo {args} {track; oninit; return {}}
2260 note [set c [chan create {r w} foo]]
2261 note [inthread $c {
2262 close $c
2263 # Close the deleted the channel.
2264 file channels rc*
2265 } c]
2266 # Channel destruction does not kill handler command!
2267 note [info command foo]
2268 rename foo {}
2269 set res
2270 } -constraints {testchannel thread} -result {{initialize rc* {read write}} rc* {finalize rc*} {} foo}
2271 test iocmd.tf-22.3 {chan finalize, for close, error, close error} -match glob -body {
2272 set res {}
2273 proc foo {args} {track; oninit; return -code error 5}
2274 note [set c [chan create {r w} foo]]
2275 notes [inthread $c {
2276 note [catch {close $c} msg]; note $msg
2277 # Channel is gone despite error.
2278 note [file channels rc*]
2279 notes
2280 } c]
2281 rename foo {}
2282 set res
2283 } -constraints {testchannel thread} -result {{initialize rc* {read write}} rc* {finalize rc*} 1 5 {}}
2284 test iocmd.tf-22.4 {chan finalize, for close, error, close errror} -match glob -body {
2285 set res {}
2286 proc foo {args} {track; oninit; error FOO}
2287 note [set c [chan create {r w} foo]]
2288 notes [inthread $c {
2289 note [catch {close $c} msg]; note $msg
2290 notes
2291 } c]
2292 rename foo {}
2293 set res
2294 } -constraints {testchannel thread} -result {{initialize rc* {read write}} rc* {finalize rc*} 1 FOO}
2295 test iocmd.tf-22.5 {chan finalize, for close, arbitrary result} -match glob -body {
2296 set res {}
2297 proc foo {args} {track; oninit; return SOMETHING}
2298 note [set c [chan create {r w} foo]]
2299 notes [inthread $c {
2300 note [catch {close $c} msg]; note $msg
2301 notes
2302 } c]
2303 rename foo {}
2304 set res
2305 } -constraints {testchannel thread} -result {{initialize rc* {read write}} rc* {finalize rc*} 0 {}}
2306 test iocmd.tf-22.6 {chan finalize, for close, break, close error} -match glob -body {
2307 set res {}
2308 proc foo {args} {track; oninit; return -code 3}
2309 note [set c [chan create {r w} foo]]
2310 notes [inthread $c {
2311 note [catch {close $c} msg]; note $msg
2312 notes
2313 } c]
2314 rename foo {}
2315 set res
2316 } -result {{initialize rc* {read write}} rc* {finalize rc*} 1 *bad code*} \
2317 -constraints {testchannel thread}
2318 test iocmd.tf-22.7 {chan finalize, for close, continue, close error} -match glob -body {
2319 set res {}
2320 proc foo {args} {track; oninit; return -code 4}
2321 note [set c [chan create {r w} foo]]
2322 notes [inthread $c {
2323 note [catch {close $c} msg]; note $msg
2324 notes
2325 } c]
2326 rename foo {}
2327 set res
2328 } -result {{initialize rc* {read write}} rc* {finalize rc*} 1 *bad code*} \
2329 -constraints {testchannel thread}
2330 test iocmd.tf-22.8 {chan finalize, for close, custom code, close error} -match glob -body {
2331 set res {}
2332 proc foo {args} {track; oninit; return -code 777 BANG}
2333 note [set c [chan create {r w} foo]]
2334 notes [inthread $c {
2335 note [catch {close $c} msg]; note $msg
2336 notes
2337 } c]
2338 rename foo {}
2339 set res
2340 } -result {{initialize rc* {read write}} rc* {finalize rc*} 1 *bad code*} \
2341 -constraints {testchannel thread}
2342 test iocmd.tf-22.9 {chan finalize, for close, ignore level, close error} -match glob -body {
2343 set res {}
2344 proc foo {args} {track; oninit; return -level 5 -code 777 BANG}
2345 note [set c [chan create {r w} foo]]
2346 notes [inthread $c {
2347 note [catch {close $c} msg opt]; note $msg; noteOpts $opt
2348 notes
2349 } c]
2350 rename foo {}
2351 set res
2352 } -result {{initialize rc* {read write}} rc* {finalize rc*} 1 *bad code* {-code 1 -level 0 -errorcode NONE -errorline 1 -errorinfo *bad code*subcommand "finalize"*}} \
2353 -constraints {testchannel thread}
2354
2355 # --- === *** ###########################
2356 # method read
2357
2358 test iocmd.tf-23.1 {chan read, regular data return} -match glob -body {
2359 set res {}
2360 proc foo {args} {
2361 oninit; onfinal; track
2362 return snarf
2363 }
2364 set c [chan create {r w} foo]
2365 notes [inthread $c {
2366 note [read $c 10]
2367 close $c
2368 notes
2369 } c]
2370 rename foo {}
2371 set res
2372 } -constraints {testchannel thread} -result {{read rc* 4096} {read rc* 4096} snarfsnarf}
2373 test iocmd.tf-23.2 {chan read, bad data return, too much} -match glob -body {
2374 set res {}
2375 proc foo {args} {
2376 oninit; onfinal; track
2377 return [string repeat snarf 1000]
2378 }
2379 set c [chan create {r w} foo]
2380 notes [inthread $c {
2381 note [catch {[read $c 2]} msg]; note $msg
2382 close $c
2383 notes
2384 } c]
2385 rename foo {}
2386 set res
2387 } -constraints {testchannel thread} -result {{read rc* 4096} 1 {read delivered more than requested}}
2388 test iocmd.tf-23.3 {chan read, for non-readable channel} -match glob -body {
2389 set res {}
2390 proc foo {args} {
2391 oninit; onfinal; track; note MUST_NOT_HAPPEN
2392 }
2393 set c [chan create {w} foo]
2394 notes [inthread $c {
2395 note [catch {[read $c 2]} msg]; note $msg
2396 close $c
2397 notes
2398 } c]
2399 rename foo {}
2400 set res
2401 } -constraints {testchannel thread} -result {1 {channel "rc*" wasn't opened for reading}}
2402 test iocmd.tf-23.4 {chan read, error return} -match glob -body {
2403 set res {}
2404 proc foo {args} {
2405 oninit; onfinal; track
2406 return -code error BOOM!
2407 }
2408 set c [chan create {r w} foo]
2409 notes [inthread $c {
2410 note [catch {read $c 2} msg]; note $msg
2411 close $c
2412 notes
2413 } c]
2414 rename foo {}
2415 set res
2416 } -result {{read rc* 4096} 1 BOOM!} \
2417 -constraints {testchannel thread}
2418 test iocmd.tf-23.5 {chan read, break return is error} -match glob -body {
2419 set res {}
2420 proc foo {args} {
2421 oninit; onfinal; track
2422 return -code break BOOM!
2423 }
2424 set c [chan create {r w} foo]
2425 notes [inthread $c {
2426 note [catch {read $c 2} msg]; note $msg
2427 close $c
2428 notes
2429 } c]
2430 rename foo {}
2431 set res
2432 } -result {{read rc* 4096} 1 *bad code*} \
2433 -constraints {testchannel thread}
2434 test iocmd.tf-23.6 {chan read, continue return is error} -match glob -body {
2435 set res {}
2436 proc foo {args} {
2437 oninit; onfinal; track
2438 return -code continue BOOM!
2439 }
2440 set c [chan create {r w} foo]
2441 notes [inthread $c {
2442 note [catch {read $c 2} msg]; note $msg
2443 close $c
2444 notes
2445 } c]
2446 rename foo {}
2447 set res
2448 } -result {{read rc* 4096} 1 *bad code*} \
2449 -constraints {testchannel thread}
2450 test iocmd.tf-23.7 {chan read, custom return is error} -match glob -body {
2451 set res {}
2452 proc foo {args} {
2453 oninit; onfinal; track
2454 return -code 777 BOOM!
2455 }
2456 set c [chan create {r w} foo]
2457 notes [inthread $c {
2458 note [catch {read $c 2} msg]; note $msg
2459 close $c
2460 notes
2461 } c]
2462 rename foo {}
2463 set res
2464 } -result {{read rc* 4096} 1 *bad code*} \
2465 -constraints {testchannel thread}
2466 test iocmd.tf-23.8 {chan read, level is squashed} -match glob -body {
2467 set res {}
2468 proc foo {args} {
2469 oninit; onfinal; track
2470 return -level 55 -code 777 BOOM!
2471 }
2472 set c [chan create {r w} foo]
2473 notes [inthread $c {
2474 note [catch {read $c 2} msg opt]; note $msg; noteOpts $opt
2475 close $c
2476 notes
2477 } c]
2478 rename foo {}
2479 set res
2480 } -result {{read rc* 4096} 1 *bad code* {-code 1 -level 0 -errorcode NONE -errorline 1 -errorinfo *bad code*subcommand "read"*}} \
2481 -constraints {testchannel thread}
2482 test iocmd.tf-23.9 {chan read, no data means eof} -match glob -setup {
2483 set res {}
2484 proc foo {args} {
2485 oninit; onfinal; track
2486 return ""
2487 }
2488 set c [chan create {r w} foo]
2489 } -body {
2490 notes [inthread $c {
2491 note [read $c 2]
2492 note [eof $c]
2493 close $c
2494 notes
2495 } c]
2496 set res
2497 } -cleanup {
2498 rename foo {}
2499 unset res
2500 } -result {{read rc* 4096} {} 1} \
2501 -constraints {testchannel thread}
2502 test iocmd.tf-23.10 {chan read, EAGAIN means no data, yet no eof either} -match glob -setup {
2503 set res {}
2504 proc foo {args} {
2505 oninit; onfinal; track
2506 error EAGAIN
2507 }
2508 set c [chan create {r w} foo]
2509 } -body {
2510 notes [inthread $c {
2511 note [read $c 2]
2512 note [eof $c]
2513 close $c
2514 notes
2515 } c]
2516 set res
2517 } -cleanup {
2518 rename foo {}
2519 unset res
2520 } -result {{read rc* 4096} {} 0} \
2521 -constraints {testchannel thread}
2522
2523 # --- === *** ###########################
2524 # method write
2525
2526 test iocmd.tf-24.1 {chan write, regular write} -match glob -body {
2527 set res {}
2528 proc foo {args} {
2529 oninit; onfinal; track
2530 set written [string length [lindex $args 2]]
2531 note $written
2532 return $written
2533 }
2534 set c [chan create {r w} foo]
2535 inthread $c {
2536 puts -nonewline $c snarf; flush $c
2537 close $c
2538 } c
2539 rename foo {}
2540 set res
2541 } -constraints {testchannel thread} -result {{write rc* snarf} 5}
2542 test iocmd.tf-24.2 {chan write, ack partial writes} -match glob -body {
2543 set res {}
2544 proc foo {args} {
2545 oninit; onfinal; track
2546 set written [string length [lindex $args 2]]
2547 if {$written > 10} {set written [expr {$written / 2}]}
2548 note $written
2549 return $written
2550 }
2551 set c [chan create {r w} foo]
2552 inthread $c {
2553 puts -nonewline $c snarfsnarfsnarf; flush $c
2554 close $c
2555 } c
2556 rename foo {}
2557 set res
2558 } -constraints {testchannel thread} -result {{write rc* snarfsnarfsnarf} 7 {write rc* arfsnarf} 8}
2559 test iocmd.tf-24.3 {chan write, failed write} -match glob -body {
2560 set res {}
2561 proc foo {args} {oninit; onfinal; track; note -1; return -1}
2562 set c [chan create {r w} foo]
2563 inthread $c {
2564 puts -nonewline $c snarfsnarfsnarf; flush $c
2565 close $c
2566 } c
2567 rename foo {}
2568 set res
2569 } -constraints {testchannel thread} -result {{write rc* snarfsnarfsnarf} -1}
2570 test iocmd.tf-24.4 {chan write, non-writable channel} -match glob -body {
2571 set res {}
2572 proc foo {args} {oninit; onfinal; track; note MUST_NOT_HAPPEN; return}
2573 set c [chan create {r} foo]
2574 notes [inthread $c {
2575 note [catch {puts -nonewline $c snarfsnarfsnarf; flush $c} msg]
2576 note $msg
2577 close $c
2578 notes
2579 } c]
2580 rename foo {}
2581 set res
2582 } -constraints {testchannel thread} -result {1 {channel "rc*" wasn't opened for writing}}
2583 test iocmd.tf-24.5 {chan write, bad result, more written than data} -match glob -body {
2584 set res {}
2585 proc foo {args} {oninit; onfinal; track; return 10000}
2586 set c [chan create {r w} foo]
2587 notes [inthread $c {
2588 note [catch {puts -nonewline $c snarf; flush $c} msg]
2589 note $msg
2590 close $c
2591 notes
2592 } c]
2593 rename foo {}
2594 set res
2595 } -constraints {testchannel thread} -result {{write rc* snarf} 1 {write wrote more than requested}}
2596 test iocmd.tf-24.6 {chan write, zero writes} -match glob -body {
2597 set res {}
2598 proc foo {args} {oninit; onfinal; track; return 0}
2599 set c [chan create {r w} foo]
2600 notes [inthread $c {
2601 note [catch {puts -nonewline $c snarf; flush $c} msg]
2602 note $msg
2603 close $c
2604 notes
2605 } c]
2606 rename foo {}
2607 set res
2608 } -constraints {testchannel thread} -result {{write rc* snarf} 1 {write wrote more than requested}}
2609 test iocmd.tf-24.7 {chan write, failed write, error return} -match glob -body {
2610 set res {}
2611 proc foo {args} {oninit; onfinal; track; return -code error BOOM!}
2612 set c [chan create {r w} foo]
2613 notes [inthread $c {
2614 note [catch {puts -nonewline $c snarfsnarfsnarf; flush $c} msg]
2615 note $msg
2616 close $c
2617 notes
2618 } c]
2619 rename foo {}
2620 set res
2621 } -result {{write rc* snarfsnarfsnarf} 1 BOOM!} \
2622 -constraints {testchannel thread}
2623 test iocmd.tf-24.8 {chan write, failed write, error return} -match glob -body {
2624 set res {}
2625 proc foo {args} {oninit; onfinal; track; error BOOM!}
2626 set c [chan create {r w} foo]
2627 notes [inthread $c {
2628 note [catch {puts -nonewline $c snarfsnarfsnarf; flush $c} msg]
2629 note $msg
2630 close $c
2631 notes
2632 } c]
2633 rename foo {}
2634 set res
2635 } -result {{write rc* snarfsnarfsnarf} 1 BOOM!} \
2636 -constraints {testchannel thread}
2637 test iocmd.tf-24.9 {chan write, failed write, break return is error} -match glob -body {
2638 set res {}
2639 proc foo {args} {oninit; onfinal; track; return -code break BOOM!}
2640 set c [chan create {r w} foo]
2641 notes [inthread $c {
2642 note [catch {puts -nonewline $c snarfsnarfsnarf; flush $c} msg]
2643 note $msg
2644 close $c
2645 notes
2646 } c]
2647 rename foo {}
2648 set res
2649 } -result {{write rc* snarfsnarfsnarf} 1 *bad code*} \
2650 -constraints {testchannel thread}
2651 test iocmd.tf-24.10 {chan write, failed write, continue return is error} -match glob -body {
2652 set res {}
2653 proc foo {args} {oninit; onfinal; track; return -code continue BOOM!}
2654 set c [chan create {r w} foo]
2655 notes [inthread $c {
2656 note [catch {puts -nonewline $c snarfsnarfsnarf; flush $c} msg]
2657 note $msg
2658 close $c
2659 notes
2660 } c]
2661 rename foo {}
2662 set res
2663 } -result {{write rc* snarfsnarfsnarf} 1 *bad code*} \
2664 -constraints {testchannel thread}
2665 test iocmd.tf-24.11 {chan write, failed write, custom return is error} -match glob -body {
2666 set res {}
2667 proc foo {args} {oninit; onfinal; track; return -code 777 BOOM!}
2668 set c [chan create {r w} foo]
2669 notes [inthread $c {
2670 note [catch {puts -nonewline $c snarfsnarfsnarf; flush $c} msg]
2671 note $msg
2672 close $c
2673 notes
2674 } c]
2675 rename foo {}
2676 set res
2677 } -result {{write rc* snarfsnarfsnarf} 1 *bad code*} \
2678 -constraints {testchannel thread}
2679 test iocmd.tf-24.12 {chan write, failed write, non-numeric return is error} -match glob -body {
2680 set res {}
2681 proc foo {args} {oninit; onfinal; track; return BANG}
2682 set c [chan create {r w} foo]
2683 notes [inthread $c {
2684 note [catch {puts -nonewline $c snarfsnarfsnarf; flush $c} msg]
2685 note $msg
2686 close $c
2687 notes
2688 } c]
2689 rename foo {}
2690 set res
2691 } -result {{write rc* snarfsnarfsnarf} 1 {expected integer but got "BANG"}} \
2692 -constraints {testchannel thread}
2693 test iocmd.tf-24.13 {chan write, failed write, level is ignored} -match glob -body {
2694 set res {}
2695 proc foo {args} {oninit; onfinal; track; return -level 55 -code 777 BOOM!}
2696 set c [chan create {r w} foo]
2697 notes [inthread $c {
2698 note [catch {puts -nonewline $c snarfsnarfsnarf; flush $c} msg opt]
2699 note $msg
2700 noteOpts $opt
2701 close $c
2702 notes
2703 } c]
2704 rename foo {}
2705 set res
2706 } -result {{write rc* snarfsnarfsnarf} 1 *bad code* {-code 1 -level 0 -errorcode NONE -errorline 1 -errorinfo *bad code*subcommand "write"*}} \
2707 -constraints {testchannel thread}
2708 test iocmd.tf-24.14 {chan write, no EAGAIN means that writing is allowed at this time, bug 2936225} -match glob -setup {
2709 set res {}
2710 proc foo {args} {
2711 oninit; onfinal; track
2712 return 3
2713 }
2714 set c [chan create {r w} foo]
2715 } -body {
2716 notes [inthread $c {
2717 note [puts -nonewline $c ABC ; flush $c]
2718 close $c
2719 notes
2720 } c]
2721 set res
2722 } -cleanup {
2723 rename foo {}
2724 unset res
2725 } -result {{write rc* ABC} {}} \
2726 -constraints {testchannel thread}
2727 test iocmd.tf-24.15 {chan write, EAGAIN means that writing is not allowed at this time, bug 2936225} -match glob -setup {
2728 set res {}
2729 proc foo {args} {
2730 oninit; onfinal; track
2731 # Note: The EAGAIN signals that the channel cannot accept
2732 # write requests right now, this in turn causes the IO core to
2733 # request the generation of writable events (see expected
2734 # result below, and compare to case 24.14 above).
2735 error EAGAIN
2736 }
2737 set c [chan create {r w} foo]
2738 } -body {
2739 notes [inthread $c {
2740 note [puts -nonewline $c ABC ; flush $c]
2741 close $c
2742 notes
2743 } c]
2744 set res
2745 } -cleanup {
2746 proc foo {args} {onfinal; set ::done-24.15 1; return 3}
2747 after 1000 {set ::done-24.15 2}
2748 vwait done-24.15
2749 rename foo {}
2750 unset res
2751 } -result {{write rc* ABC} {watch rc* write} {}} \
2752 -constraints {testchannel thread}
2753
2754 test iocmd.tf-24.16 {chan write, note the background flush setup by close due to the EAGAIN leaving data in buffers.} -match glob -setup {
2755 set res {}
2756 proc foo {args} {
2757 oninit; onfinal; track
2758 # Note: The EAGAIN signals that the channel cannot accept
2759 # write requests right now, this in turn causes the IO core to
2760 # request the generation of writable events (see expected
2761 # result below, and compare to case 24.14 above).
2762 error EAGAIN
2763 }
2764 set c [chan create {r w} foo]
2765 } -body {
2766 notes [inthread $c {
2767 note [puts -nonewline $c ABC ; flush $c]
2768 close $c
2769 notes
2770 } c]
2771 # Replace handler with all-tracking one which doesn't error.
2772 # This will tell us if a write-due-flush is there.
2773 proc foo {args} { onfinal; note BG ; track ; set ::endbody-24.16 1}
2774 # Flush (sic!) the event-queue to capture the write from a
2775 # BG-flush.
2776 after 1000 {set ::endbody-24.16 2}
2777 vwait endbody-24.16
2778 set res
2779 } -cleanup {
2780 proc foo {args} {onfinal; set ::done-24.16 1; return 3}
2781 after 1000 {set ::done-24.16 2}
2782 vwait done-24.16
2783 rename foo {}
2784 unset res
2785 } -result {{write rc* ABC} {watch rc* write} {} BG {write rc* ABC}} \
2786 -constraints {testchannel thread}
2787
2788 test iocmd.tf-24.17.bug3522560 {postevent for transfered channel} \
2789 -constraints {testchannel thread} -setup {
2790 # This test exposes how the execution of postevent in the handler thread causes
2791 # a crash if we are not properly injecting the events into the owning thread instead.
2792 # With the injection the test will simply complete without crash.
2793
2794 set beat 10000
2795 set drive 999
2796 set data ...---...
2797
2798 proc LOG {text} {
2799 #puts stderr "[thread::id]: $text"
2800 return
2801 }
2802
2803 proc POST {hi} {
2804 LOG "-> [info level 0]"
2805 chan postevent $hi read
2806 LOG "<- [info level 0]"
2807
2808 set ::timer [after $::drive [info level 0]]
2809 return
2810 }
2811
2812 proc HANDLER {op ch args} {
2813 lappend ::res [lrange [info level 0] 1 end]
2814 LOG "-> [info level 0]"
2815 set ret {}
2816 switch -glob -- $op {
2817 init* {set ret {initialize finalize watch read}}
2818 watch {
2819 set l [lindex $args 0]
2820 catch {after cancel $::timer}
2821 if {[llength $l]} {
2822 set ::timer [after $::drive [list POST $ch]]
2823 }
2824 }
2825 finalize {
2826 catch { after cancel $::timer }
2827 after 500 {set ::forever now}
2828 }
2829 read {
2830 set ret $::data
2831 set ::data {} ; # Next is EOF.
2832 }
2833 }
2834 LOG "<- [info level 0] : $ret"
2835 return $ret
2836 }
2837 } -body {
2838 LOG BEGIN
2839 set ch [chan create {read} HANDLER]
2840
2841 set tid [thread::create {
2842 proc LOG {text} {
2843 #puts stderr "\t\t\t\t\t\t[thread::id]: $text"
2844 return
2845 }
2846 LOG THREAD-STARTED
2847 load {} Tcltest
2848 proc bgerror s {
2849 LOG BGERROR:$s
2850 }
2851 vwait forever
2852 LOG THREAD-DONE
2853 }]
2854
2855 testchannel cut $ch
2856 thread::send $tid [list set thech $ch]
2857 thread::send $tid [list set beat $beat]
2858 thread::send -async $tid {
2859 LOG SPLICE-BEG
2860 testchannel splice $thech
2861 LOG SPLICE-END
2862 proc PROCESS {ch} {
2863 LOG "-> [info level 0]"
2864 if {[eof $ch]} {
2865 close $ch
2866 set ::done 1
2867 set c <<EOF>>
2868 } else {
2869 set c [read $ch 1]
2870 }
2871 LOG "GOTCHAR: $c"
2872 LOG "<- [info level 0]"
2873 }
2874 LOG THREAD-FILEEVENT
2875 fconfigure $thech -translation binary -blocking 0
2876 fileevent $thech readable [list PROCESS $thech]
2877 LOG THREAD-NOEVENT-LOOP
2878 set done 0
2879 while {!$done} {
2880 after $beat
2881 LOG THREAD-HEARTBEAT
2882 update
2883 }
2884 LOG THREAD-LOOP-DONE
2885 #thread::exit
2886 # Thread exits cause leaks; Use clean thread shutdown
2887 set forever yourGirl
2888 }
2889
2890 LOG MAIN_WAITING
2891 vwait forever
2892 LOG MAIN_DONE
2893
2894 set res
2895 } -cleanup {
2896 after cancel $::timer
2897 rename LOG {}
2898 rename POST {}
2899 rename HANDLER {}
2900 unset beat drive data forever res tid ch timer
2901 } -match glob \
2902 -result {{initialize rc* read} {watch rc* read} {read rc* 4096} {watch rc* {}} {watch rc* read} {read rc* 4096} {watch rc* {}} {finalize rc*}}
2903
2904 # --- === *** ###########################
2905 # method cgetall
2906
2907 test iocmd.tf-25.1 {chan configure, cgetall, standard options} -match glob -body {
2908 set res {}
2909 proc foo {args} {oninit; onfinal; track; note MUST_NOT_HAPPEN; return}
2910 set c [chan create {r w} foo]
2911 notes [inthread $c {
2912 note [fconfigure $c]
2913 close $c
2914 notes
2915 } c]
2916 rename foo {}
2917 set res
2918 } -constraints {testchannel thread} \
2919 -result {{-blocking 1 -buffering full -buffersize 4096 -encoding * -eofchar {{} {}} -translation {auto *}}}
2920 test iocmd.tf-25.2 {chan configure, cgetall, no options} -match glob -body {
2921 set res {}
2922 proc foo {args} {oninit cget cgetall; onfinal; track; return ""}
2923 set c [chan create {r w} foo]
2924 notes [inthread $c {
2925 note [fconfigure $c]
2926 close $c
2927 notes
2928 } c]
2929 rename foo {}
2930 set res
2931 } -constraints {testchannel thread} \
2932 -result {{cgetall rc*} {-blocking 1 -buffering full -buffersize 4096 -encoding * -eofchar {{} {}} -translation {auto *}}}
2933 test iocmd.tf-25.3 {chan configure, cgetall, regular result} -match glob -body {
2934 set res {}
2935 proc foo {args} {
2936 oninit cget cgetall; onfinal; track
2937 return "-bar foo -snarf x"
2938 }
2939 set c [chan create {r w} foo]
2940 notes [inthread $c {
2941 note [fconfigure $c]
2942 close $c
2943 notes
2944 } c]
2945 rename foo {}
2946 set res
2947 } -constraints {testchannel thread} \
2948 -result {{cgetall rc*} {-blocking 1 -buffering full -buffersize 4096 -encoding * -eofchar {{} {}} -translation {auto *} -bar foo -snarf x}}
2949 test iocmd.tf-25.4 {chan configure, cgetall, bad result, list of uneven length} -match glob -body {
2950 set res {}
2951 proc foo {args} {
2952 oninit cget cgetall; onfinal; track
2953 return "-bar"
2954 }
2955 set c [chan create {r w} foo]
2956 notes [inthread $c {
2957 note [catch {fconfigure $c} msg]
2958 note $msg
2959 close $c
2960 notes
2961 } c]
2962 rename foo {}
2963 set res
2964 } -constraints {testchannel thread} -result {{cgetall rc*} 1 {Expected list with even number of elements, got 1 element instead}}
2965 test iocmd.tf-25.5 {chan configure, cgetall, bad result, not a list} -match glob -body {
2966 set res {}
2967 proc foo {args} {
2968 oninit cget cgetall; onfinal; track
2969 return "\{"
2970 }
2971 set c [chan create {r w} foo]
2972 notes [inthread $c {
2973 note [catch {fconfigure $c} msg]
2974 note $msg
2975 close $c
2976 notes
2977 } c]
2978 rename foo {}
2979 set res
2980 } -constraints {testchannel thread} -result {{cgetall rc*} 1 {unmatched open brace in list}}
2981 test iocmd.tf-25.6 {chan configure, cgetall, error return} -match glob -body {
2982 set res {}
2983 proc foo {args} {
2984 oninit cget cgetall; onfinal; track
2985 return -code error BOOM!
2986 }
2987 set c [chan create {r w} foo]
2988 notes [inthread $c {
2989 note [catch {fconfigure $c} msg]
2990 note $msg
2991 close $c
2992 notes
2993 } c]
2994 rename foo {}
2995 set res
2996 } -constraints {testchannel thread} -result {{cgetall rc*} 1 BOOM!}
2997 test iocmd.tf-25.7 {chan configure, cgetall, break return is error} -match glob -body {
2998 set res {}
2999 proc foo {args} {
3000 oninit cget cgetall; onfinal; track
3001 return -code break BOOM!
3002 }
3003 set c [chan create {r w} foo]
3004 notes [inthread $c {
3005 note [catch {fconfigure $c} msg]
3006 note $msg
3007 close $c
3008 notes
3009 } c]
3010 rename foo {}
3011 set res
3012 } -result {{cgetall rc*} 1 *bad code*} \
3013 -constraints {testchannel thread}
3014 test iocmd.tf-25.8 {chan configure, cgetall, continue return is error} -match glob -body {
3015 set res {}
3016 proc foo {args} {
3017 oninit cget cgetall; onfinal; track
3018 return -code continue BOOM!
3019 }
3020 set c [chan create {r w} foo]
3021 notes [inthread $c {
3022 note [catch {fconfigure $c} msg]
3023 note $msg
3024 close $c
3025 notes
3026 } c]
3027 rename foo {}
3028 set res
3029 } -result {{cgetall rc*} 1 *bad code*} \
3030 -constraints {testchannel thread}
3031 test iocmd.tf-25.9 {chan configure, cgetall, custom return is error} -match glob -body {
3032 set res {}
3033 proc foo {args} {
3034 oninit cget cgetall; onfinal; track
3035 return -code 777 BOOM!
3036 }
3037 set c [chan create {r w} foo]
3038 notes [inthread $c {
3039 note [catch {fconfigure $c} msg]
3040 note $msg
3041 close $c
3042 notes
3043 } c]
3044 rename foo {}
3045 set res
3046 } -result {{cgetall rc*} 1 *bad code*} \
3047 -constraints {testchannel thread}
3048 test iocmd.tf-25.10 {chan configure, cgetall, level is ignored} -match glob -body {
3049 set res {}
3050 proc foo {args} {
3051 oninit cget cgetall; onfinal; track
3052 return -level 55 -code 777 BANG
3053 }
3054 set c [chan create {r w} foo]
3055 notes [inthread $c {
3056 note [catch {fconfigure $c} msg opt]
3057 note $msg
3058 noteOpts $opt
3059 close $c
3060 notes
3061 } c]
3062 rename foo {}
3063 set res
3064 } -result {{cgetall rc*} 1 *bad code* {-code 1 -level 0 -errorcode NONE -errorline 1 -errorinfo *bad code*subcommand "cgetall"*}} \
3065 -constraints {testchannel thread}
3066
3067 # --- === *** ###########################
3068 # method configure
3069
3070 test iocmd.tf-26.1 {chan configure, set standard option} -match glob -body {
3071 set res {}
3072 proc foo {args} {
3073 oninit configure; onfinal; track; note MUST_NOT_HAPPEN; return
3074 }
3075 set c [chan create {r w} foo]
3076 notes [inthread $c {
3077 note [fconfigure $c -translation lf]
3078 close $c
3079 notes
3080 } c]
3081 rename foo {}
3082 set res
3083 } -constraints {testchannel thread} -result {{}}
3084 test iocmd.tf-26.2 {chan configure, set option, error return} -match glob -body {
3085 set res {}
3086 proc foo {args} {
3087 oninit configure; onfinal; track
3088 return -code error BOOM!
3089 }
3090 set c [chan create {r w} foo]
3091 notes [inthread $c {
3092 note [catch {fconfigure $c -rc-foo bar} msg]
3093 note $msg
3094 close $c
3095 notes
3096 } c]
3097 rename foo {}
3098 set res
3099 } -constraints {testchannel thread} -result {{configure rc* -rc-foo bar} 1 BOOM!}
3100 test iocmd.tf-26.3 {chan configure, set option, ok return} -match glob -body {
3101 set res {}
3102 proc foo {args} {oninit configure; onfinal; track; return}
3103 set c [chan create {r w} foo]
3104 notes [inthread $c {
3105 note [fconfigure $c -rc-foo bar]
3106 close $c
3107 notes
3108 } c]
3109 rename foo {}
3110 set res
3111 } -constraints {testchannel thread} -result {{configure rc* -rc-foo bar} {}}
3112 test iocmd.tf-26.4 {chan configure, set option, break return is error} -match glob -body {
3113 set res {}
3114 proc foo {args} {
3115 oninit configure; onfinal; track
3116 return -code break BOOM!
3117 }
3118 set c [chan create {r w} foo]
3119 notes [inthread $c {
3120 note [catch {fconfigure $c -rc-foo bar} msg]
3121 note $msg
3122 close $c
3123 notes
3124 } c]
3125 rename foo {}
3126 set res
3127 } -result {{configure rc* -rc-foo bar} 1 *bad code*} \
3128 -constraints {testchannel thread}
3129 test iocmd.tf-26.5 {chan configure, set option, continue return is error} -match glob -body {
3130 set res {}
3131 proc foo {args} {
3132 oninit configure; onfinal; track
3133 return -code continue BOOM!
3134 }
3135 set c [chan create {r w} foo]
3136 notes [inthread $c {
3137 note [catch {fconfigure $c -rc-foo bar} msg]
3138 note $msg
3139 close $c
3140 notes
3141 } c]
3142 rename foo {}
3143 set res
3144 } -result {{configure rc* -rc-foo bar} 1 *bad code*} \
3145 -constraints {testchannel thread}
3146 test iocmd.tf-26.6 {chan configure, set option, custom return is error} -match glob -body {
3147 set res {}
3148 proc foo {args} {
3149 oninit configure; onfinal; track
3150 return -code 444 BOOM!
3151 }
3152 set c [chan create {r w} foo]
3153 notes [inthread $c {
3154 note [catch {fconfigure $c -rc-foo bar} msg]
3155 note $msg
3156 close $c
3157 notes
3158 } c]
3159 rename foo {}
3160 set res
3161 } -result {{configure rc* -rc-foo bar} 1 *bad code*} \
3162 -constraints {testchannel thread}
3163 test iocmd.tf-26.7 {chan configure, set option, level is ignored} -match glob -body {
3164 set res {}
3165 proc foo {args} {
3166 oninit configure; onfinal; track
3167 return -level 55 -code 444 BANG
3168 }
3169 set c [chan create {r w} foo]
3170 notes [inthread $c {
3171 note [catch {fconfigure $c -rc-foo bar} msg opt]
3172 note $msg
3173 noteOpts $opt
3174 close $c
3175 notes
3176 } c]
3177 rename foo {}
3178 set res
3179 } -result {{configure rc* -rc-foo bar} 1 *bad code* {-code 1 -level 0 -errorcode NONE -errorline 1 -errorinfo *bad code*subcommand "configure"*}} \
3180 -constraints {testchannel thread}
3181
3182 # --- === *** ###########################
3183 # method cget
3184
3185 test iocmd.tf-27.1 {chan configure, get option, ok return} -match glob -body {
3186 set res {}
3187 proc foo {args} {oninit cget cgetall; onfinal; track; return foo}
3188 set c [chan create {r w} foo]
3189 notes [inthread $c {
3190 note [fconfigure $c -rc-foo]
3191 close $c
3192 notes
3193 } c]
3194 rename foo {}
3195 set res
3196 } -constraints {testchannel thread} -result {{cget rc* -rc-foo} foo}
3197 test iocmd.tf-27.2 {chan configure, get option, error return} -match glob -body {
3198 set res {}
3199 proc foo {args} {
3200 oninit cget cgetall; onfinal; track
3201 return -code error BOOM!
3202 }
3203 set c [chan create {r w} foo]
3204 notes [inthread $c {
3205 note [catch {fconfigure $c -rc-foo} msg]
3206 note $msg
3207 close $c
3208 notes
3209 } c]
3210 rename foo {}
3211 set res
3212 } -constraints {testchannel thread} -result {{cget rc* -rc-foo} 1 BOOM!}
3213 test iocmd.tf-27.3 {chan configure, get option, break return is error} -match glob -body {
3214 set res {}
3215 proc foo {args} {
3216 oninit cget cgetall; onfinal; track
3217 return -code error BOOM!
3218 }
3219 set c [chan create {r w} foo]
3220 notes [inthread $c {
3221 note [catch {fconfigure $c -rc-foo} msg]
3222 note $msg
3223 close $c
3224 notes
3225 } c]
3226 rename foo {}
3227 set res
3228 } -result {{cget rc* -rc-foo} 1 BOOM!} \
3229 -constraints {testchannel thread}
3230 test iocmd.tf-27.4 {chan configure, get option, continue return is error} -match glob -body {
3231 set res {}
3232 proc foo {args} {
3233 oninit cget cgetall; onfinal; track
3234 return -code continue BOOM!
3235 }
3236 set c [chan create {r w} foo]
3237 notes [inthread $c {
3238 note [catch {fconfigure $c -rc-foo} msg]
3239 note $msg
3240 close $c
3241 notes
3242 } c]
3243 rename foo {}
3244 set res
3245 } -result {{cget rc* -rc-foo} 1 *bad code*} \
3246 -constraints {testchannel thread}
3247 test iocmd.tf-27.5 {chan configure, get option, custom return is error} -match glob -body {
3248 set res {}
3249 proc foo {args} {
3250 oninit cget cgetall; onfinal; track
3251 return -code 333 BOOM!
3252 }
3253 set c [chan create {r w} foo]
3254 notes [inthread $c {
3255 note [catch {fconfigure $c -rc-foo} msg]
3256 note $msg
3257 close $c
3258 notes
3259 } c]
3260 rename foo {}
3261 set res
3262 } -result {{cget rc* -rc-foo} 1 *bad code*} \
3263 -constraints {testchannel thread}
3264 test iocmd.tf-27.6 {chan configure, get option, level is ignored} -match glob -body {
3265 set res {}
3266 proc foo {args} {
3267 oninit cget cgetall; onfinal; track
3268 return -level 77 -code 333 BANG
3269 }
3270 set c [chan create {r w} foo]
3271 notes [inthread $c {
3272 note [catch {fconfigure $c -rc-foo} msg opt]
3273 note $msg
3274 noteOpts $opt
3275 close $c
3276 notes
3277 } c]
3278 rename foo {}
3279 set res
3280 } -result {{cget rc* -rc-foo} 1 *bad code* {-code 1 -level 0 -errorcode NONE -errorline 1 -errorinfo *bad code*subcommand "cget"*}} \
3281 -constraints {testchannel thread}
3282
3283 # --- === *** ###########################
3284 # method seek
3285
3286 test iocmd.tf-28.1 {chan tell, not supported by handler} -match glob -body {
3287 set res {}
3288 proc foo {args} {oninit; onfinal; track; note MUST_NOT_HAPPEN; return}
3289 set c [chan create {r w} foo]
3290 notes [inthread $c {
3291 note [tell $c]
3292 close $c
3293 notes
3294 } c]
3295 rename foo {}
3296 set res
3297 } -result {-1} \
3298 -constraints {testchannel thread}
3299 test iocmd.tf-28.2 {chan tell, error return} -match glob -body {
3300 set res {}
3301 proc foo {args} {oninit seek; onfinal; track; return -code error BOOM!}
3302 set c [chan create {r w} foo]
3303 notes [inthread $c {
3304 note [catch {tell $c} msg]
3305 note $msg
3306 close $c
3307 notes
3308 } c]
3309 rename foo {}
3310 set res
3311 } -result {{seek rc* 0 current} 1 BOOM!} \
3312 -constraints {testchannel thread}
3313 test iocmd.tf-28.3 {chan tell, break return is error} -match glob -body {
3314 set res {}
3315 proc foo {args} {oninit seek; onfinal; track; return -code break BOOM!}
3316 set c [chan create {r w} foo]
3317 notes [inthread $c {
3318 note [catch {tell $c} msg]
3319 note $msg
3320 close $c
3321 notes
3322 } c]
3323 rename foo {}
3324 set res
3325 } -result {{seek rc* 0 current} 1 *bad code*} \
3326 -constraints {testchannel thread}
3327 test iocmd.tf-28.4 {chan tell, continue return is error} -match glob -body {
3328 set res {}
3329 proc foo {args} {oninit seek; onfinal; track; return -code continue BOOM!}
3330 set c [chan create {r w} foo]
3331 notes [inthread $c {
3332 note [catch {tell $c} msg]
3333 note $msg
3334 close $c
3335 notes
3336 } c]
3337 rename foo {}
3338 set res
3339 } -result {{seek rc* 0 current} 1 *bad code*} \
3340 -constraints {testchannel thread}
3341 test iocmd.tf-28.5 {chan tell, custom return is error} -match glob -body {
3342 set res {}
3343 proc foo {args} {oninit seek; onfinal; track; return -code 222 BOOM!}
3344 set c [chan create {r w} foo]
3345 notes [inthread $c {
3346 note [catch {tell $c} msg]
3347 note $msg
3348 close $c
3349 notes
3350 } c]
3351 rename foo {}
3352 set res
3353 } -result {{seek rc* 0 current} 1 *bad code*} \
3354 -constraints {testchannel thread}
3355 test iocmd.tf-28.6 {chan tell, level is ignored} -match glob -body {
3356 set res {}
3357 proc foo {args} {oninit seek; onfinal; track; return -level 11 -code 222 BANG}
3358 set c [chan create {r w} foo]
3359 notes [inthread $c {
3360 note [catch {tell $c} msg opt]
3361 note $msg
3362 noteOpts $opt
3363 close $c
3364 notes
3365 } c]
3366 rename foo {}
3367 set res
3368 } -result {{seek rc* 0 current} 1 *bad code* {-code 1 -level 0 -errorcode NONE -errorline 1 -errorinfo *bad code*subcommand "seek"*}} \
3369 -constraints {testchannel thread}
3370 test iocmd.tf-28.7 {chan tell, regular return} -match glob -body {
3371 set res {}
3372 proc foo {args} {oninit seek; onfinal; track; return 88}
3373 set c [chan create {r w} foo]
3374 notes [inthread $c {
3375 note [tell $c]
3376 close $c
3377 notes
3378 } c]
3379 rename foo {}
3380 set res
3381 } -result {{seek rc* 0 current} 88} \
3382 -constraints {testchannel thread}
3383 test iocmd.tf-28.8 {chan tell, negative return} -match glob -body {
3384 set res {}
3385 proc foo {args} {oninit seek; onfinal; track; return -1}
3386 set c [chan create {r w} foo]
3387 notes [inthread $c {
3388 note [catch {tell $c} msg]
3389 note $msg
3390 close $c
3391 notes
3392 } c]
3393 rename foo {}
3394 set res
3395 } -result {{seek rc* 0 current} 1 {Tried to seek before origin}} \
3396 -constraints {testchannel thread}
3397 test iocmd.tf-28.9 {chan tell, string return} -match glob -body {
3398 set res {}
3399 proc foo {args} {oninit seek; onfinal; track; return BOGUS}
3400 set c [chan create {r w} foo]
3401 notes [inthread $c {
3402 note [catch {tell $c} msg]
3403 note $msg
3404 close $c
3405 notes
3406 } c]
3407 rename foo {}
3408 set res
3409 } -result {{seek rc* 0 current} 1 {expected integer but got "BOGUS"}} \
3410 -constraints {testchannel thread}
3411 test iocmd.tf-28.10 {chan seek, not supported by handler} -match glob -body {
3412 set res {}
3413 proc foo {args} {oninit; onfinal; track; note MUST_NOT_HAPPEN; return}
3414 set c [chan create {r w} foo]
3415 notes [inthread $c {
3416 note [catch {seek $c 0 start} msg]
3417 note $msg
3418 close $c
3419 notes
3420 } c]
3421 rename foo {}
3422 set res
3423 } -result {1 {error during seek on "rc*": invalid argument}} \
3424 -constraints {testchannel thread}
3425 test iocmd.tf-28.11 {chan seek, error return} -match glob -body {
3426 set res {}
3427 proc foo {args} {oninit seek; onfinal; track; return -code error BOOM!}
3428 set c [chan create {r w} foo]
3429 notes [inthread $c {
3430 note [catch {seek $c 0 start} msg]
3431 note $msg
3432 close $c
3433 notes
3434 } c]
3435 rename foo {}
3436 set res
3437 } -result {{seek rc* 0 start} 1 BOOM!} \
3438 -constraints {testchannel thread}
3439 test iocmd.tf-28.12 {chan seek, break return is error} -match glob -body {
3440 set res {}
3441 proc foo {args} {oninit seek; onfinal; track; return -code break BOOM!}
3442 set c [chan create {r w} foo]
3443 notes [inthread $c {
3444 note [catch {seek $c 0 start} msg]
3445 note $msg
3446 close $c
3447 notes
3448 } c]
3449 rename foo {}
3450 set res
3451 } -result {{seek rc* 0 start} 1 *bad code*} \
3452 -constraints {testchannel thread}
3453 test iocmd.tf-28.13 {chan seek, continue return is error} -match glob -body {
3454 set res {}
3455 proc foo {args} {oninit seek; onfinal; track; return -code continue BOOM!}
3456 set c [chan create {r w} foo]
3457 notes [inthread $c {
3458 note [catch {seek $c 0 start} msg]
3459 note $msg
3460 close $c
3461 notes
3462 } c]
3463 rename foo {}
3464 set res
3465 } -result {{seek rc* 0 start} 1 *bad code*} \
3466 -constraints {testchannel thread}
3467 test iocmd.tf-28.14 {chan seek, custom return is error} -match glob -body {
3468 set res {}
3469 proc foo {args} {oninit seek; onfinal; track; return -code 99 BOOM!}
3470 set c [chan create {r w} foo]
3471 notes [inthread $c {
3472 note [catch {seek $c 0 start} msg]
3473 note $msg
3474 close $c
3475 notes
3476 } c]
3477 rename foo {}
3478 set res
3479 } -result {{seek rc* 0 start} 1 *bad code*} \
3480 -constraints {testchannel thread}
3481 test iocmd.tf-28.15 {chan seek, level is ignored} -match glob -body {
3482 set res {}
3483 proc foo {args} {oninit seek; onfinal; track; return -level 33 -code 99 BANG}
3484 set c [chan create {r w} foo]
3485 notes [inthread $c {
3486 note [catch {seek $c 0 start} msg opt]
3487 note $msg
3488 noteOpts $opt
3489 close $c
3490 notes
3491 } c]
3492 rename foo {}
3493 set res
3494 } -result {{seek rc* 0 start} 1 *bad code* {-code 1 -level 0 -errorcode NONE -errorline 1 -errorinfo *bad code*subcommand "seek"*}} \
3495 -constraints {testchannel thread}
3496 test iocmd.tf-28.16 {chan seek, bogus return, negative location} -match glob -body {
3497 set res {}
3498 proc foo {args} {oninit seek; onfinal; track; return -45}
3499 set c [chan create {r w} foo]
3500 notes [inthread $c {
3501 note [catch {seek $c 0 start} msg]
3502 note $msg
3503 close $c
3504 notes
3505 } c]
3506 rename foo {}
3507 set res
3508 } -result {{seek rc* 0 start} 1 {Tried to seek before origin}} \
3509 -constraints {testchannel thread}
3510 test iocmd.tf-28.17 {chan seek, bogus return, string return} -match glob -body {
3511 set res {}
3512 proc foo {args} {oninit seek; onfinal; track; return BOGUS}
3513 set c [chan create {r w} foo]
3514 notes [inthread $c {
3515 note [catch {seek $c 0 start} msg]
3516 note $msg
3517 close $c
3518 notes
3519 } c]
3520 rename foo {}
3521 set res
3522 } -result {{seek rc* 0 start} 1 {expected integer but got "BOGUS"}} \
3523 -constraints {testchannel thread}
3524 test iocmd.tf-28.18 {chan seek, ok result} -match glob -body {
3525 set res {}
3526 proc foo {args} {oninit seek; onfinal; track; return 23}
3527 set c [chan create {r w} foo]
3528 notes [inthread $c {
3529 note [seek $c 0 current]
3530 close $c
3531 notes
3532 } c]
3533 rename foo {}
3534 set res
3535 } -result {{seek rc* 0 current} {}} \
3536 -constraints {testchannel thread}
3537 foreach {testname code} {
3538 iocmd.tf-28.19.0 start
3539 iocmd.tf-28.19.1 current
3540 iocmd.tf-28.19.2 end
3541 } {
3542 test $testname "chan seek, base conversion, $code" -match glob -body {
3543 set res {}
3544 proc foo {args} {oninit seek; onfinal; track; return 0}
3545 set c [chan create {r w} foo]
3546 notes [inthread $c {
3547 note [seek $c 0 $code]
3548 close $c
3549 notes
3550 } c code]
3551 rename foo {}
3552 set res
3553 } -result [list [list seek rc* 0 $code] {}] \
3554 -constraints {testchannel thread}
3555 }
3556
3557 # --- === *** ###########################
3558 # method blocking
3559
3560 test iocmd.tf-29.1 {chan blocking, no handler support} -match glob -body {
3561 set res {}
3562 proc foo {args} {oninit; onfinal; track; note MUST_NOT_HAPPEN; return}
3563 set c [chan create {r w} foo]
3564 notes [inthread $c {
3565 note [fconfigure $c -blocking]
3566 close $c
3567 notes
3568 } c]
3569 rename foo {}
3570 set res
3571 } -result {1} \
3572 -constraints {testchannel thread}
3573 test iocmd.tf-29.2 {chan blocking, no handler support} -match glob -body {
3574 set res {}
3575 proc foo {args} {oninit; onfinal; track; note MUST_NOT_HAPPEN; return}
3576 set c [chan create {r w} foo]
3577 notes [inthread $c {
3578 note [fconfigure $c -blocking 0]
3579 note [fconfigure $c -blocking]
3580 close $c
3581 notes
3582 } c]
3583 rename foo {}
3584 set res
3585 } -result {{} 0} \
3586 -constraints {testchannel thread}
3587 test iocmd.tf-29.3 {chan blocking, retrieval, handler support} -match glob -body {
3588 set res {}
3589 proc foo {args} {oninit blocking; onfinal; track; note MUST_NOT_HAPPEN; return}
3590 set c [chan create {r w} foo]
3591 notes [inthread $c {
3592 note [fconfigure $c -blocking]
3593 close $c
3594 notes
3595 } c]
3596 rename foo {}
3597 set res
3598 } -result {1} \
3599 -constraints {testchannel thread}
3600 test iocmd.tf-29.4 {chan blocking, resetting, handler support} -match glob -body {
3601 set res {}
3602 proc foo {args} {oninit blocking; onfinal; track; return}
3603 set c [chan create {r w} foo]
3604 notes [inthread $c {
3605 note [fconfigure $c -blocking 0]
3606 note [fconfigure $c -blocking]
3607 close $c
3608 notes
3609 } c]
3610 rename foo {}
3611 set res
3612 } -result {{blocking rc* 0} {} 0} \
3613 -constraints {testchannel thread}
3614 test iocmd.tf-29.5 {chan blocking, setting, handler support} -match glob -body {
3615 set res {}
3616 proc foo {args} {oninit blocking; onfinal; track; return}
3617 set c [chan create {r w} foo]
3618 notes [inthread $c {
3619 note [fconfigure $c -blocking 1]
3620 note [fconfigure $c -blocking]
3621 close $c
3622 notes
3623 } c]
3624 rename foo {}
3625 set res
3626 } -result {{blocking rc* 1} {} 1} \
3627 -constraints {testchannel thread}
3628 test iocmd.tf-29.6 {chan blocking, error return} -match glob -body {
3629 set res {}
3630 proc foo {args} {oninit blocking; onfinal; track; error BOOM!}
3631 set c [chan create {r w} foo]
3632 notes [inthread $c {
3633 note [catch {fconfigure $c -blocking 0} msg]
3634 note $msg
3635 # Catch the close. It changes blocking mode internally, and runs into the error result.
3636 catch {close $c}
3637 notes
3638 } c]
3639 rename foo {}
3640 set res
3641 } -result {{blocking rc* 0} 1 BOOM!} \
3642 -constraints {testchannel thread}
3643 test iocmd.tf-29.7 {chan blocking, break return is error} -match glob -body {
3644 set res {}
3645 proc foo {args} {oninit blocking; onfinal; track; return -code break BOOM!}
3646 set c [chan create {r w} foo]
3647 notes [inthread $c {
3648 note [catch {fconfigure $c -blocking 0} msg]
3649 note $msg
3650 catch {close $c}
3651 notes
3652 } c]
3653 rename foo {}
3654 set res
3655 } -result {{blocking rc* 0} 1 *bad code*} \
3656 -constraints {testchannel thread}
3657 test iocmd.tf-29.8 {chan blocking, continue return is error} -match glob -body {
3658 set res {}
3659 proc foo {args} {oninit blocking; onfinal; track; return -code continue BOOM!}
3660 set c [chan create {r w} foo]
3661 notes [inthread $c {
3662 note [catch {fconfigure $c -blocking 0} msg]
3663 note $msg
3664 catch {close $c}
3665 notes
3666 } c]
3667 rename foo {}
3668 set res
3669 } -result {{blocking rc* 0} 1 *bad code*} \
3670 -constraints {testchannel thread}
3671 test iocmd.tf-29.9 {chan blocking, custom return is error} -match glob -body {
3672 set res {}
3673 proc foo {args} {oninit blocking; onfinal; track; return -code 44 BOOM!}
3674 set c [chan create {r w} foo]
3675 notes [inthread $c {
3676 note [catch {fconfigure $c -blocking 0} msg]
3677 note $msg
3678 catch {close $c}
3679 notes
3680 } c]
3681 rename foo {}
3682 set res
3683 } -result {{blocking rc* 0} 1 *bad code*} \
3684 -constraints {testchannel thread}
3685 test iocmd.tf-29.10 {chan blocking, level is ignored} -match glob -body {
3686 set res {}
3687 proc foo {args} {oninit blocking; onfinal; track; return -level 99 -code 44 BANG}
3688 set c [chan create {r w} foo]
3689 notes [inthread $c {
3690 note [catch {fconfigure $c -blocking 0} msg opt]
3691 note $msg
3692 noteOpts $opt
3693 catch {close $c}
3694 notes
3695 } c]
3696 rename foo {}
3697 set res
3698 } -result {{blocking rc* 0} 1 *bad code* {-code 1 -level 0 -errorcode NONE -errorline 1 -errorinfo *bad code*subcommand "blocking"*}} \
3699 -constraints {testchannel thread}
3700 test iocmd.tf-29.11 {chan blocking, regular return ok, value ignored} -match glob -body {
3701 set res {}
3702 proc foo {args} {oninit blocking; onfinal; track; return BOGUS}
3703 set c [chan create {r w} foo]
3704 notes [inthread $c {
3705 note [catch {fconfigure $c -blocking 0} msg]
3706 note $msg
3707 catch {close $c}
3708 notes
3709 } c]
3710 rename foo {}
3711 set res
3712 } -result {{blocking rc* 0} 0 {}} \
3713 -constraints {testchannel thread}
3714
3715 # --- === *** ###########################
3716 # method watch
3717
3718 test iocmd.tf-30.1 {chan watch, read interest, some return} -match glob -body {
3719 set res {}
3720 proc foo {args} {oninit; onfinal; track; return IGNORED}
3721 set c [chan create {r w} foo]
3722 notes [inthread $c {
3723 note [fileevent $c readable {set tick $tick}]
3724 close $c ;# 2nd watch, interest zero.
3725 notes
3726 } c]
3727 rename foo {}
3728 set res
3729 } -constraints {testchannel thread} -result {{watch rc* read} {watch rc* {}} {}}
3730 test iocmd.tf-30.2 {chan watch, write interest, error return} -match glob -body {
3731 set res {}
3732 proc foo {args} {oninit; onfinal; track; return -code error BOOM!_IGNORED}
3733 set c [chan create {r w} foo]
3734 notes [inthread $c {
3735 note [fileevent $c writable {set tick $tick}]
3736 note [fileevent $c writable {}]
3737 close $c
3738 notes
3739 } c]
3740 rename foo {}
3741 set res
3742 } -constraints {testchannel thread} -result {{watch rc* write} {watch rc* {}} {} {}}
3743 test iocmd.tf-30.3 {chan watch, accumulated interests} -match glob -body {
3744 set res {}
3745 proc foo {args} {oninit; onfinal; track; return}
3746 set c [chan create {r w} foo]
3747 notes [inthread $c {
3748 note [fileevent $c writable {set tick $tick}]
3749 note [fileevent $c readable {set tick $tick}]
3750 note [fileevent $c writable {}]
3751 note [fileevent $c readable {}]
3752 close $c
3753 notes
3754 } c]
3755 rename foo {}
3756 set res
3757 } -constraints {testchannel thread} \
3758 -result {{watch rc* write} {watch rc* {read write}} {watch rc* read} {watch rc* {}} {} {} {} {}}
3759 test iocmd.tf-30.4 {chan watch, unchanged interest not forwarded} -match glob -body {
3760 set res {}
3761 proc foo {args} {oninit; onfinal; track; return}
3762 set c [chan create {r w} foo]
3763 notes [inthread $c {
3764 note [fileevent $c writable {set tick $tick}]
3765 note [fileevent $c readable {set tick $tick}] ;# Script is changing,
3766 note [fileevent $c readable {set tock $tock}] ;# interest does not.
3767 close $c ;# 3rd and 4th watch, removing the event handlers.
3768 notes
3769 } c]
3770 rename foo {}
3771 set res
3772 } -constraints {testchannel thread} \
3773 -result {{watch rc* write} {watch rc* {read write}} {watch rc* write} {watch rc* {}} {} {} {}}
3774
3775 # --- === *** ###########################
3776 # postevent
3777 # Not possible from a thread not containing the command handler.
3778 # Check that this is rejected.
3779
3780 test iocmd.tf-31.8 {chan postevent, bad input} -match glob -body {
3781 set res {}
3782 proc foo {args} {oninit; onfinal; track; return}
3783 set c [chan create {r w} foo]
3784 notes [inthread $c {
3785 catch {chan postevent $c r} msg
3786 note $msg
3787 close $c
3788 notes
3789 } c]
3790 rename foo {}
3791 set res
3792 } -constraints {testchannel thread} \
3793 -result {{can not find reflected channel named "rc*"}}
3794
3795 # --- === *** ###########################
3796 # 'Pull the rug' tests. Create channel in a thread A, move to other
3797 # thread B, destroy the origin thread (A) before or during access from
3798 # B. Must not crash, must return proper errors.
3799
3800 test iocmd.tf-32.0 {origin thread of moved channel gone} -match glob -body {
3801
3802 #puts <<$tcltest::mainThread>>main
3803 set tida [thread::create -preserved];#puts <<$tida>>
3804 thread::send $tida {load {} Tcltest}
3805
3806 set tidb [thread::create -preserved];#puts <<$tidb>>
3807 thread::send $tidb {load {} Tcltest}
3808
3809 # Set up channel in thread
3810 thread::send $tida $helperscript
3811 set chan [thread::send $tida {
3812 proc foo {args} {oninit seek; onfinal; track; return}
3813 set chan [chan create {r w} foo]
3814 fconfigure $chan -buffering none
3815 set chan
3816 }]
3817
3818 # Move channel to 2nd thread.
3819 thread::send $tida [list testchannel cut $chan]
3820 thread::send $tidb [list testchannel splice $chan]
3821
3822 # Kill origin thread, then access channel from 2nd thread.
3823 thread::release $tida
3824
3825 set res {}
3826 lappend res [catch {thread::send $tidb [list puts $chan shoo]} msg] $msg
3827
3828 lappend res [catch {thread::send $tidb [list tell $chan]} msg] $msg
3829 lappend res [catch {thread::send $tidb [list seek $chan 1]} msg] $msg
3830 lappend res [catch {thread::send $tidb [list gets $chan]} msg] $msg
3831 lappend res [catch {thread::send $tidb [list close $chan]} msg] $msg
3832 thread::release $tidb
3833 set res
3834
3835 } -constraints {testchannel thread} \
3836 -result {1 {Owner lost} 1 {Owner lost} 1 {Owner lost} 1 {Owner lost} 1 {Owner lost}}
3837
3838
3839 # The test iocmd.tf-32.1 unavoidably exhibits a memory leak. We are testing
3840 # the ability of the reflected channel system to react to the situation where
3841 # the thread in which the driver routines runs exits during driver operations.
3842 # In this case, thread exit handlers signal back to the owner thread so that the
3843 # channel operation does not hang. There's no way to test this without actually
3844 # exiting a thread in mid-operation, and that action is unavoidably leaky (which
3845 # is why [thread::exit] is advised against).
3846 #
3847 # Use constraints to skip this test while valgrinding so this expected leak
3848 # doesn't prevent a finding of "leak-free".
3849 #
3850 test iocmd.tf-32.1 {origin thread of moved channel destroyed during access} -match glob -body {
3851
3852 #puts <<$tcltest::mainThread>>main
3853 set tida [thread::create -preserved];#puts <<$tida>>
3854 thread::send $tida {load {} Tcltest}
3855 set tidb [thread::create -preserved];#puts <<$tidb>>
3856 thread::send $tidb {load {} Tcltest}
3857
3858 # Set up channel in thread
3859 thread::send $tida $helperscript
3860 set chan [thread::send $tida {
3861 proc foo {args} {
3862 oninit; onfinal; track;
3863 # destroy thread during channel access
3864 thread::exit
3865 }
3866 set chan [chan create {r w} foo]
3867 fconfigure $chan -buffering none
3868 set chan
3869 }]
3870
3871 # Move channel to 2nd thread.
3872 thread::send $tida [list testchannel cut $chan]
3873 thread::send $tidb [list testchannel splice $chan]
3874
3875 # Run access from thread B, wait for response from A (A is not
3876 # using event loop at this point, so the event pile up in the
3877 # queue.
3878
3879 thread::send $tidb [list set chan $chan]
3880 thread::send $tidb [list set mid [thread::id]]
3881 thread::send -async $tidb {
3882 # wait a bit, give the main thread the time to start its event
3883 # loop to wait for the response from B
3884 after 2000
3885 catch { puts $chan shoo } res
3886 thread::send -async $mid [list set ::res $res]
3887 }
3888 vwait ::res
3889
3890 catch {thread::release $tida}
3891 thread::release $tidb
3892 set res
3893 } -constraints {testchannel thread notValgrind} \
3894 -result {Owner lost}
3895
3896 # ### ### ### ######### ######### #########
3897
3898 # ### ### ### ######### ######### #########
3899
3900 rename track {}
3901 # cleanup
3902
3903
3904 # Eliminate valgrind "still reachable" reports on outstanding "Detached"
3905 # structures in the detached list which stem from PipeClose2Proc not waiting
3906 # around for background processes to complete, meaning that previous calls to
3907 # Tcl_ReapDetachedProcs might not have had a chance to reap all processes.
3908 after 10
3909 exec [info nameofexecutable] << {}
3910
3911
3912 foreach file [list test1 test2 test3 test4] {
3913 removeFile $file
3914 }
3915 # delay long enough for background processes to finish
3916 after 500
3917 removeFile test5
3918 cleanupTests
3919 return