forked from natural/java2python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync_test.py
More file actions
executable file
·88 lines (62 loc) · 1.78 KB
/
sync_test.py
File metadata and controls
executable file
·88 lines (62 loc) · 1.78 KB
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
#!/usr/bin/env python
from threading import RLock
_locks = {}
def lock_for_object(obj, locks=_locks):
return locks.setdefault(id(obj), RLock())
def synchronized(call):
def inner(*args, **kwds):
with lock_for_object(call):
return call(*args, **kwds)
return inner
class Main(object):
def __init__(self):
self.attr = object()
def b1(self):
r = []
with lock_for_object(self.attr):
r.append(0)
return r
def b2(self):
r = []
with lock_for_object(self.attr):
r.append(0)
return r
def m1(self):
return id(lock_for_object(self))
def m2(self):
return id(lock_for_object(self))
@classmethod
def c1(cls):
return id(lock_for_object(cls))
@classmethod
def c2(cls):
return id(lock_for_object(cls))
@synchronized
def s1(self, *values, **kwargs):
return [values, kwargs]
@synchronized
def s2(self, *values, **kwargs):
return [values, kwargs]
@classmethod
@synchronized
def cs1(cls, *values, **kwargs):
return [cls, values, kwargs]
@classmethod
@synchronized
def cs2(cls, *values, **kwargs):
return [cls, values, kwargs]
if __name__ == '__main__':
x = Main()
expected_count = 0
assert x.b1() == x.b2()
expected_count += 1 # one for the attr, used twice
assert x.c1() == x.c2()
expected_count += 1 # one for the class, used twice
assert x.m1() == x.m2()
expected_count += 1 # one for the instance, used twice
assert x.s1() == x.s2()
expected_count += 2 # one for each instance method
assert x.cs1() == x.cs2()
expected_count += 2 # one for each class method
assert expected_count == len(_locks)
print '[PASS]'