init
This commit is contained in:
36
tests/test_image_queue.py
Normal file
36
tests/test_image_queue.py
Normal file
@@ -0,0 +1,36 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""图片队列测试"""
|
||||
import sys
|
||||
import asyncio
|
||||
from pathlib import Path
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
|
||||
|
||||
|
||||
async def test_queue_semaphore():
|
||||
"""测试队列并发限制"""
|
||||
from utils.image_queue import init, run_with_queue, release
|
||||
init(max_concurrent=2, max_queue=5)
|
||||
running = 0
|
||||
max_running = 0
|
||||
|
||||
async def fake_task():
|
||||
nonlocal running, max_running
|
||||
running += 1
|
||||
max_running = max(max_running, running)
|
||||
await asyncio.sleep(0.1)
|
||||
running -= 1
|
||||
return "ok"
|
||||
|
||||
results = await asyncio.gather(
|
||||
run_with_queue(fake_task()),
|
||||
run_with_queue(fake_task()),
|
||||
run_with_queue(fake_task()),
|
||||
)
|
||||
assert all(r == "ok" for r in results)
|
||||
assert max_running <= 2
|
||||
print("image queue OK")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(test_queue_semaphore())
|
||||
print("All image queue tests passed")
|
||||
Reference in New Issue
Block a user