Import Debian changes 20180207-1
[hcoop/debian/mlton.git] / regression / test-spawn.sml
1 fun statusToString status =
2 case status of
3 Posix.Process.W_EXITED => "W_EXITED"
4 | Posix.Process.W_EXITSTATUS w => concat ["W_EXITSTATUS ", Word8.toString w]
5 | Posix.Process.W_SIGNALED s =>
6 concat ["W_SIGNALED ", SysWord.toString (Posix.Signal.toWord s)]
7 | Posix.Process.W_STOPPED s =>
8 concat ["W_STOPPED ", SysWord.toString (Posix.Signal.toWord s)]
9
10 val cmd = CommandLine.name ()
11
12 fun stdout () =
13 TextIO.output (TextIO.stdOut, "Hello world! [stdout]\n")
14 fun exit () = Posix.Process.exit 0wx7
15 fun diverge () = diverge ()
16
17 fun test () =
18 let
19 fun spawn arg =
20 let
21 val _ = TextIO.flushOut (TextIO.stdOut)
22 val _ = TextIO.flushOut (TextIO.stdErr)
23 in
24 MLton.Process.spawn
25 {path = cmd, args = [cmd, arg]}
26 end
27 fun waitpid pid =
28 let
29 val (pid', status) =
30 Posix.Process.waitpid (Posix.Process.W_CHILD pid, [])
31 val () =
32 if pid <> pid'
33 then raise Fail "reap: pid <> pid'"
34 else ()
35 in
36 status
37 end
38 fun kill (pid, signal) =
39 Posix.Process.kill (Posix.Process.K_PROC pid, signal)
40 fun doTest (arg, withPid) =
41 let
42 val _ = print (concat ["testing ", arg, "...\n"])
43 val pid = spawn arg
44 val () = withPid pid
45 val status = waitpid pid
46 val _ = print (concat ["exit_status: ", statusToString status, "\n"])
47 in
48 ()
49 end
50 fun doSimpleTest arg = doTest (arg, fn _ => ())
51 in
52 print "spawn test:\n"
53 ; doSimpleTest "stdout"
54 ; doSimpleTest "exit"
55 ; doTest ("diverge", fn pid => kill (pid, Posix.Signal.kill))
56 end
57
58 val _ =
59 case CommandLine.arguments () of
60 [] => test ()
61 | ["stdout"] => stdout ()
62 | ["exit"] => exit ()
63 | ["diverge"] => diverge ()
64 | _ => raise Match