(ns net.deertopia.doerg.common-test (:require [net.deertopia.doerg.common :as sut] [babashka.process :as p] [clojure.test :as t])) (defn sleep-vs-timeout [& {:keys [sleep timeout]}] (sut/deref-with-timeout (p/process "sleep" (format "%ds" sleep)) (* timeout 1000))) ;; Ideally we would test the following property: ;; ;; For natural numbers n and m, evaluating the form ;; (sut/deref-with-timeout ;; (p/process "sleep" (format "%ds" n)) ;; (* m 1000)) ;; will throw an exception iff n < m (probably with some margin of ;; error lol). ;; ;; But, this is not something that we want to run dozens-to-hundreds ;; of times. }:p (t/deftest long-sleep-vs-short-timeout (t/testing "long sleep vs. short timeout" (t/is (thrown-with-msg? Exception #".*timed out.*" (sleep-vs-timeout :sleep 5 :timeout 1))))) (t/deftest short-sleep-vs-long-timeout (t/testing "short sleep vs. long timeout" (t/is (instance? babashka.process.Process (sleep-vs-timeout :sleep 1 :timeout 5)))))