 (ns catacumba.websocket-tests
-  (:require [clojure.core.async :refer [put! take! chan <! >! go close! go-loop onto-chan timeout]]
+  (:require [clojure.core.async :refer [put! take! chan <! <!! >! >!! go close! go-loop onto-chan timeout thread]]
             [clojure.test :refer :all]
             [clojure.java.io :as io]
             [clojure.pprint :refer [pprint]]
             [clojure.core.async :as async]
-            [clj-http.client :as client]
-            [qbits.jet.client.websocket :as ws]
+            [aleph.http :as http]
+            [manifold.deferred :as d]
+            [manifold.stream :as s]
             [catacumba.core :as ct]
             [catacumba.core-tests :refer [with-server]]))
 

          (handler [context]
            (ct/websocket context websocket))]
    (with-server handler
      (let [p (promise)]
        (ws/connect! "ws://localhost:5050/"
                     (fn [{:keys [in out]}]
                       (go
                         (>! out "PING")
                         (when (= "PONG" (<! in))
                           (close! out)
                           (deliver p true)))))
        (is (deref p 1000 false))))))

(deftest websocket-handshake-websocket-handler
  (letfn [(handler [{:keys [in out]}]

                (close! out))))]
    (with-server (with-meta handler
                   {:handler-type :catacumba/websocket})

      (let [conn @(http/websocket-client "ws://localhost:5050/")]
        (deref (s/put! conn "PING"))
        (let [rsp @(s/take! conn)]
          (is (= "PONG" rsp))
          (s/close! conn))))))

(deftest websockets-backpressure
  (let [p1 (promise)
        p2 (promise)
        p3 (promise)
        p4 (promise)]
    (letfn [(handler [{:keys [in out]}]
              (go
                (let [received (<! in)]
