From 5164f4270ce551dfc0641118b5d1d0ecccb00d3c Mon Sep 17 00:00:00 2001 From: kiskaadee Date: Sat, 31 Oct 2026 21:11:25 -0500 Subject: [PATCH] test(daemon): add true multi-threaded lock contention concurrency test (#24) --- tests/unit/test_appctl_daemon.py | 52 ++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/tests/unit/test_appctl_daemon.py b/tests/unit/test_appctl_daemon.py index 4f9b4b7..c1b60f5 100644 --- a/tests/unit/test_appctl_daemon.py +++ b/tests/unit/test_appctl_daemon.py @@ -18,9 +18,12 @@ Asserts: from __future__ import annotations +import concurrent.futures import hashlib import json import socket +import threading +import time from pathlib import Path from unittest.mock import patch @@ -435,6 +438,55 @@ def test_concurrent_mutation_conflict(daemon_env): assert resp.error.code.http_status == 409 +def test_daemon_multi_threaded_lock_contention_rejects_concurrent_mutation(daemon_env): + """ + Simultaneous mutating requests for the same target result in one 200 SUCCESS + and one 409 ERR_CONCURRENT_MUTATION via multi-threaded lock contention. + """ + deploy_req = OperationRequest(operation=OperationType.WORKLOAD_DEPLOY, target="docs") + first_started = threading.Event() + + def slow_subproc(cmd, *args, **kwargs): + first_started.set() + time.sleep(0.2) + return (0, "Started", "") + + def send_first(): + return send_daemon_request( + endpoint=daemon_env.socket_path, + operation_request=deploy_req, + token=ADMIN_SECRET, + ) + + def send_second(): + first_started.wait(timeout=2.0) + return send_daemon_request( + endpoint=daemon_env.socket_path, + operation_request=deploy_req, + token=ADMIN_SECRET, + ) + + with ( + patch("appctl_router.run_bounded_subprocess", side_effect=slow_subproc), + patch("appctl_router.OperationalRouter._trigger_homepage_sync"), + concurrent.futures.ThreadPoolExecutor(max_workers=2) as executor, + ): + fut1 = executor.submit(send_first) + fut2 = executor.submit(send_second) + resp1 = fut1.result(timeout=5.0) + resp2 = fut2.result(timeout=5.0) + + responses = [resp1, resp2] + successes = [r for r in responses if r.success] + conflicts = [r for r in responses if not r.success and r.error and r.error.code == ErrorCode.ERR_CONCURRENT_MUTATION] + + assert len(successes) == 1 + assert len(conflicts) == 1 + assert successes[0].result is not None + assert successes[0].result["action"] == "deployed" + assert conflicts[0].error.code.http_status == 409 + + def test_execution_failure_audit(daemon_env): """ Failing Docker subprocess returns 500 ERR_EXECUTION_FAILED and records phase: FAILED.