home *** CD-ROM | disk | FTP | other *** search
/ Serving the Web / ServingTheWeb1995.disc1of1.iso / linux / slacksrce / tcl / tcl+tk+t / tclx7.3bl / tclx7 / tclX7.3b / tests / signal.test < prev    next >
Encoding:
Text File  |  1994-07-16  |  8.5 KB  |  320 lines

  1. #
  2. # signal.test
  3. #
  4. # Tests for the signal and kill commands.
  5. #---------------------------------------------------------------------------
  6. # Copyright 1992-1994 Karl Lehenbauer and Mark Diekhans.
  7. #
  8. # Permission to use, copy, modify, and distribute this software and its
  9. # documentation for any purpose and without fee is hereby granted, provided
  10. # that the above copyright notice appear in all copies.  Karl Lehenbauer and
  11. # Mark Diekhans make no representations about the suitability of this
  12. # software for any purpose.  It is provided "as is" without express or
  13. # implied warranty.
  14. #------------------------------------------------------------------------------
  15. # $Id: signal.test,v 4.0 1994/07/16 05:25:52 markd Rel $
  16. #------------------------------------------------------------------------------
  17. #
  18.  
  19. if {[info procs test] != "test"} then {source testlib.tcl}
  20.  
  21. #
  22. # Fork without exec will not work under Tk, skip this test
  23. #
  24. if ![lempty [info commands button]] {
  25.     puts "*************************************************************"
  26.     puts "Signal tests are constructed in a way that does not work"
  27.     puts "under Tk.  Test skipped."
  28.     puts "*************************************************************"
  29.     puts ""
  30.     return
  31. }
  32.  
  33. #
  34. # Determine if we have Posix signals.
  35. #
  36. set msg {}
  37. set posix 1
  38. catch {signal unblock SIGHUP} msg
  39. if {$msg == "Posix signals are not available on this system"} {
  40.     set posix 0
  41. }
  42.  
  43. Test signal-1.1 {signal tests} {
  44.     signal ignore SIGHUP
  45.     kill HUP [id process]
  46. } 0 {}
  47.  
  48. Test signal-1.2 {signal tests} {
  49.     global errorInfo
  50.     set errorInfo {}
  51.     signal error HUP
  52.     proc KillMe3 {} {kill SIGHUP [id process]}
  53.     proc KillMe2 {} {KillMe3}
  54.     proc KillMe1 {} {KillMe2}
  55.     list [catch {KillMe1} msg] $msg $errorInfo
  56. } 0 {1 {SIGHUP signal received} {SIGHUP signal received
  57.     while executing
  58. "kill SIGHUP [id process]"
  59.     (procedure "KillMe3" line 1)
  60.     invoked from within
  61. "KillMe3"
  62.     (procedure "KillMe2" line 1)
  63.     invoked from within
  64. "KillMe2"
  65.     (procedure "KillMe1" line 1)
  66.     invoked from within
  67. "KillMe1"}}
  68.  
  69. Test signal-1.3 {signal tests} {
  70.     signal error {HUP SIGTERM}
  71.     set one [list [catch {kill HUP  [id process]} msg] $msg]
  72.     set two [list [catch {kill TERM [id process]} msg] $msg]
  73.     list $one $two
  74. } 0 {{1 {SIGHUP signal received}} {1 {SIGTERM signal received}}}
  75.  
  76. Test signal-1.4 {signal tests} {
  77.     set signalWeGot {}
  78.     signal trap 1 {set signalWeGot %S}
  79.     kill SIGHUP [id process]
  80.     signal default 1
  81.     set signalWeGot
  82. } 0 {SIGHUP}
  83.  
  84. Test signal-1.41 {signal tests} {
  85.     set signalWeGot {}
  86.     set signalTrash {}
  87.     signal trap 1 {set signalWeGot %S; set signalTrash "%%"}
  88.     kill SIGHUP [id process]
  89.     signal default 1
  90.     list $signalWeGot $signalTrash
  91. } 0 {SIGHUP %%}
  92.  
  93. Test signal-1.42 {signal tests} {
  94.     signal trap 1 {set signalWeGot %s; set signalTrash "%%"}
  95.     kill SIGHUP [id process]
  96. } 1 {bad signal trap command formatting specification "%s", expected one of "%%" or "%S"}
  97. signal default SIGHUP
  98.  
  99. Test signal-1.5 {signal tests} {
  100.     signal default {SIGHUP SIGINT}
  101.     signal get {SIGHUP SIGINT}
  102. } 0 {{SIGHUP {default 0}} {SIGINT {default 0}}}
  103.  
  104. Test signal-1.6 {signal tests} {
  105.     signal default SIGHUP
  106.     signal ignore  SIGINT
  107.     signal get {SIGHUP SIGINT}
  108. } 0 {{SIGHUP {default 0}} {SIGINT {ignore 0}}}
  109.  
  110. Test signal-1.7 {signal tests} {
  111.     signal trap {SIGHUP SIGINT} {error "Should not get this signal"}
  112.     signal get {SIGHUP SIGINT}
  113. } 0 [list {SIGHUP {trap 0 {error "Should not get this signal"}}} \
  114.           {SIGINT {trap 0 {error "Should not get this signal"}}} ]
  115.  
  116. Test signal-1.8 {signal tests} {
  117.     signal error {SIGHUP SIGINT}
  118.     signal get {SIGHUP SIGINT}
  119. } 0 {{SIGHUP {error 0}} {SIGINT {error 0}}}
  120.  
  121. Test signal-1.8.1 {signal tests} {
  122.     signal error {SIGHUP SIGINT}
  123.     set sigkl [signal get *]
  124.     list [keylget sigkl SIGHUP]  [keylget sigkl SIGINT]
  125. } 0 {{error 0} {error 0}}
  126.  
  127. Test signal-1.8.2 {signal tests} {
  128.     set sigkl [signal get *]
  129.     list [keylget sigkl SIGALRM {}] [keylget sigkl SIGPIPE {}] \
  130.         [keylget sigkl SIGINT {}]
  131. } 0 {1 1 1}
  132.  
  133. Test signal-1.8.3 {signal tests} {
  134.     signal error {SIGHUP SIGINT}
  135.     set sigkl [signal get *]
  136.     signal default {SIGHUP SIGINT}
  137.     signal set $sigkl
  138.     signal get {SIGHUP SIGINT}
  139. } 0 {{SIGHUP {error 0}} {SIGINT {error 0}}}
  140.  
  141. Test signal-1.9 {signal tests} {
  142.     global errorInfo
  143.     set errorInfo {}
  144.     proc KillMe3 {} {kill SIGHUP [id process]}
  145.     proc KillMe2 {} {KillMe3}
  146.     proc KillMe1 {} {KillMe2}
  147.     signal trap SIGHUP {error "Blew it in the trap code"}
  148.     list [catch {KillMe1} msg ] $msg $errorInfo
  149. } 0 {1 {Blew it in the trap code} {Blew it in the trap code
  150.     while executing
  151. "error "Blew it in the trap code""
  152.     while executing signal trap code for SIGHUP signal
  153.     invoked from within
  154. "kill SIGHUP [id process]"
  155.     (procedure "KillMe3" line 1)
  156.     invoked from within
  157. "KillMe3"
  158.     (procedure "KillMe2" line 1)
  159.     invoked from within
  160. "KillMe2"
  161.     (procedure "KillMe1" line 1)
  162.     invoked from within
  163. "KillMe1"}}
  164.  
  165. Test signal-1.10 {signal tests} {
  166.     signal
  167. } 1 {wrong # args: signal action signalList ?command?}
  168.  
  169. Test signal-1.11 {signal tests} {
  170.     signal ignore foo
  171. } 1 {invalid signal "foo"}
  172.  
  173. Test signal-1.12 {signal tests} {
  174.     signal ignore sigint "echo foo"
  175. } 1 {command may not be specified for "ignore" action}
  176.  
  177. Test signal-1.13 {signal tests} {
  178.     signal baz sigint
  179. } 1 {invalid signal action specified: baz: expected one of "default", "ignore", "error", "trap", "get", "set", "block", or "unblock"}
  180.  
  181. #
  182. # Complex test for the death of a child.
  183. #
  184.  
  185. proc PollSigChld {} {
  186.     global G_gotChild
  187.     set sleepCnt 0
  188.     while {!$G_gotChild} {
  189.         incr sleepCnt
  190.         if {$sleepCnt > 90} {
  191.             error "signal-1.14: SIGCHLD lost"
  192.         }
  193.         sleep 1
  194.     }
  195. }
  196.  
  197.  
  198. proc ForkChild {exitCode} {
  199.     flush stdout  ;# Not going to exec, must clean up the buffers.
  200.     flush stderr
  201.     set childPid [fork]
  202.     if {$childPid == 0} {
  203.         exit $exitCode
  204.     }
  205.     return $childPid
  206. }
  207.  
  208. if $posix {
  209.     set expect {123 {{SIGCHLD {trap 0 {global G_gotChild;set G_gotChild 1;sleep 1}}}}}
  210. } else {
  211.     set expect {123 {{SIGCHLD {default 0}}}}
  212. }
  213. set expect 
  214. Test signal-1.15 {signal tests} {
  215.     global G_gotChild
  216.     set G_gotChild 0
  217.     signal trap SIGCHLD {global G_gotChild;set G_gotChild 1;sleep 1}
  218.     set pid1 [ForkChild 123] 
  219.     PollSigChld
  220.     set status1 [wait $pid1]
  221.     list [lindex $status1 2] [signal get SIGCHLD]
  222. } 0 $expect
  223.  
  224. signal default SIGCHLD
  225.  
  226. #
  227. # Check that the signals are left in the correct state after receiving
  228. # a signal (on SIGCHLD is different if we have Posix signals).
  229. #
  230.  
  231. Test signal-1.16 {signal tests} {
  232.     global G_gotChild
  233.     set G_gotChild 0
  234.     signal trap SIGCHLD {global G_gotChild;set G_gotChild 1}
  235.     kill SIGCHLD [id process]
  236.     set gotChild1 $G_gotChild
  237.     set G_gotChild 0
  238.     kill SIGCHLD [id process]
  239.     set gotChild2 $G_gotChild
  240.     set G_gotChild 0
  241.     signal trap SIGCHLD {global G_gotChild;set G_gotChild 1}
  242.     kill SIGCHLD [id process]
  243.     set gotChild3 $G_gotChild
  244.     signal default SIGCHLD
  245.     list $gotChild1 $gotChild2 $gotChild3
  246. } 0 [list 1 $posix 1]
  247.  
  248.  
  249. Test signal-1.17 {signal tests} {
  250.     global G_gotPipe
  251.     set G_gotPipe 0
  252.     signal trap SIGPIPE {global G_gotPipe;set G_gotPipe 1}
  253.     kill SIGPIPE [id process]
  254.     set gotPipe1 $G_gotPipe
  255.     set G_gotPipe 0
  256.     kill SIGPIPE [id process]
  257.     set gotPipe2 $G_gotPipe
  258.     signal default SIGPIPE
  259.     list $gotPipe1 $gotPipe2
  260. } 0 {1 1}
  261.  
  262. if $posix {
  263.     Test signal-1.18 {signal tests} {
  264.         signal error SIGHUP
  265.         signal block SIGHUP
  266.         signal get SIGHUP
  267.     } 0 {{SIGHUP {error 1}}}
  268.  
  269.     Test signal-1.19 {signal tests} {
  270.         signal unblock SIGHUP
  271.         signal get SIGHUP
  272.     } 0 {{SIGHUP {error 0}}}
  273.  
  274.     Test signal-1.18 {signal tests} {
  275.         signal block SIGHUP
  276.         signal error SIGHUP
  277.         signal get SIGHUP
  278.     } 0 {{SIGHUP {error 1}}}
  279.  
  280.     signal unblock SIGHUP
  281.     signal default SIGHUP
  282. }
  283.  
  284.  
  285. Test signal-2.1 {kill tests} {
  286.     kill
  287. } 1 {wrong # args: kill ?-pgroup? ?signal? idlist}
  288.  
  289. signal error SIGINT
  290.  
  291. Test signal-2.2 {kill tests} {
  292.     kill 2 [id process]
  293. } 1 {SIGINT signal received}
  294.  
  295. Test signal-2.3 {kill tests} {
  296.     kill INT [id process]
  297. } 1 {SIGINT signal received}
  298.  
  299. Test signal-2.4 {kill tests} {
  300.     kill SIGINT [id process]
  301. } 1 {SIGINT signal received}
  302.  
  303. Test signal-2.5 {kill tests} {
  304.     kill 10000 [id process]
  305. } 1 {invalid signal "10000"}
  306.  
  307. Test signal-2.6 {kill tests} {
  308.     kill SIGFOO [id process]
  309. } 1 {invalid signal "SIGFOO"}
  310.  
  311. Test signal-2.7 {kill tests} {
  312.     kill 0 [id process]
  313. } 0 {}
  314.  
  315. Test signal-2.8 {kill tests} {
  316.     set pgrp [ForkLoopingChild 1]
  317.     kill -pgroup SIGKILL $pgrp
  318.     lrange [wait -pgroup $pgrp] 1 2
  319. } 0 {SIG SIGKILL}
  320.