Debian Codegraph @

tests/ioTrans.test

raw
1 # -*- tcl -*-
2 # Functionality covered: operation of the reflected transformation
3 #
4 # This file contains a collection of tests for one or more of the Tcl
5 # built-in commands. Sourcing this file into Tcl runs the tests and
6 # generates output for errors. No output means no errors were found.
7 #
8 # Copyright (c) 2007 Andreas Kupries <[email protected]>
9 # <[email protected]>
10 #
11 # See the file "license.terms" for information on usage and redistribution
12 # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
13
14 if {"::tcltest" ni [namespace children]} {
15 package require tcltest 2.5
16 namespace import -force ::tcltest::*
17 }
18
19 ::tcltest::loadTestedCommands
20 catch [list package require -exact Tcltest [info patchlevel]]
21
22 # Custom constraints used in this file
23 testConstraint testchannel [llength [info commands testchannel]]
24 testConstraint thread [expr {0 == [catch {package require Thread 2.7-}]}]
25
26 # testchannel cut|splice Both needed to test the reflection in threads.
27 # thread::send
28
29 #----------------------------------------------------------------------
30
31 # ### ### ### ######### ######### #########
32 ## Testing the reflected transformation.
33
34 # Helper commands to record the arguments to handler methods. Stored in a
35 # script so that the tests needing this code do not need their own copy but
36 # can access this variable.
37
38 set helperscript {
39 if {"::tcltest" ni [namespace children]} {
40 package require tcltest 2.5
41 namespace import -force ::tcltest::*
42 }
43
44 # This forces the return options to be in the order that the test expects!
45 variable optorder {
46 -code !?! -level !?! -errorcode !?! -errorline !?! -errorinfo !?!
47 -errorstack !?!
48 }
49 proc noteOpts opts {
50 variable optorder
51 lappend ::res [dict merge $optorder $opts]
52 }
53
54 # Helper command, canned result for 'initialize' method. Gets the
55 # optional methods as arguments. Use return features to post the result
56 # higher up.
57
58 proc handle.initialize {args} {
59 upvar args hargs
60 if {[lindex $hargs 0] eq "initialize"} {
61 return -code return [list {*}$args initialize finalize read write]
62 }
63 }
64 proc handle.finalize {} {
65 upvar args hargs
66 if {[lindex $hargs 0] eq "finalize"} {
67 return -code return ""
68 }
69 }
70 proc handle.read {} {
71 upvar args hargs
72 if {[lindex $hargs 0] eq "read"} {
73 return -code return "@"
74 }
75 }
76 proc handle.drain {} {
77 upvar args hargs
78 if {[lindex $hargs 0] eq "drain"} {
79 return -code return "<>"
80 }
81 }
82 proc handle.clear {} {
83 upvar args hargs
84 if {[lindex $hargs 0] eq "clear"} {
85 return -code return ""
86 }
87 }
88
89 proc tempchan {{mode r+}} {
90 global tempchan
91 return [set tempchan [open [makeFile {test data} tempchanfile] $mode]]
92 }
93 proc tempdone {} {
94 global tempchan
95 catch {close $tempchan}
96 removeFile tempchanfile
97 return
98 }
99 proc tempview {} { viewFile tempchanfile }
100 }
101
102 # Set everything up in the main thread.
103 eval $helperscript
104
105 #puts <<[file channels]>>
106
107 # ### ### ### ######### ######### #########
108
109 test iortrans-1.0 {chan, wrong#args} -returnCodes error -body {
110 chan
111 } -result {wrong # args: should be "chan subcommand ?arg ...?"}
112 test iortrans-1.1 {chan, unknown method} -returnCodes error -body {
113 chan foo
114 } -match glob -result {unknown or ambiguous subcommand "foo": must be*}
115
116 # --- --- --- --------- --------- ---------
117 # chan push, and method "initialize"
118
119 test iortrans-2.0 {chan push, wrong#args, not enough} -returnCodes error -body {
120 chan push
121 } -result {wrong # args: should be "chan push channel cmdprefix"}
122 test iortrans-2.1 {chan push, wrong#args, too many} -returnCodes error -body {
123 chan push a b c
124 } -result {wrong # args: should be "chan push channel cmdprefix"}
125 test iortrans-2.2 {chan push, invalid channel} -setup {
126 proc foo {} {}
127 } -returnCodes error -body {
128 chan push {} foo
129 } -cleanup {
130 rename foo {}
131 } -result {can not find channel named ""}
132 test iortrans-2.3 {chan push, bad handler, not a list} -body {
133 chan push [tempchan] "foo \{"
134 } -returnCodes error -cleanup {
135 tempdone
136 } -result {unmatched open brace in list}
137 test iortrans-2.4 {chan push, bad handler, not a command} -body {
138 chan push [tempchan] foo
139 } -returnCodes error -cleanup {
140 tempdone
141 } -result {invalid command name "foo"}
142 test iortrans-2.5 {chan push, initialize failed, bad signature} -body {
143 proc foo {} {}
144 chan push [tempchan] foo
145 } -returnCodes error -cleanup {
146 tempdone
147 rename foo {}
148 } -result {wrong # args: should be "foo"}
149 test iortrans-2.6 {chan push, initialize failed, bad signature} -body {
150 proc foo {} {}
151 chan push [tempchan] ::foo
152 } -returnCodes error -cleanup {
153 tempdone
154 rename foo {}
155 } -result {wrong # args: should be "::foo"}
156 test iortrans-2.7 {chan push, initialize failed, bad result, not a list} -body {
157 proc foo {args} {return "\{"}
158 catch {chan push [tempchan] foo}
159 return $::errorInfo
160 } -cleanup {
161 tempdone
162 rename foo {}
163 } -match glob -result {chan handler "foo initialize" returned non-list: *}
164 test iortrans-2.8 {chan push, initialize failed, bad result, not a list} -body {
165 proc foo {args} {return \{\{\}}
166 chan push [tempchan] foo
167 } -returnCodes error -cleanup {
168 tempdone
169 rename foo {}
170 } -match glob -result {chan handler "foo initialize" returned non-list: *}
171 test iortrans-2.9 {chan push, initialize failed, bad result, empty list} -body {
172 proc foo {args} {}
173 chan push [tempchan] foo
174 } -returnCodes error -cleanup {
175 tempdone
176 rename foo {}
177 } -match glob -result {*all required methods*}
178 test iortrans-2.10 {chan push, initialize failed, bad result, bogus method name} -body {
179 proc foo {args} {return 1}
180 chan push [tempchan] foo
181 } -returnCodes error -cleanup {
182 tempdone
183 rename foo {}
184 } -match glob -result {*bad method "1": must be *}
185 test iortrans-2.11 {chan push, initialize failed, bad result, bogus method name} -body {
186 proc foo {args} {return {a b c}}
187 chan push [tempchan] foo
188 } -returnCodes error -cleanup {
189 tempdone
190 rename foo {}
191 } -match glob -result {*bad method "c": must be *}
192 test iortrans-2.12 {chan push, initialize failed, bad result, required methods missing} -body {
193 # Required: initialize, and finalize.
194 proc foo {args} {return {initialize}}
195 chan push [tempchan] foo
196 } -returnCodes error -cleanup {
197 tempdone
198 rename foo {}
199 } -match glob -result {*all required methods*}
200 test iortrans-2.13 {chan push, initialize failed, bad result, illegal method name} -body {
201 proc foo {args} {return {initialize finalize BOGUS}}
202 chan push [tempchan] foo
203 } -returnCodes error -cleanup {
204 tempdone
205 rename foo {}
206 } -match glob -result {*returned bad method "BOGUS": must be clear, drain, finalize, flush, initialize, limit?, read, or write}
207 test iortrans-2.14 {chan push, initialize failed, bad result, mode/handler mismatch} -body {
208 proc foo {args} {return {initialize finalize}}
209 chan push [tempchan] foo
210 } -returnCodes error -cleanup {
211 tempdone
212 rename foo {}
213 } -match glob -result {*makes the channel inaccessible}
214 # iortrans-2.15 event/watch methods elimimated, removed these tests.
215 # iortrans-2.16
216 test iortrans-2.17 {chan push, initialize failed, bad result, drain/read mismatch} -body {
217 proc foo {args} {return {initialize finalize drain write}}
218 chan push [tempchan] foo
219 } -returnCodes error -cleanup {
220 tempdone
221 rename foo {}
222 } -match glob -result {*supports "drain" but not "read"}
223 test iortrans-2.18 {chan push, initialize failed, bad result, flush/write mismatch} -body {
224 proc foo {args} {return {initialize finalize flush read}}
225 chan push [tempchan] foo
226 } -returnCodes error -cleanup {
227 tempdone
228 rename foo {}
229 } -match glob -result {*supports "flush" but not "write"}
230 test iortrans-2.19 {chan push, initialize ok, creates channel} -setup {
231 set res {}
232 } -match glob -body {
233 proc foo {args} {
234 global res
235 lappend res $args
236 if {[lindex $args 0] ne "initialize"} {return}
237 return {initialize finalize drain flush read write}
238 }
239 lappend res [file channel rt*]
240 lappend res [chan push [tempchan] foo]
241 lappend res [close [lindex $res end]]
242 lappend res [file channel rt*]
243 } -cleanup {
244 tempdone
245 rename foo {}
246 } -result {{} {initialize rt* {read write}} file* {drain rt*} {flush rt*} {finalize rt*} {} {}}
247 test iortrans-2.20 {chan push, init failure -> no channel, no finalize} -setup {
248 set res {}
249 } -match glob -body {
250 proc foo {args} {
251 global res
252 lappend res $args
253 return
254 }
255 lappend res [file channel rt*]
256 lappend res [catch {chan push [tempchan] foo} msg] $msg
257 lappend res [file channel rt*]
258 } -cleanup {
259 tempdone
260 rename foo {}
261 } -result {{} {initialize rt* {read write}} 1 {*all required methods*} {}}
262
263 # --- --- --- --------- --------- ---------
264 # method finalize (via close)
265
266 # General note: file channels rt* finds the transform channel, however the
267 # name reported will be that of the underlying base driver, fileXX here. This
268 # actually allows us to see if the whole channel is gone, or only the
269 # transformation, but not the base.
270
271 test iortrans-3.1 {chan finalize, handler destruction has no effect on channel} -setup {
272 set res {}
273 } -match glob -body {
274 proc foo {args} {
275 lappend ::res $args
276 handle.initialize
277 return
278 }
279 lappend res [set c [chan push [tempchan] foo]]
280 rename foo {}
281 lappend res [file channels file*]
282 lappend res [file channels rt*]
283 lappend res [catch {close $c} msg] $msg
284 lappend res [file channels file*]
285 lappend res [file channels rt*]
286 } -cleanup {
287 tempdone
288 } -result {{initialize rt* {read write}} file* file* {} 1 {invalid command name "foo"} {} {}}
289 test iortrans-3.2 {chan finalize, for close} -setup {
290 set res {}
291 } -match glob -body {
292 proc foo {args} {
293 lappend ::res $args
294 handle.initialize
295 return
296 }
297 lappend res [set c [chan push [tempchan] foo]]
298 close $c
299 # Close deleted the channel.
300 lappend res [file channels rt*]
301 # Channel destruction does not kill handler command!
302 lappend res [info command foo]
303 } -cleanup {
304 rename foo {}
305 tempdone
306 } -result {{initialize rt* {read write}} file* {finalize rt*} {} foo}
307 test iortrans-3.3 {chan finalize, for close, error, close error} -setup {
308 set res {}
309 } -match glob -body {
310 proc foo {args} {
311 lappend ::res $args
312 handle.initialize
313 return -code error 5
314 }
315 lappend res [set c [chan push [tempchan] foo]]
316 lappend res [catch {close $c} msg] $msg
317 # Channel is gone despite error.
318 lappend res [file channels rt*]
319 } -cleanup {
320 rename foo {}
321 tempdone
322 } -result {{initialize rt* {read write}} file* {finalize rt*} 1 5 {}}
323 test iortrans-3.4 {chan finalize, for close, error, close error} -setup {
324 set res {}
325 } -match glob -body {
326 proc foo {args} {
327 lappend ::res $args
328 handle.initialize
329 error FOO
330 }
331 lappend res [set c [chan push [tempchan] foo]]
332 lappend res [catch {close $c} msg] $msg $::errorInfo
333 } -cleanup {
334 rename foo {}
335 tempdone
336 } -result {{initialize rt* {read write}} file* {finalize rt*} 1 FOO {FOO
337 *"close $c"}}
338 test iortrans-3.5 {chan finalize, for close, arbitrary result, ignored} -setup {
339 set res {}
340 } -match glob -body {
341 proc foo {args} {
342 lappend ::res $args
343 handle.initialize
344 return SOMETHING
345 }
346 lappend res [set c [chan push [tempchan] foo]]
347 lappend res [catch {close $c} msg] $msg
348 } -cleanup {
349 rename foo {}
350 tempdone
351 } -result {{initialize rt* {read write}} file* {finalize rt*} 0 {}}
352 test iortrans-3.6 {chan finalize, for close, break, close error} -setup {
353 set res {}
354 } -match glob -body {
355 proc foo {args} {
356 lappend ::res $args
357 handle.initialize
358 return -code 3
359 }
360 lappend res [set c [chan push [tempchan] foo]]
361 lappend res [catch {close $c} msg] $msg
362 } -cleanup {
363 rename foo {}
364 tempdone
365 } -result {{initialize rt* {read write}} file* {finalize rt*} 1 *bad code*}
366 test iortrans-3.7 {chan finalize, for close, continue, close error} -setup {
367 set res {}
368 } -match glob -body {
369 proc foo {args} {
370 lappend ::res $args
371 handle.initialize
372 return -code 4
373 }
374 lappend res [set c [chan push [tempchan] foo]]
375 lappend res [catch {close $c} msg] $msg
376 } -cleanup {
377 rename foo {}
378 tempdone
379 } -result {{initialize rt* {read write}} file* {finalize rt*} 1 *bad code*}
380 test iortrans-3.8 {chan finalize, for close, custom code, close error} -setup {
381 set res {}
382 } -match glob -body {
383 proc foo {args} {
384 lappend ::res $args
385 handle.initialize
386 return -code 777 BANG
387 }
388 lappend res [set c [chan push [tempchan] foo]]
389 lappend res [catch {close $c} msg] $msg
390 } -cleanup {
391 rename foo {}
392 tempdone
393 } -result {{initialize rt* {read write}} file* {finalize rt*} 1 *bad code*}
394 test iortrans-3.9 {chan finalize, for close, ignore level, close error} -setup {
395 set res {}
396 } -body {
397 proc foo {args} {
398 lappend ::res $args
399 handle.initialize
400 return -level 5 -code 777 BANG
401 }
402 lappend res [set c [chan push [tempchan] foo]]
403 lappend res [catch {close $c} msg opt] $msg
404 noteOpts $opt
405 } -match glob -cleanup {
406 rename foo {}
407 tempdone
408 } -result {{initialize rt* {read write}} file* {finalize rt*} 1 *bad code* {-code 1 -level 0 -errorcode NONE -errorline 1 -errorinfo *bad code*subcommand "finalize"*}}
409
410 # --- === *** ###########################
411 # method read (via read)
412
413 test iortrans-4.1 {chan read, transform call and return} -setup {
414 set res {}
415 } -match glob -body {
416 proc foo {args} {
417 handle.initialize
418 handle.finalize
419 lappend ::res $args
420 return snarf
421 }
422 set c [chan push [tempchan] foo]
423 lappend res [read $c 10]
424 } -cleanup {
425 tempdone
426 rename foo {}
427 } -result {{read rt* {test data
428 }} snarf}
429 test iortrans-4.2 {chan read, for non-readable channel} -setup {
430 set res {}
431 } -match glob -body {
432 proc foo {args} {
433 handle.initialize
434 handle.finalize
435 lappend ::res $args MUST_NOT_HAPPEN
436 }
437 set c [chan push [tempchan w] foo]
438 lappend res [catch {read $c 2} msg] $msg
439 } -cleanup {
440 tempdone
441 rename foo {}
442 } -result {1 {channel "file*" wasn't opened for reading}}
443 test iortrans-4.3 {chan read, error return} -setup {
444 set res {}
445 } -match glob -body {
446 proc foo {args} {
447 handle.initialize
448 handle.finalize
449 lappend ::res $args
450 return -code error BOOM!
451 }
452 set c [chan push [tempchan] foo]
453 lappend res [catch {read $c 2} msg] $msg
454 } -cleanup {
455 tempdone
456 rename foo {}
457 } -result {{read rt* {test data
458 }} 1 BOOM!}
459 test iortrans-4.4 {chan read, break return is error} -setup {
460 set res {}
461 } -match glob -body {
462 proc foo {args} {
463 handle.initialize
464 handle.finalize
465 lappend ::res $args
466 return -code break BOOM!
467 }
468 set c [chan push [tempchan] foo]
469 lappend res [catch {read $c 2} msg] $msg
470 } -cleanup {
471 tempdone
472 rename foo {}
473 } -result {{read rt* {test data
474 }} 1 *bad code*}
475 test iortrans-4.5 {chan read, continue return is error} -setup {
476 set res {}
477 } -match glob -body {
478 proc foo {args} {
479 handle.initialize
480 handle.finalize
481 lappend ::res $args
482 return -code continue BOOM!
483 }
484 set c [chan push [tempchan] foo]
485 lappend res [catch {read $c 2} msg] $msg
486 } -cleanup {
487 tempdone
488 rename foo {}
489 } -result {{read rt* {test data
490 }} 1 *bad code*}
491 test iortrans-4.6 {chan read, custom return is error} -setup {
492 set res {}
493 } -match glob -body {
494 proc foo {args} {
495 handle.initialize
496 handle.finalize
497 lappend ::res $args
498 return -code 777 BOOM!
499 }
500 set c [chan push [tempchan] foo]
501 lappend res [catch {read $c 2} msg] $msg
502 } -cleanup {
503 tempdone
504 rename foo {}
505 } -result {{read rt* {test data
506 }} 1 *bad code*}
507 test iortrans-4.7 {chan read, level is squashed} -setup {
508 set res {}
509 } -match glob -body {
510 proc foo {args} {
511 handle.initialize
512 handle.finalize
513 lappend ::res $args
514 return -level 55 -code 777 BOOM!
515 }
516 set c [chan push [tempchan] foo]
517 lappend res [catch {read $c 2} msg opt] $msg
518 noteOpts $opt
519 } -cleanup {
520 tempdone
521 rename foo {}
522 } -result {{read rt* {test data
523 }} 1 *bad code* {-code 1 -level 0 -errorcode NONE -errorline 1 -errorinfo *bad code*subcommand "read"*}}
524 test iortrans-4.8 {chan read, read, bug 2921116} -setup {
525 set res {}
526 } -match glob -body {
527 proc foo {fd args} {
528 handle.initialize
529 handle.finalize
530 lappend ::res $args
531 # Kill and recreate transform while it is operating
532 chan pop $fd
533 chan push $fd [list foo $fd]
534 }
535 set c [chan push [set c [tempchan]] [list foo $c]]
536 lappend res [read $c]
537 #lappend res [gets $c]
538 } -cleanup {
539 tempdone
540 rename foo {}
541 } -result {{read rt* {test data
542 }} {}}
543 test iortrans-4.8.1 {chan read, bug 721ec69271} -setup {
544 set res {}
545 } -match glob -body {
546 proc foo {fd args} {
547 handle.initialize
548 handle.finalize
549 lappend ::res $args
550 # Kill and recreate transform while it is operating
551 chan pop $fd
552 chan push $fd [list foo $fd]
553 }
554 set c [chan push [set c [tempchan]] [list foo $c]]
555 chan configure $c -buffersize 2
556 lappend res [read $c]
557 } -cleanup {
558 tempdone
559 rename foo {}
560 } -result {{read rt* te} {read rt* st} {read rt* { d}} {read rt* at} {read rt* {a
561 }} {}}
562 test iortrans-4.8.2 {chan read, bug 721ec69271} -setup {
563 set res {}
564 } -match glob -body {
565 proc foo {fd args} {
566 handle.initialize
567 handle.finalize
568 lappend ::res $args
569 # Kill and recreate transform while it is operating
570 chan pop $fd
571 chan push $fd [list foo $fd]
572 return x
573 }
574 set c [chan push [set c [tempchan]] [list foo $c]]
575 chan configure $c -buffersize 1
576 lappend res [read $c]
577 } -cleanup {
578 tempdone
579 rename foo {}
580 } -result {{read rt* t} {read rt* e} {read rt* s} {read rt* t} {read rt* { }} {read rt* d} {read rt* a} {read rt* t} {read rt* a} {read rt* {
581 }} {}}
582 test iortrans-4.9 {chan read, gets, bug 2921116} -setup {
583 set res {}
584 } -match glob -body {
585 proc foo {fd args} {
586 handle.initialize
587 handle.finalize
588 lappend ::res $args
589 # Kill and recreate transform while it is operating
590 chan pop $fd
591 chan push $fd [list foo $fd]
592 }
593 set c [chan push [set c [tempchan]] [list foo $c]]
594 lappend res [gets $c]
595 } -cleanup {
596 tempdone
597 rename foo {}
598 } -result {{read rt* {test data
599 }} {}}
600
601 # Driver for a base channel that emits several short "files"
602 # with each terminated by a fleeting EOF
603 proc driver {cmd args} {
604 variable ::tcl::buffer
605 variable ::tcl::index
606 set chan [lindex $args 0]
607 switch -- $cmd {
608 initialize {
609 set index($chan) 0
610 set buffer($chan) .....
611 return {initialize finalize watch read}
612 }
613 finalize {
614 if {![info exists index($chan)]} {return}
615 unset index($chan) buffer($chan)
616 array unset index
617 array unset buffer
618 return
619 }
620 watch {}
621 read {
622 set n [lindex $args 1]
623 if {![info exists index($chan)]} {
624 driver initialize $chan
625 }
626 set new [expr {$index($chan) + $n}]
627 set result [string range $buffer($chan) $index($chan) $new-1]
628 set index($chan) $new
629 if {[string length $result] == 0} {
630 driver finalize $chan
631 }
632 return $result
633 }
634 }
635 }
636
637
638
639 namespace eval reflector {
640 proc initialize {_ chan mode} {
641 return {initialize finalize watch read}
642 }
643
644
645 proc finalize {_ chan} {
646 foreach id [after info] {
647 after cancel $id
648 }
649 namespace delete $_
650 }
651
652
653 proc read {_ chan count} {
654 namespace upvar $_ source source
655 set res [string range $source 0 $count-1]
656 set source [string range $source $count end]
657 return $res
658 }
659
660
661 proc watch {_ chan events} {
662 after 0 [list chan postevent $chan read]
663 return read
664 }
665
666 namespace ensemble create -parameters _
667 namespace export *
668 }
669
670
671
672
673 namespace eval inputfilter {
674 proc initialize {chan mode} {
675 return {initialize finalize read}
676 }
677
678 proc read {chan buffer} {
679 return $buffer
680 }
681
682 proc finalize chan {
683 namespace delete $chan
684 }
685
686 namespace ensemble create
687 namespace export *
688 }
689
690
691
692 # Channel read transform that is just the identity - pass all through
693 proc idxform {cmd handle args} {
694 switch -- $cmd {
695 initialize {
696 return {initialize finalize read}
697 }
698 finalize {
699 return
700 }
701 read {
702 lassign $args buffer
703 return $buffer
704 }
705 }
706 }
707
708 # Test that all EOFs pass through full xform stack. Proper data boundaries.
709 # Check robustness against buffer sizes.
710 test iortrans-4.10 {[5adbc350683] chan read, handle fleeting EOF} -body {
711 set chan [chan push [chan create read driver] idxform]
712 list [eof $chan] [read $chan] [eof $chan] [read $chan 0] [eof $chan] \
713 [read $chan] [eof $chan]
714 } -cleanup {
715 close $chan
716 } -result {0 ..... 1 {} 0 ..... 1}
717 test iortrans-4.10.1 {[5adbc350683] chan read, handle fleeting EOF} -body {
718 set chan [chan push [chan create read driver] idxform]
719 chan configure $chan -buffersize 3
720 list [eof $chan] [read $chan] [eof $chan] [read $chan 0] [eof $chan] \
721 [read $chan] [eof $chan]
722 } -cleanup {
723 close $chan
724 } -result {0 ..... 1 {} 0 ..... 1}
725 test iortrans-4.10.2 {[5adbc350683] chan read, handle fleeting EOF} -body {
726 set chan [chan push [chan create read driver] idxform]
727 chan configure $chan -buffersize 5
728 list [eof $chan] [read $chan] [eof $chan] [read $chan 0] [eof $chan] \
729 [read $chan] [eof $chan]
730 } -cleanup {
731 close $chan
732 } -result {0 ..... 1 {} 0 ..... 1}
733
734 rename idxform {}
735
736 # Channel read transform that delays the data and always returns something
737 proc delayxform {cmd handle args} {
738 variable store
739 switch -- $cmd {
740 initialize {
741 set store($handle) {}
742 return {initialize finalize read drain}
743 }
744 finalize {
745 unset store($handle)
746 return
747 }
748 read {
749 lassign $args buffer
750 if {$store($handle) eq {}} {
751 set reply [string index $buffer 0]
752 set store($handle) [string range $buffer 1 end]
753 } else {
754 set reply $store($handle)
755 set store($handle) $buffer
756 }
757 return $reply
758 }
759 drain {
760 delayxform read $handle {}
761 }
762 }
763 }
764
765 # Test that all EOFs pass through full xform stack. Proper data boundaries.
766 # Check robustness against buffer sizes.
767 test iortrans-4.11 {[5adbc350683] chan read, handle fleeting EOF} -body {
768 set chan [chan push [chan create read driver] delayxform]
769 list [eof $chan] [read $chan] [eof $chan] [read $chan 0] [eof $chan] \
770 [read $chan] [eof $chan]
771 } -cleanup {
772 close $chan
773 } -result {0 ..... 1 {} 0 ..... 1}
774 test iortrans-4.11.1 {[5adbc350683] chan read, handle fleeting EOF} -body {
775 set chan [chan push [chan create read driver] delayxform]
776 chan configure $chan -buffersize 3
777 list [eof $chan] [read $chan] [eof $chan] [read $chan 0] [eof $chan] \
778 [read $chan] [eof $chan]
779 } -cleanup {
780 close $chan
781 } -result {0 ..... 1 {} 0 ..... 1}
782 test iortrans-4.11.2 {[5adbc350683] chan read, handle fleeting EOF} -body {
783 set chan [chan push [chan create read driver] delayxform]
784 chan configure $chan -buffersize 5
785 list [eof $chan] [read $chan] [eof $chan] [read $chan 0] [eof $chan] \
786 [read $chan] [eof $chan]
787 } -cleanup {
788 close $chan
789 } -result {0 ..... 1 {} 0 ..... 1}
790
791 rename delayxform {}
792
793 # Channel read transform that delays the data and may return {}
794 proc delay2xform {cmd handle args} {
795 variable store
796 switch -- $cmd {
797 initialize {
798 set store($handle) {}
799 return {initialize finalize read drain}
800 }
801 finalize {
802 unset store($handle)
803 return
804 }
805 read {
806 lassign $args buffer
807 set reply $store($handle)
808 set store($handle) $buffer
809 return $reply
810 }
811 drain {
812 delay2xform read $handle {}
813 }
814 }
815 }
816
817 test iortrans-4.12 {[5adbc350683] chan read, handle fleeting EOF} -body {
818 set chan [chan push [chan create read driver] delay2xform]
819 list [eof $chan] [read $chan] [eof $chan] [read $chan 0] [eof $chan] \
820 [read $chan] [eof $chan]
821 } -cleanup {
822 close $chan
823 } -result {0 ..... 1 {} 0 ..... 1}
824
825 rename delay2xform {}
826 rename driver {}
827
828
829 # --- === *** ###########################
830 # method write (via puts)
831
832 test iortrans-5.1 {chan write, regular write} -setup {
833 set res {}
834 } -match glob -body {
835 proc foo {args} {
836 handle.initialize
837 handle.finalize
838 lappend ::res $args
839 return transformresult
840 }
841 set c [chan push [tempchan] foo]
842 puts -nonewline $c snarf
843 flush $c
844 close $c
845 lappend res [tempview]
846 } -cleanup {
847 tempdone
848 rename foo {}
849 } -result {{write rt* snarf} transformresult}
850 test iortrans-5.2 {chan write, no write is ok, no change to file} -setup {
851 set res {}
852 } -match glob -body {
853 proc foo {args} {
854 handle.initialize
855 handle.finalize
856 lappend ::res $args
857 return
858 }
859 set c [chan push [tempchan] foo]
860 puts -nonewline $c snarfsnarfsnarf
861 flush $c
862 close $c
863 lappend res [tempview]; # This has to show the original data, as nothing was written
864 } -cleanup {
865 tempdone
866 rename foo {}
867 } -result {{write rt* snarfsnarfsnarf} {test data}}
868 test iortrans-5.3 {chan write, failed write} -setup {
869 set res {}
870 } -match glob -body {
871 proc foo {args} {
872 handle.initialize
873 handle.finalize
874 lappend ::res $args
875 return -code error FAIL!
876 }
877 set c [chan push [tempchan] foo]
878 puts -nonewline $c snarfsnarfsnarf
879 lappend res [catch {flush $c} msg] $msg
880 } -cleanup {
881 tempdone
882 rename foo {}
883 } -result {{write rt* snarfsnarfsnarf} 1 FAIL!}
884 test iortrans-5.4 {chan write, non-writable channel} -setup {
885 set res {}
886 } -match glob -body {
887 proc foo {args} {
888 handle.initialize
889 handle.finalize
890 lappend ::res $args MUST_NOT_HAPPEN
891 return
892 }
893 set c [chan push [tempchan r] foo]
894 lappend res [catch {
895 puts -nonewline $c snarfsnarfsnarf
896 flush $c
897 } msg] $msg
898 } -cleanup {
899 close $c
900 tempdone
901 rename foo {}
902 } -result {1 {channel "file*" wasn't opened for writing}}
903 test iortrans-5.5 {chan write, failed write, error return} -setup {
904 set res {}
905 } -match glob -body {
906 proc foo {args} {
907 handle.initialize
908 handle.finalize
909 lappend ::res $args
910 return -code error BOOM!
911 }
912 set c [chan push [tempchan] foo]
913 lappend res [catch {
914 puts -nonewline $c snarfsnarfsnarf
915 flush $c
916 } msg] $msg
917 } -cleanup {
918 tempdone
919 rename foo {}
920 } -result {{write rt* snarfsnarfsnarf} 1 BOOM!}
921 test iortrans-5.6 {chan write, failed write, error return} -setup {
922 set res {}
923 } -match glob -body {
924 proc foo {args} {
925 handle.initialize
926 handle.finalize
927 lappend ::res $args
928 error BOOM!
929 }
930 set c [chan push [tempchan] foo]
931 lappend res {*}[catch {
932 puts -nonewline $c snarfsnarfsnarf
933 flush $c
934 } msg] $msg
935 } -cleanup {
936 tempdone
937 rename foo {}
938 } -result {{write rt* snarfsnarfsnarf} 1 BOOM!}
939 test iortrans-5.7 {chan write, failed write, break return is error} -setup {
940 set res {}
941 } -match glob -body {
942 proc foo {args} {
943 handle.initialize
944 handle.finalize
945 lappend ::res $args
946 return -code break BOOM!
947 }
948 set c [chan push [tempchan] foo]
949 lappend res [catch {
950 puts -nonewline $c snarfsnarfsnarf
951 flush $c
952 } msg] $msg
953 } -cleanup {
954 tempdone
955 rename foo {}
956 } -result {{write rt* snarfsnarfsnarf} 1 *bad code*}
957 test iortrans-5.8 {chan write, failed write, continue return is error} -setup {
958 set res {}
959 } -match glob -body {
960 proc foo {args} {
961 handle.initialize
962 handle.finalize
963 lappend ::res $args
964 return -code continue BOOM!
965 }
966 set c [chan push [tempchan] foo]
967 lappend res [catch {
968 puts -nonewline $c snarfsnarfsnarf
969 flush $c
970 } msg] $msg
971 } -cleanup {
972 tempdone
973 rename foo {}
974 } -result {{write rt* snarfsnarfsnarf} 1 *bad code*}
975 test iortrans-5.9 {chan write, failed write, custom return is error} -setup {
976 set res {}
977 } -match glob -body {
978 proc foo {args} {
979 handle.initialize
980 handle.finalize
981 lappend ::res $args
982 return -code 777 BOOM!
983 }
984 set c [chan push [tempchan] foo]
985 lappend res [catch {
986 puts -nonewline $c snarfsnarfsnarf
987 flush $c
988 } msg] $msg
989 } -cleanup {
990 tempdone
991 rename foo {}
992 } -result {{write rt* snarfsnarfsnarf} 1 *bad code*}
993 test iortrans-5.10 {chan write, failed write, level is ignored} -setup {
994 set res {}
995 } -match glob -body {
996 proc foo {args} {
997 handle.initialize
998 handle.finalize
999 lappend ::res $args
1000 return -level 55 -code 777 BOOM!
1001 }
1002 set c [chan push [tempchan] foo]
1003 lappend res [catch {
1004 puts -nonewline $c snarfsnarfsnarf
1005 flush $c
1006 } msg opt] $msg
1007 noteOpts $opt
1008 } -cleanup {
1009 tempdone
1010 rename foo {}
1011 } -result {{write rt* snarfsnarfsnarf} 1 *bad code* {-code 1 -level 0 -errorcode NONE -errorline * -errorinfo *bad code*subcommand "write"*}}
1012 test iortrans-5.11 {chan write, bug 2921116} -match glob -setup {
1013 set res {}
1014 set level 0
1015 } -body {
1016 proc foo {fd args} {
1017 handle.initialize
1018 handle.finalize
1019 lappend ::res $args
1020 # pop - invokes flush - invokes 'foo write' - infinite recursion - stop it
1021 global level
1022 if {$level} {
1023 return
1024 }
1025 incr level
1026 # Kill and recreate transform while it is operating
1027 chan pop $fd
1028 chan push $fd [list foo $fd]
1029 }
1030 set c [chan push [set c [tempchan]] [list foo $c]]
1031 lappend res [puts -nonewline $c abcdef]
1032 lappend res [flush $c]
1033 } -cleanup {
1034 tempdone
1035 rename foo {}
1036 } -result {{} {write rt* abcdef} {write rt* abcdef} {}}
1037
1038 # --- === *** ###########################
1039 # method limit?, drain (via read)
1040
1041 test iortrans-6.1 {chan read, read limits} -setup {
1042 set res {}
1043 } -match glob -body {
1044 proc foo {args} {
1045 handle.initialize limit?
1046 handle.finalize
1047 lappend ::res $args
1048 handle.read
1049 return 6
1050 }
1051 set c [chan push [tempchan] foo]
1052 lappend res [read $c 10]
1053 } -cleanup {
1054 tempdone
1055 rename foo {}
1056 } -result {{limit? rt*} {read rt* {test d}} {limit? rt*} {read rt* {ata
1057 }} {limit? rt*} @@}
1058 test iortrans-6.2 {chan read, read transform drain on eof} -setup {
1059 set res {}
1060 } -match glob -body {
1061 proc foo {args} {
1062 handle.initialize drain
1063 handle.finalize
1064 lappend ::res $args
1065 handle.read
1066 handle.drain
1067 return
1068 }
1069 set c [chan push [tempchan] foo]
1070 lappend res [read $c]
1071 lappend res [close $c]
1072 } -cleanup {
1073 tempdone
1074 rename foo {}
1075 } -result {{read rt* {test data
1076 }} {drain rt*} @<> {}}
1077
1078 # --- === *** ###########################
1079 # method clear (via puts, seek)
1080
1081 test iortrans-7.1 {chan write, write clears read buffers} -setup {
1082 set res {}
1083 } -match glob -body {
1084 proc foo {args} {
1085 handle.initialize clear
1086 handle.finalize
1087 lappend ::res $args
1088 handle.clear
1089 return transformresult
1090 }
1091 set c [chan push [tempchan] foo]
1092 puts -nonewline $c snarf
1093 flush $c
1094 return $res
1095 } -cleanup {
1096 tempdone
1097 rename foo {}
1098 } -result {{clear rt*} {write rt* snarf}}
1099 test iortrans-7.2 {seek clears read buffers} -setup {
1100 set res {}
1101 } -match glob -body {
1102 proc foo {args} {
1103 handle.initialize clear
1104 handle.finalize
1105 lappend ::res $args
1106 return
1107 }
1108 set c [chan push [tempchan] foo]
1109 seek $c 2
1110 return $res
1111 } -cleanup {
1112 tempdone
1113 rename foo {}
1114 } -result {{clear rt*}}
1115 test iortrans-7.3 {clear, any result is ignored} -setup {
1116 set res {}
1117 } -match glob -body {
1118 proc foo {args} {
1119 handle.initialize clear
1120 handle.finalize
1121 lappend ::res $args
1122 return -code error "X"
1123 }
1124 set c [chan push [tempchan] foo]
1125 seek $c 2
1126 return $res
1127 } -cleanup {
1128 tempdone
1129 rename foo {}
1130 } -result {{clear rt*}}
1131 test iortrans-7.4 {chan clear, bug 2921116} -match glob -setup {
1132 set res {}
1133 } -body {
1134 proc foo {fd args} {
1135 handle.initialize clear
1136 handle.finalize
1137 lappend ::res $args
1138 # Kill and recreate transform while it is operating
1139 chan pop $fd
1140 chan push $fd [list foo $fd]
1141 }
1142 set c [chan push [set c [tempchan]] [list foo $c]]
1143 seek $c 2
1144 return $res
1145 } -cleanup {
1146 tempdone
1147 rename foo {}
1148 } -result {{clear rt*}}
1149
1150 # --- === *** ###########################
1151 # method flush (via seek, close)
1152
1153 test iortrans-8.1 {seek flushes write buffers, ignores data} -setup {
1154 set res {}
1155 } -match glob -body {
1156 proc foo {args} {
1157 handle.initialize flush
1158 handle.finalize
1159 lappend ::res $args
1160 return X
1161 }
1162 set c [chan push [tempchan] foo]
1163 # Flush, no writing
1164 seek $c 2
1165 # The close flushes again, this modifies the file!
1166 lappend res |
1167 lappend res [close $c] | [tempview]
1168 } -cleanup {
1169 tempdone
1170 rename foo {}
1171 } -result {{flush rt*} | {flush rt*} {} | {teXt data}}
1172 test iortrans-8.2 {close flushes write buffers, writes data} -setup {
1173 set res {}
1174 } -match glob -body {
1175 proc foo {args} {
1176 handle.initialize flush
1177 lappend ::res $args
1178 handle.finalize
1179 return .flushed.
1180 }
1181 set c [chan push [tempchan] foo]
1182 close $c
1183 lappend res [tempview]
1184 } -cleanup {
1185 tempdone
1186 rename foo {}
1187 } -result {{flush rt*} {finalize rt*} .flushed.}
1188 test iortrans-8.3 {chan flush, bug 2921116} -match glob -setup {
1189 set res {}
1190 } -body {
1191 proc foo {fd args} {
1192 handle.initialize flush
1193 handle.finalize
1194 lappend ::res $args
1195 # Kill and recreate transform while it is operating
1196 chan pop $fd
1197 chan push $fd [list foo $fd]
1198 }
1199 set c [chan push [set c [tempchan]] [list foo $c]]
1200 seek $c 2
1201 set res
1202 } -cleanup {
1203 tempdone
1204 rename foo {}
1205 } -result {{flush rt*}}
1206
1207 # --- === *** ###########################
1208 # method watch - removed from TIP (rev 1.12+)
1209
1210 # --- === *** ###########################
1211 # method event - removed from TIP (rev 1.12+)
1212
1213 # --- === *** ###########################
1214 # 'Pull the rug' tests. Create channel in a interpreter A, move to other
1215 # interpreter B, destroy the origin interpreter (A) before or during access
1216 # from B. Must not crash, must return proper errors.
1217 test iortrans-11.0 {origin interpreter of moved transform gone} -setup {
1218 set ida [interp create]; #puts <<$ida>>
1219 set idb [interp create]; #puts <<$idb>>
1220 # Magic to get the test* commands in the children
1221 load {} Tcltest $ida
1222 load {} Tcltest $idb
1223 } -constraints {testchannel} -match glob -body {
1224 # Set up channel and transform in interpreter
1225 interp eval $ida $helperscript
1226 interp eval $ida [list ::variable tempchan [tempchan]]
1227 interp transfer {} $::tempchan $ida
1228 set chan [interp eval $ida {
1229 variable tempchan
1230 proc foo {args} {
1231 handle.initialize clear drain flush limit? read write
1232 handle.finalize
1233 lappend ::res $args
1234 return
1235 }
1236 set chan [chan push $tempchan foo]
1237 fconfigure $chan -buffering none
1238 set chan
1239 }]
1240 # Move channel to 2nd interpreter, transform goes with it.
1241 interp eval $ida [list testchannel cut $chan]
1242 interp eval $idb [list testchannel splice $chan]
1243 # Kill origin interpreter, then access channel from 2nd interpreter.
1244 interp delete $ida
1245 set res {}
1246 lappend res \
1247 [catch {interp eval $idb [list puts $chan shoo]} msg] $msg \
1248 [catch {interp eval $idb [list tell $chan]} msg] $msg \
1249 [catch {interp eval $idb [list seek $chan 1]} msg] $msg \
1250 [catch {interp eval $idb [list gets $chan]} msg] $msg \
1251 [catch {interp eval $idb [list close $chan]} msg] $msg
1252 #lappend res [interp eval $ida {set res}]
1253 # actions: clear|write|clear|write|clear|flush|limit?|drain|flush
1254 # The 'tell' is ok, as it passed through the transform to the base channel
1255 # without invoking the transform handler.
1256 } -cleanup {
1257 tempdone
1258 interp delete $idb
1259 } -result {1 {Owner lost} 0 0 1 {Owner lost} 1 {Owner lost} 1 {Owner lost}}
1260 test iortrans-11.1 {origin interpreter of moved transform destroyed during access} -setup {
1261 set ida [interp create]; #puts <<$ida>>
1262 set idb [interp create]; #puts <<$idb>>
1263 # Magic to get the test* commands in the children
1264 load {} Tcltest $ida
1265 load {} Tcltest $idb
1266 } -constraints {testchannel} -match glob -body {
1267 # Set up channel in thread
1268 set chan [interp eval $ida $helperscript]
1269 interp eval $ida [list ::variable tempchan [tempchan]]
1270 interp transfer {} $::tempchan $ida
1271 set chan [interp eval $ida {
1272 proc foo {args} {
1273 handle.initialize clear drain flush limit? read write
1274 handle.finalize
1275 lappend ::res $args
1276 # Destroy interpreter during channel access.
1277 suicide
1278 }
1279 set chan [chan push $tempchan foo]
1280 fconfigure $chan -buffering none
1281 set chan
1282 }]
1283 interp alias $ida suicide {} interp delete $ida
1284 # Move channel to 2nd thread, transform goes with it.
1285 interp eval $ida [list testchannel cut $chan]
1286 interp eval $idb [list testchannel splice $chan]
1287 # Run access from interpreter B, this will give us a synchronous response.
1288 interp eval $idb [list set chan $chan]
1289 interp eval $idb [list set mid $tcltest::mainThread]
1290 set res [interp eval $idb {
1291 # Wait a bit, give the main thread the time to start its event loop to
1292 # wait for the response from B
1293 after 50
1294 catch { puts $chan shoo } res
1295 set res
1296 }]
1297 } -cleanup {
1298 interp delete $idb
1299 tempdone
1300 } -result {Owner lost}
1301 test iortrans-11.2 {delete interp of reflected transform} -setup {
1302 interp create child
1303 # Magic to get the test* commands into the child
1304 load {} Tcltest child
1305 } -constraints {testchannel} -body {
1306 # Get base channel into the child
1307 set c [tempchan]
1308 testchannel cut $c
1309 interp eval child [list testchannel splice $c]
1310 interp eval child [list set c $c]
1311 child eval {
1312 proc no-op args {}
1313 proc driver {c sub args} {
1314 return {initialize finalize read write}
1315 }
1316 set t [chan push $c [list driver $c]]
1317 chan event $c readable no-op
1318 }
1319 interp delete child
1320 } -cleanup {
1321 tempdone
1322 } -result {}
1323
1324 # ### ### ### ######### ######### #########
1325 ## Same tests as above, but exercising the code forwarding and receiving
1326 ## driver operations to the originator thread.
1327
1328 # ### ### ### ######### ######### #########
1329 ## Testing the reflected channel (Thread forwarding).
1330 #
1331 ## The id numbers refer to the original test without thread forwarding, and
1332 ## gaps due to tests not applicable to forwarding are left to keep this
1333 ## association.
1334
1335 # ### ### ### ######### ######### #########
1336 ## Helper command. Runs a script in a separate thread and returns the result.
1337 ## A channel is transferred into the thread as well, and a list of configuration
1338 ## variables
1339
1340 proc inthread {chan script args} {
1341 # Test thread.
1342 set tid [thread::create -preserved]
1343 thread::send $tid {load {} Tcltest}
1344
1345 # Init thread configuration.
1346 # - Listed variables
1347 # - Id of main thread
1348 # - A number of helper commands
1349
1350 foreach v $args {
1351 upvar 1 $v x
1352 thread::send $tid [list set $v $x]
1353 }
1354 thread::send $tid [list set mid [thread::id]]
1355 thread::send $tid {
1356 proc notes {} {
1357 return $::notes
1358 }
1359 proc noteOpts opts {
1360 lappend ::notes [dict merge {
1361 -code !?! -level !?! -errorcode !?! -errorline !?!
1362 -errorinfo !?! -errorstack !?!
1363 } $opts]
1364 }
1365 }
1366 thread::send $tid [list proc s {} [list uplevel 1 $script]]; # (*)
1367
1368 # Transfer channel (cut/splice aka detach/attach)
1369
1370 testchannel cut $chan
1371 thread::send $tid [list testchannel splice $chan]
1372
1373 # Run test script, also run local event loop! The local event loop waits
1374 # for the result to come back. It is also necessary for the execution of
1375 # forwarded channel operations.
1376
1377 set ::tres ""
1378 thread::send -async $tid {
1379 after 50
1380 catch {s} res; # This runs the script, 's' was defined at (*)
1381 thread::send -async $mid [list set ::tres $res]
1382 }
1383 vwait ::tres
1384 # Remove test thread, and return the captured result.
1385
1386 thread::release $tid
1387 return $::tres
1388 }
1389
1390 # ### ### ### ######### ######### #########
1391
1392 test iortrans.tf-3.2 {chan finalize, for close} -setup {
1393 set res {}
1394 } -constraints {testchannel thread} -match glob -body {
1395 proc foo {args} {
1396 lappend ::res $args
1397 handle.initialize
1398 return {}
1399 }
1400 lappend res [set c [chan push [tempchan] foo]]
1401 lappend res [inthread $c {
1402 close $c
1403 # Close the deleted the channel.
1404 file channels rt*
1405 } c]
1406 # Channel destruction does not kill handler command!
1407 lappend res [info command foo]
1408 } -cleanup {
1409 rename foo {}
1410 } -result {{initialize rt* {read write}} file* {finalize rt*} {} foo}
1411 test iortrans.tf-3.3 {chan finalize, for close, error, close error} -setup {
1412 set res {}
1413 } -constraints {testchannel thread} -match glob -body {
1414 proc foo {args} {
1415 lappend ::res $args
1416 handle.initialize
1417 return -code error 5
1418 }
1419 lappend res [set c [chan push [tempchan] foo]]
1420 lappend res {*}[inthread $c {
1421 lappend notes [catch {close $c} msg] $msg
1422 # Channel is gone despite error.
1423 lappend notes [file channels rt*]
1424 notes
1425 } c]
1426 } -cleanup {
1427 rename foo {}
1428 } -result {{initialize rt* {read write}} file* {finalize rt*} 1 5 {}}
1429 test iortrans.tf-3.4 {chan finalize, for close, error, close errror} -setup {
1430 set res {}
1431 } -constraints {testchannel thread} -body {
1432 proc foo {args} {
1433 lappend ::res $args
1434 handle.initialize
1435 error FOO
1436 }
1437 lappend res [set c [chan push [tempchan] foo]]
1438 lappend res {*}[inthread $c {
1439 lappend notes [catch {close $c} msg] $msg
1440 notes
1441 } c]
1442 } -match glob -cleanup {
1443 rename foo {}
1444 } -result {{initialize rt* {read write}} file* {finalize rt*} 1 FOO}
1445 test iortrans.tf-3.5 {chan finalize, for close, arbitrary result} -setup {
1446 set res {}
1447 } -constraints {testchannel thread} -match glob -body {
1448 proc foo {args} {
1449 lappend ::res $args
1450 handle.initialize
1451 return SOMETHING
1452 }
1453 lappend res [set c [chan push [tempchan] foo]]
1454 lappend res {*}[inthread $c {
1455 lappend notes [catch {close $c} msg] $msg
1456 notes
1457 } c]
1458 } -cleanup {
1459 rename foo {}
1460 } -result {{initialize rt* {read write}} file* {finalize rt*} 0 {}}
1461 test iortrans.tf-3.6 {chan finalize, for close, break, close error} -setup {
1462 set res {}
1463 } -constraints {testchannel thread} -match glob -body {
1464 proc foo {args} {
1465 lappend ::res $args
1466 handle.initialize
1467 return -code 3
1468 }
1469 lappend res [set c [chan push [tempchan] foo]]
1470 lappend res {*}[inthread $c {
1471 lappend notes [catch {close $c} msg] $msg
1472 notes
1473 } c]
1474 } -cleanup {
1475 rename foo {}
1476 } -result {{initialize rt* {read write}} file* {finalize rt*} 1 *bad code*}
1477 test iortrans.tf-3.7 {chan finalize, for close, continue, close error} -setup {
1478 set res {}
1479 } -constraints {testchannel thread} -match glob -body {
1480 proc foo {args} {
1481 lappend ::res $args
1482 handle.initialize
1483 return -code 4
1484 }
1485 lappend res [set c [chan push [tempchan] foo]]
1486 lappend res {*}[inthread $c {
1487 lappend notes [catch {close $c} msg] $msg
1488 notes
1489 } c]
1490 } -cleanup {
1491 rename foo {}
1492 } -result {{initialize rt* {read write}} file* {finalize rt*} 1 *bad code*}
1493 test iortrans.tf-3.8 {chan finalize, for close, custom code, close error} -setup {
1494 set res {}
1495 } -constraints {testchannel thread} -match glob -body {
1496 proc foo {args} {
1497 lappend ::res $args
1498 handle.initialize
1499 return -code 777 BANG
1500 }
1501 lappend res [set c [chan push [tempchan] foo]]
1502 lappend res {*}[inthread $c {
1503 lappend notes [catch {close $c} msg] $msg
1504 notes
1505 } c]
1506 } -cleanup {
1507 rename foo {}
1508 } -result {{initialize rt* {read write}} file* {finalize rt*} 1 *bad code*}
1509 test iortrans.tf-3.9 {chan finalize, for close, ignore level, close error} -setup {
1510 set res {}
1511 } -constraints {testchannel thread} -match glob -body {
1512 proc foo {args} {
1513 lappend ::res $args
1514 handle.initialize
1515 return -level 5 -code 777 BANG
1516 }
1517 lappend res [set c [chan push [tempchan] foo]]
1518 lappend res {*}[inthread $c {
1519 lappend notes [catch {close $c} msg opt] $msg
1520 noteOpts $opt
1521 notes
1522 } c]
1523 } -cleanup {
1524 rename foo {}
1525 } -result {{initialize rt* {read write}} file* {finalize rt*} 1 *bad code* {-code 1 -level 0 -errorcode NONE -errorline 1 -errorinfo *bad code*subcommand "finalize"*}}
1526
1527 # --- === *** ###########################
1528 # method read
1529
1530 test iortrans.tf-4.1 {chan read, transform call and return} -setup {
1531 set res {}
1532 } -constraints {testchannel thread} -body {
1533 proc foo {args} {
1534 handle.initialize
1535 handle.finalize
1536 lappend ::res $args
1537 return snarf
1538 }
1539 set c [chan push [tempchan] foo]
1540 lappend res {*}[inthread $c {
1541 lappend notes [read $c 10]
1542 close $c
1543 notes
1544 } c]
1545 } -cleanup {
1546 tempdone
1547 rename foo {}
1548 } -match glob -result {{read rt* {test data
1549 }} snarf}
1550 test iortrans.tf-4.2 {chan read, for non-readable channel} -setup {
1551 set res {}
1552 } -constraints {testchannel thread} -body {
1553 proc foo {args} {
1554 handle.initialize
1555 handle.finalize
1556 lappend ::res $args MUST_NOT_HAPPEN
1557 }
1558 set c [chan push [tempchan w] foo]
1559 lappend res {*}[inthread $c {
1560 lappend notes [catch {[read $c 2]} msg] $msg
1561 close $c
1562 notes
1563 } c]
1564 } -cleanup {
1565 tempdone
1566 rename foo {}
1567 } -match glob -result {1 {channel "file*" wasn't opened for reading}}
1568 test iortrans.tf-4.3 {chan read, error return} -setup {
1569 set res {}
1570 } -constraints {testchannel thread} -body {
1571 proc foo {args} {
1572 handle.initialize
1573 handle.finalize
1574 lappend ::res $args
1575 return -code error BOOM!
1576 }
1577 set c [chan push [tempchan] foo]
1578 lappend res {*}[inthread $c {
1579 lappend notes [catch {read $c 2} msg] $msg
1580 close $c
1581 notes
1582 } c]
1583 } -cleanup {
1584 tempdone
1585 rename foo {}
1586 } -match glob -result {{read rt* {test data
1587 }} 1 BOOM!}
1588 test iortrans.tf-4.4 {chan read, break return is error} -setup {
1589 set res {}
1590 } -constraints {testchannel thread} -body {
1591 proc foo {args} {
1592 handle.initialize
1593 handle.finalize
1594 lappend ::res $args
1595 return -code break BOOM!
1596 }
1597 set c [chan push [tempchan] foo]
1598 lappend res {*}[inthread $c {
1599 lappend notes [catch {read $c 2} msg] $msg
1600 close $c
1601 notes
1602 } c]
1603 } -cleanup {
1604 tempdone
1605 rename foo {}
1606 } -match glob -result {{read rt* {test data
1607 }} 1 *bad code*}
1608 test iortrans.tf-4.5 {chan read, continue return is error} -setup {
1609 set res {}
1610 } -constraints {testchannel thread} -body {
1611 proc foo {args} {
1612 handle.initialize
1613 handle.finalize
1614 lappend ::res $args
1615 return -code continue BOOM!
1616 }
1617 set c [chan push [tempchan] foo]
1618 lappend res {*}[inthread $c {
1619 lappend notes [catch {read $c 2} msg] $msg
1620 close $c
1621 notes
1622 } c]
1623 } -cleanup {
1624 tempdone
1625 rename foo {}
1626 } -match glob -result {{read rt* {test data
1627 }} 1 *bad code*}
1628 test iortrans.tf-4.6 {chan read, custom return is error} -setup {
1629 set res {}
1630 } -constraints {testchannel thread} -body {
1631 proc foo {args} {
1632 handle.initialize
1633 handle.finalize
1634 lappend ::res $args
1635 return -code 777 BOOM!
1636 }
1637 set c [chan push [tempchan] foo]
1638 lappend res {*}[inthread $c {
1639 lappend notes [catch {read $c 2} msg] $msg
1640 close $c
1641 notes
1642 } c]
1643 } -cleanup {
1644 tempdone
1645 rename foo {}
1646 } -match glob -result {{read rt* {test data
1647 }} 1 *bad code*}
1648 test iortrans.tf-4.7 {chan read, level is squashed} -setup {
1649 set res {}
1650 } -constraints {testchannel thread} -body {
1651 proc foo {args} {
1652 handle.initialize
1653 handle.finalize
1654 lappend ::res $args
1655 return -level 55 -code 777 BOOM!
1656 }
1657 set c [chan push [tempchan] foo]
1658 lappend res {*}[inthread $c {
1659 lappend notes [catch {read $c 2} msg opt] $msg
1660 noteOpts $opt
1661 close $c
1662 notes
1663 } c]
1664 } -cleanup {
1665 tempdone
1666 rename foo {}
1667 } -match glob -result {{read rt* {test data
1668 }} 1 *bad code* {-code 1 -level 0 -errorcode NONE -errorline 1 -errorinfo *bad code*subcommand "read"*}}
1669
1670 # --- === *** ###########################
1671 # method write
1672
1673 test iortrans.tf-5.1 {chan write, regular write} -setup {
1674 set res {}
1675 } -constraints {testchannel thread} -match glob -body {
1676 proc foo {args} {
1677 handle.initialize
1678 handle.finalize
1679 lappend ::res $args
1680 return transformresult
1681 }
1682 set c [chan push [tempchan] foo]
1683 inthread $c {
1684 puts -nonewline $c snarf
1685 flush $c
1686 close $c
1687 } c
1688 lappend res [tempview]
1689 } -cleanup {
1690 tempdone
1691 rename foo {}
1692 } -result {{write rt* snarf} transformresult}
1693 test iortrans.tf-5.2 {chan write, no write is ok, no change to file} -setup {
1694 set res {}
1695 } -constraints {testchannel thread} -match glob -body {
1696 proc foo {args} {
1697 handle.initialize
1698 handle.finalize
1699 lappend ::res $args
1700 return
1701 }
1702 set c [chan push [tempchan] foo]
1703 inthread $c {
1704 puts -nonewline $c snarfsnarfsnarf
1705 flush $c
1706 close $c
1707 } c
1708 lappend res [tempview]; # This has to show the original data, as nothing was written
1709 } -cleanup {
1710 tempdone
1711 rename foo {}
1712 } -result {{write rt* snarfsnarfsnarf} {test data}}
1713 test iortrans.tf-5.3 {chan write, failed write} -setup {
1714 set res {}
1715 } -constraints {testchannel thread} -match glob -body {
1716 proc foo {args} {
1717 handle.initialize
1718 handle.finalize
1719 lappend ::res $args
1720 return -code error FAIL!
1721 }
1722 set c [chan push [tempchan] foo]
1723 lappend res {*}[inthread $c {
1724 puts -nonewline $c snarfsnarfsnarf
1725 lappend notes [catch {flush $c} msg] $msg
1726 close $c
1727 notes
1728 } c]
1729 } -cleanup {
1730 tempdone
1731 rename foo {}
1732 } -result {{write rt* snarfsnarfsnarf} 1 FAIL!}
1733 test iortrans.tf-5.4 {chan write, non-writable channel} -setup {
1734 set res {}
1735 } -constraints {testchannel thread} -match glob -body {
1736 proc foo {args} {
1737 handle.initialize
1738 handle.finalize
1739 lappend ::res $args MUST_NOT_HAPPEN
1740 return
1741 }
1742 set c [chan push [tempchan r] foo]
1743 lappend res {*}[inthread $c {
1744 lappend notes [catch {
1745 puts -nonewline $c snarfsnarfsnarf
1746 flush $c
1747 } msg] $msg
1748 close $c
1749 notes
1750 } c]
1751 } -cleanup {
1752 tempdone
1753 rename foo {}
1754 } -result {1 {channel "file*" wasn't opened for writing}}
1755 test iortrans.tf-5.5 {chan write, failed write, error return} -setup {
1756 set res {}
1757 } -constraints {testchannel thread} -match glob -body {
1758 proc foo {args} {
1759 handle.initialize
1760 handle.finalize
1761 lappend ::res $args
1762 return -code error BOOM!
1763 }
1764 set c [chan push [tempchan] foo]
1765 lappend res {*}[inthread $c {
1766 lappend notes [catch {
1767 puts -nonewline $c snarfsnarfsnarf
1768 flush $c
1769 } msg] $msg
1770 close $c
1771 notes
1772 } c]
1773 } -cleanup {
1774 tempdone
1775 rename foo {}
1776 } -result {{write rt* snarfsnarfsnarf} 1 BOOM!}
1777 test iortrans.tf-5.6 {chan write, failed write, error return} -setup {
1778 set res {}
1779 } -constraints {testchannel thread} -match glob -body {
1780 proc foo {args} {
1781 handle.initialize
1782 handle.finalize
1783 lappend ::res $args
1784 error BOOM!
1785 }
1786 set c [chan push [tempchan] foo]
1787 lappend res {*}[inthread $c {
1788 lappend notes [catch {
1789 puts -nonewline $c snarfsnarfsnarf
1790 flush $c
1791 } msg] $msg
1792 close $c
1793 notes
1794 } c]
1795 } -cleanup {
1796 tempdone
1797 rename foo {}
1798 } -result {{write rt* snarfsnarfsnarf} 1 BOOM!}
1799 test iortrans.tf-5.7 {chan write, failed write, break return is error} -setup {
1800 set res {}
1801 } -constraints {testchannel thread} -match glob -body {
1802 proc foo {args} {
1803 handle.initialize
1804 handle.finalize
1805 lappend ::res $args
1806 return -code break BOOM!
1807 }
1808 set c [chan push [tempchan] foo]
1809 lappend res {*}[inthread $c {
1810 lappend notes [catch {
1811 puts -nonewline $c snarfsnarfsnarf
1812 flush $c
1813 } msg] $msg
1814 close $c
1815 notes
1816 } c]
1817 } -cleanup {
1818 tempdone
1819 rename foo {}
1820 } -result {{write rt* snarfsnarfsnarf} 1 *bad code*}
1821 test iortrans.tf-5.8 {chan write, failed write, continue return is error} -setup {
1822 set res {}
1823 } -constraints {testchannel thread} -match glob -body {
1824 proc foo {args} {
1825 handle.initialize
1826 handle.finalize
1827 lappend ::res $args
1828 return -code continue BOOM!
1829 }
1830 set c [chan push [tempchan] foo]
1831 lappend res {*}[inthread $c {
1832 lappend notes [catch {
1833 puts -nonewline $c snarfsnarfsnarf
1834 flush $c
1835 } msg] $msg
1836 close $c
1837 notes
1838 } c]
1839 } -cleanup {
1840 rename foo {}
1841 } -result {{write rt* snarfsnarfsnarf} 1 *bad code*}
1842 test iortrans.tf-5.9 {chan write, failed write, custom return is error} -setup {
1843 set res {}
1844 } -constraints {testchannel thread} -body {
1845 proc foo {args} {
1846 handle.initialize
1847 handle.finalize
1848 lappend ::res $args
1849 return -code 777 BOOM!
1850 }
1851 set c [chan push [tempchan] foo]
1852 lappend res {*}[inthread $c {
1853 lappend notes [catch {
1854 puts -nonewline $c snarfsnarfsnarf
1855 flush $c
1856 } msg] $msg
1857 close $c
1858 notes
1859 } c]
1860 } -cleanup {
1861 tempdone
1862 rename foo {}
1863 } -match glob -result {{write rt* snarfsnarfsnarf} 1 *bad code*}
1864 test iortrans.tf-5.10 {chan write, failed write, level is ignored} -setup {
1865 set res {}
1866 } -constraints {testchannel thread} -match glob -body {
1867 proc foo {args} {
1868 handle.initialize
1869 handle.finalize
1870 lappend ::res $args
1871 return -level 55 -code 777 BOOM!
1872 }
1873 set c [chan push [tempchan] foo]
1874 lappend res {*}[inthread $c {
1875 lappend notes [catch {
1876 puts -nonewline $c snarfsnarfsnarf
1877 flush $c
1878 } msg opt] $msg
1879 noteOpts $opt
1880 close $c
1881 notes
1882 } c]
1883 } -cleanup {
1884 tempdone
1885 rename foo {}
1886 } -result {{write rt* snarfsnarfsnarf} 1 *bad code* {-code 1 -level 0 -errorcode NONE -errorline * -errorinfo *bad code*subcommand "write"*}}
1887
1888 # --- === *** ###########################
1889 # method limit?, drain (via read)
1890
1891 test iortrans.tf-6.1 {chan read, read limits} -setup {
1892 set res {}
1893 } -constraints {testchannel thread} -match glob -body {
1894 proc foo {args} {
1895 handle.initialize limit?
1896 handle.finalize
1897 lappend ::res $args
1898 handle.read
1899 return 6
1900 }
1901 set c [chan push [tempchan] foo]
1902 lappend res {*}[inthread $c {
1903 lappend notes [read $c 10]
1904 close $c
1905 notes
1906 } c]
1907 } -cleanup {
1908 tempdone
1909 rename foo {}
1910 } -result {{limit? rt*} {read rt* {test d}} {limit? rt*} {read rt* {ata
1911 }} {limit? rt*} @@}
1912 test iortrans.tf-6.2 {chan read, read transform drain on eof} -setup {
1913 set res {}
1914 } -constraints {testchannel thread} -match glob -body {
1915 proc foo {args} {
1916 handle.initialize drain
1917 handle.finalize
1918 lappend ::res $args
1919 handle.read
1920 handle.drain
1921 return
1922 }
1923 set c [chan push [tempchan] foo]
1924 lappend res {*}[inthread $c {
1925 lappend notes [read $c]
1926 lappend notes [close $c]
1927 } c]
1928 } -cleanup {
1929 tempdone
1930 rename foo {}
1931 } -result {{read rt* {test data
1932 }} {drain rt*} @<> {}}
1933
1934 # --- === *** ###########################
1935 # method clear (via puts, seek)
1936
1937 test iortrans.tf-7.1 {chan write, write clears read buffers} -setup {
1938 set res {}
1939 } -constraints {testchannel thread} -match glob -body {
1940 proc foo {args} {
1941 handle.initialize clear
1942 handle.finalize
1943 lappend ::res $args
1944 handle.clear
1945 return transformresult
1946 }
1947 set c [chan push [tempchan] foo]
1948 inthread $c {
1949 puts -nonewline $c snarf
1950 flush $c
1951 close $c
1952 } c
1953 return $res
1954 } -cleanup {
1955 tempdone
1956 rename foo {}
1957 } -result {{clear rt*} {write rt* snarf}}
1958 test iortrans.tf-7.2 {seek clears read buffers} -setup {
1959 set res {}
1960 } -constraints {testchannel thread} -match glob -body {
1961 proc foo {args} {
1962 handle.initialize clear
1963 handle.finalize
1964 lappend ::res $args
1965 return
1966 }
1967 set c [chan push [tempchan] foo]
1968 inthread $c {
1969 seek $c 2
1970 close $c
1971 } c
1972 return $res
1973 } -cleanup {
1974 tempdone
1975 rename foo {}
1976 } -result {{clear rt*}}
1977 test iortrans.tf-7.3 {clear, any result is ignored} -setup {
1978 set res {}
1979 } -constraints {testchannel thread} -match glob -body {
1980 proc foo {args} {
1981 handle.initialize clear
1982 handle.finalize
1983 lappend ::res $args
1984 return -code error "X"
1985 }
1986 set c [chan push [tempchan] foo]
1987 inthread $c {
1988 seek $c 2
1989 close $c
1990 } c
1991 return $res
1992 } -cleanup {
1993 tempdone
1994 rename foo {}
1995 } -result {{clear rt*}}
1996
1997 # --- === *** ###########################
1998 # method flush (via seek, close)
1999
2000 test iortrans.tf-8.1 {seek flushes write buffers, ignores data} -setup {
2001 set res {}
2002 } -constraints {testchannel thread} -match glob -body {
2003 proc foo {args} {
2004 handle.initialize flush
2005 handle.finalize
2006 lappend ::res $args
2007 return X
2008 }
2009 set c [chan push [tempchan] foo]
2010 lappend res {*}[inthread $c {
2011 # Flush, no writing
2012 seek $c 2
2013 # The close flushes again, this modifies the file!
2014 lappend notes | [close $c] |
2015 # NOTE: The flush generated by the close is recorded immediately, the
2016 # other note's here are deferred until after the thread is done. This
2017 # changes the order of the result a bit from the non-threaded case
2018 # (The first | moves one to the right). This is an artifact of the
2019 # 'inthread' framework, not of the transformation itself.
2020 notes
2021 } c]
2022 lappend res [tempview]
2023 } -cleanup {
2024 tempdone
2025 rename foo {}
2026 } -result {{flush rt*} {flush rt*} | {} | {teXt data}}
2027 test iortrans.tf-8.2 {close flushes write buffers, writes data} -setup {
2028 set res {}
2029 } -constraints {testchannel thread} -match glob -body {
2030 proc foo {args} {
2031 handle.initialize flush
2032 lappend ::res $args
2033 handle.finalize
2034 return .flushed.
2035 }
2036 set c [chan push [tempchan] foo]
2037 inthread $c {
2038 close $c
2039 } c
2040 lappend res [tempview]
2041 } -cleanup {
2042 tempdone
2043 rename foo {}
2044 } -result {{flush rt*} {finalize rt*} .flushed.}
2045
2046 # --- === *** ###########################
2047 # method watch - removed from TIP (rev 1.12+)
2048
2049 # --- === *** ###########################
2050 # method event - removed from TIP (rev 1.12+)
2051
2052 # --- === *** ###########################
2053 # 'Pull the rug' tests. Create channel in a thread A, move to other thread B,
2054 # destroy the origin thread (A) before or during access from B. Must not
2055 # crash, must return proper errors.
2056
2057 test iortrans.tf-11.0 {origin thread of moved transform gone} -setup {
2058 #puts <<$tcltest::mainThread>>main
2059 set tida [thread::create -preserved]; #puts <<$tida>>
2060 thread::send $tida {load {} Tcltest}
2061 set tidb [thread::create -preserved]; #puts <<$tida>>
2062 thread::send $tidb {load {} Tcltest}
2063 } -constraints {testchannel thread} -match glob -body {
2064 # Set up channel in thread
2065 thread::send $tida $helperscript
2066 thread::send $tidb $helperscript
2067 set chan [thread::send $tida {
2068 proc foo {args} {
2069 handle.initialize clear drain flush limit? read write
2070 handle.finalize
2071 lappend ::res $args
2072 return
2073 }
2074 set chan [chan push [tempchan] foo]
2075 fconfigure $chan -buffering none
2076 set chan
2077 }]
2078
2079 # Move channel to 2nd thread, transform goes with it.
2080 thread::send $tida [list testchannel cut $chan]
2081 thread::send $tidb [list testchannel splice $chan]
2082
2083 # Kill origin thread, then access channel from 2nd thread.
2084 thread::release -wait $tida
2085
2086 set res {}
2087 lappend res [catch {thread::send $tidb [list puts $chan shoo]} msg] $msg
2088 lappend res [catch {thread::send $tidb [list tell $chan]} msg] $msg
2089 lappend res [catch {thread::send $tidb [list seek $chan 1]} msg] $msg
2090 lappend res [catch {thread::send $tidb [list gets $chan]} msg] $msg
2091 lappend res [catch {thread::send $tidb [list close $chan]} msg] $msg
2092 # The 'tell' is ok, as it passed through the transform to the base
2093 # channel without invoking the transform handler.
2094 } -cleanup {
2095 thread::send $tidb tempdone
2096 thread::release $tidb
2097 } -result {1 {Owner lost} 0 0 1 {Owner lost} 1 {Owner lost} 1 {Owner lost}}
2098
2099 test iortrans.tf-11.1 {origin thread of moved transform destroyed during access} -setup {
2100 #puts <<$tcltest::mainThread>>main
2101 set tida [thread::create -preserved]; #puts <<$tida>>
2102 thread::send $tida {load {} Tcltest}
2103 set tidb [thread::create -preserved]; #puts <<$tidb>>
2104 thread::send $tidb {load {} Tcltest}
2105 } -constraints {testchannel thread notValgrind} -match glob -body {
2106 # Set up channel in thread
2107 thread::send $tida $helperscript
2108 thread::send $tidb $helperscript
2109 set chan [thread::send $tida {
2110 proc foo {args} {
2111 handle.initialize clear drain flush limit? read write
2112 handle.finalize
2113 lappend ::res $args
2114 # destroy thread during channel access
2115 thread::exit
2116 }
2117 set chan [chan push [tempchan] foo]
2118 fconfigure $chan -buffering none
2119 set chan
2120 }]
2121
2122 # Move channel to 2nd thread, transform goes with it.
2123 thread::send $tida [list testchannel cut $chan]
2124 thread::send $tidb [list testchannel splice $chan]
2125
2126 # Run access from thread B, wait for response from A (A is not using event
2127 # loop at this point, so the event pile up in the queue.
2128 thread::send $tidb [list set chan $chan]
2129 thread::send $tidb [list set mid [thread::id]]
2130 thread::send -async $tidb {
2131 # Wait a bit, give the main thread the time to start its event loop to
2132 # wait for the response from B
2133 after 50
2134 catch { puts $chan shoo } res
2135 catch { close $chan }
2136 thread::send -async $mid [list set ::res $res]
2137 }
2138 vwait ::res
2139 set res
2140 } -cleanup {
2141 thread::send $tidb tempdone
2142 thread::release $tidb
2143 } -result {Owner lost}
2144
2145
2146 test iortrans-ea69b0258a9833cb {
2147 Crash when using a channel transformation on TCP client socket
2148
2149 "line two" does not make it into result. This issue should probably be
2150 addressed, but it is outside the scope of this test.
2151 } -setup {
2152 set res {}
2153 set read 0
2154 } -body {
2155 namespace eval reflector1 {
2156 variable source "line one\nline two"
2157 interp alias {} [namespace current]::dispatch {} [
2158 namespace parent]::reflector [namespace current]
2159 }
2160 set chan [chan create read [namespace which reflector1::dispatch]]
2161 chan configure $chan -blocking 0
2162 chan push $chan inputfilter
2163 chan event $chan read [list ::apply [list chan {
2164 variable res
2165 variable read
2166 set gets [gets $chan]
2167 append res $gets
2168 incr read
2169 } [namespace current]] $chan]
2170 vwait [namespace current]::read
2171 chan pop $chan
2172 vwait [namespace current]::read
2173 return $res
2174 } -cleanup {
2175 catch {unset read}
2176 close $chan
2177 } -result {line one}
2178
2179 cleanupTests
2180 return