-
Notifications
You must be signed in to change notification settings - Fork 32
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
1 parent
8c5136e
commit 481466b
Showing
4 changed files
with
216 additions
and
108 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import pytest | ||
from anyio import create_task_group, sleep | ||
from jupyverse_api.contents import FileLock | ||
|
||
pytestmark = pytest.mark.anyio | ||
|
||
|
||
async def do_op(operation, file_lock, operations): | ||
op, path = operation | ||
async with file_lock(path): | ||
operations.append(operation + ["start"]) | ||
await sleep(0.1) | ||
operations.append(operation + ["done"]) | ||
|
||
|
||
async def test_file_lock(): | ||
file_lock = FileLock() | ||
|
||
# test concurrent accesses to the same file | ||
path = "path/to/file" | ||
operations = [] | ||
async with create_task_group() as tg: | ||
tg.start_soon(do_op, ["write0", path], file_lock, operations) | ||
await sleep(0.01) | ||
tg.start_soon(do_op, ["write1", path], file_lock, operations) | ||
await sleep(0.01) | ||
tg.start_soon(do_op, ["read0", path], file_lock, operations) | ||
|
||
assert operations == [ | ||
["write0", path, "start"], | ||
["write0", path, "done"], | ||
["write1", path, "start"], | ||
["write1", path, "done"], | ||
["read0", path, "start"], | ||
["read0", path, "done"], | ||
] | ||
|
||
# test concurrent accesses to different files | ||
path0 = "path/to/file0" | ||
path1 = "path/to/file1" | ||
operations = [] | ||
async with create_task_group() as tg: | ||
tg.start_soon(do_op, ["write0", path0], file_lock, operations) | ||
await sleep(0.01) | ||
tg.start_soon(do_op, ["write1", path1], file_lock, operations) | ||
await sleep(0.01) | ||
tg.start_soon(do_op, ["read0", path0], file_lock, operations) | ||
await sleep(0.01) | ||
tg.start_soon(do_op, ["read1", path1], file_lock, operations) | ||
|
||
assert operations == [ | ||
["write0", path0, "start"], | ||
["write1", path1, "start"], | ||
["write0", path0, "done"], | ||
["read0", path0, "start"], | ||
["write1", path1, "done"], | ||
["read1", path1, "start"], | ||
["read0", path0, "done"], | ||
["read1", path1, "done"], | ||
] |
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
Oops, something went wrong.