From 9a5f9f39e23c14116e2776852c6e9e02f510f47c Mon Sep 17 00:00:00 2001 From: Thomas Leonard Date: Thu, 17 Nov 2022 11:25:07 +0000 Subject: [PATCH] Tidy tests --- lib_eio_luv/tests/process.md | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/lib_eio_luv/tests/process.md b/lib_eio_luv/tests/process.md index babec9efa..46aae4ac0 100644 --- a/lib_eio_luv/tests/process.md +++ b/lib_eio_luv/tests/process.md @@ -12,12 +12,11 @@ A helper function for reading all of the bytes from a handle. ```ocaml let read_all handle buf = - let rec read acc = - try - let i = Eio_luv.Low_level.Stream.read_into handle buf in - read (acc + i) - with End_of_file -> acc - in read 0 + let rec read acc = + match Eio_luv.Low_level.Stream.read_into handle buf with + | i -> read (acc + i) + | exception End_of_file -> acc + in read 0 ``` A simple `echo hello` process redirects to stdout. @@ -42,7 +41,7 @@ Using a pipe to redirect output to a buffer. let parent_pipe = Eio_luv.Low_level.Pipe.init ~sw () in let buf = Luv.Buffer.create 32 in let redirect = Eio_luv.Low_level.Process.[ - to_parent_pipe ~fd:Luv.Process.stdout ~parent_pipe:parent_pipe () + to_parent_pipe ~fd:Luv.Process.stdout ~parent_pipe () ] in let t = Process.spawn ~sw ~redirect "echo" [ "echo"; "Hello,"; "World!" ] in let read = read_all parent_pipe buf in @@ -88,17 +87,14 @@ Forgetting to wait for a process to finish stops the process. ```ocaml # Eio_luv.run @@ fun _env -> - let proc = ref None in - let run () = + let proc = Switch.run @@ fun sw -> let redirect = Luv.Process.[ inherit_fd ~fd:stdout ~from_parent_fd:stdout () ] in - let t = Process.spawn ~sw ~redirect "sleep" [ "sleep"; "10" ] in - proc := Some t + Process.spawn ~sw ~redirect "sleep" [ "sleep"; "10" ] in - run (); - Process.await_exit (Option.get !proc);; + Process.await_exit proc;; - : int * int64 = (9, 0L) ``` @@ -106,17 +102,15 @@ Stopping a process interacts nicely with switches. ```ocaml # Eio_luv.run @@ fun _env -> - let proc = ref None in - let run () = + let proc = Switch.run @@ fun sw -> let redirect = Luv.Process.[ inherit_fd ~fd:stdout ~from_parent_fd:stdout () ] in let t = Process.spawn ~sw ~redirect "sleep" [ "sleep"; "10" ] in Process.send_signal t Luv.Signal.sigkill; - proc := Some t + t in - run (); - Process.await_exit (Option.get !proc);; + Process.await_exit proc;; - : int * int64 = (9, 0L) ```