From cda30dc0b7de26878d9db188abc96d9061f8dbec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Thir=C3=A9?= Date: Sat, 17 Aug 2024 15:22:15 +0200 Subject: [PATCH 1/2] Tezt/Cloud: No need to press twice with 'keep-alive' --- tezt/lib_cloud/cloud.ml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tezt/lib_cloud/cloud.ml b/tezt/lib_cloud/cloud.ml index 896d02397f12..6acd5e1d4d91 100644 --- a/tezt/lib_cloud/cloud.ml +++ b/tezt/lib_cloud/cloud.ml @@ -27,11 +27,13 @@ let sigint = previous_behaviour := previous_handler ; promise +let input = Lwt_io.read_line Lwt_io.stdin + let eof = let promise, resolver = Lwt.task () in Lwt.dont_wait (fun () -> - let* _ = Lwt_io.read_line Lwt_io.stdin in + let* _ = input in Lwt.return_unit) (fun _ -> Lwt.wakeup resolver ()) ; promise @@ -51,7 +53,6 @@ let shutdown ?exn t = let* () = if Env.keep_alive then ( Log.info "Please press to terminate the scenario." ; - let* _ = Lwt_io.read_line Lwt_io.stdin in Lwt.return_unit) else Lwt.return_unit in -- GitLab From 49f9a1dc83a025d60c85ae44d5a073c72df5d5cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Thir=C3=A9?= Date: Sat, 17 Aug 2024 15:44:29 +0200 Subject: [PATCH 2/2] Tezt/Cloud: Make inputs reading better and safer --- tezt/lib_cloud/cloud.ml | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/tezt/lib_cloud/cloud.ml b/tezt/lib_cloud/cloud.ml index 6acd5e1d4d91..fb5f306e76de 100644 --- a/tezt/lib_cloud/cloud.ml +++ b/tezt/lib_cloud/cloud.ml @@ -27,14 +27,40 @@ let sigint = previous_behaviour := previous_handler ; promise -let input = Lwt_io.read_line Lwt_io.stdin +module Input : sig + (** This module should be the only one that reads on [stdin]. *) + + (** [next ()] returns the next line on stdin. *) + val next : unit -> string Lwt.t +end = struct + type t = {mutable resolvers : string Lwt.u list} + + let state = {resolvers = []} + + let next () = + let t, u = Lwt.task () in + state.resolvers <- u :: state.resolvers ; + t + + let rec loop () = + let* input = Lwt_io.read_line Lwt_io.stdin in + state.resolvers + |> List.iter (fun resolver -> Lwt.wakeup_later resolver input) ; + state.resolvers <- [] ; + loop () + + let _ = loop () +end let eof = let promise, resolver = Lwt.task () in Lwt.dont_wait (fun () -> - let* _ = input in - Lwt.return_unit) + let rec loop () = + let* _ = Input.next () in + loop () + in + loop ()) (fun _ -> Lwt.wakeup resolver ()) ; promise @@ -53,6 +79,7 @@ let shutdown ?exn t = let* () = if Env.keep_alive then ( Log.info "Please press to terminate the scenario." ; + let* _ = Input.next () in Lwt.return_unit) else Lwt.return_unit in -- GitLab