droboticflow/scripts/local/test_query_replica_runner.py
d-robotics 412a12cfe5
Some checks are pending
web-ci / checks (push) Waiting to run
web-ci / pool-search-mongo (push) Waiting to run
web-ci / work-availability (push) Blocked by required conditions
web-ci / query-serving (push) Blocked by required conditions
🛠️ Recover expired query snapshots and bound role-change reads
2026-11-16 08:21:41 -08:00

90 lines
4.7 KiB
Python

import importlib.util
from pathlib import Path
import unittest
from unittest.mock import patch, MagicMock
spec = importlib.util.spec_from_file_location("replica_runner", Path(__file__).parents[1] / "test_query_replica.py")
runner = importlib.util.module_from_spec(spec)
# The runner's lib package is normally resolved by executing scripts/ directly.
import sys
sys.path.insert(0, str(Path(__file__).parents[1]))
spec.loader.exec_module(runner)
class ReplicaRunnerSafety(unittest.TestCase):
def test_bounded_override_is_used_for_every_compose_command(self):
with patch.object(runner.subprocess, "check_output", return_value="unix:///owned.sock"), patch.object(runner, "run", return_value=(0, "ok")) as call:
runner.compose({"QUERY_REPLICA_LOCAL_QUERY_BUDGET": "query25"}, "up", "-d")
argv = call.call_args.args[0]
self.assertIn(str(runner.ROOT / "scripts/local/query-replica.query25-local.compose.yml"), argv)
def test_partial_owned_source_can_start_missing_query(self):
def connection(address, timeout):
if address[1] == 27119:
return MagicMock()
raise OSError("no listener")
with patch.object(runner.socket, "create_connection", side_effect=connection), patch.object(runner, "compose", side_effect=["source", "started", "primary"]) as call:
runner.start({})
self.assertEqual(call.call_args_list[1].args[1:], ("up", "-d"))
def test_unowned_busy_query_is_rejected(self):
with patch.object(runner.socket, "create_connection", return_value=MagicMock()), patch.object(runner, "compose", return_value="source") as call:
with self.assertRaisesRegex(RuntimeError, "unowned"):
runner.start({})
self.assertEqual(call.call_count, 1)
class ReplicaRunnerBatchSafety(unittest.TestCase):
def test_bounded_batches_cover_every_registered_case_once(self):
required = {f'TestCase{i:03d}' for i in range(53)}
batches = runner.case_batches(required, True, False)
self.assertEqual([len(x) for x in batches], [20, 20, 13])
self.assertEqual([x for group in batches for x in group], sorted(required))
self.assertEqual(len(runner.case_batches(required, False, False)), 1)
self.assertEqual(len(runner.case_batches(required, True, True)), 1)
with self.assertRaisesRegex(RuntimeError, 'no registered'):
runner.case_batches(set(), True, False)
def test_restart_refuses_other_clients_without_mutating_fixture(self):
import json
fixture = {'Name': '/fanyu-query-replica-local-query-1',
'HostConfig': {'Memory': 1024 ** 3, 'NanoCpus': 1_000_000_000},
'Config': {'Labels': {'com.docker.compose.project': 'fanyu-query-replica-local'}},
'Mounts': [{'Destination': '/data/db'}]}
with patch.object(runner.subprocess, 'check_output', return_value=json.dumps([fixture])), \
patch.object(runner, 'query_probe', return_value=(3, 'other clients')), \
patch.object(runner.time, 'sleep'), patch.object(runner, 'compose') as change:
with self.assertRaisesRegex(RuntimeError, 'other_clients'):
runner.restart_query_fixture({}, 'synthetic-probe', 7)
change.assert_not_called()
def test_restart_mount_order_is_irrelevant_but_mount_identity_is_not(self):
import json
from copy import deepcopy
before = {'Id': 'owned', 'Name': '/fanyu-query-replica-local-query-1',
'HostConfig': {'Memory': 1024 ** 3, 'NanoCpus': 1_000_000_000},
'Config': {'Labels': {'com.docker.compose.project': 'fanyu-query-replica-local'}},
'Mounts': [{'Destination': '/data/db', 'Name': 'data'},
{'Destination': '/data/configdb', 'Name': 'config'}]}
for changed in (False, True):
after = deepcopy(before)
after['Mounts'].reverse()
if changed:
after['Mounts'][0]['Name'] = 'different-volume'
with patch.object(runner.subprocess, 'check_output', side_effect=[json.dumps([before]), json.dumps([after])]), \
patch.object(runner, 'query_probe', return_value=(0, '')), \
patch.object(runner, 'compose'):
if changed:
with self.assertRaisesRegex(RuntimeError, 'identity'):
runner.restart_query_fixture({}, 'synthetic-probe', 7)
else:
proof = runner.restart_query_fixture({}, 'synthetic-probe', 7)
self.assertTrue(proof['sameVolume'] and proof['majorityJournalRecovered'])
if __name__ == "__main__":
unittest.main()