-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
134 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import pickle | ||
from textwrap import dedent as D | ||
|
||
|
||
import pytest | ||
|
||
import extrainterpreters as ei | ||
|
||
|
||
from extrainterpreters import Lock, RLock | ||
from extrainterpreters.lock import IntRLock | ||
|
||
@pytest.mark.parametrize("LockCls", [Lock, RLock, IntRLock]) | ||
def test_locks_are_acquireable(LockCls): | ||
lock = LockCls() | ||
assert not lock.locked() | ||
lock.acquire() | ||
assert lock.locked() | ||
lock.release() | ||
assert not lock.locked() | ||
|
||
|
||
@pytest.mark.parametrize("LockCls", [Lock, RLock, IntRLock]) | ||
def test_locks_work_as_context_manager(LockCls): | ||
lock = LockCls() | ||
assert not lock.locked() | ||
with lock: | ||
assert lock.locked() | ||
assert not lock.locked() | ||
|
||
|
||
|
||
def test_lock_cant_be_reacquired(): | ||
lock = Lock() | ||
|
||
lock.acquire() | ||
|
||
with pytest.raises(TimeoutError): | ||
lock.acquire(timeout=0) | ||
|
||
|
||
@pytest.mark.parametrize("LockCls", [Lock, RLock, IntRLock]) | ||
def test_locks_cant_be_passed_to_other_interpreter(LockCls): | ||
lock = LockCls() | ||
interp = ei.Interpreter().start() | ||
interp.run_string( | ||
D( | ||
f""" | ||
import extrainterpreters; extrainterpreters.DEBUG=True | ||
lock = pickle.loads({pickle.dumps(lock)}) | ||
assert lock._lock._data == 0 | ||
""" | ||
) | ||
) | ||
lock._lock._data[0] = 2 | ||
interp.run_string( | ||
D( | ||
""" | ||
assert lock._lock._data[0] == 2 | ||
lock._lock._data[0] = 5 | ||
""" | ||
) | ||
) | ||
assert lock._lock._data[0] == 5 | ||
interp.close() | ||
|
||
|
||
#@pytest.mark.parametrize("LockCls", [Lock, RLock, IntRLock]) | ||
#def test_locks_cant_be_acquired_in_other_interpreter(LockCls): | ||
#lock = LockCls() | ||
#interp = ei.Interpreter().start() | ||
#board.new_item((1, 2)) | ||
#interp.run_string( | ||
#D( | ||
#f""" | ||
#import extrainterpreters; extrainterpreters.DEBUG=True | ||
#lock = pickle.loads({pickle.dumps(lock)}) | ||
|
||
#index, item = board.fetch_item() | ||
#assert item == (1,2) | ||
#board.new_item((3,4)) | ||
#""" | ||
#) | ||
#) |