;; This Source Code Form is subject to the terms of the Mozilla Public ;; License, v. 2.0. If a copy of the MPL was not distributed with this ;; file, You can obtain one at http://mozilla.org/MPL/2.0/. ;; `line-read` in a bjoroutine: it suspends while it waits, other fibers ;; run, and a deadline cancels it with the modes restored. ;; ;; bjo test tests/async.bjo ;; ;; The test runs itself again under `script`, as tests/terminal.bjo does. (import (std prelude)) (import (std simpletest)) (import (std run)) (import (bjoterm terminal)) (import (bjoterm line)) (import/extern (command-line (: System.Environment.GetCommandLineArgs (-> (Array string)) #:no-raise))) ;; ---- the child ------------------------------------------------------------- (: (-> string)) (defun (modes) (match (run/output '(sh "-c" "stty -a (Box bool) int)) (defbjo (ticks stop) (let go ((n 0)) (if (box-ref stop) n (begin (sync (timeout (milliseconds 50))) (go (+ n 1)))))) (: (-> LineEditor Terminal string)) (defbjo (read-with-ticks ed term) (def stop (make-box #f)) (def counter (bjo (ticks stop))) (def result (line-read ed term "> ")) (box-set! stop #t) (def n (match (sync (promise-join counter)) ((Ok n) n) ((Err _) -1))) (str (match result ((LineText text) (str "read " (written text))) (LineEof "eof") (LineCancelled "cancelled") ((LineKey _ _) "key")) (if (> n 5) ", ticks ran" ", no ticks"))) (: (-> LineEditor Terminal string)) (defbjo (read-with-deadline ed term) (match (try (with-deadline (milliseconds 400) (line-read ed term "> ")) #:catch (System.Exception)) ((Ok _) "not cancelled") ((Err _) "cancelled"))) (: (-> (Promise string) string)) (defun (joined p) (match (sync/blocking (promise-join p)) ((Ok text) text) ((Err e) (str "raised " (.-Message e))))) (: (-> void)) (defun (child) (match (terminal-open) ((Err e) (println (str "open: " (terminal-error-message e)))) ((Ok term) (def ed (make-line-editor)) (println (str "first: " (joined (bjo (read-with-ticks ed term))))) (println (str "deadline: " (joined (bjo (read-with-deadline ed term))))) (println (str "after deadline: " (modes) ", raw? " (if (terminal-raw? term) "yes" "no"))) (println (str "next: " (joined (bjo (read-with-ticks ed term)))))))) ;; ---- the parent ------------------------------------------------------------ (: (-> void)) (defun (parent) (def self (str "dotnet " (array-ref (command-line) 0))) ;; The first line comes after 1.5 s of ticks. Nothing comes during the ;; deadline. The last line comes after it. (def keys "sleep 2.5; printf 'hello\\r'; sleep 2.5; printf 'again\\r'; sleep 1") (def command (str "(" keys ") | script -qec '" self " child' /dev/null")) (def out (match (run/output '(sh "-c" ,command)) ((Ok (Tuple _ text)) (string-replace text "\r" "")) ((Err e) (run-error-message e)))) (expect-true "the terminal opens" (not (string-contains? out "open:"))) (expect-true "line-read in a bjoroutine reads, and another fiber runs meanwhile" (string-contains? out "first: read \"hello\", ticks ran")) (expect-true "a deadline cancels line-read" (string-contains? out "deadline: cancelled")) (expect-true "and the modes are restored" (string-contains? out "after deadline: cooked, raw? no")) (expect-true "the next line-read works" (string-contains? out "next: read \"again\", ticks ran")) (unless (string-contains? out "next: read") (println out))) (defun (main args) (cond ((= args ["child"]) (child)) ((file-exists? "/usr/bin/script") (parent)) (else (println "skipped: no script command"))) 0)