Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
f7eaa0c6
Commit
f7eaa0c6
authored
Jul 03, 2014
by
Berker Peksag
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue #21755: Skip {Frozen,Source}_DeadlockAvoidanceTests tests when
Python is built without threads.
parent
748ff8bf
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
78 additions
and
66 deletions
+78
-66
Lib/test/test_importlib/test_locks.py
Lib/test/test_importlib/test_locks.py
+78
-66
No files found.
Lib/test/test_importlib/test_locks.py
View file @
f7eaa0c6
...
...
@@ -52,74 +52,86 @@ else:
pass
@
unittest
.
skipUnless
(
threading
,
"threads needed for this test"
)
class
DeadlockAvoidanceTests
:
def
setUp
(
self
):
try
:
self
.
old_switchinterval
=
sys
.
getswitchinterval
()
sys
.
setswitchinterval
(
0.000001
)
except
AttributeError
:
self
.
old_switchinterval
=
None
def
tearDown
(
self
):
if
self
.
old_switchinterval
is
not
None
:
sys
.
setswitchinterval
(
self
.
old_switchinterval
)
def
run_deadlock_avoidance_test
(
self
,
create_deadlock
):
NLOCKS
=
10
locks
=
[
self
.
LockType
(
str
(
i
))
for
i
in
range
(
NLOCKS
)]
pairs
=
[(
locks
[
i
],
locks
[(
i
+
1
)
%
NLOCKS
])
for
i
in
range
(
NLOCKS
)]
if
create_deadlock
:
NTHREADS
=
NLOCKS
else
:
NTHREADS
=
NLOCKS
-
1
barrier
=
threading
.
Barrier
(
NTHREADS
)
results
=
[]
def
_acquire
(
lock
):
"""Try to acquire the lock. Return True on success, False on deadlock."""
if
threading
is
not
None
:
class
DeadlockAvoidanceTests
:
def
setUp
(
self
):
try
:
lock
.
acquire
()
except
self
.
DeadlockError
:
return
False
self
.
old_switchinterval
=
sys
.
getswitchinterval
()
sys
.
setswitchinterval
(
0.000001
)
except
AttributeError
:
self
.
old_switchinterval
=
None
def
tearDown
(
self
):
if
self
.
old_switchinterval
is
not
None
:
sys
.
setswitchinterval
(
self
.
old_switchinterval
)
def
run_deadlock_avoidance_test
(
self
,
create_deadlock
):
NLOCKS
=
10
locks
=
[
self
.
LockType
(
str
(
i
))
for
i
in
range
(
NLOCKS
)]
pairs
=
[(
locks
[
i
],
locks
[(
i
+
1
)
%
NLOCKS
])
for
i
in
range
(
NLOCKS
)]
if
create_deadlock
:
NTHREADS
=
NLOCKS
else
:
return
True
def
f
():
a
,
b
=
pairs
.
pop
()
ra
=
_acquire
(
a
)
barrier
.
wait
()
rb
=
_acquire
(
b
)
results
.
append
((
ra
,
rb
))
if
rb
:
b
.
release
()
if
ra
:
a
.
release
()
lock_tests
.
Bunch
(
f
,
NTHREADS
).
wait_for_finished
()
self
.
assertEqual
(
len
(
results
),
NTHREADS
)
return
results
def
test_deadlock
(
self
):
results
=
self
.
run_deadlock_avoidance_test
(
True
)
# At least one of the threads detected a potential deadlock on its
# second acquire() call. It may be several of them, because the
# deadlock avoidance mechanism is conservative.
nb_deadlocks
=
results
.
count
((
True
,
False
))
self
.
assertGreaterEqual
(
nb_deadlocks
,
1
)
self
.
assertEqual
(
results
.
count
((
True
,
True
)),
len
(
results
)
-
nb_deadlocks
)
def
test_no_deadlock
(
self
):
results
=
self
.
run_deadlock_avoidance_test
(
False
)
self
.
assertEqual
(
results
.
count
((
True
,
False
)),
0
)
self
.
assertEqual
(
results
.
count
((
True
,
True
)),
len
(
results
))
DEADLOCK_ERRORS
=
{
kind
:
splitinit
.
_bootstrap
.
_DeadlockError
for
kind
,
splitinit
in
init
.
items
()}
(
Frozen_DeadlockAvoidanceTests
,
Source_DeadlockAvoidanceTests
)
=
test_util
.
test_both
(
DeadlockAvoidanceTests
,
LockType
=
LOCK_TYPES
,
DeadlockError
=
DEADLOCK_ERRORS
)
NTHREADS
=
NLOCKS
-
1
barrier
=
threading
.
Barrier
(
NTHREADS
)
results
=
[]
def
_acquire
(
lock
):
"""Try to acquire the lock. Return True on success,
False on deadlock."""
try
:
lock
.
acquire
()
except
self
.
DeadlockError
:
return
False
else
:
return
True
def
f
():
a
,
b
=
pairs
.
pop
()
ra
=
_acquire
(
a
)
barrier
.
wait
()
rb
=
_acquire
(
b
)
results
.
append
((
ra
,
rb
))
if
rb
:
b
.
release
()
if
ra
:
a
.
release
()
lock_tests
.
Bunch
(
f
,
NTHREADS
).
wait_for_finished
()
self
.
assertEqual
(
len
(
results
),
NTHREADS
)
return
results
def
test_deadlock
(
self
):
results
=
self
.
run_deadlock_avoidance_test
(
True
)
# At least one of the threads detected a potential deadlock on its
# second acquire() call. It may be several of them, because the
# deadlock avoidance mechanism is conservative.
nb_deadlocks
=
results
.
count
((
True
,
False
))
self
.
assertGreaterEqual
(
nb_deadlocks
,
1
)
self
.
assertEqual
(
results
.
count
((
True
,
True
)),
len
(
results
)
-
nb_deadlocks
)
def
test_no_deadlock
(
self
):
results
=
self
.
run_deadlock_avoidance_test
(
False
)
self
.
assertEqual
(
results
.
count
((
True
,
False
)),
0
)
self
.
assertEqual
(
results
.
count
((
True
,
True
)),
len
(
results
))
DEADLOCK_ERRORS
=
{
kind
:
splitinit
.
_bootstrap
.
_DeadlockError
for
kind
,
splitinit
in
init
.
items
()}
(
Frozen_DeadlockAvoidanceTests
,
Source_DeadlockAvoidanceTests
)
=
test_util
.
test_both
(
DeadlockAvoidanceTests
,
LockType
=
LOCK_TYPES
,
DeadlockError
=
DEADLOCK_ERRORS
)
else
:
DEADLOCK_ERRORS
=
{}
class
Frozen_DeadlockAvoidanceTests
(
unittest
.
TestCase
):
pass
class
Source_DeadlockAvoidanceTests
(
unittest
.
TestCase
):
pass
class
LifetimeTests
:
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment