parent
e89782686e
commit
110f6d7644
8 changed files with 64 additions and 10 deletions
|
|
@ -39,6 +39,7 @@
|
||||||
java.lang.Long
|
java.lang.Long
|
||||||
java.lang.NumberFormatException
|
java.lang.NumberFormatException
|
||||||
java.lang.Math
|
java.lang.Math
|
||||||
|
java.lang.Runtime
|
||||||
java.lang.RuntimeException
|
java.lang.RuntimeException
|
||||||
java.util.concurrent.LinkedBlockingQueue
|
java.util.concurrent.LinkedBlockingQueue
|
||||||
java.lang.Object
|
java.lang.Object
|
||||||
|
|
|
||||||
14
src/babashka/impl/sigint_handler.clj
Normal file
14
src/babashka/impl/sigint_handler.clj
Normal file
|
|
@ -0,0 +1,14 @@
|
||||||
|
(ns babashka.impl.sigint-handler
|
||||||
|
{:no-doc true}
|
||||||
|
(:import [sun.misc Signal]
|
||||||
|
[sun.misc SignalHandler]))
|
||||||
|
|
||||||
|
(set! *warn-on-reflection* true)
|
||||||
|
|
||||||
|
(defn handle-sigint! []
|
||||||
|
(Signal/handle
|
||||||
|
(Signal. "INT")
|
||||||
|
(reify SignalHandler
|
||||||
|
(handle [_ _]
|
||||||
|
;; This is needed to run shutdown hooks on interrupt, System/exit triggers those
|
||||||
|
(System/exit 0)))))
|
||||||
|
|
@ -18,6 +18,7 @@
|
||||||
[babashka.impl.nrepl-server :as nrepl-server]
|
[babashka.impl.nrepl-server :as nrepl-server]
|
||||||
[babashka.impl.pipe-signal-handler :refer [handle-pipe! pipe-signal-received?]]
|
[babashka.impl.pipe-signal-handler :refer [handle-pipe! pipe-signal-received?]]
|
||||||
[babashka.impl.repl :as repl]
|
[babashka.impl.repl :as repl]
|
||||||
|
[babashka.impl.sigint-handler :as sigint-handler]
|
||||||
[babashka.impl.socket-repl :as socket-repl]
|
[babashka.impl.socket-repl :as socket-repl]
|
||||||
[babashka.impl.test :as t]
|
[babashka.impl.test :as t]
|
||||||
[babashka.impl.tools.cli :refer [tools-cli-namespace]]
|
[babashka.impl.tools.cli :refer [tools-cli-namespace]]
|
||||||
|
|
@ -303,6 +304,7 @@ Everything after that is bound to *command-line-args*."))
|
||||||
(defn main
|
(defn main
|
||||||
[& args]
|
[& args]
|
||||||
(handle-pipe!)
|
(handle-pipe!)
|
||||||
|
(sigint-handler/handle-sigint!)
|
||||||
#_(binding [*out* *err*]
|
#_(binding [*out* *err*]
|
||||||
(prn "M" (meta (get bindings 'future))))
|
(prn "M" (meta (get bindings 'future))))
|
||||||
(binding [*unrestricted* true]
|
(binding [*unrestricted* true]
|
||||||
|
|
@ -368,6 +370,7 @@ Everything after that is bound to *command-line-args*."))
|
||||||
Math java.lang.Math
|
Math java.lang.Math
|
||||||
NumberFormatException java.lang.NumberFormatException
|
NumberFormatException java.lang.NumberFormatException
|
||||||
Object java.lang.Object
|
Object java.lang.Object
|
||||||
|
Runtime java.lang.Runtime
|
||||||
RuntimeException java.lang.RuntimeException
|
RuntimeException java.lang.RuntimeException
|
||||||
ProcessBuilder java.lang.ProcessBuilder
|
ProcessBuilder java.lang.ProcessBuilder
|
||||||
String java.lang.String
|
String java.lang.String
|
||||||
|
|
|
||||||
|
|
@ -1,14 +1,14 @@
|
||||||
(ns babashka.http-connection-test
|
(ns babashka.http-connection-test
|
||||||
(:require
|
(:require
|
||||||
[babashka.test-utils :as tu]
|
[babashka.test-utils :as tu]
|
||||||
[clojure.test :as t :refer [deftest is]]
|
[clojure.string :as str]
|
||||||
[clojure.string :as str]))
|
[clojure.test :as t :refer [deftest is]]))
|
||||||
|
|
||||||
(defn bb [& args]
|
(defn bb [& args]
|
||||||
(apply tu/bb nil (map str args)))
|
(apply tu/bb nil (map str args)))
|
||||||
|
|
||||||
(deftest open-connection-test
|
(deftest open-connection-test
|
||||||
(is (= "\"1\"" (str/trim (bb "-e" "
|
(is (try (= "\"1\"" (str/trim (bb "-e" "
|
||||||
(require '[cheshire.core :as json])
|
(require '[cheshire.core :as json])
|
||||||
(let [conn ^java.net.HttpURLConnection (.openConnection (java.net.URL. \"https://postman-echo.com/get?foo=1\"))]
|
(let [conn ^java.net.HttpURLConnection (.openConnection (java.net.URL. \"https://postman-echo.com/get?foo=1\"))]
|
||||||
(.setConnectTimeout conn 1000)
|
(.setConnectTimeout conn 1000)
|
||||||
|
|
@ -18,4 +18,6 @@
|
||||||
err (.getErrorStream conn)
|
err (.getErrorStream conn)
|
||||||
response (json/decode (slurp is) true)]
|
response (json/decode (slurp is) true)]
|
||||||
(-> response :args :foo)))
|
(-> response :args :foo)))
|
||||||
")))))
|
")))
|
||||||
|
(catch Exception e
|
||||||
|
(str/includes? (str e) "timed out")))))
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,9 @@
|
||||||
[clojure.test :as test :refer [deftest is testing]]
|
[clojure.test :as test :refer [deftest is testing]]
|
||||||
[sci.core :as sci]))
|
[sci.core :as sci]))
|
||||||
|
|
||||||
|
#_(defmethod clojure.test/report :begin-test-var [m]
|
||||||
|
(println (-> m :var meta :name)))
|
||||||
|
|
||||||
(defn bb [input & args]
|
(defn bb [input & args]
|
||||||
(edn/read-string (apply test-utils/bb (when (some? input) (str input)) (map str args))))
|
(edn/read-string (apply test-utils/bb (when (some? input) (str input)) (map str args))))
|
||||||
|
|
||||||
|
|
@ -164,7 +167,7 @@
|
||||||
|
|
||||||
(deftest future-test
|
(deftest future-test
|
||||||
(is (= 6 (bb nil "@(future (+ 1 2 3))"))))
|
(is (= 6 (bb nil "@(future (+ 1 2 3))"))))
|
||||||
|
|
||||||
(deftest promise-test
|
(deftest promise-test
|
||||||
(is (= :timeout (bb nil "(deref (promise) 1 :timeout)")))
|
(is (= :timeout (bb nil "(deref (promise) 1 :timeout)")))
|
||||||
(is (= :ok (bb nil "(let [x (promise)]
|
(is (= :ok (bb nil "(let [x (promise)]
|
||||||
|
|
@ -376,7 +379,9 @@
|
||||||
(is v))))
|
(is v))))
|
||||||
|
|
||||||
(deftest download-and-extract-test
|
(deftest download-and-extract-test
|
||||||
(is (= 6 (bb nil (io/file "test" "babashka" "scripts" "download_and_extract_zip.bb")))))
|
(is (try (= 6 (bb nil (io/file "test" "babashka" "scripts" "download_and_extract_zip.bb")))
|
||||||
|
(catch Exception e
|
||||||
|
(is (str/includes? (str e) "timed out"))))))
|
||||||
|
|
||||||
(deftest get-message-on-exception-info-test
|
(deftest get-message-on-exception-info-test
|
||||||
(is "foo" (bb nil "(try (throw (ex-info \"foo\" {})) (catch Exception e (.getMessage e)))")))
|
(is "foo" (bb nil "(try (throw (ex-info \"foo\" {})) (catch Exception e (.getMessage e)))")))
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,9 @@
|
||||||
tmp-dir (System/getProperty "java.io.tmpdir")
|
tmp-dir (System/getProperty "java.io.tmpdir")
|
||||||
zip-file (io/file tmp-dir "bb-0.0.78.zip")
|
zip-file (io/file tmp-dir "bb-0.0.78.zip")
|
||||||
source (URL. (format "https://github.com/borkdude/babashka/releases/download/v0.0.78/babashka-0.0.78-%s-amd64.zip" os))
|
source (URL. (format "https://github.com/borkdude/babashka/releases/download/v0.0.78/babashka-0.0.78-%s-amd64.zip" os))
|
||||||
conn ^HttpURLConnection (.openConnection ^URL source)]
|
conn ^HttpURLConnection (.openConnection ^URL source)
|
||||||
|
_ (.setConnectTimeout conn 2000)
|
||||||
|
_ (.setReadTimeout conn 2000)]
|
||||||
(.connect conn)
|
(.connect conn)
|
||||||
(with-open [is (.getInputStream conn)]
|
(with-open [is (.getInputStream conn)]
|
||||||
(io/copy is zip-file))
|
(io/copy is zip-file))
|
||||||
|
|
@ -26,6 +28,3 @@
|
||||||
(.delete bb-file)
|
(.delete bb-file)
|
||||||
(.delete zip-file)
|
(.delete zip-file)
|
||||||
(println out))))
|
(println out))))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
6
test/babashka/scripts/interrupt_handler.bb
Normal file
6
test/babashka/scripts/interrupt_handler.bb
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
(require '[babashka.signal :as signal])
|
||||||
|
|
||||||
|
(signal/add-interrupt-handler! :quit (fn [k] (println "bye1" k)))
|
||||||
|
(signal/add-interrupt-handler! :quit2 (fn [k] (println "bye2" k)))
|
||||||
|
|
||||||
|
(System/exit 0)
|
||||||
24
test/babashka/shutdown_hook_test.clj
Normal file
24
test/babashka/shutdown_hook_test.clj
Normal file
|
|
@ -0,0 +1,24 @@
|
||||||
|
(ns babashka.shutdown-hook-test
|
||||||
|
{:no-doc true}
|
||||||
|
(:import [java.nio.charset Charset])
|
||||||
|
(:require [babashka.test-utils :as tu]
|
||||||
|
[clojure.java.io :as io]
|
||||||
|
[clojure.test :refer [deftest is]]))
|
||||||
|
|
||||||
|
(defn- stream-to-string
|
||||||
|
([in] (stream-to-string in (.name (Charset/defaultCharset))))
|
||||||
|
([in enc]
|
||||||
|
(with-open [bout (java.io.StringWriter.)]
|
||||||
|
(io/copy in bout :encoding enc)
|
||||||
|
(.toString bout))))
|
||||||
|
|
||||||
|
(deftest interrupt-handler-test
|
||||||
|
(let [script "(-> (Runtime/getRuntime) (.addShutdownHook (Thread. #(println \"bye\"))))"
|
||||||
|
pb (ProcessBuilder. (if tu/jvm?
|
||||||
|
["lein" "bb" "-e" script]
|
||||||
|
["./bb" "-e" script]))
|
||||||
|
process (.start pb)
|
||||||
|
output (.getInputStream process)]
|
||||||
|
(when-let [s (not-empty (stream-to-string (.getErrorStream process)))]
|
||||||
|
(prn "ERROR:" s))
|
||||||
|
(is (= "bye\n" (stream-to-string output)))))
|
||||||
Loading…
Reference in a new issue