Skip to content

Commit

Permalink
Wait for test-server ready before running tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lread committed Mar 16, 2024
1 parent 4a86cd0 commit 1f99736
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions script/test_server.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
(ns test-server
(:require [babashka.cli :as cli]
[babashka.http-server :as server]
[babashka.http-client :as client]
[lread.status-line :as status]))

(def cli-spec {:help {:desc "This usage help" :alias :h}
Expand Down Expand Up @@ -31,5 +32,19 @@
(if (:help opts)
(usage-help)
(do
(status/line :detail "Static dir: %s" (:dir opts))
(server/exec opts)))))
(status/line :detail "Test server static dir: %s" (:dir opts))
(let [server (future (server/exec opts))
deadline (+ (System/currentTimeMillis) 15000)
test-url (str "http://localhost:" (:port opts)) ]
(loop []
(let [resp (try (client/get test-url)
(catch Throwable _ :not-ready))]
(when (= :not-ready resp)
(if (< (System/currentTimeMillis) deadline)
(do
(println "- waiting for test-server to be ready" test-url)
(Thread/sleep 1000)
(recur))
(throw (ex-info "Timed out waiting for ready test server" {}))))))
(println "Test server ready")
@server)))))

0 comments on commit 1f99736

Please sign in to comment.