Fix nil return value

This commit is contained in:
Michiel Borkent 2020-05-16 23:42:00 +02:00
parent ed996f6f41
commit 016e20dc52
5 changed files with 27 additions and 9 deletions

View file

@ -64,12 +64,20 @@
(ex-info message data)) (ex-info message data))
value) value)
chan (get @chans id) chan (get @chans id)
promise? (instance? clojure.lang.IPending chan)
out (some-> (get reply "out") out (some-> (get reply "out")
bytes->string) bytes->string)
err (some-> (get reply "err") err (some-> (get reply "err")
bytes->string)] bytes->string)]
(when (or value* error?) (async/put! chan value)) (when (or value* error?)
(when (or done? error?) (async/close! chan)) (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 (when out
(binding [*out* out-stream] (binding [*out* out-stream]
(println out))) (println out)))
@ -91,14 +99,15 @@
:edn pr-str :edn pr-str
:json cheshire/generate-string) :json cheshire/generate-string)
id (next-id) id (next-id)
chan (async/chan) chan (if async? (async/chan)
(promise))
_ (swap! chans assoc id chan) _ (swap! chans assoc id chan)
_ (write stream {"id" id _ (write stream {"id" id
"op" "invoke" "op" "invoke"
"var" (str pod-var) "var" (str pod-var)
"args" (write-fn args)})] "args" (write-fn args)})]
(if async? chan ;; TODO: https://blog.jakubholy.net/2019/core-async-error-handling/ (if async? chan ;; TODO: https://blog.jakubholy.net/2019/core-async-error-handling/
(let [v (async/<!! chan)] (let [v @chan]
(if (instance? Throwable v) (if (instance? Throwable v)
(throw v) (throw v)
v))))) v)))))

View file

@ -56,7 +56,8 @@
{"name" "assoc"} {"name" "assoc"}
{"name" "error"} {"name" "error"}
{"name" "print"} {"name" "print"}
{"name" "print-err"}]}] {"name" "print-err"}
{"name" "return-nil"}]}]
"ops" {"shutdown" {}}}) "ops" {"shutdown" {}}})
(recur)) (recur))
:invoke (let [var (-> (get message "var") :invoke (let [var (-> (get message "var")
@ -107,7 +108,12 @@
"id" id}) "id" id})
(write (write
{"status" ["done"] {"status" ["done"]
"id" id}))) "id" id}))
pod.test-pod/return-nil
(write
{"status" ["done"]
"id" id
"value" "nil"}))
(recur)) (recur))
:shutdown (System/exit 0)))))))) :shutdown (System/exit 0))))))))

View file

@ -14,6 +14,7 @@
(is (= '[{:a 1, :b 2} (is (= '[{:a 1, :b 2}
6 6
[1 2 3 4 5 6 7 8 9] [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 (= "nil\n(\"hello\" \"print\" \"this\" \"debugging\" \"message\")\n" (str out)))
(is (= "(\"hello\" \"print\" \"this\" \"error\")\n" (str err))))) (is (= "(\"hello\" \"print\" \"this\" \"error\")\n" (str err)))))

View file

@ -20,6 +20,7 @@
(is (= '[{:a 1, :b 2} (is (= '[{:a 1, :b 2}
6 6
[1 2 3 4 5 6 7 8 9] [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 (= "nil\n(\"hello\" \"print\" \"this\" \"debugging\" \"message\")\n" (str out)))
(is (= "(\"hello\" \"print\" \"this\" \"error\")\n" (str err))))) (is (= "(\"hello\" \"print\" \"this\" \"error\")\n" (str err)))))

View file

@ -21,4 +21,5 @@
[(pod/assoc {:a 1} :b 2) [(pod/assoc {:a 1} :b 2)
(pod.test-pod/add-sync 1 2 3) (pod.test-pod/add-sync 1 2 3)
@stream-results @stream-results
ex-result]") ex-result
(pod.test-pod/return-nil)]")