Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yito88 committed Oct 31, 2024
1 parent 40f5416 commit a6766a0
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 30 deletions.
40 changes: 20 additions & 20 deletions scalardb/src/scalardb/transfer.clj
Original file line number Diff line number Diff line change
Expand Up @@ -125,24 +125,18 @@
(assoc op :type :fail :error {:results results})))))

(defn- read-record
"Read and update the specified record with a transaction.
Return nil if the read fails or a conflict happens."
"Read and update the specified record with a transaction"
[test i]
(try
(let [tx (scalar/start-transaction test)
tx-result (.get tx (prepare-get i))
;; Need Storage API to read the transaction metadata
result (.get @(:storage test) (prepare-get i))]
;; Put the same balance to check conflicts with in-flight transactions
(->> (calc-new-balance tx-result 0)
(prepare-put i)
(.put tx))
(.commit tx)
result)
(catch Exception e
;; Read failure or the transaction conflicted
(warn (.getMessage e))
nil)))
(let [tx (scalar/start-transaction test)
tx-result (.get tx (prepare-get i))
;; Need Storage API to read the transaction metadata
result (.get @(:storage test) (prepare-get i))]
;; Put the same balance to check conflicts with in-flight transactions
(->> (calc-new-balance tx-result 0)
(prepare-put i)
(.put tx))
(.commit tx)
result))

(defn- read-record-with-retry
[test i]
Expand All @@ -151,15 +145,21 @@
(scalar/prepare-transaction-service! test)
(scalar/prepare-storage-service! test))
test
(read-record test i)))
(try
(read-record test i)
(catch Exception e
;; Read failure or the transaction conflicted
(warn (.getMessage e))
nil))))

(defn read-all-with-retry
[test n]
(scalar/check-transaction-connection! test)
(scalar/check-storage-connection! test)
(try
(pmap #(read-record-with-retry test %) (range n))
(catch Exception _ nil)))
(doall (pmap #(read-record-with-retry test %) (range n)))
;; unwrap the exception
(catch java.util.concurrent.ExecutionException e (throw (.getCause e)))))

(defrecord TransferClient [initialized? n initial-balance max-txs]
client/Client
Expand Down
18 changes: 13 additions & 5 deletions scalardb/test/scalardb/transfer_2pc_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -227,18 +227,26 @@
scalar/prepare-transaction-service! (spy/spy)
scalar/prepare-storage-service! (spy/spy)
scalar/start-transaction (spy/stub mock-transaction-throws-exception)]
(let [client (client/open! (transfer-2pc/->TransferClient (atom false) 5 100 1)
nil nil)]
(let [num-accounts 5
client (client/open! (transfer-2pc/->TransferClient (atom false)
num-accounts
100 1)
nil nil)
retries-reconnection (* num-accounts
(+ (quot scalar/RETRIES
scalar/RETRIES_FOR_RECONNECTION)
1))]
(is (thrown? clojure.lang.ExceptionInfo
(client/invoke! client {:db mock-db
:storage (ref mock-storage)}
(#'transfer/get-all {:client client}
nil))))
(is (spy/called-n-times? scalar/exponential-backoff scalar/RETRIES))
(is (spy/called-n-times? scalar/exponential-backoff
(* scalar/RETRIES num-accounts)))
(is (spy/called-n-times? scalar/prepare-transaction-service!
(+ (quot scalar/RETRIES scalar/RETRIES_FOR_RECONNECTION) 1)))
retries-reconnection))
(is (spy/called-n-times? scalar/prepare-storage-service!
(+ (quot scalar/RETRIES scalar/RETRIES_FOR_RECONNECTION) 1))))))
retries-reconnection)))))

(deftest transfer-client-check-tx-test
(with-redefs [scalar/check-transaction-states (spy/stub 1)]
Expand Down
16 changes: 11 additions & 5 deletions scalardb/test/scalardb/transfer_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -197,18 +197,24 @@
scalar/prepare-transaction-service! (spy/spy)
scalar/prepare-storage-service! (spy/spy)
scalar/start-transaction (spy/stub mock-transaction-throws-exception)]
(let [client (client/open! (transfer/->TransferClient (atom false) 5 100 1)
nil nil)]
(let [num-accounts 5
client (client/open! (transfer/->TransferClient (atom false)
num-accounts 100 1)
nil nil)
retries-reconnection (* num-accounts
(+ (quot scalar/RETRIES
scalar/RETRIES_FOR_RECONNECTION)
1))]
(is (thrown? clojure.lang.ExceptionInfo
(client/invoke! client {:db mock-db
:storage (ref mock-storage)}
(#'transfer/get-all {:client client}
nil))))
(is (spy/called-n-times? scalar/exponential-backoff scalar/RETRIES))
(is (spy/called-n-times? scalar/exponential-backoff (* scalar/RETRIES 5)))
(is (spy/called-n-times? scalar/prepare-transaction-service!
(+ (quot scalar/RETRIES scalar/RETRIES_FOR_RECONNECTION) 1)))
retries-reconnection))
(is (spy/called-n-times? scalar/prepare-storage-service!
(+ (quot scalar/RETRIES scalar/RETRIES_FOR_RECONNECTION) 1))))))
retries-reconnection)))))

(deftest transfer-client-check-tx-test
(with-redefs [scalar/check-transaction-states (spy/stub 1)]
Expand Down

0 comments on commit a6766a0

Please sign in to comment.