This repository has been archived by the owner on Oct 18, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbank_test.py
144 lines (114 loc) · 3.72 KB
/
bank_test.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
from __future__ import print_function
# import platform
import sys
import threading
import time
import unittest
class UnsyncedBankAccount(object):
"""Bank account with no synchronization lock, prone to race condition."""
def __init__(self):
self.is_open = False
self.balance = 0
def get_balance(self):
if self.is_open:
return self.balance
else:
raise ValueError
def open(self):
self.is_open = True
def deposit(self, amount):
if self.is_open and amount > 0:
self.balance += amount
else:
raise ValueError
def withdraw(self, amount):
if self.is_open and 0 < amount <= self.balance:
self.balance -= amount
else:
raise ValueError
def close(self):
self.is_open = False
class SyncedBankAccount(object):
"""Bank account with synchronization strategy, thread-safe."""
def __init__(self):
self.is_open = False
self.balance = 0
self.lock = threading.Lock()
def get_balance(self):
with self.lock:
if self.is_open:
return self.balance
else:
raise ValueError
def open(self):
self.is_open = True
def deposit(self, amount):
with self.lock:
if self.is_open and amount > 0:
self.balance += amount
else:
raise ValueError
def withdraw(self, amount):
with self.lock:
if self.is_open and 0 < amount <= self.balance:
self.balance -= amount
else:
raise ValueError
def close(self):
self.is_open = False
def adjust_balance_concurrently(account):
def transact():
account.deposit(5)
time.sleep(0.001)
account.withdraw(5)
# Greatly improve the chance of an operation being interrupted
# by thread switch, thus testing synchronization effectively.
# Feel free to tweak the parameters below to see their impact.
try:
sys.setswitchinterval(1e-12)
except AttributeError:
# Python 2 compatible
sys.setcheckinterval(1)
threads = []
for _ in range(1000):
t = threading.Thread(target=transact)
threads.append(t)
t.start()
for thread in threads:
thread.join()
class TestBankAccount(unittest.TestCase):
def test_unsynced(self):
account = UnsyncedBankAccount()
account.open()
account.deposit(1000)
for _ in range(10):
adjust_balance_concurrently(account)
self.assertNotEqual(account.balance, 1000)
def test_synced(self):
account = SyncedBankAccount()
account.open()
account.deposit(1000)
for _ in range(10):
adjust_balance_concurrently(account)
self.assertEqual(account.balance, 1000)
if __name__ == '__main__':
unittest.main()
# Initialization
# unsync_account = UnsyncedBankAccount()
# unsync_account.open()
# unsync_account.deposit(1000)
# sync_account = SyncedBankAccount()
# sync_account.open()
# sync_account.deposit(1000)
# Test unsynced bank account
# for _ in range(10):
# adjust_balance_concurrently(unsync_account)
# Test synced bank account
# for _ in range(10):
# adjust_balance_concurrently(sync_account)
# Report results
# print("Python version: {}\n".format(platform.python_version()))
# print("Balance of unsynced account after concurrent transactions:")
# print("{}. Expected: 1000\n".format(unsync_account.balance))
# print("Balance of synced account after concurrent transactions:")
# print("{}. Expected: 1000\n".format(sync_account.balance))