| 1 |
"""Tests for the VideoLUT verify/re-load logic that mitigates issue #694. |
| 2 |
|
| 3 |
The shared detection/recovery helpers live on ``worker.Worker`` and are used |
| 4 |
both by the in-session calibration flow (worker.install_profile) and by the |
| 5 |
profile loader's macOS watch. |
| 6 |
""" |
| 7 |
|
| 8 |
from unittest import mock |
| 9 |
|
| 10 |
from DisplayCAL import config |
| 11 |
from DisplayCAL.profile_loader import ProfileLoader |
| 12 |
from DisplayCAL.worker import Worker, reload_black_videoluts |
| 13 |
|
| 14 |
|
| 15 |
class FakeMultiDisplayWorker: |
| 16 |
"""A stand-in for Worker exposing just the ``displays`` list.""" |
| 17 |
|
| 18 |
def __init__(self, ndisplays): |
| 19 |
self.displays = [f"Display {i + 1}" for i in range(ndisplays)] |
| 20 |
|
| 21 |
|
| 22 |
def _profile_loader_stub(): |
| 23 |
"""A ProfileLoader instance without the heavy wx/thread __init__ side effects.""" |
| 24 |
pl = ProfileLoader.__new__(ProfileLoader) |
| 25 |
pl._shutdown = False |
| 26 |
return pl |
| 27 |
|
| 28 |
|
| 29 |
class FakeExec: |
| 30 |
"""Records dispwin args and feeds canned 'Verify:' output to a Worker.""" |
| 31 |
|
| 32 |
def __init__(self, errors=None, output=None): |
| 33 |
self._errors = errors or [] |
| 34 |
self._output = output or [] |
| 35 |
self.calls = [] |
| 36 |
|
| 37 |
def __call__(self, worker, cmd, args, **kwargs): |
| 38 |
self.calls.append((cmd, args, kwargs)) |
| 39 |
worker.errors = list(self._errors) |
| 40 |
worker.output = list(self._output) |
| 41 |
return True |
| 42 |
|
| 43 |
|
| 44 |
def _worker_with_output(errors=None, output=None): |
| 45 |
"""A Worker whose exec_cmd is replaced by a FakeExec.""" |
| 46 |
worker = Worker.__new__(Worker) |
| 47 |
worker.errors = [] |
| 48 |
worker.output = [] |
| 49 |
fake = FakeExec(errors=errors, output=output) |
| 50 |
worker.exec_cmd = lambda cmd, args, **kw: fake(worker, cmd, args, **kw) |
| 51 |
worker._fake = fake |
| 52 |
return worker |
| 53 |
|
| 54 |
|
| 55 |
def test_discrepancy_parsed_from_not_loaded(): |
| 56 |
worker = _worker_with_output( |
| 57 |
output=["Verify: '/tmp/p.icc' is NOT loaded (discrepancy 8.6%)"] |
| 58 |
) |
| 59 |
assert worker.get_calibration_load_discrepancy("dispwin", 0, "/tmp/p.icc") == 8.6 |
| 60 |
|
| 61 |
|
| 62 |
def test_discrepancy_parsed_from_is_loaded(): |
| 63 |
worker = _worker_with_output( |
| 64 |
output=["Verify: '/tmp/p.icc' IS loaded (discrepancy 0.3%)"] |
| 65 |
) |
| 66 |
assert worker.get_calibration_load_discrepancy("dispwin", 0, "/tmp/p.icc") == 0.3 |
| 67 |
|
| 68 |
|
| 69 |
def test_discrepancy_none_without_videolut_access(): |
| 70 |
worker = _worker_with_output(errors=["We don't have access to the VideoLUT"]) |
| 71 |
assert worker.get_calibration_load_discrepancy("dispwin", 0, "/tmp/p.icc") is None |
| 72 |
|
| 73 |
|
| 74 |
def test_discrepancy_none_when_unparseable(): |
| 75 |
worker = _worker_with_output(output=["some unrelated message"]) |
| 76 |
assert worker.get_calibration_load_discrepancy("dispwin", 0, "/tmp/p.icc") is None |
| 77 |
|
| 78 |
|
| 79 |
def test_verify_uses_verify_only_args(): |
| 80 |
"""Verification must use -V (read-only) and not load (-L would change the LUT).""" |
| 81 |
worker = _worker_with_output( |
| 82 |
output=["Verify: '/tmp/p.icc' IS loaded (discrepancy 0.1%)"] |
| 83 |
) |
| 84 |
worker.get_calibration_load_discrepancy("dispwin", 1, "/tmp/p.icc") |
| 85 |
_cmd, args, _kw = worker._fake.calls[0] |
| 86 |
assert "-V" in args and "-d2" in args and "/tmp/p.icc" in args |
| 87 |
assert "-L" not in args |
| 88 |
|
| 89 |
|
| 90 |
def test_clobbered_true_for_black_screen(): |
| 91 |
config.initcfg() |
| 92 |
worker = _worker_with_output( |
| 93 |
output=["Verify: '/tmp/p.icc' is NOT loaded (discrepancy 98.4%)"] |
| 94 |
) |
| 95 |
assert worker.calibration_is_clobbered("dispwin", 0, "/tmp/p.icc") is True |
| 96 |
|
| 97 |
|
| 98 |
def test_clobbered_false_for_quantization_noise(): |
| 99 |
"""A small discrepancy (quantization) must NOT be treated as clobbered. |
| 100 |
|
| 101 |
dispwin would report this as 'is NOT loaded' (its tolerance is ~0.4%), but |
| 102 |
re-loading on such noise would needlessly re-trigger the macOS bug. |
| 103 |
""" |
| 104 |
config.initcfg() |
| 105 |
worker = _worker_with_output( |
| 106 |
output=["Verify: '/tmp/p.icc' is NOT loaded (discrepancy 3.0%)"] |
| 107 |
) |
| 108 |
assert worker.calibration_is_clobbered("dispwin", 0, "/tmp/p.icc") is False |
| 109 |
|
| 110 |
|
| 111 |
def test_clobbered_false_when_undetermined(): |
| 112 |
config.initcfg() |
| 113 |
worker = _worker_with_output(errors=["We don't have access to the VideoLUT"]) |
| 114 |
assert worker.calibration_is_clobbered("dispwin", 0, "/tmp/p.icc") is False |
| 115 |
|
| 116 |
|
| 117 |
def test_clobbered_threshold_is_configurable(): |
| 118 |
config.initcfg() |
| 119 |
config.setcfg("profile_loader.clobbered_discrepancy_threshold", 5) |
| 120 |
worker = _worker_with_output( |
| 121 |
output=["Verify: '/tmp/p.icc' is NOT loaded (discrepancy 8.6%)"] |
| 122 |
) |
| 123 |
try: |
| 124 |
assert worker.calibration_is_clobbered("dispwin", 0, "/tmp/p.icc") is True |
| 125 |
finally: |
| 126 |
config.setcfg("profile_loader.clobbered_discrepancy_threshold", 25) |
| 127 |
|
| 128 |
|
| 129 |
def test_reload_black_videoluts_noop_off_macos(): |
| 130 |
"""The recovery is macOS-only; elsewhere it does nothing.""" |
| 131 |
with mock.patch("DisplayCAL.worker.sys.platform", "win32"): |
| 132 |
assert reload_black_videoluts("dispwin") == [] |
| 133 |
|
| 134 |
|
| 135 |
def test_reload_black_videoluts_reloads_only_black_displays(): |
| 136 |
"""Only displays whose VideoLUT max is below threshold get re-loaded.""" |
| 137 |
runs = [] |
| 138 |
# display 0 is fine (1.0), display 1 is black (0.0); after one reload all fine. |
| 139 |
maxima_seq = iter([[1.0, 0.0], [1.0, 1.0]]) |
| 140 |
with mock.patch("DisplayCAL.worker.sys.platform", "darwin"): |
| 141 |
reloaded = reload_black_videoluts( |
| 142 |
"dispwin", |
| 143 |
retries=4, |
| 144 |
_maxima_fn=lambda: next(maxima_seq), |
| 145 |
_run=runs.append, |
| 146 |
_sleep=lambda s: None, |
| 147 |
) |
| 148 |
assert reloaded == [1] |
| 149 |
assert runs == [["dispwin", "-v", "-d2", "-L"]] |
| 150 |
|
| 151 |
|
| 152 |
def test_reload_black_videoluts_retries_until_sticks(): |
| 153 |
"""If a re-load gets clobbered again, it retries until it sticks.""" |
| 154 |
runs = [] |
| 155 |
# black, black again after first reload, then clean. |
| 156 |
maxima_seq = iter([[0.0], [0.0], [1.0]]) |
| 157 |
with mock.patch("DisplayCAL.worker.sys.platform", "darwin"): |
| 158 |
reloaded = reload_black_videoluts( |
| 159 |
"dispwin", |
| 160 |
retries=5, |
| 161 |
_maxima_fn=lambda: next(maxima_seq), |
| 162 |
_run=runs.append, |
| 163 |
_sleep=lambda s: None, |
| 164 |
) |
| 165 |
assert reloaded == [0] |
| 166 |
assert runs == [["dispwin", "-v", "-d1", "-L"], ["dispwin", "-v", "-d1", "-L"]] |
| 167 |
|
| 168 |
|
| 169 |
def test_reload_black_videoluts_noop_when_all_fine(): |
| 170 |
"""No re-load when every display has a healthy VideoLUT.""" |
| 171 |
runs = [] |
| 172 |
with mock.patch("DisplayCAL.worker.sys.platform", "darwin"): |
| 173 |
reloaded = reload_black_videoluts( |
| 174 |
"dispwin", |
| 175 |
_maxima_fn=lambda: [1.0, 0.98], |
| 176 |
_run=runs.append, |
| 177 |
_sleep=lambda s: None, |
| 178 |
) |
| 179 |
assert reloaded == [] |
| 180 |
assert runs == [] |
| 181 |
|
| 182 |
|
| 183 |
def test_reload_black_videoluts_ignores_unreadable_displays(): |
| 184 |
"""A display that couldn't be read (None) is left alone.""" |
| 185 |
runs = [] |
| 186 |
with mock.patch("DisplayCAL.worker.sys.platform", "darwin"): |
| 187 |
reloaded = reload_black_videoluts( |
| 188 |
"dispwin", |
| 189 |
_maxima_fn=lambda: [None, 1.0], |
| 190 |
_run=runs.append, |
| 191 |
_sleep=lambda s: None, |
| 192 |
) |
| 193 |
assert reloaded == [] |
| 194 |
assert runs == [] |
| 195 |
|
| 196 |
|
| 197 |
def _maxima_from_clobbered(ndisplays, clobbered): |
| 198 |
"""Build a get_macos_videolut_maxima()-shaped list from a clobbered set.""" |
| 199 |
return [0.0 if i in clobbered else 1.0 for i in range(ndisplays)] |
| 200 |
|
| 201 |
|
| 202 |
def test_clobbered_displays_skips_virtual_displays(): |
| 203 |
"""Virtual displays are skipped even if their VideoLUT reads as black.""" |
| 204 |
worker = FakeMultiDisplayWorker(ndisplays=3) |
| 205 |
pl = _profile_loader_stub() |
| 206 |
with ( |
| 207 |
mock.patch( |
| 208 |
"DisplayCAL.profile_loader.config.is_virtual_display", |
| 209 |
side_effect=lambda i: i == 2, |
| 210 |
), |
| 211 |
mock.patch( |
| 212 |
"DisplayCAL.worker.get_macos_videolut_maxima", |
| 213 |
return_value=_maxima_from_clobbered(3, {0, 2}), |
| 214 |
), |
| 215 |
): |
| 216 |
assert pl._clobbered_displays(worker, "dispwin") == [0] |
| 217 |
|
| 218 |
|
| 219 |
def test_reload_clobbered_displays_batches_simultaneous_clobbers_in_one_pass(): |
| 220 |
"""Two displays black at once are both reloaded within a single call.""" |
| 221 |
worker = FakeMultiDisplayWorker(ndisplays=2) |
| 222 |
clobbered = {0, 1} |
| 223 |
pl = _profile_loader_stub() |
| 224 |
calls = [] |
| 225 |
|
| 226 |
def fake_reload(indices, log=None): |
| 227 |
calls.append(list(indices)) |
| 228 |
for i in indices: |
| 229 |
clobbered.discard(i) |
| 230 |
return list(indices) |
| 231 |
|
| 232 |
with ( |
| 233 |
mock.patch( |
| 234 |
"DisplayCAL.profile_loader.config.is_virtual_display", |
| 235 |
return_value=False, |
| 236 |
), |
| 237 |
mock.patch( |
| 238 |
"DisplayCAL.worker.get_macos_videolut_maxima", |
| 239 |
side_effect=lambda: _maxima_from_clobbered(2, clobbered), |
| 240 |
), |
| 241 |
mock.patch( |
| 242 |
"DisplayCAL.worker.reload_macos_videoluts", side_effect=fake_reload |
| 243 |
), |
| 244 |
mock.patch("DisplayCAL.profile_loader.time.sleep"), |
| 245 |
): |
| 246 |
converged = pl._reload_clobbered_displays( |
| 247 |
worker, "dispwin", max_passes=5, settle=0 |
| 248 |
) |
| 249 |
assert calls == [[0, 1]] |
| 250 |
assert converged is True |
| 251 |
|
| 252 |
|
| 253 |
def test_reload_clobbered_displays_converges_after_a_reload_not_sticking(): |
| 254 |
"""A display whose reload doesn't stick right away is retried until it converges. |
| 255 |
|
| 256 |
Reloading all clobbered displays from one process (rather than one |
| 257 |
dispwin subprocess per display) avoids the deterministic multi-display |
| 258 |
ping-pong that issue #824 was originally about, but the pass loop |
| 259 |
remains as a safety net for a display simply not sticking on the first |
| 260 |
try (e.g. disturbed again before the next scan). |
| 261 |
""" |
| 262 |
worker = FakeMultiDisplayWorker(ndisplays=2) |
| 263 |
clobbered = {0} |
| 264 |
pl = _profile_loader_stub() |
| 265 |
calls = [] |
| 266 |
passes_done = 0 |
| 267 |
|
| 268 |
def fake_reload(indices, log=None): |
| 269 |
nonlocal passes_done |
| 270 |
calls.append(list(indices)) |
| 271 |
for i in indices: |
| 272 |
clobbered.discard(i) |
| 273 |
if passes_done < 2: |
| 274 |
clobbered.add(0) |
| 275 |
return list(indices) |
| 276 |
|
| 277 |
def fake_sleep(_seconds): |
| 278 |
nonlocal passes_done |
| 279 |
passes_done += 1 |
| 280 |
|
| 281 |
with ( |
| 282 |
mock.patch( |
| 283 |
"DisplayCAL.profile_loader.config.is_virtual_display", |
| 284 |
return_value=False, |
| 285 |
), |
| 286 |
mock.patch( |
| 287 |
"DisplayCAL.worker.get_macos_videolut_maxima", |
| 288 |
side_effect=lambda: _maxima_from_clobbered(2, clobbered), |
| 289 |
), |
| 290 |
mock.patch( |
| 291 |
"DisplayCAL.worker.reload_macos_videoluts", side_effect=fake_reload |
| 292 |
), |
| 293 |
mock.patch("DisplayCAL.profile_loader.time.sleep", side_effect=fake_sleep), |
| 294 |
): |
| 295 |
converged = pl._reload_clobbered_displays( |
| 296 |
worker, "dispwin", max_passes=10, settle=0 |
| 297 |
) |
| 298 |
assert not clobbered |
| 299 |
assert calls == [[0], [0], [0]] |
| 300 |
assert converged is True |
| 301 |
|
| 302 |
|
| 303 |
def test_reload_clobbered_displays_stops_after_max_passes_without_hanging(): |
| 304 |
"""A display that never sticks must be bounded, not retried forever.""" |
| 305 |
worker = FakeMultiDisplayWorker(ndisplays=2) |
| 306 |
clobbered = {0} |
| 307 |
pl = _profile_loader_stub() |
| 308 |
calls = [] |
| 309 |
|
| 310 |
def fake_reload(indices, log=None): |
| 311 |
calls.append(list(indices)) |
| 312 |
# Reload "succeeds" but the display is immediately clobbered again. |
| 313 |
return list(indices) |
| 314 |
|
| 315 |
with ( |
| 316 |
mock.patch( |
| 317 |
"DisplayCAL.profile_loader.config.is_virtual_display", |
| 318 |
return_value=False, |
| 319 |
), |
| 320 |
mock.patch( |
| 321 |
"DisplayCAL.worker.get_macos_videolut_maxima", |
| 322 |
side_effect=lambda: _maxima_from_clobbered(2, clobbered), |
| 323 |
), |
| 324 |
mock.patch( |
| 325 |
"DisplayCAL.worker.reload_macos_videoluts", side_effect=fake_reload |
| 326 |
), |
| 327 |
mock.patch("DisplayCAL.profile_loader.time.sleep"), |
| 328 |
): |
| 329 |
converged = pl._reload_clobbered_displays( |
| 330 |
worker, "dispwin", max_passes=4, settle=0 |
| 331 |
) |
| 332 |
assert len(calls) == 4 |
| 333 |
assert clobbered |
| 334 |
assert converged is False |
| 335 |
|
| 336 |
|
| 337 |
def test_reload_clobbered_displays_stops_on_shutdown(): |
| 338 |
"""The shutdown flag must break out of the reload loop promptly.""" |
| 339 |
worker = FakeMultiDisplayWorker(ndisplays=1) |
| 340 |
clobbered = {0} |
| 341 |
pl = _profile_loader_stub() |
| 342 |
calls = [] |
| 343 |
|
| 344 |
def fake_reload(indices, log=None): |
| 345 |
calls.append(list(indices)) |
| 346 |
pl._shutdown = True |
| 347 |
# Still "clobbered" - a real shutdown just stops trying. |
| 348 |
return [] |
| 349 |
|
| 350 |
with ( |
| 351 |
mock.patch( |
| 352 |
"DisplayCAL.profile_loader.config.is_virtual_display", |
| 353 |
return_value=False, |
| 354 |
), |
| 355 |
mock.patch( |
| 356 |
"DisplayCAL.worker.get_macos_videolut_maxima", |
| 357 |
side_effect=lambda: _maxima_from_clobbered(1, clobbered), |
| 358 |
), |
| 359 |
mock.patch( |
| 360 |
"DisplayCAL.worker.reload_macos_videoluts", side_effect=fake_reload |
| 361 |
), |
| 362 |
mock.patch("DisplayCAL.profile_loader.time.sleep"), |
| 363 |
): |
| 364 |
converged = pl._reload_clobbered_displays( |
| 365 |
worker, "dispwin", max_passes=10, settle=0 |
| 366 |
) |
| 367 |
assert calls == [[0]] |
| 368 |
assert converged is True |
| 369 |
|
| 370 |
|
| 371 |
def test_macos_watch_backs_off_after_repeated_non_convergence(): |
| 372 |
"""Non-converging displays trigger a notification and a backoff pause. |
| 373 |
|
| 374 |
Some multi-display Macs never converge (issue #824): reloading display A |
| 375 |
deterministically clobbers display B and vice-versa, confirmed by hand. |
| 376 |
Retrying forever would just keep flickering the screens, so after a few |
| 377 |
consecutive non-converging ticks the watch must stop touching the |
| 378 |
displays for a while and notify the user once, instead of hammering |
| 379 |
dispwin indefinitely. |
| 380 |
""" |
| 381 |
config.initcfg() |
| 382 |
config.setcfg("profile_loader.macos_reapply_watch_interval", 1) |
| 383 |
config.setcfg("profile_loader.macos_reapply_watch_unstable_threshold", 2) |
| 384 |
config.setcfg("profile_loader.macos_reapply_watch_backoff_seconds", 9999) |
| 385 |
|
| 386 |
pl = _profile_loader_stub() |
| 387 |
pl._reload_clobbered_displays = mock.Mock(return_value=False) |
| 388 |
pl._notify_macos_watch_unstable = mock.Mock() |
| 389 |
|
| 390 |
tick_count = {"n": 0} |
| 391 |
|
| 392 |
def fake_is_displaycal_running(): |
| 393 |
tick_count["n"] += 1 |
| 394 |
if tick_count["n"] >= 5: |
| 395 |
pl._shutdown = True |
| 396 |
return False |
| 397 |
|
| 398 |
pl._is_displaycal_running = fake_is_displaycal_running |
| 399 |
|
| 400 |
try: |
| 401 |
with ( |
| 402 |
mock.patch("DisplayCAL.profile_loader.time.sleep"), |
| 403 |
mock.patch("DisplayCAL.worker.get_argyll_util", return_value="dispwin"), |
| 404 |
mock.patch("DisplayCAL.worker.Worker") as mock_worker_cls, |
| 405 |
): |
| 406 |
mock_worker_cls.return_value.enumerate_displays_and_ports = mock.Mock() |
| 407 |
pl._macos_watch() |
| 408 |
# Only the first two ticks actually attempt a reload (that's what |
| 409 |
# trips the unstable threshold); the rest are skipped by the backoff. |
| 410 |
assert pl._reload_clobbered_displays.call_count == 2 |
| 411 |
assert pl._notify_macos_watch_unstable.call_count == 1 |
| 412 |
finally: |
| 413 |
config.setcfg("profile_loader.macos_reapply_watch_interval", 5) |
| 414 |
config.setcfg("profile_loader.macos_reapply_watch_unstable_threshold", 3) |
| 415 |
config.setcfg("profile_loader.macos_reapply_watch_backoff_seconds", 300) |