tolitius / mount

managing Clojure and ClojureScript app state since (reset)
Eclipse Public License 1.0
1.22k stars 88 forks source link

boot watch and stop #103

Closed stuartc closed 5 years ago

stuartc commented 5 years ago

Hi there!

I'm trying to get my database connection to be closed off between test runs. I appear to be collecting threads after each run, eventually eating up all my memory.

I've managed to make the situation a lot better by moving the connection inself into a defstate.

However I can't quite figure out how to get stop to be called between test runs. Since boot watch is reloading the code between runs, should I expect stop to just be called automatically? Or do I need to extend/augment boot in order to ensure the state is stopped.

The gist of my applications setup phase looks like this:

(mount/defstate
  mongo-db-conn :start (do
                         (println "starting mongo")
                         (mongo/get-db (-> config :mongo :conn-string)))
                :stop (do
                        (println "stopping mongo")
                        (mongo/disconnect mongo-db-conn)))

(mount/start)
tolitius commented 5 years ago

usually you would have a different/new connection to a database in test(s) which you could start / stop along with other resources in fixtures and then use use-fixtures :each or use-fixtures :once to start stop resources before or after tests:

(defn with-db [f]
  (create-db)
  (f)
  (destroy-db))
(use-fixtures :each with-db)

this way every time boot test runs new db connection will be created and destroyed to run the tests.

stuartc commented 5 years ago

@tolitius awesome that makes a lot of sense, thank you!