KendallB
KendallB

Reputation: 4807

Future in closure not firing (Clojure)

I have a closure in which a future takes a do block. Each function inside the do block is provided by the arguments of the closure:

(defn accept-order
  [persist record track notify log] 
  (fn [sponsor order]
    (let [datetime (to-timestamp (local-now))
          order (merge order {:network_reviewed_at datetime
                              :workflow_state "unconfirmed"
                              :sponsor_id (:id sponsor)})]
      (future
        (do
          (persist order
                   (select-keys order [:network_reviewed_at
                                       :workflow_state
                                       :sponsor_id]))
          (record sponsor order true)
          (track)
          (notify sponsor order)
          (log sponsor order)))
      order)))

No function in the do block is fired. If I deref the future, it works. If I remove the future it works. If I run from a REPL, it works. But if I run lein test, it won't work.

Any ideas? Thank you!

Upvotes: 2

Views: 203

Answers (1)

Michał Marczyk
Michał Marczyk

Reputation: 84331

Adding a (Thread/sleep 2000) to a test invoking your function causes the future to run, so I'd venture a guess that Leiningen is killing the VM before your future gets to run (or at least before it manages to cause its side effects). Leiningen does kill the VM immediately after running tests.

As a side note, you don't need the do. future takes a body, not a single expression.

Upvotes: 4

Related Questions