|
| 1 | +(ns eca.server-test |
| 2 | + (:require |
| 3 | + [babashka.process :as p] |
| 4 | + [clojure.test :refer [deftest is testing]] |
| 5 | + [eca.server :as server] |
| 6 | + [eca.test-helper :as h])) |
| 7 | + |
| 8 | +(defn ^:private spawn-blocking-process [] |
| 9 | + ;; Long-running child whose pid we own. `sleep 600` is fine on Linux/macOS; |
| 10 | + ;; subprocess-based tests are skipped on Windows. |
| 11 | + (p/process {:cmd ["sleep" "600"] |
| 12 | + :shutdown p/destroy-tree})) |
| 13 | + |
| 14 | +(defn ^:private pid-of [proc] |
| 15 | + (.pid ^java.lang.Process (:proc proc))) |
| 16 | + |
| 17 | +(deftest start-liveness-probe-with-missing-pid-test |
| 18 | + (testing "an absent parent triggers on-exit at start" |
| 19 | + (let [exited? (promise)] |
| 20 | + (#'server/start-liveness-probe! Long/MAX_VALUE |
| 21 | + #(deliver exited? true)) |
| 22 | + (is (= true (deref exited? 200 :timeout)) |
| 23 | + "on-exit must fire when the parent is not present")))) |
| 24 | + |
| 25 | +(deftest start-liveness-probe-survives-on-exit-throwing-test |
| 26 | + (testing "an exception in on-exit does not propagate out of start!" |
| 27 | + (is (nil? (#'server/start-liveness-probe! Long/MAX_VALUE |
| 28 | + #(throw (ex-info "boom" {})))) |
| 29 | + "start! must not raise even when on-exit throws"))) |
| 30 | + |
| 31 | +(deftest start-liveness-probe-with-alive-parent-test |
| 32 | + (when-not h/windows? |
| 33 | + (testing "an alive parent does not trigger on-exit" |
| 34 | + (let [proc (spawn-blocking-process) |
| 35 | + exited? (promise)] |
| 36 | + (try |
| 37 | + (#'server/start-liveness-probe! (pid-of proc) |
| 38 | + #(deliver exited? true)) |
| 39 | + (is (= :still-alive (deref exited? 100 :still-alive)) |
| 40 | + "on-exit must not fire while the parent is alive") |
| 41 | + (finally |
| 42 | + (p/destroy-tree proc))))))) |
| 43 | + |
| 44 | +(deftest start-liveness-probe-fires-when-parent-dies-test |
| 45 | + (when-not h/windows? |
| 46 | + (testing "killing the parent triggers on-exit" |
| 47 | + (let [proc (spawn-blocking-process) |
| 48 | + exited? (promise)] |
| 49 | + (#'server/start-liveness-probe! (pid-of proc) |
| 50 | + #(deliver exited? :fired)) |
| 51 | + (p/destroy-tree proc) |
| 52 | + (is (= :fired (deref exited? 2000 :timeout)) |
| 53 | + "on-exit must fire shortly after the parent dies"))))) |
0 commit comments