From 4b6a4b165801731617fd8ca9b020c54aa3aa3eed Mon Sep 17 00:00:00 2001 From: anatoly Date: Wed, 9 Dec 2015 00:06:23 -0500 Subject: [PATCH] bringing all the clj tests home. cljs suite next. --- test/mount/test/cleanup_dirty_states.cljc | 21 +++ test/mount/test/start_with.cljc | 54 ++++++++ test/mount/test/start_without.cljc | 30 +++++ test/mount/test/stop_except.cljc | 39 ++++++ test/mount/test/suspend_resume.cljc | 151 ++++++++++++++++++++++ 5 files changed, 295 insertions(+) create mode 100644 test/mount/test/cleanup_dirty_states.cljc create mode 100644 test/mount/test/start_with.cljc create mode 100644 test/mount/test/start_without.cljc create mode 100644 test/mount/test/stop_except.cljc create mode 100644 test/mount/test/suspend_resume.cljc diff --git a/test/mount/test/cleanup_dirty_states.cljc b/test/mount/test/cleanup_dirty_states.cljc new file mode 100644 index 0000000..55a01eb --- /dev/null +++ b/test/mount/test/cleanup_dirty_states.cljc @@ -0,0 +1,21 @@ +(ns mount.test.cleanup_dirty_states + (:require + #?@(:cljs [[cljs.test :as t :refer-macros [is are deftest testing use-fixtures]] + [mount.core :as mount :refer-macros [defstate]] + [app.websockets :refer [system-a]] + [app.conf :refer [config]] + [app.audit-log :refer [log]]] + :clj [[clojure.test :as t :refer [is are deftest testing use-fixtures]] + [mount.core :as mount :refer [defstate]] + [app.example]]) + [mount.test.helper :refer [dval]])) + +#?(:clj + (deftest cleanup-dirty-states + (let [_ (mount/start)] + (is (not (.isClosed (:server-socket (dval app.example/nrepl))))) + (require 'app.example :reload) + (mount/start) ;; should not result in "BindException Address already in use" since the clean up will stop the previous instance + (is (not (.isClosed (:server-socket (dval app.example/nrepl))))) + (mount/stop) + (is (instance? mount.core.NotStartedState (dval app.example/nrepl)))))) diff --git a/test/mount/test/start_with.cljc b/test/mount/test/start_with.cljc new file mode 100644 index 0000000..aa1628a --- /dev/null +++ b/test/mount/test/start_with.cljc @@ -0,0 +1,54 @@ +(ns mount.test.start-with + (:require + #?@(:cljs [[cljs.test :as t :refer-macros [is are deftest testing use-fixtures]] + [mount.core :as mount :refer-macros [defstate]] + [app.websockets :refer [system-a]] + [app.conf :refer [config]] + [app.audit-log :refer [log]]] + :clj [[clojure.test :as t :refer [is are deftest testing use-fixtures]] + [mount.core :as mount :refer [defstate]] + [app.conf :refer [config]] + [app.nyse :refer [conn]] + [app.example :refer [nrepl]]]) + [mount.test.helper :refer [dval]])) + +(defstate test-conn :start 42 + :stop (constantly 0)) + +(defstate test-nrepl :start []) + +#?(:clj + (deftest start-with + + (testing "should start with substitutes" + (let [_ (mount/start-with {#'app.nyse/conn #'mount.test.start-with/test-conn + #'app.example/nrepl #'mount.test.start-with/test-nrepl})] + (is (map? (dval config))) + (is (vector? (dval nrepl))) + (is (= (dval conn) 42)) + (mount/stop))) + + (testing "should not start the substitute itself" + (let [_ (mount/start-with {#'app.nyse/conn #'mount.test.start-with/test-conn})] + (is (instance? mount.core.NotStartedState (dval test-conn))) + (is (= (dval conn) 42)) + (mount/stop))) + + (testing "should start normally after start-with" + (let [_ (mount/start)] + (is (map? (dval config))) + (is (instance? clojure.tools.nrepl.server.Server (dval nrepl))) + (is (instance? datomic.peer.LocalConnection (dval conn))) + (is (= (dval test-conn )42)) + (is (vector? (dval test-nrepl))) + (mount/stop))) + + (testing "should start-without normally after start-with" + (let [_ (mount/start-without #'mount.test.start-with/test-conn + #'mount.test.start-with/test-nrepl)] + (is (map? (dval config))) + (is (instance? clojure.tools.nrepl.server.Server (dval nrepl))) + (is (instance? datomic.peer.LocalConnection (dval conn))) + (is (instance? mount.core.NotStartedState (dval test-conn))) + (is (instance? mount.core.NotStartedState (dval test-nrepl))) + (mount/stop))))) diff --git a/test/mount/test/start_without.cljc b/test/mount/test/start_without.cljc new file mode 100644 index 0000000..c855508 --- /dev/null +++ b/test/mount/test/start_without.cljc @@ -0,0 +1,30 @@ +(ns mount.test.start-without + (:require + #?@(:cljs [[cljs.test :as t :refer-macros [is are deftest testing use-fixtures]] + [mount.core :as mount :refer-macros [defstate]] + [app.websockets :refer [system-a]] + [app.conf :refer [config]] + [app.audit-log :refer [log]]] + :clj [[clojure.test :as t :refer [is are deftest testing use-fixtures]] + [mount.core :as mount :refer [defstate]] + [app.conf :refer [config]] + [app.nyse :refer [conn]] + [app.example :refer [nrepl]]]) + [mount.test.helper :refer [dval]])) + +#?(:clj + (defn without [f] + (mount/start-without #'app.nyse/conn #'app.example/nrepl) + (f) + (mount/stop))) + + (use-fixtures :once + #?(:cljs {:before #(mount/start-without #'app.websockets/system-a #'app.audit-log/log) + :after mount/stop} + :clj without)) + +#?(:clj + (deftest start-without-states + (is (map? (dval config))) + (is (instance? mount.core.NotStartedState (dval nrepl))) + (is (instance? mount.core.NotStartedState (dval conn))))) diff --git a/test/mount/test/stop_except.cljc b/test/mount/test/stop_except.cljc new file mode 100644 index 0000000..22161ad --- /dev/null +++ b/test/mount/test/stop_except.cljc @@ -0,0 +1,39 @@ +(ns mount.test.stop-except + (:require + #?@(:cljs [[cljs.test :as t :refer-macros [is are deftest testing use-fixtures]] + [mount.core :as mount :refer-macros [defstate]] + [app.websockets :refer [system-a]] + [app.conf :refer [config]] + [app.audit-log :refer [log]]] + :clj [[clojure.test :as t :refer [is are deftest testing use-fixtures]] + [mount.core :as mount :refer [defstate]] + [app.conf :refer [config]] + [app.nyse :refer [conn]] + [app.example :refer [nrepl]]]) + [mount.test.helper :refer [dval]])) + +#?(:clj + (deftest stop-except + + (testing "should stop all except nrepl" + (let [_ (mount/start) + _ (mount/stop-except #'app.nyse/conn #'app.conf/config)] + (is (map? (dval config))) + (is (instance? datomic.peer.LocalConnection (dval conn))) + (is (instance? mount.core.NotStartedState (dval nrepl))) + (mount/stop))) + + (testing "should start normally after stop-except" + (let [_ (mount/start)] + (is (map? (dval config))) + (is (instance? clojure.tools.nrepl.server.Server (dval nrepl))) + (is (instance? datomic.peer.LocalConnection (dval conn))) + (mount/stop))) + + (testing "should stop all normally after stop-except" + (let [_ (mount/start) + _ (mount/stop-except #'app.nyse/conn #'app.conf/config) + _ (mount/stop)] + (is (instance? mount.core.NotStartedState (dval config))) + (is (instance? mount.core.NotStartedState (dval conn))) + (is (instance? mount.core.NotStartedState (dval nrepl))))))) diff --git a/test/mount/test/suspend_resume.cljc b/test/mount/test/suspend_resume.cljc new file mode 100644 index 0000000..6bf3849 --- /dev/null +++ b/test/mount/test/suspend_resume.cljc @@ -0,0 +1,151 @@ +(ns mount.test.suspend-resume + (:require + #?@(:cljs [[cljs.test :as t :refer-macros [is are deftest testing use-fixtures]] + [mount.core :as mount :refer-macros [defstate]] + [app.websockets :refer [system-a]] + [app.conf :refer [config]] + [app.audit-log :refer [log]]] + :clj [[clojure.test :as t :refer [is are deftest testing use-fixtures]] + [mount.core :as mount :refer [defstate]] + [app.conf :refer [config]] + [app.nyse :refer [conn]] + [app.example :refer [nrepl]]]) + [mount.test.helper :refer [dval]])) + +(defn koncat [k s] + (-> (name k) + (str "-" (name s)) + keyword)) + +(defn start [s] (koncat s :started)) +(defn stop [s] (koncat s :stopped)) +(defn suspend [s] (koncat s :suspended)) +(defn resume [s] (koncat s :resumed)) + +(defstate web-server :start (start :w) + :stop (stop :w) + :suspend (suspend :w) + :resume (resume :w)) + +(defstate q-listener :start (start :q) + :stop (stop :q) + :suspend (suspend :q) + :resume (resume :q)) + +(defstate randomizer :start (rand-int 42)) + +#?(:clj + (deftest suspendable-lifecycle + + (testing "should suspend _only suspendable_ states that are currently started" + (let [_ (mount/start) + _ (mount/suspend)] + (is (map? (dval config))) + (is (instance? clojure.tools.nrepl.server.Server (dval nrepl))) + (is (instance? datomic.peer.LocalConnection (dval conn))) + (is (= (dval web-server) :w-suspended)) + (mount/stop))) + + (testing "should resume _only suspendable_ states that are currently suspended" + (let [_ (mount/start) + _ (mount/stop #'app.example/nrepl) + _ (mount/suspend) + _ (mount/resume)] + (is (map? (dval config))) + (is (instance? mount.core.NotStartedState (dval nrepl))) + (is (instance? datomic.peer.LocalConnection (dval conn))) + (is (= (dval web-server) :w-resumed)) + (mount/stop))) + + (testing "should start all the states, except the ones that are currently suspended, should resume them instead" + (let [_ (mount/start) + _ (mount/suspend) + _ (mount/start)] + (is (map? (dval config))) + (is (instance? clojure.tools.nrepl.server.Server (dval nrepl))) + (is (instance? datomic.peer.LocalConnection (dval conn))) + (is (= (dval web-server) :w-resumed)) + (mount/stop))) + + (testing "should stop all: started and suspended" + (let [_ (mount/start) + _ (mount/suspend) + _ (mount/stop)] + (is (instance? mount.core.NotStartedState (dval config))) + (is (instance? mount.core.NotStartedState (dval nrepl))) + (is (instance? mount.core.NotStartedState (dval conn))) + (is (instance? mount.core.NotStartedState (dval web-server))))))) + + +#?(:clj + (deftest suspendable-start-with + + (testing "when replacing a non suspendable state with a suspendable one, + the later should be able to suspend/resume, + the original should not be suspendable after resume and preserve its lifecycle fns after rollback/stop" + (let [_ (mount/start-with {#'app.example/nrepl #'mount.test.suspend-resume/web-server}) + _ (mount/suspend)] + (is (= (dval nrepl) :w-suspended)) + (is (instance? mount.core.NotStartedState (dval web-server))) + (mount/stop) + (mount/start) + (mount/suspend) + (is (instance? clojure.tools.nrepl.server.Server (dval nrepl))) + (is (= (dval web-server) :w-suspended)) + (mount/stop))) + + ;; this is a messy use case, but can still happen especially at REPL time + ;; it also messy, because usually :stop function refers the _original_ state by name (i.e. #(disconnect conn)) + ;; (unchanged/not substituted in its lexical scope), and original state won't be started + (testing "when replacing a suspendable state with a non suspendable one, + the later should not be suspendable, + the original should still be suspendable and preserve its lifecycle fns after the rollback/stop" + (let [_ (mount/start-with {#'mount.test.suspend-resume/web-server #'mount.test.suspend-resume/randomizer}) + _ (mount/suspend)] + (is (integer? (dval web-server))) + (is (instance? mount.core.NotStartedState (dval randomizer))) + (mount/stop) + (mount/start) + (mount/suspend) + (is (integer? (dval randomizer))) + (is (= (dval web-server) :w-suspended)) + (mount/stop))) + + ;; this is a messy use case, but can still happen especially at REPL time + (testing "when replacing a suspended state with a non suspendable started one, + the later should not be suspendable, + the original should still be suspended and preserve its lifecycle fns after the rollback/stop" + (let [_ (mount/start) + _ (mount/suspend) + _ (mount/start-with {#'mount.test.suspend-resume/web-server #'app.nyse/conn}) ;; TODO: good to WARN on started states during "start-with" + _ (mount/suspend)] + (is (instance? datomic.peer.LocalConnection (dval conn))) + (is (= (dval web-server) :w-suspended)) ;; since the "conn" does not have a resume method, so web-server was not started + (mount/stop) + (mount/start) + (mount/suspend) + (is (instance? datomic.peer.LocalConnection (dval conn))) + (is (= (dval web-server) :w-suspended)) + (mount/stop))) + + ;; this is a messy use case, but can still happen especially at REPL time + (testing "when replacing a suspended state with a suspendable one, + the later should be suspendable, + the original should still be suspended and preserve its lifecycle fns after the rollback/stop" + (let [_ (mount/start) + _ (mount/suspend) + _ (mount/start-with {#'mount.test.suspend-resume/web-server + #'mount.test.suspend-resume/q-listener})] ;; TODO: good to WARN on started states during "start-with" + (is (= (dval q-listener) :q-suspended)) + (is (= (dval web-server) :q-resumed)) + (mount/suspend) + (is (= (dval q-listener) :q-suspended)) + (is (= (dval web-server) :q-suspended)) + (mount/stop) + (is (instance? mount.core.NotStartedState (dval web-server))) + (is (instance? mount.core.NotStartedState (dval q-listener))) + (mount/start) + (mount/suspend) + (is (= (dval q-listener) :q-suspended)) + (is (= (dval web-server) :w-suspended)) + (mount/stop)))))