|
2 | 2 | (:require
|
3 | 3 | [clojure.core.async :as async]
|
4 | 4 | [clojure.string :as string]
|
5 |
| - [clojure.test :refer [deftest is testing]] |
| 5 | + [clojure.test :refer [deftest is testing use-fixtures]] |
6 | 6 | [lsp4clj.io-chan :as io-chan]
|
7 | 7 | [lsp4clj.test-helper :as h]))
|
8 | 8 |
|
9 | 9 | (set! *warn-on-reflection* true)
|
10 | 10 |
|
| 11 | +(defn- silence-uncaught-exceptions [f] |
| 12 | + (let [handler (Thread/getDefaultUncaughtExceptionHandler)] |
| 13 | + (Thread/setDefaultUncaughtExceptionHandler |
| 14 | + (reify Thread$UncaughtExceptionHandler |
| 15 | + (uncaughtException [_this _thread _e]))) |
| 16 | + (f) |
| 17 | + (Thread/setDefaultUncaughtExceptionHandler handler))) |
| 18 | + |
| 19 | +(use-fixtures :once silence-uncaught-exceptions) |
| 20 | + |
11 | 21 | (defn ^:private message-lines [arr]
|
12 | 22 | (string/join "\r\n" arr))
|
13 | 23 |
|
14 | 24 | (defn mock-input-stream [^String input]
|
15 | 25 | (.getBytes input "utf-8"))
|
16 | 26 |
|
| 27 | +(defn error-output-stream [] |
| 28 | + (proxy [java.io.OutputStream] [] |
| 29 | + (close [] (throw (java.io.IOException. "close failed"))) |
| 30 | + (flush [] (throw (java.io.IOException. "flush failed"))) |
| 31 | + (write [& _] (throw (java.io.IOException. "write failed"))))) |
| 32 | + |
17 | 33 | (deftest output-stream-should-camel-case-output
|
18 | 34 | (let [output-stream (java.io.ByteArrayOutputStream.)
|
19 | 35 | output-ch (io-chan/output-stream->output-chan output-stream)]
|
|
50 | 66 | "{\"key\":\"äpfel\"}"])
|
51 | 67 | (.toString output-stream "utf-8")))))
|
52 | 68 |
|
| 69 | +(deftest output-stream-error-should-close-output-channel |
| 70 | + (testing "when JSON serialisation fails" |
| 71 | + (let [output-stream (java.io.ByteArrayOutputStream.) |
| 72 | + output-ch (io-chan/output-stream->output-chan output-stream)] |
| 73 | + (async/>!! output-ch {:not-serializable output-stream}) |
| 74 | + (Thread/sleep 200) |
| 75 | + (is (false? (async/put! output-ch {:test "should be closed"}))))) |
| 76 | + (testing "when an I/O exception occurs" |
| 77 | + (let [output-stream (error-output-stream) |
| 78 | + output-ch (io-chan/output-stream->output-chan output-stream)] |
| 79 | + (async/>!! output-ch {:test "ok"}) |
| 80 | + (Thread/sleep 200) |
| 81 | + (is (false? (async/put! output-ch {:test "should be closed"})))))) |
| 82 | + |
53 | 83 | (deftest input-stream-should-kebab-case-input
|
54 | 84 | (let [input-stream (mock-input-stream
|
55 | 85 | (message-lines
|
|
0 commit comments