2

我有一个闭包,其中 afuture需要一个do块。do 块中的每个函数都由闭包的参数提供:

(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)))

块中没有函数do被触发。如果我取消未来,它会起作用。如果我删除未来它会起作用。如果我从 REPL 运行,它可以工作。但是,如果我运行lein test,它将无法正常工作。

有任何想法吗?谢谢!

4

1 回答 1

4

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.

于 2013-07-13T04:54:13.407 回答