Review comment: ensure report is non-nil after in-transaction!.

This commit is contained in:
Nick Alexander 2016-10-13 12:23:25 -07:00
parent cea0e3d60f
commit b20c70fc2a
2 changed files with 20 additions and 4 deletions

View file

@ -586,7 +586,7 @@
(go (go
(>! token-chan (gensym "transactor-token")) (>! token-chan (gensym "transactor-token"))
(loop [] (loop []
(let [token (<! token-chan)] (when-let [token (<! token-chan)]
(when-let [[tx-data result close?] (<! (:transact-chan conn))] (when-let [[tx-data result close?] (<! (:transact-chan conn))]
(let [pair (let [pair
(<! (go-pair ;; Catch exceptions, return the pair. (<! (go-pair ;; Catch exceptions, return the pair.
@ -594,10 +594,14 @@
report (<? (db/in-transaction! report (<? (db/in-transaction!
db db
#(-> (<with db tx-data))))] #(-> (<with db tx-data))))]
;; We only get here if the transaction is committed. (when report
(reset! (:current-db conn) (:db-after report)) ;; <with returns non-nil or throws, but we still check report just in
(>! (:listener-source conn) report) ;; case. Here, in-transaction! function completed and returned non-nil,
;; so the transaction has committed.
(reset! (:current-db conn) (:db-after report))
(>! (:listener-source conn) report))
report)))] report)))]
;; Even when report is nil (transaction not committed), pair is non-nil.
(>! result pair)) (>! result pair))
(>! token-chan token) (>! token-chan token)
(when close? (when close?

View file

@ -209,4 +209,16 @@
(tempids r))) (tempids r)))
(is (= nil (a/poll! lc))))))) (is (= nil (a/poll! lc)))))))
(deftest-db test-failing-transacts conn
(let [{tx0 :tx} (<? (d/<transact! conn test-schema))]
(testing "failing transact throws"
(is (thrown-with-msg?
ExceptionInfo #"expected :db.type/string"
(<? (d/<transact! conn [{:db/id (d/id-literal :db.part/user -1) :name 1}])))))
(testing "transaction after bad transaction is applied"
(<? (d/<transact! conn [{:db/id 101 :name "Petr"}]))
(is (= (<? (<datoms-after (d/db conn) tx0))
#{[101 :name "Petr"]})))))
#_ (time (t/run-tests)) #_ (time (t/run-tests))