forked from ni/nimi-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsystem_test_utilities.py
More file actions
106 lines (86 loc) · 3.34 KB
/
system_test_utilities.py
File metadata and controls
106 lines (86 loc) · 3.34 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import os
import pathlib
import pytest
import re
import subprocess
import threading
import time
class GrpcServerProcess:
def __init__(self, config_file_path):
server_exe = self._get_grpc_server_exe()
self._proc = subprocess.Popen([str(server_exe), config_file_path], stdout=subprocess.PIPE)
# Read/parse output until we find the port number or the process exits; discard the rest.
try:
self.server_port = None
while self.server_port is None and self._proc.poll() is None:
line = self._proc.stdout.readline()
match = re.search(rb"Server listening on port (\d+)", line)
if match:
self.server_port = int(match.group(1))
if self._proc.poll() is not None:
raise RuntimeError(f"Server exited with return code {self._proc.returncode}")
self._stdout_thread = threading.Thread(target=self._discard_output, args=(self._proc.stdout,), daemon=True)
self._stdout_thread.start()
except Exception:
self._proc.kill()
raise
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self._proc.kill()
def _get_grpc_server_exe(self):
if os.name != "nt":
pytest.skip("Only supported on Windows")
import winreg
try:
reg = winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE)
read64key = winreg.KEY_READ | winreg.KEY_WOW64_64KEY
with winreg.OpenKey(reg, r"SOFTWARE\National Instruments\Common\Installer", access=read64key) as key:
shared_dir, _ = winreg.QueryValueEx(key, "NISHAREDDIR64")
except OSError:
pytest.skip("NI gRPC Device Server not installed")
server_exe = pathlib.Path(shared_dir) / "NI gRPC Device Server" / "ni_grpc_device_server.exe"
if not server_exe.exists():
pytest.skip("NI gRPC Device Server not installed")
return server_exe
def _discard_output(self, stdout):
while True:
data = stdout.read(8196)
if not data:
return
def impl_test_multi_threading_lock_unlock(session):
t1_lock_engaged = threading.Event()
release_t1_lock = threading.Event()
def lock_wait_unlock():
session.lock()
t1_lock_engaged.set()
assert session.instrument_manufacturer != ''
release_t1_lock.wait()
session.unlock()
def lock_unlock():
session.lock()
assert session.instrument_model != ''
session.unlock()
# test that lock, unlock functions work properly
t1 = threading.Thread(target=lock_wait_unlock)
t2 = threading.Thread(target=lock_unlock)
t1.start()
t2.start()
t1_lock_engaged.wait()
time.sleep(2.0)
# t1 is blocked by the release event, t2 should be blocked by t1's lock
assert t2.is_alive()
release_t1_lock.set()
t2.join()
assert not t1.is_alive()
assert not t2.is_alive()
def impl_test_multi_threading_ivi_synchronized_wrapper_releases_lock(ivi_method_to_call):
# test that the 2nd thread doesn't hang
t1 = threading.Thread(target=ivi_method_to_call)
t2 = threading.Thread(target=ivi_method_to_call)
t1.start()
t1.join()
assert not t1.is_alive()
t2.start()
t2.join()
assert not t2.is_alive()