From 016e20dc52d553049694d7496602dbbccbd6a949 Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Sat, 16 May 2020 23:42:00 +0200 Subject: [PATCH] Fix nil return value --- src/babashka/pods/impl.clj | 17 +++++++++++++---- test-pod/pod/test_pod.clj | 10 ++++++++-- test/babashka/pods/jvm_test.clj | 3 ++- test/babashka/pods/sci_test.clj | 3 ++- test/babashka/pods/test_common.clj | 3 ++- 5 files changed, 27 insertions(+), 9 deletions(-) diff --git a/src/babashka/pods/impl.clj b/src/babashka/pods/impl.clj index 07d2a0e..6d6d968 100644 --- a/src/babashka/pods/impl.clj +++ b/src/babashka/pods/impl.clj @@ -64,12 +64,20 @@ (ex-info message data)) value) chan (get @chans id) + promise? (instance? clojure.lang.IPending chan) out (some-> (get reply "out") bytes->string) err (some-> (get reply "err") bytes->string)] - (when (or value* error?) (async/put! chan value)) - (when (or done? error?) (async/close! chan)) + (when (or value* error?) + (if promise? + (deliver chan value) + (async/put! chan value))) + (when (or done? error?) + (if promise? + (deliver chan nil) ;; some ops don't receive a value but are + ;; still done. + (async/close! chan))) (when out (binding [*out* out-stream] (println out))) @@ -91,14 +99,15 @@ :edn pr-str :json cheshire/generate-string) id (next-id) - chan (async/chan) + chan (if async? (async/chan) + (promise)) _ (swap! chans assoc id chan) _ (write stream {"id" id "op" "invoke" "var" (str pod-var) "args" (write-fn args)})] (if async? chan ;; TODO: https://blog.jakubholy.net/2019/core-async-error-handling/ - (let [v (async/ (get message "var") @@ -107,7 +108,12 @@ "id" id}) (write {"status" ["done"] - "id" id}))) + "id" id})) + pod.test-pod/return-nil + (write + {"status" ["done"] + "id" id + "value" "nil"})) (recur)) :shutdown (System/exit 0)))))))) diff --git a/test/babashka/pods/jvm_test.clj b/test/babashka/pods/jvm_test.clj index 4a4d3bc..a8c1895 100644 --- a/test/babashka/pods/jvm_test.clj +++ b/test/babashka/pods/jvm_test.clj @@ -14,6 +14,7 @@ (is (= '[{:a 1, :b 2} 6 [1 2 3 4 5 6 7 8 9] - "Illegal arguments / {:args (1 2 3)}"] ret)) + "Illegal arguments / {:args (1 2 3)}" + nil] ret)) (is (= "nil\n(\"hello\" \"print\" \"this\" \"debugging\" \"message\")\n" (str out))) (is (= "(\"hello\" \"print\" \"this\" \"error\")\n" (str err))))) diff --git a/test/babashka/pods/sci_test.clj b/test/babashka/pods/sci_test.clj index 034c5eb..407c24e 100644 --- a/test/babashka/pods/sci_test.clj +++ b/test/babashka/pods/sci_test.clj @@ -20,6 +20,7 @@ (is (= '[{:a 1, :b 2} 6 [1 2 3 4 5 6 7 8 9] - "Illegal arguments / {:args (1 2 3)}"] ret)) + "Illegal arguments / {:args (1 2 3)}" + nil] ret)) (is (= "nil\n(\"hello\" \"print\" \"this\" \"debugging\" \"message\")\n" (str out))) (is (= "(\"hello\" \"print\" \"this\" \"error\")\n" (str err))))) diff --git a/test/babashka/pods/test_common.clj b/test/babashka/pods/test_common.clj index d2dcc71..777f2d2 100644 --- a/test/babashka/pods/test_common.clj +++ b/test/babashka/pods/test_common.clj @@ -21,4 +21,5 @@ [(pod/assoc {:a 1} :b 2) (pod.test-pod/add-sync 1 2 3) @stream-results - ex-result]") + ex-result + (pod.test-pod/return-nil)]")