Fork me on GitHub
#xtdb
<
2021-04-17
>
bartuka01:04:51

I am using a (start-node {}) to create in-memory nodes for testing, but I noticed that writes are not sync operations ans this makes writing setup scenarios a little harder. There are recommendations on how to turn the writes sync or more general setups for unit tests?

nivekuil01:04:37

you can do (crux/await-tx node (crux/submit-tx ..))

🙏 6
Steven Deobald01:04:39

@UBSREKQ5Q We have a "Testing How-To" doc pending. What sort of advice would be most useful for you beyond await-tx on an in-memory node?

bartuka01:04:25

Thanks @U797MAJ8M. The usage of await-tx means that I will need to include specific code to my functions writing to crux to behave different in test? Would be nice if I could pass a node that had "await-tx" true built in hehe

bartuka01:04:27

@U01AVNG2XNF being able to setup an in-memory db has great fit with unit testing using dependency management libraries like Component, Integrant and others

bartuka01:04:14

can't think about anything else that does not depend on specific app code to ease testing

bartuka02:04:54

what I meant in my first comment was something like this:

(defrecord CruxTesting []
  component/Lifecycle
  (start [this]
    (assoc this :tempo/node (with-meta (crux/start-node {}) {:testing true})))
  (stop [this]
    (.close (:tempo/node this))
    (dissoc this :tempo/node)))

(defn save
  [crux-node contract]
  (let [res (crux/submit-tx crux-node [[:crux.tx/put contract]])]
    (if (:testing (meta crux-node))
      (crux/await-tx crux-node res)
      res)))

nivekuil02:04:47

you could also use speculative transactions, I think those would get you the new db instantly: https://opencrux.com/reference/transactions.html#speculative-transactions

bartuka02:04:35

but I think this case is even worst when we think about changes in code base to support tests. The best scenario would be to not change anything and provide an empty db. The function save above should not know that I am testing or not

bartuka02:04:58

no big deal to use something like the meta above, curious to know how people deal with this

nivekuil02:04:08

well, if you want submit-tx to always block you could extend the CruxNode record itself, changing the protocol that controls submit-tx and use that for testing? although I'm not sure what kind of tests you're writing, to me it seems like you could just block in the test code without changing the semantics of application logic

bartuka04:04:32

yes, current tests are calling endpoints and checking expected payload responses. not able to block the submit in there 😕

Darin Douglass10:04:37

You could make a fixture that redefs your save fn:

(defn serial-save-fixture
  [test]
  (let [og-save save]
    (with-redefs [save (fn [& args] (crux/await-tx (apply og-save args)))]
      (test))))
(Sorry for any typos, typing clojure on a phone is difficult)