From 9d7c5576f81998c0c40fab57c1b6623ba9bd089f Mon Sep 17 00:00:00 2001 From: anatoly Date: Sat, 21 Nov 2015 12:04:10 -0500 Subject: [PATCH] [#5]: suspendable example app.. [in progress] --- dev/dev.clj | 14 +--- project.clj | 15 +++- test/app/app.clj | 53 +------------ test/app/config.clj | 2 +- test/app/db.clj | 50 ++++++++++++ test/app/nyse.clj | 33 ++++---- test/app/www.clj | 37 +++++++++ test/check/parts_test.clj | 17 ---- test/check/start_with_test.clj | 46 ----------- test/check/start_without_test.clj | 18 ----- test/check/stop_except_test.clj | 31 -------- test/check/suspend_resume_test.clj | 123 ----------------------------- test/resources/config.edn | 4 +- 13 files changed, 128 insertions(+), 315 deletions(-) create mode 100644 test/app/db.clj create mode 100644 test/app/www.clj delete mode 100644 test/check/parts_test.clj delete mode 100644 test/check/start_with_test.clj delete mode 100644 test/check/start_without_test.clj delete mode 100644 test/check/stop_except_test.clj delete mode 100644 test/check/suspend_resume_test.clj diff --git a/dev/dev.clj b/dev/dev.clj index cff03fc..7f8b0d3 100644 --- a/dev/dev.clj +++ b/dev/dev.clj @@ -13,21 +13,15 @@ [clojure.test :as test] ;; [clojure.core.async :refer [>!! ! (routes mount-example-routes) + (handler/site) + (run-jetty {:join? false + :port (:port www)}))) + +(defn resume-nyse [conf] + ;; making decision to whether call start / do something ro resume / or just do nothing + ;; ... + ) + +(defstate nyse-app :start (start-nyse app-config) + :resume (resume-nyse app-config) + :stop (.stop nyse-app)) ;; it's a "org.eclipse.jetty.server.Server" at this point diff --git a/test/check/parts_test.clj b/test/check/parts_test.clj deleted file mode 100644 index a8c506f..0000000 --- a/test/check/parts_test.clj +++ /dev/null @@ -1,17 +0,0 @@ -(ns check.parts-test - (:require [mount.core :as mount :refer [defstate] :as m] - [app.nyse :refer [conn]] - [clojure.test :refer :all])) - -(defstate should-not-start :start (constantly 42)) - -(defn with-parts [f] - (m/start #'app.config/app-config #'app.nyse/conn) - (f) - (m/stop)) - -(use-fixtures :each with-parts) - -(deftest start-only-parts - (is (instance? datomic.peer.LocalConnection conn)) - (is (instance? mount.core.NotStartedState should-not-start))) diff --git a/test/check/start_with_test.clj b/test/check/start_with_test.clj deleted file mode 100644 index b5fe507..0000000 --- a/test/check/start_with_test.clj +++ /dev/null @@ -1,46 +0,0 @@ -(ns check.start-with-test - (:require [mount.core :as mount :refer [defstate]] - [app.config :refer [app-config]] - [app.nyse :refer [conn]] - [app :refer [nrepl]] - [clojure.test :refer :all])) - -(defstate test-conn :start (long 42) - :stop (constantly 0)) - -(defstate test-nrepl :start (vector)) - -(deftest start-with - - (testing "should start with substitutes" - (let [_ (mount/start-with {#'app.nyse/conn #'check.start-with-test/test-conn - #'app/nrepl #'check.start-with-test/test-nrepl})] - (is (map? app-config)) - (is (vector? nrepl)) - (is (= conn 42)) - (mount/stop))) - - (testing "should not start the substitute itself" - (let [_ (mount/start-with {#'app.nyse/conn #'check.start-with-test/test-conn})] - (is (instance? mount.core.NotStartedState test-conn)) - (is (= conn 42)) - (mount/stop))) - - (testing "should start normally after start-with" - (let [_ (mount/start)] - (is (map? app-config)) - (is (instance? clojure.tools.nrepl.server.Server nrepl)) - (is (instance? datomic.peer.LocalConnection conn)) - (is (= test-conn 42)) - (is (vector? test-nrepl)) - (mount/stop))) - - (testing "should start-without normally after start-with" - (let [_ (mount/start-without #'check.start-with-test/test-conn - #'check.start-with-test/test-nrepl)] - (is (map? app-config)) - (is (instance? clojure.tools.nrepl.server.Server nrepl)) - (is (instance? datomic.peer.LocalConnection conn)) - (is (instance? mount.core.NotStartedState test-conn)) - (is (instance? mount.core.NotStartedState test-nrepl)) - (mount/stop)))) diff --git a/test/check/start_without_test.clj b/test/check/start_without_test.clj deleted file mode 100644 index 1459dae..0000000 --- a/test/check/start_without_test.clj +++ /dev/null @@ -1,18 +0,0 @@ -(ns check.start-without-test - (:require [mount.core :as m] - [app.config :refer [app-config]] - [app.nyse :refer [conn]] - [app :refer [nrepl]] - [clojure.test :refer :all])) - -(defn without [f] - (m/start-without #'app.nyse/conn #'app/nrepl) - (f) - (m/stop)) - -(use-fixtures :each without) - -(deftest start-without-states - (is (map? app-config)) - (is (instance? mount.core.NotStartedState nrepl)) - (is (instance? mount.core.NotStartedState conn))) diff --git a/test/check/stop_except_test.clj b/test/check/stop_except_test.clj deleted file mode 100644 index b314c31..0000000 --- a/test/check/stop_except_test.clj +++ /dev/null @@ -1,31 +0,0 @@ -(ns check.stop-except-test - (:require [mount.core :as mount :refer [defstate]] - [app.config :refer [app-config]] - [app.nyse :refer [conn]] - [app :refer [nrepl]] - [clojure.test :refer :all])) - -(deftest stop-except - - (testing "should stop all except nrepl" - (let [_ (mount/start) - _ (mount/stop-except #'app.nyse/conn #'app.config/app-config)] - (is (map? app-config)) - (is (instance? datomic.peer.LocalConnection conn)) - (is (instance? mount.core.NotStartedState nrepl)) - (mount/stop))) - - (testing "should start normally after stop-except" - (let [_ (mount/start)] - (is (map? app-config)) - (is (instance? clojure.tools.nrepl.server.Server nrepl)) - (is (instance? datomic.peer.LocalConnection conn)) - (mount/stop))) - - (testing "should stop all normally after stop-except" - (let [_ (mount/start) - _ (mount/stop-except #'app.nyse/conn #'app.config/app-config) - _ (mount/stop)] - (is (instance? mount.core.NotStartedState app-config)) - (is (instance? mount.core.NotStartedState conn)) - (is (instance? mount.core.NotStartedState nrepl))))) diff --git a/test/check/suspend_resume_test.clj b/test/check/suspend_resume_test.clj deleted file mode 100644 index a4eb724..0000000 --- a/test/check/suspend_resume_test.clj +++ /dev/null @@ -1,123 +0,0 @@ -(ns check.suspend-resume-test - (:require [mount.core :as mount :refer [defstate]] - [app.config :refer [app-config]] - [app.nyse :refer [conn]] - [app :refer [nrepl]] - [clojure.test :refer :all])) - -(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)) - -(deftest suspendable - - ;; lifecycle - - (testing "should suspend _only suspendable_ states that are currently started" - (let [_ (mount/start) - _ (mount/suspend)] - (is (map? app-config)) - (is (instance? clojure.tools.nrepl.server.Server nrepl)) - (is (instance? datomic.peer.LocalConnection conn)) - (is (= web-server :w-suspended)) - (mount/stop))) - - (testing "should resume _only suspendable_ states that are currently suspended" - (let [_ (mount/start) - _ (mount/stop #'app/nrepl) - _ (mount/suspend) - _ (mount/resume)] - (is (map? app-config)) - (is (instance? mount.core.NotStartedState nrepl)) - (is (instance? datomic.peer.LocalConnection conn)) - (is (= 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? app-config)) - (is (instance? clojure.tools.nrepl.server.Server nrepl)) - (is (instance? datomic.peer.LocalConnection conn)) - (is (= 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 app-config)) - (is (instance? mount.core.NotStartedState nrepl)) - (is (instance? mount.core.NotStartedState conn)) - (is (instance? mount.core.NotStartedState web-server)))) - - ;; 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/nrepl #'check.suspend-resume-test/web-server}) - _ (mount/suspend)] - (is (= nrepl :w-suspended)) - (is (instance? mount.core.NotStartedState web-server)) - (mount/stop) - (mount/start) - (mount/suspend) - (is (instance? clojure.tools.nrepl.server.Server nrepl)) - (is (= 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 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 {#'check.suspend-resume-test/web-server #'app.nyse/conn}) ;; TODO: good to WARN on started states during "start-with" - _ (mount/suspend)] - (is (instance? datomic.peer.LocalConnection conn)) - (is (instance? datomic.peer.LocalConnection web-server)) - (mount/stop) - (mount/start) - (mount/suspend) - (is (instance? datomic.peer.LocalConnection conn)) - (is (= 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 {#'check.suspend-resume-test/web-server #'check.suspend-resume-test/q-listener})] ;; TODO: good to WARN on started states during "start-with" - (is (= q-listener :q-suspended)) - (is (= web-server :q-resumed)) - (mount/suspend) - (is (= q-listener :q-suspended)) - (is (= web-server :q-suspended)) - (mount/stop) - (is (instance? mount.core.NotStartedState web-server)) - (is (instance? mount.core.NotStartedState q-listener)) - (mount/start) - (mount/suspend) - (is (= q-listener :q-suspended)) - (is (= web-server :w-suspended)) - (mount/stop)))) diff --git a/test/resources/config.edn b/test/resources/config.edn index e927032..82a8bea 100644 --- a/test/resources/config.edn +++ b/test/resources/config.edn @@ -1,6 +1,8 @@ -{:datomic +{:datomic {:uri "datomic:mem://mount"} + :www {:port 4242} + :h2 {:classname "org.h2.Driver" :subprotocol "h2"