|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import threading |
| 4 | +import time |
| 5 | + |
| 6 | +import anyio |
| 7 | +import pytest |
| 8 | + |
| 9 | +import typed_diskcache |
| 10 | + |
| 11 | +pytestmark = pytest.mark.anyio |
| 12 | + |
| 13 | + |
| 14 | +def test_lock(cache): |
| 15 | + state = {"num": 0} |
| 16 | + lock = typed_diskcache.SyncLock(cache, "demo") |
| 17 | + |
| 18 | + def worker() -> None: |
| 19 | + state["num"] += 1 |
| 20 | + with lock: |
| 21 | + assert lock.locked |
| 22 | + state["num"] += 1 |
| 23 | + time.sleep(0.1) |
| 24 | + |
| 25 | + with lock: |
| 26 | + thread = threading.Thread(target=worker) |
| 27 | + thread.start() |
| 28 | + time.sleep(0.1) |
| 29 | + assert state["num"] == 1 |
| 30 | + thread.join() |
| 31 | + assert state["num"] == 2 |
| 32 | + |
| 33 | + |
| 34 | +def test_rlock(cache): |
| 35 | + state = {"num": 0} |
| 36 | + rlock = typed_diskcache.SyncRLock(cache, "demo") |
| 37 | + |
| 38 | + def worker() -> None: |
| 39 | + state["num"] += 1 |
| 40 | + with rlock: |
| 41 | + with rlock: |
| 42 | + state["num"] += 1 |
| 43 | + time.sleep(0.1) |
| 44 | + |
| 45 | + with rlock: |
| 46 | + thread = threading.Thread(target=worker) |
| 47 | + thread.start() |
| 48 | + time.sleep(0.1) |
| 49 | + assert state["num"] == 1 |
| 50 | + thread.join() |
| 51 | + assert state["num"] == 2 |
| 52 | + |
| 53 | + |
| 54 | +def test_semaphore(cache): |
| 55 | + state = {"num": 0} |
| 56 | + semaphore = typed_diskcache.SyncSemaphore(cache, "demo", value=3) |
| 57 | + |
| 58 | + def worker() -> None: |
| 59 | + state["num"] += 1 |
| 60 | + with semaphore: |
| 61 | + state["num"] += 1 |
| 62 | + time.sleep(0.1) |
| 63 | + |
| 64 | + semaphore.acquire() |
| 65 | + semaphore.acquire() |
| 66 | + with semaphore: |
| 67 | + thread = threading.Thread(target=worker) |
| 68 | + thread.start() |
| 69 | + time.sleep(0.1) |
| 70 | + assert state["num"] == 1 |
| 71 | + thread.join() |
| 72 | + assert state["num"] == 2 |
| 73 | + semaphore.release() |
| 74 | + semaphore.release() |
| 75 | + |
| 76 | + |
| 77 | +async def test_async_lock(cache): |
| 78 | + state = {"num": 0} |
| 79 | + lock = typed_diskcache.AsyncLock(cache, "demo") |
| 80 | + |
| 81 | + async def worker() -> None: |
| 82 | + state["num"] += 1 |
| 83 | + async with lock: |
| 84 | + assert lock.locked |
| 85 | + state["num"] += 1 |
| 86 | + await anyio.sleep(0.1) |
| 87 | + |
| 88 | + async with lock: |
| 89 | + thread = threading.Thread(target=anyio.run, args=(worker,)) |
| 90 | + thread.start() |
| 91 | + await anyio.sleep(0.1) |
| 92 | + assert state["num"] == 1 |
| 93 | + thread.join() |
| 94 | + assert state["num"] == 2 |
| 95 | + |
| 96 | + |
| 97 | +@pytest.mark.only |
| 98 | +async def test_async_rlock(cache): |
| 99 | + state = {"num": 0} |
| 100 | + rlock = typed_diskcache.AsyncRLock(cache, "demo") |
| 101 | + |
| 102 | + async def worker() -> None: |
| 103 | + state["num"] += 1 |
| 104 | + async with rlock: |
| 105 | + async with rlock: |
| 106 | + state["num"] += 1 |
| 107 | + await anyio.sleep(0.1) |
| 108 | + |
| 109 | + async with rlock: |
| 110 | + thread = threading.Thread(target=anyio.run, args=(worker,)) |
| 111 | + thread.start() |
| 112 | + await anyio.sleep(0.1) |
| 113 | + assert state["num"] == 1 |
| 114 | + thread.join() |
| 115 | + assert state["num"] == 2 |
| 116 | + |
| 117 | + |
| 118 | +async def test_async_semaphore(cache): |
| 119 | + state = {"num": 0} |
| 120 | + semaphore = typed_diskcache.AsyncSemaphore(cache, "demo", value=3) |
| 121 | + |
| 122 | + async def worker() -> None: |
| 123 | + state["num"] += 1 |
| 124 | + async with semaphore: |
| 125 | + state["num"] += 1 |
| 126 | + await anyio.sleep(0.1) |
| 127 | + |
| 128 | + await semaphore.acquire() |
| 129 | + await semaphore.acquire() |
| 130 | + async with semaphore: |
| 131 | + thread = threading.Thread(target=anyio.run, args=(worker,)) |
| 132 | + thread.start() |
| 133 | + await anyio.sleep(0.1) |
| 134 | + assert state["num"] == 1 |
| 135 | + thread.join() |
| 136 | + assert state["num"] == 2 |
| 137 | + await semaphore.release() |
| 138 | + await semaphore.release() |
0 commit comments