|
17 | 17 | [db :as db]]))
|
18 | 18 |
|
19 | 19 | (def table-name "accounts")
|
| 20 | +(def err-unable-to-distribute-balances (str |
| 21 | + "Unable to distribute initial balances uniformly " |
| 22 | + "for given total-amount and accounts")) |
20 | 23 |
|
21 | 24 | (defrecord BankClientWithLua [conn]
|
22 | 25 | client/Client
|
|
27 | 30 |
|
28 | 31 | (setup! [this test node]
|
29 | 32 | (locking BankClientWithLua
|
30 |
| - (let [conn (cl/open node test)] |
| 33 | + (let [conn (cl/open node test) |
| 34 | + ; Distribute initial balances uniformly. |
| 35 | + initial-balance-per-account (/ (:total-amount test) |
| 36 | + (count (:accounts test)))] |
| 37 | + assert((integer? initial-balance-per-account) |
| 38 | + err-unable-to-distribute-balances) |
31 | 39 | (Thread/sleep 10000) ; wait for leader election and joining to a cluster
|
32 | 40 | (when (= node (first (db/primaries test)))
|
33 | 41 | (cl/with-conn-failure-retry conn
|
|
37 | 45 | balance INT NOT NULL)")])
|
38 | 46 | (doseq [a (:accounts test)]
|
39 | 47 | (info "Populating account")
|
40 |
| - (sql/insert! conn table-name {:id a |
41 |
| - :balance (if (= a (first (:accounts test))) |
42 |
| - (:total-amount test) |
43 |
| - 0)})))) |
| 48 | + (sql/insert! conn table-name |
| 49 | + {:id a |
| 50 | + :balance initial-balance-per-account})))) |
| 51 | + |
44 | 52 | (assoc this :conn conn :node node))))
|
45 | 53 |
|
46 | 54 | (invoke! [this test op]
|
|
79 | 87 |
|
80 | 88 | (setup! [this test node]
|
81 | 89 | (locking tbl-created?
|
82 |
| - (let [conn (cl/open node test)] |
| 90 | + (let [conn (cl/open node test) |
| 91 | + ; Distribute initial balances uniformly. |
| 92 | + initial-balance-per-account (/ (:total-amount test) |
| 93 | + (count (:accounts test)))] |
| 94 | + assert((integer? initial-balance-per-account) |
| 95 | + err-unable-to-distribute-balances) |
83 | 96 | (Thread/sleep 10000) ; wait for leader election and joining to a cluster
|
84 | 97 | (when (= node (first (db/primaries test)))
|
85 | 98 | (when (compare-and-set! tbl-created? false true)
|
|
92 | 105 | "balance INT NOT NULL)")])
|
93 | 106 | (info "Populating account" a)
|
94 | 107 | (sql/insert! conn (str table-name a)
|
95 |
| - {:id 0 |
96 |
| - :account_id a |
97 |
| - :balance 10}))))) |
| 108 | + {:id 0 |
| 109 | + :account_id a |
| 110 | + :balance initial-balance-per-account}))))) |
| 111 | + |
98 | 112 | (assoc this :conn conn :node node))))
|
99 | 113 |
|
100 | 114 | (invoke! [this test op]
|
|
147 | 161 |
|
148 | 162 | (setup! [this test node]
|
149 | 163 | (locking BankClient
|
150 |
| - (let [conn (cl/open node test)] |
| 164 | + (let [conn (cl/open node test) |
| 165 | + ; Distribute initial balances uniformly. |
| 166 | + initial-balance-per-account (/ (:total-amount test) |
| 167 | + (count (:accounts test)))] |
| 168 | + assert((integer? initial-balance-per-account) |
| 169 | + err-unable-to-distribute-balances) |
151 | 170 | (Thread/sleep 10000) ; wait for leader election and joining to a cluster
|
152 | 171 | (when (= node (first (db/primaries test)))
|
153 | 172 | (cl/with-conn-failure-retry conn
|
|
157 | 176 | balance INT NOT NULL)")])
|
158 | 177 | (doseq [a (:accounts test)]
|
159 | 178 | (info "Populating account")
|
160 |
| - (sql/insert! conn table-name {:id a |
161 |
| - :balance (if (= a (first (:accounts test))) |
162 |
| - (:total-amount test) |
163 |
| - 0)})))) |
| 179 | + (sql/insert! conn table-name |
| 180 | + {:id a |
| 181 | + :balance initial-balance-per-account})))) |
| 182 | + |
164 | 183 | (assoc this :conn conn :node node))))
|
165 | 184 |
|
166 | 185 | (invoke! [this test op]
|
|
209 | 228 |
|
210 | 229 | (setup! [this test node]
|
211 | 230 | (locking tbl-created?
|
212 |
| - (let [conn (cl/open node test)] |
| 231 | + (let [conn (cl/open node test) |
| 232 | + ; Distribute initial balances uniformly. |
| 233 | + initial-balance-per-account (/ (:total-amount test) |
| 234 | + (count (:accounts test)))] |
| 235 | + assert((integer? initial-balance-per-account) |
| 236 | + err-unable-to-distribute-balances) |
213 | 237 | (Thread/sleep 10000) ; wait for leader election and joining to a cluster
|
214 | 238 | (when (= node (first (db/primaries test)))
|
215 | 239 | (when (compare-and-set! tbl-created? false true)
|
|
221 | 245 | "balance INT NOT NULL)")])
|
222 | 246 | (info "Populating account" a)
|
223 | 247 | (sql/insert! conn (str table-name a)
|
224 |
| - {:id 0 |
225 |
| - :balance (if (= a (first (:accounts test))) |
226 |
| - (:total-amount test) |
227 |
| - 0)}))))) |
| 248 | + {:id 0 |
| 249 | + :balance initial-balance-per-account}))))) |
| 250 | + |
228 | 251 | (assoc this :conn conn :node node))))
|
229 | 252 |
|
230 | 253 | (invoke! [this test op]
|
|
0 commit comments