Skip to content

Instantly share code, notes, and snippets.

@jcrossley3
Created January 27, 2015 15:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jcrossley3/a50dfc633fadb94e9f3f to your computer and use it in GitHub Desktop.
Save jcrossley3/a50dfc633fadb94e9f3f to your computer and use it in GitHub Desktop.
(deftest sse
(let [closed (promise)
result (atom [])
app (fn [req]
(as-sse-channel req
:on-open (fn [ch]
(doseq [x (range 5 0 -1)]
(send! ch x))
(send! ch {:event "close", :data "bye!"})
(send! ch "to detect the client close")),
:on-close (fn [ch _] (deliver closed :success))))
server (run app)
client (event-source "http://localhost:8080")]
(.register client (reify EventListener
(onEvent [_ e]
(swap! result conj (.readData e))
(when (= "close" (.getName e))
(.close client)))))
(.open client)
(is (= :success (deref closed 5000 :fail)))
(is (not (.isOpen client)))
(is (= ["5" "4" "3" "2" "1" "bye!"] @result))
(stop server)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment