-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbank_transaction_good.py
55 lines (40 loc) · 1.55 KB
/
bank_transaction_good.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import threading
BALANCE_START = 10000
AMOUNT_WITHDRAW = 5000
AMOUNT_DEPOSIT = 5000
BALANCE_EXPECTED = BALANCE_START - AMOUNT_WITHDRAW + AMOUNT_DEPOSIT
class BankAccount(object):
def __init__(self, initial_balance=0):
self.lock = threading.Lock()
self._balance = initial_balance
def get_balance(self):
return self._balance
def set_balance(self, balance):
self._balance = balance
def withdraw(amount, account):
with account.lock:
balance = account.get_balance()
new_balance = balance - amount
account.set_balance(new_balance)
def deposit(amount, account):
with account.lock:
balance = account.get_balance()
new_balance = balance + amount
account.set_balance(new_balance)
if __name__ == '__main__':
account = BankAccount(BALANCE_START)
withdraw_thread = threading.Thread(target=withdraw,
args=(AMOUNT_WITHDRAW, account))
deposit_thread = threading.Thread(target=deposit,
args=(AMOUNT_DEPOSIT, account))
withdraw_thread.start()
deposit_thread.start()
withdraw_thread.join()
deposit_thread.join()
final_balance = account.get_balance()
assert final_balance == BALANCE_EXPECTED, "balance mismatch; Expected: %i Found: %i" % (BALANCE_EXPECTED, final_balance)
print "Success!",
# if final_balance != BALANCE_EXPECTED:
# print "Error - balance mismatch; Expected: %i Found: %i" % (BALANCE_EXPECTED, final_balance)
# else:
# print "Success!"