| 1 |
# Standard Library Imports |
| 2 |
import sys |
| 3 |
from queue import Empty |
| 4 |
from unittest.mock import patch |
| 5 |
|
| 6 |
# Third Party Imports |
| 7 |
import pytest |
| 8 |
|
| 9 |
# Local Imports |
| 10 |
import DisplayCAL.mscms as mscms_module |
| 11 |
from DisplayCAL.mscms import WCSManagerProxy, WCSManagerShutdownError |
| 12 |
|
| 13 |
# Tests including mocks and multiprocess do not work under Windows due to spawn != fork, so this is why some tests are ugly |
| 14 |
|
| 15 |
|
| 16 |
def _mock_wcs_worker_process(request_queue, response_queue, log_queue): |
| 17 |
"""Test replacement for the real _wcs_worker_process.""" |
| 18 |
while True: |
| 19 |
try: |
| 20 |
request = request_queue.get(timeout=1.0) |
| 21 |
except Empty: |
| 22 |
continue |
| 23 |
|
| 24 |
if request is None: |
| 25 |
break |
| 26 |
|
| 27 |
request_id = request["id"] |
| 28 |
method_name = request["method"] |
| 29 |
args = request.get("args", ()) |
| 30 |
kwargs = request.get("kwargs", {}) |
| 31 |
|
| 32 |
response = { |
| 33 |
"type": "resp_success", |
| 34 |
"id": request_id, |
| 35 |
"result": {"method": method_name, "args": args, "kwargs": kwargs}, |
| 36 |
} |
| 37 |
response_queue.put(response) |
| 38 |
|
| 39 |
|
| 40 |
def _oserror_wcs_worker_process(request_queue, response_queue, log_queue): |
| 41 |
while True: |
| 42 |
try: |
| 43 |
request = request_queue.get(timeout=1.0) |
| 44 |
except Empty: |
| 45 |
continue |
| 46 |
if request is None: |
| 47 |
break |
| 48 |
|
| 49 |
request_id = request["id"] |
| 50 |
method_name = request["method"] |
| 51 |
|
| 52 |
if method_name == "SetCalibrationManagementState": |
| 53 |
error = OSError(5, "Access is denied") |
| 54 |
response = { |
| 55 |
"type": "resp_error", |
| 56 |
"id": request_id, |
| 57 |
"error": { |
| 58 |
"type": "OSError", |
| 59 |
"message": str(error), |
| 60 |
"errno": error.errno, |
| 61 |
}, |
| 62 |
} |
| 63 |
else: |
| 64 |
response = { |
| 65 |
"type": "resp_success", |
| 66 |
"id": request_id, |
| 67 |
"result": {"method": method_name}, |
| 68 |
} |
| 69 |
|
| 70 |
response_queue.put(response) |
| 71 |
|
| 72 |
|
| 73 |
@pytest.fixture |
| 74 |
def wcs_manager(): |
| 75 |
original_worker = mscms_module._wcs_worker_process |
| 76 |
mscms_module._wcs_worker_process = _mock_wcs_worker_process |
| 77 |
|
| 78 |
manager = None |
| 79 |
try: |
| 80 |
manager = mscms_module.WCSManager() |
| 81 |
yield manager |
| 82 |
finally: |
| 83 |
if manager: |
| 84 |
manager.shutdown() |
| 85 |
|
| 86 |
mscms_module._wcs_worker_process = original_worker |
| 87 |
|
| 88 |
|
| 89 |
def test_manager_initialization(wcs_manager): |
| 90 |
assert wcs_manager._worker_process.is_alive() |
| 91 |
|
| 92 |
|
| 93 |
def test_successful_method_call(wcs_manager): |
| 94 |
result = wcs_manager.get_calibration_management_state() |
| 95 |
assert result["method"] == "GetCalibrationManagementState" |
| 96 |
assert result["args"] == () |
| 97 |
assert result["kwargs"] == {} |
| 98 |
|
| 99 |
|
| 100 |
def test_method_call_with_args(wcs_manager): |
| 101 |
scope = "user" |
| 102 |
device_key = r"\\?\DISPLAY#..." |
| 103 |
result = wcs_manager.get_default_color_profile(scope, device_key) |
| 104 |
assert result["method"] == "GetDefaultColorProfile" |
| 105 |
assert scope in result["args"] |
| 106 |
assert device_key in result["args"] |
| 107 |
|
| 108 |
|
| 109 |
def test_call_after_shutdown_raises_error(): |
| 110 |
import DisplayCAL.mscms as mscms_module |
| 111 |
|
| 112 |
original_worker = mscms_module._wcs_worker_process |
| 113 |
mscms_module._wcs_worker_process = _mock_wcs_worker_process |
| 114 |
|
| 115 |
manager = None |
| 116 |
try: |
| 117 |
manager = mscms_module.WCSManager() |
| 118 |
|
| 119 |
manager.shutdown() |
| 120 |
|
| 121 |
with pytest.raises( |
| 122 |
WCSManagerShutdownError, match="WCSManager is set to shut down" |
| 123 |
): |
| 124 |
manager.get_calibration_management_state() |
| 125 |
|
| 126 |
finally: |
| 127 |
mscms_module._wcs_worker_process = original_worker |
| 128 |
if manager and not manager._shutdown_event.is_set(): |
| 129 |
manager.shutdown() |
| 130 |
|
| 131 |
|
| 132 |
def test_exception_transparency_oserror(): |
| 133 |
original_worker = mscms_module._wcs_worker_process |
| 134 |
mscms_module._wcs_worker_process = _oserror_wcs_worker_process |
| 135 |
|
| 136 |
manager = None |
| 137 |
try: |
| 138 |
manager = mscms_module.WCSManager() |
| 139 |
|
| 140 |
with pytest.raises(OSError) as exc_info: |
| 141 |
manager.set_calibration_management_state(True) |
| 142 |
|
| 143 |
assert exc_info.value.errno == 5 |
| 144 |
assert "Access is denied" in str(exc_info.value) |
| 145 |
|
| 146 |
finally: |
| 147 |
if manager: |
| 148 |
manager.shutdown() |
| 149 |
mscms_module._wcs_worker_process = original_worker |
| 150 |
|
| 151 |
|
| 152 |
@pytest.fixture |
| 153 |
def clean_wcs_proxy(): |
| 154 |
"""Reset the singleton before and after each test.""" |
| 155 |
WCSManagerProxy._instance = None |
| 156 |
yield |
| 157 |
WCSManagerProxy._instance = None |
| 158 |
|
| 159 |
|
| 160 |
def test_wcs_proxy_lazy_init(clean_wcs_proxy): |
| 161 |
proxy = WCSManagerProxy() |
| 162 |
|
| 163 |
assert WCSManagerProxy._instance is None |
| 164 |
|
| 165 |
with patch("DisplayCAL.mscms.WCSManager") as MockManager: |
| 166 |
mock_instance = MockManager.return_value |
| 167 |
mock_instance.get_calibration_management_state.return_value = True |
| 168 |
|
| 169 |
assert WCSManagerProxy._instance is None |
| 170 |
result = proxy.get_calibration_management_state() |
| 171 |
assert WCSManagerProxy._instance is not None |
| 172 |
|
| 173 |
MockManager.assert_called_once() |
| 174 |
mock_instance.get_calibration_management_state.assert_called_once() |
| 175 |
assert result is True |
| 176 |
|
| 177 |
|
| 178 |
def test_wcs_proxy_singleton(clean_wcs_proxy): |
| 179 |
proxy = WCSManagerProxy() |
| 180 |
proxy2 = WCSManagerProxy() |
| 181 |
|
| 182 |
with patch("DisplayCAL.mscms.WCSManager") as MockManager: |
| 183 |
mock_instance = MockManager.return_value |
| 184 |
mock_instance.get_calibration_management_state.return_value = True |
| 185 |
|
| 186 |
proxy.get_calibration_management_state() |
| 187 |
|
| 188 |
assert proxy._instance == proxy2._instance |