Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dissoc sub-components when shutting down a system #11

Closed
wants to merge 1 commit into from
Closed

Dissoc sub-components when shutting down a system #11

wants to merge 1 commit into from

Conversation

r0man
Copy link

@r0man r0man commented May 26, 2014

Hey Stuart,

when developing components/systems with your library I usually
make sure that components are idempotent. In the start function I
usually assoc onto the component, and in the stop function I do
cleanup. Like in the following component:

(defrecord ComponentF [state]
  component/Lifecycle
  (start [this]
    (log 'ComponentF.start this)
    (assoc this ::started? true))
  (stop [this]
    (log 'ComponentF.stop this)
    (dissoc this ::started?)))

(defn component-f []
  (->ComponentF (rand-int Integer/MAX_VALUE)))

Then I usually write tests like this to make sure my system
doesn't break when starting/stopping it multiple times. So
starting an already started system should return the already
started one, and for stopping vice versa.

Given a system that I start and then stop, I would expect to get
the same system back as the one I passed to the start
function. At the moment this is not true, because the sub
components are not dissoced from the stopped system.

(deftest test-idempotence
  (let [system (-> (component/system-map
                    :f-1 (component-f)
                    :f-2 (component-f)
                    :f-3 (component-f))
                   (component/system-using
                    {:f-3 [:f-1 :f-2]}))
        started (component/start system)]
    (is (= started (component/start system)))
    (let [stopped (component/stop started)]
      (is (= system stopped))
      (is (= stopped (component/stop stopped))))))

I think this is the right/expected behaviour. What do you think?
This pull request addresses this problem and dissoces the sub
components after stopping them.

Would you like to merge this? And if so, I would be happy about a
new release. Otherwise this is a great library!

Thanks, Roman.

@r0man
Copy link
Author

r0man commented May 26, 2014

Ahh, just saw this one #9

@stuartsierra
Copy link
Collaborator

Duplicate of #9.

It was never my intent that stop should return the same system you had before calling start. For example, you may have loaded or added configuration parameters during or after start which should persist after stop.

The change in d4f25cb should make it easier to implement this kind of behavior if you want.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants