OverviewFilesCommitsIssuesDocsReleasesPeers

cthuloid / blender-addon / tests / test_disable_forgets.py

50 lines · python ref:
"""Unticking a fixture's Enabled must stop the sender driving it.

Run: <blender> -b --factory-startup --python blender-addon/tests/test_disable_forgets.py

Before this, Enabled only stopped NEW payloads being produced (engine.push_scene
skips disabled fixtures); the sender kept the target and its reconnect loop
forever, so an unplugged unit kept costing a ~5 s mDNS lookup every backoff
window until Blender was restarted.
"""

import sys
import time

import addon_utils
import bpy

MOD = "bl_ext.user_default.cthuloid_lights"
addon_utils.enable(MOD, default_set=False)
net = sys.modules[MOD + ".net"]

HOST = "cthuloid-test-nonexistent.local"   # never resolves: no real fixture touched
PORT = 1337

scene = bpy.context.scene
fixture = scene.cthuloid.fixtures.add()
fixture.id = "test"
fixture.host = HOST
fixture.port = PORT

net.SENDER.send(HOST, PORT, "pwm 0\n")
tgt = net.SENDER._targets.get((HOST, PORT))
assert tgt is not None, "precondition: send() should create the target"

fixture.enabled = False

ok = True
if (HOST, PORT) in net.SENDER._targets:
    print("FAIL: target still registered after Enabled was unticked")
    ok = False
else:
    # Its worker may be inside a ~5 s lookup; it must exit once that returns.
    tgt.thread.join(timeout=8.0)
    if tgt.thread.is_alive():
        print("FAIL: worker thread still running after Enabled was unticked")
        ok = False

addon_utils.disable(MOD, default_set=False)
print("PASS: unticking Enabled released the target" if ok else "FAILED")
sys.exit(0 if ok else 1)