             [clojure.java.jdbc :as sql]))
 
 (set-log-level! (or (System/getenv "LOG_LEVEL") :OFF))
-(def tx-cache (csh/cache "tx-test" :transactional? true))
-(def non-tx-cache (csh/cache "non-tx-test" :transactional? false))
+(def cache (csh/cache "tx-test" :transactional? true))
 (def queue (msg/queue "/queue/test" :durable? false))
 (def local-remote-queue (msg/queue "remote" :durable? false))
 (def conn (msg/context (cond-> {:host "localhost" :xa? true}

(defn verify-success []
  (is (= "kiwi" (msg/receive queue :timeout 1000)))
  (is (= "starfruit" (msg/receive local-remote-queue :timeout 1000)))
  (is (= 1 (:a tx-cache)))
  (is (= "tangerine" (:name (read-thing-from-db spec "tangerine"))))
  (is (= 1 (count-things-in-db spec))))

(defn verify-failure []
  (is (nil? (msg/receive queue :timeout 1000)))
  (is (nil? (msg/receive local-remote-queue :timeout 1000)))
  (is (nil? (:a tx-cache)))
  (is (nil? (read-thing-from-db spec "tangerine")))
  (is (= 0 (count-things-in-db spec))))


  (with-open [_ (msg/listen trigger listener)]
    (msg/publish trigger {:tx? true :throw? true})
    (verify-failure)
    (is (= 10 (:deliveries cache)))))

(deftest transactional-writes-in-listener-should-fail-on-rollback
  (with-open [_ (msg/listen trigger listener)]
    (msg/publish trigger {:tx? true :rollback? true})
    (verify-failure)
    (is (= 1 (:deliveries cache)))))

(deftest non-transactional-writes-in-listener-with-exception
  (with-open [_ (msg/listen trigger listener :mode :auto-ack)]
    (msg/publish trigger {:throw? true})
    (is (= 10 (loop [i 0]
                (Thread/sleep 100)
                (if (or (= 50 i) (= 10 (:deliveries cache)))
                  (:deliveries cache)
                  (recur (inc i))))))
    (is (= 1 (:a cache)))
    (is (= (repeat 10 "kiwi")      (repeatedly 10 #(msg/receive queue))))
    (is (= (repeat 10 "starfruit") (repeatedly 10 #(msg/receive local-remote-queue))))))
