adding m/start-with

This commit is contained in:
anatoly 2015-11-15 22:28:34 -05:00
parent 46d2f98c73
commit 09e7c78d22
4 changed files with 60 additions and 16 deletions

View file

@ -14,11 +14,14 @@
;; [clojure.core.async :refer [>!! <!! >! <! go-loop alt! timeout]]
[clojure.tools.namespace.repl :as tn]
[check.parts-test]
[check.start-with-test]
[mount]
[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 []
(mount/start-without #'check.parts-test/should-not-start)) ;; example on how to start app without certain states
(mount/start-without #'check.start-with-test/test-conn
#'check.start-with-test/test-nrepl
#'check.parts-test/should-not-start)) ;; example on how to start app without certain states
(defn stop []
(mount/stop))

View file

@ -72,20 +72,33 @@
(map second)
(filter mount-state?)))
;; TODO: narrow down by {:mount {:include-ns
;; {:starts-with ["app.foo" "bar.baz"]
;; :nss ["app.nyse" "app.tools.datomic"] }
;; :exclude-ns
;; {:starts-with ["dont.want.this" "app.debug"]
;; :nss ["dev" "app.stage"]}}}
;;
;; would come from boot/lein dev profile
(defn- bring [states fun order]
(->> states
(sort-by (comp :order meta) order)
(map #(fun % (meta %)))
doall))
(defn- rollback! [state]
(let [{:keys [origin start stop sub?]} (meta state)]
(when origin
(alter-meta! state assoc :origin nil
:start (or (:start origin) start)
:stop (or (:stop origin) stop)))))
(defn- unsub [state]
(when (-> (meta state) :sub?)
(alter-meta! state assoc :sub? nil
:started false)))
(defn- substitute! [state with]
(let [{:keys [start stop] :as origin} (meta state)
m-with (meta with)]
(alter-meta! with assoc :sub? true :started? true) ;; pre: called by "start-with"
(alter-meta! state assoc :origin {:start start
:stop stop}
:start (:start m-with)
:stop (:stop m-with))))
(defn start [& states]
(let [states (or (seq states) (find-all-states))]
(bring states up <)
@ -93,7 +106,9 @@
(defn stop [& states]
(let [states (or states (find-all-states))]
(doall (map unsub states)) ;; unmark substitutions marked by "start-with"
(bring states down >)
(doall (map rollback! states)) ;; restore to origin from "start-with"
:stopped))
(defn start-with-args [xs & states]
@ -103,12 +118,10 @@
(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))
(let [app (find-all-states)]
(doall
(for [[from to] with]
(substitute! from to)))
(start)))
(defn start-without [& states]

View file

@ -0,0 +1,28 @@
(ns check.start-with-test
(:require [mount :refer [defstate] :as m]
[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 [_ (m/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 start normally after start-with"
(let [_ (m/start)]
(is (map? app-config))
(is (instance? clojure.tools.nrepl.server.Server nrepl))
(is (instance? datomic.peer.LocalConnection conn))
(mount/stop))))

View file

@ -1,4 +1,4 @@
(ns check.without-test
(ns check.start-without-test
(:require [mount :as m]
[app.config :refer [app-config]]
[app.nyse :refer [conn]]