Skip to content

Commit

Permalink
Tests refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
jonpither committed Mar 21, 2016
1 parent 1da9eda commit b783895
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 81 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ pom.xml.asc
/core/target/stale/extract-native.dependencies
/core/.nrepl-port
/core/.lein-failures
/core/.lein-env
77 changes: 77 additions & 0 deletions core/test/cider_spy_nrepl/connectivity_integration_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
(ns cider-spy-nrepl.connectivity-integration-test
(:require [clojure.test :refer :all]
[clojure.tools.nrepl :as nrepl]
[clojure.tools.nrepl.transport :as transport]
[cider-spy-nrepl.middleware.hub-settings :as hub-settings]
[cider-spy-nrepl.hub.server :as hub-server]
[cider-spy-nrepl.nrepl-test-utils :refer [messages-chan! take-from-chan!]]
[cider-spy-nrepl.test-utils :refer [some-eval msgs-by-id alias-and-dev msg->summary
start-up-repl-server stop-repl-server]]))

(defn- wrap-setup-once [f]
(with-redefs [hub-settings/hub-host-and-port (constantly ["localhost" 7778])
cider-spy-nrepl.middleware.alias/alias-from-env (constantly "foodude")]
(let [server (start-up-repl-server)
hub-server (hub-server/start 7778)]
(f)
(stop-repl-server server)
(hub-server/shutdown hub-server))))

(use-fixtures :each wrap-setup-once)

(defn- register-user-on-hub-with-summary [port expected-alias]
(let [transport (nrepl/connect :port port :host "localhost")
msgs-chan (messages-chan! transport)]

;; Create the session:
(transport/send transport {:op "clone" :id "session-create-id"})

(let [session-id (->> msgs-chan (take-from-chan! 1 1000) first :new-session)]
(assert session-id)

;; Register the summary message
(transport/send transport {:session session-id :id "session-msg-ig" :op "cider-spy-summary"})
(assert (->> msgs-chan (take-from-chan! 1 1000) first msg->summary :session :started))

;; Register connection buffer for status messages
(transport/send transport {:session session-id :id "hub-connection-buffer-id" :op "cider-spy-hub-register-connection-buffer"})
(assert (->> msgs-chan (take-from-chan! 1 1000)))

;; Do an eval to prompt connection to the hub:
(transport/send transport (some-eval session-id))
(let [msgs (->> msgs-chan (take-from-chan! 10 2000))]
(assert (= 10 (count msgs)) (count msgs))
(assert (= 2 (count (msgs-by-id "eval-msg" msgs))))
(assert (= 4 (count (->> msgs (msgs-by-id "hub-connection-buffer-id") (filter :value)))))
;; 1 after the eval, 1 after location comes back, 1 after registration
(assert (= 3 (count (->> msgs (msgs-by-id "session-msg-ig")))))
(assert (= expected-alias (->> msgs (filter :hub-registered-alias) first :hub-registered-alias)))
(let [registered-devs (->> msgs
(msgs-by-id "session-msg-ig")
(map msg->summary)
(map :devs)
(mapcat vals)
(map :alias)
(set))]
(assert (registered-devs expected-alias))
[transport msgs-chan session-id registered-devs])))))

(deftest user-registrations
(let [port1 7774
port2 7775
server-1 (start-up-repl-server 7774)
server-2 (start-up-repl-server 7775)]

(try
(let [[transport-for-1 msgs-chan-for-1 session-id-1] (register-user-on-hub-with-summary 7774 "foodude")
[transport-for-2 msgs-chan-for-2 session-id-2 registered-devs] (register-user-on-hub-with-summary 7775 "foodude")]

(is (= #{"foodude" "foodude~2"}))

(stop-repl-server server-2)

(is (= [#{"foodude"} "foodude"]
(->> msgs-chan-for-1 (take-from-chan! 1 2000) first msg->summary alias-and-dev))))
(finally
(stop-repl-server server-1)
(stop-repl-server server-2)))))
61 changes: 2 additions & 59 deletions core/test/cider_spy_nrepl/hub_integration_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,9 @@
[cider-spy-nrepl.middleware.hub-settings :as hub-settings]
[clojure.test :refer :all]
[cider-spy-nrepl.nrepl-test-utils :refer [messages-chan! take-from-chan!]]
[cider-spy-nrepl.test-utils :refer [some-eval start-up-repl-server stop-repl-server msg->summary alias-and-dev msgs-by-id]]
[clojure.tools.nrepl :as nrepl]))

(defn- start-up-repl-server []
(let [server
(nrserver/start-server
:port 7777
:handler (nrserver/default-handler
#'cider-spy-nrepl.middleware.cider-spy-session/wrap-cider-spy-session
#'cider-spy-nrepl.middleware.cider-spy-multi-repl/wrap-multi-repl
#'cider-spy-nrepl.middleware.cider-spy-hub/wrap-cider-spy-hub
#'cider-spy-nrepl.middleware.cider-spy/wrap-cider-spy))]
server))

(defn- stop-repl-server [server]
(nrserver/stop-server server))

(defn- wrap-setup-once [f]
(with-redefs [hub-settings/hub-host-and-port (constantly ["localhost" 7778])
cider-spy-nrepl.middleware.alias/alias-from-env (constantly "foodude")]
Expand All @@ -38,21 +25,6 @@

(use-fixtures :each wrap-setup-once)

(defn- some-eval [session-id]
{:session session-id :ns "clojure.string" :op "eval" :code "( + 1 1)" :file "*cider-repl blog*" :line 12 :column 6 :id "eval-msg"})

(defn- msg->summary [msg]
{:pre [msg (:value msg)]}
(-> msg
:value
(json/parse-string keyword)))

(defn- msgs-by-id [id msgs]
(filter #(= id (:id %)) msgs))

(defn- alias-and-dev [summary-msg]
((juxt (comp set (partial map :alias) vals :devs) (comp :alias :hub-connection)) summary-msg))

(deftest test-connect-to-hub-and-change-alias
(let [transport (nrepl/connect :port 7777 :host "localhost")
session-id (:new-session (first (nrepl/response-seq (transport/send transport {:op "clone" :id "session-create-id"}) 5000)))
Expand Down Expand Up @@ -131,41 +103,12 @@
;; Do an eval to prompt connection to the hub:
(transport/send transport (some-eval session-id))
(let [msgs (->> msgs-chan (take-from-chan! 7 1000))]
(assert (= 7 (count msgs)) (count msgs))
(assert (= 2 (count (msgs-by-id "eval-msg" msgs))))
(assert (= 4 (count (->> msgs (msgs-by-id "hub-connection-buffer-id") (filter :value)))))
(assert (= expected-alias (->> msgs (filter :hub-registered-alias) first :hub-registered-alias))))
[transport msgs-chan session-id])))

;; This is a great test, it's picking up the problem behavour, users not cleanly disconnected

(deftest user-registrations
(let [[transport-for-1 msgs-chan-for-1 session-id-1] (register-user-on-hub "foodude")]

(testing "Summary returns single-dev"
(transport/send transport-for-1 {:session session-id-1 :id "session-msg-ig" :op "cider-spy-summary"})

(is (= [#{"foodude"} "foodude"]
(->> msgs-chan-for-1 (take-from-chan! 1 1000) first msg->summary alias-and-dev))))

(let [[transport-for-2 msgs-chan-for-2 session-id-2] (register-user-on-hub "foodude~2")]
(testing "Summary returns two devs"
(transport/send transport-for-2 {:session session-id-2 :id "session-msg-ig" :op "cider-spy-summary"})

(is (= [#{"foodude" "foodude~2"} "foodude~2"]
(->> msgs-chan-for-2 (take-from-chan! 1 1000) first msg->summary alias-and-dev)))

(is (= [#{"foodude" "foodude~2"} "foodude"]
(->> msgs-chan-for-1 (take-from-chan! 1 1000) first msg->summary alias-and-dev))))

(.close transport-for-2)

(is (= [#{"foodude"} "foodude"]
(->> msgs-chan-for-1 (take-from-chan! 1 2000) first msg->summary alias-and-dev)))
(is (= [#{"foodude"} "foodude"]
(->> msgs-chan-for-1 (take-from-chan! 1 2000) first msg->summary alias-and-dev)))
(is (= [#{"foodude"} "foodude"]
(->> msgs-chan-for-1 (take-from-chan! 1 2000) first msg->summary alias-and-dev))))))

(deftest test-send-messages
(let [[transport-for-1 msgs-chan-for-1 session-id-1] (register-user-on-hub "foodude")
[transport-for-2 msgs-chan-for-2 session-id-2] (register-user-on-hub "foodude~2")]
Expand Down
22 changes: 0 additions & 22 deletions core/test/cider_spy_nrepl/interactions_test.clj

This file was deleted.

35 changes: 35 additions & 0 deletions core/test/cider_spy_nrepl/test_utils.clj
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
(ns cider-spy-nrepl.test-utils
(:require [cheshire.core :as json]
[clojure.tools.nrepl.server :as nrserver]
[cider-spy-nrepl.hub.client :as client]
[cider-spy-nrepl.hub.client-events :as client-events]
[cider-spy-nrepl.hub.server-events :as server-events]
Expand Down Expand Up @@ -91,3 +92,37 @@
:session session}))

cider-chan))

(defn some-eval [session-id]
{:session session-id :ns "clojure.string" :op "eval" :code "( + 1 1)" :file "*cider-repl blog*" :line 12 :column 6 :id "eval-msg"})

(defn msg->summary [msg]
{:pre [msg (:value msg)]}
(-> msg
:value
(json/parse-string keyword)))

(defn msgs-by-id [id msgs]
(filter #(= id (:id %)) msgs))

(defn alias-and-dev [summary-msg]
((juxt (comp set (partial map :alias) vals :devs) (comp :alias :hub-connection)) summary-msg))

(defn start-up-repl-server
([] (start-up-repl-server 7777))
([port]
(let [server
(nrserver/start-server
:port port
:handler (nrserver/default-handler
#'cider-spy-nrepl.middleware.cider-spy-session/wrap-cider-spy-session
#'cider-spy-nrepl.middleware.cider-spy-multi-repl/wrap-multi-repl
#'cider-spy-nrepl.middleware.cider-spy-hub/wrap-cider-spy-hub
#'cider-spy-nrepl.middleware.cider-spy/wrap-cider-spy))]
server)))

(defn stop-repl-server [server]
(try
(nrserver/stop-server server)
(catch Throwable t
(println "Couldn't stop nrepl server"))))

0 comments on commit b783895

Please sign in to comment.