[#336] Add java.lang.Runtime to support shutdown hooks (#338)

This commit is contained in:
Michiel Borkent 2020-04-08 21:18:25 +02:00 committed by GitHub
parent e89782686e
commit 110f6d7644
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 64 additions and 10 deletions

View file

@ -39,6 +39,7 @@
java.lang.Long
java.lang.NumberFormatException
java.lang.Math
java.lang.Runtime
java.lang.RuntimeException
java.util.concurrent.LinkedBlockingQueue
java.lang.Object

View 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)))))

View file

@ -18,6 +18,7 @@
[babashka.impl.nrepl-server :as nrepl-server]
[babashka.impl.pipe-signal-handler :refer [handle-pipe! pipe-signal-received?]]
[babashka.impl.repl :as repl]
[babashka.impl.sigint-handler :as sigint-handler]
[babashka.impl.socket-repl :as socket-repl]
[babashka.impl.test :as t]
[babashka.impl.tools.cli :refer [tools-cli-namespace]]
@ -303,6 +304,7 @@ Everything after that is bound to *command-line-args*."))
(defn main
[& args]
(handle-pipe!)
(sigint-handler/handle-sigint!)
#_(binding [*out* *err*]
(prn "M" (meta (get bindings 'future))))
(binding [*unrestricted* true]
@ -368,6 +370,7 @@ Everything after that is bound to *command-line-args*."))
Math java.lang.Math
NumberFormatException java.lang.NumberFormatException
Object java.lang.Object
Runtime java.lang.Runtime
RuntimeException java.lang.RuntimeException
ProcessBuilder java.lang.ProcessBuilder
String java.lang.String

View file

@ -1,14 +1,14 @@
(ns babashka.http-connection-test
(:require
[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]
(apply tu/bb nil (map str args)))
(deftest open-connection-test
(is (= "\"1\"" (str/trim (bb "-e" "
(is (try (= "\"1\"" (str/trim (bb "-e" "
(require '[cheshire.core :as json])
(let [conn ^java.net.HttpURLConnection (.openConnection (java.net.URL. \"https://postman-echo.com/get?foo=1\"))]
(.setConnectTimeout conn 1000)
@ -18,4 +18,6 @@
err (.getErrorStream conn)
response (json/decode (slurp is) true)]
(-> response :args :foo)))
")))))
")))
(catch Exception e
(str/includes? (str e) "timed out")))))

View file

@ -9,6 +9,9 @@
[clojure.test :as test :refer [deftest is testing]]
[sci.core :as sci]))
#_(defmethod clojure.test/report :begin-test-var [m]
(println (-> m :var meta :name)))
(defn bb [input & args]
(edn/read-string (apply test-utils/bb (when (some? input) (str input)) (map str args))))
@ -376,7 +379,9 @@
(is v))))
(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
(is "foo" (bb nil "(try (throw (ex-info \"foo\" {})) (catch Exception e (.getMessage e)))")))

View file

@ -11,7 +11,9 @@
tmp-dir (System/getProperty "java.io.tmpdir")
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))
conn ^HttpURLConnection (.openConnection ^URL source)]
conn ^HttpURLConnection (.openConnection ^URL source)
_ (.setConnectTimeout conn 2000)
_ (.setReadTimeout conn 2000)]
(.connect conn)
(with-open [is (.getInputStream conn)]
(io/copy is zip-file))
@ -26,6 +28,3 @@
(.delete bb-file)
(.delete zip-file)
(println out))))

View 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)

View 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)))))