Repository navigation
Expand file tree
/
Copy pathtest_result_queue.py
More file actions
74 lines (55 loc) · 1.99 KB
/
Copy pathtest_result_queue.py
File metadata and controls
74 lines (55 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
"""扫描线程用 queue.Queue.put(timeout=),验证协程用 take_queued_result。"""
import asyncio
import os
import queue
import threading
from scanner import ScanResult
from validator_optimized import take_queued_result
def _load_main():
import scanner_main
return scanner_main
KEY = "sk-proj-Qw9Rt8Yu7Ip6Lk5Jh4Gf3Ds2Az1Xc0Vb"
def _item():
return ScanResult(
platform="openai",
api_key=KEY,
base_url="https://api.openai.com/v1",
source_url="https://github.com/example/repo",
)
def test_remote_main_uses_sync_queue():
path = os.path.join(os.path.dirname(__file__), "scanner_main.py")
with open(path, encoding="utf-8") as handle:
source = handle.read().replace("\r\n", "\n")
assert "queue.Queue(maxsize=10000)" in source
assert "from queue_manager import create_queue" not in source
assert "asyncio.Queue(maxsize=10000)" not in source
assert "self.db = Database(config.db_path)" in source
assert "start_scanner(\n self.result_queue,\n self.db," in source
assert source.count("db=self.db") == 5
main = _load_main()
scanner = main.OptimizedSecretScannerV22(target_org="example")
assert isinstance(scanner.result_queue, queue.Queue)
def test_asyncio_queue_put_rejects_timeout():
pending = asyncio.Queue()
try:
pending.put(_item(), timeout=5)
except TypeError:
return
raise AssertionError("asyncio.Queue.put must not accept timeout=")
def test_thread_put_timeout_reaches_take_queued_result():
pending = queue.Queue(maxsize=10000)
item = _item()
errors = []
def producer():
try:
pending.put(item, timeout=5)
except Exception as exc:
errors.append(exc)
thread = threading.Thread(target=producer)
thread.start()
async def consume():
return await take_queued_result(pending, timeout=2.0)
got = asyncio.run(consume())
thread.join(timeout=2)
assert errors == []
assert got is item