-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathtest_ffi_runtime_host.py
More file actions
56 lines (44 loc) · 1.69 KB
/
Copy pathtest_ffi_runtime_host.py
File metadata and controls
56 lines (44 loc) · 1.69 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
import threading
import time
from unittest.mock import patch
from copilot._ffi_runtime_host import FfiRuntimeHost
class _TestLibrary:
def __init__(self) -> None:
self.allow_close = False
self.close_calls = 0
self.shutdown_calls = 0
self.shutdown = threading.Event()
def connection_close(self, _connection_id: int) -> bool:
self.close_calls += 1
return self.allow_close
def host_shutdown(self, _server_id: int) -> bool:
self.shutdown_calls += 1
self.shutdown.set()
return True
def test_dispose_retains_callback_until_connection_close_succeeds():
library = _TestLibrary()
with (
patch("copilot._ffi_runtime_host._load_library", return_value=library),
patch("copilot._ffi_runtime_host._CLEANUP_RETRY_INTERVAL_SECONDS", 0.01),
):
host = FfiRuntimeHost("test-runtime", None)
callback = object()
host._server_id = 11
host._connection_id = 21
host._outbound_callback = callback
host.dispose()
assert host._outbound_callback is callback
assert host._connection_id == 21
assert library.close_calls == 1
assert library.shutdown_calls == 0
library.allow_close = True
assert library.shutdown.wait(5), "Deferred native cleanup did not complete"
assert host._outbound_callback is None
assert host._connection_id == 0
assert library.close_calls >= 2
assert library.shutdown_calls == 1
close_calls_after_cleanup = library.close_calls
host.dispose()
time.sleep(0.05)
assert library.close_calls == close_calls_after_cleanup
assert library.shutdown_calls == 1