adding m/start-without

This commit is contained in:
anatoly 2015-11-15 14:40:34 -05:00
parent 9ee066ed96
commit 4ec7bb5e57
4 changed files with 41 additions and 5 deletions

View file

@ -13,11 +13,12 @@
[clojure.test :as test] [clojure.test :as test]
;; [clojure.core.async :refer [>!! <!! >! <! go-loop alt! timeout]] ;; [clojure.core.async :refer [>!! <!! >! <! go-loop alt! timeout]]
[clojure.tools.namespace.repl :as tn] [clojure.tools.namespace.repl :as tn]
[check.parts-test]
[mount] [mount]
[app :refer [create-nyse-schema find-orders add-order]])) ;; <<<< replace this your "app" namespace(s) you want to be available at REPL time [app :refer [create-nyse-schema find-orders add-order]])) ;; <<<< replace this your "app" namespace(s) you want to be available at REPL time
(defn start [] (defn start []
(mount/start)) (mount/start-without #'check.parts-test/should-not-start)) ;; example on how to start app without certain states
(defn stop [] (defn stop []
(mount/stop)) (mount/stop))

View file

@ -1,7 +1,7 @@
(ns mount (ns mount
(:require [clojure.tools.macro :as macro] (:require [clojure.tools.macro :as macro]
[clojure.tools.namespace.repl :refer [disable-reload!]] [clojure.tools.namespace.repl :refer [disable-reload!]]
[clojure.tools.logging :refer [info debug error]])) [clojure.tools.logging :refer [info warn debug error]]))
(disable-reload!) (disable-reload!)
@ -48,12 +48,13 @@
(intern ns (symbol name) s) (intern ns (symbol name) s)
(alter-meta! var assoc :started? true)))) (alter-meta! var assoc :started? true))))
(defn- down [var {:keys [name stop started?]}] (defn- down [var {:keys [ns name stop started?]}]
(when started? (when started?
(info "<< stopping.. " name) (info "<< stopping.. " name)
(when stop (when stop
(try (try
(stop) (stop)
(intern ns (symbol name) (NotStartedState. name))
(catch Throwable t (catch Throwable t
(throw (RuntimeException. (str "could not stop [" name "] due to") t))))) (throw (RuntimeException. (str "could not stop [" name "] due to") t)))))
(alter-meta! var assoc :started? false))) (alter-meta! var assoc :started? false)))
@ -100,3 +101,19 @@
(if (first states) (if (first states)
(start states) (start states)
(start))) (start)))
(defn start-with [with]
(if (seq with)
(let [app (find-all-states)]
;; needs more thinking on merging, since the ns should not change
;; because it could be used in other states, so only start/stop need to be merged
(warn "substituting states is not _yet_ implemented")
(start))
(start)))
(defn start-without [& states]
(if (first states)
(let [app (set (find-all-states))
without (remove (set states) app)]
(apply start without))
(start)))

View file

@ -3,12 +3,12 @@
[app.nyse :refer [conn]] [app.nyse :refer [conn]]
[clojure.test :refer :all])) [clojure.test :refer :all]))
(defstate should-not-start :start (throw (RuntimeException. "should not have been started!"))) (defstate should-not-start :start (constantly 42))
(defn with-parts [f] (defn with-parts [f]
(m/start #'app.config/app-config #'app.nyse/conn) (m/start #'app.config/app-config #'app.nyse/conn)
(f) (f)
(m/stop #'app.config/app-config #'app.nyse/conn)) (m/stop))
(use-fixtures :each with-parts) (use-fixtures :each with-parts)

View file

@ -0,0 +1,18 @@
(ns check.without-test
(:require [mount :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.NotStartedState nrepl))
(is (instance? mount.NotStartedState conn)))