This commit is contained in:
Your Name
2026-07-28 09:00:19 +08:00
parent 8ba13a8ff9
commit 153db97dc7
14 changed files with 793 additions and 75 deletions
+64
View File
@@ -458,6 +458,70 @@ class BackgroundTrafficLimitTests(unittest.IsolatedAsyncioTestCase):
self.assertEqual(len(tasks_seen), 3)
self.assertTrue(all(task is tasks_seen[0] for task in tasks_seen))
async def test_startup_request_is_not_buried_behind_normal_backlog(self):
with patch.dict(
os.environ,
{"KEFU_BACKGROUND_NETWORK_CONCURRENCY": "1"},
):
controller = TrafficController()
self.addAsyncCleanup(controller.stop)
entered: list[str] = []
releases = {
name: asyncio.Event()
for name in ("active", "normal-1", "normal-2", "startup")
}
async def request(name: str, *, startup: bool = False) -> None:
async with controller.background_slot(
1,
name,
startup=startup,
):
entered.append(name)
await releases[name].wait()
active = asyncio.create_task(request("active"))
while entered != ["active"]:
await asyncio.sleep(0)
normal_one = asyncio.create_task(request("normal-1"))
normal_two = asyncio.create_task(request("normal-2"))
# Let one normal request reach the shared semaphore while the other is
# held at normal admission, then add the priority startup request.
await asyncio.sleep(0)
await asyncio.sleep(0)
startup = asyncio.create_task(request("startup", startup=True))
await asyncio.sleep(0)
releases["active"].set()
while len(entered) < 2:
await asyncio.sleep(0)
self.assertEqual(entered[:2], ["active", "normal-1"])
releases["normal-1"].set()
while len(entered) < 3:
await asyncio.sleep(0)
self.assertEqual(entered[:3], ["active", "normal-1", "startup"])
releases["startup"].set()
while len(entered) < 4:
await asyncio.sleep(0)
releases["normal-2"].set()
await asyncio.wait_for(
asyncio.gather(active, normal_one, normal_two, startup),
timeout=0.2,
)
self.assertEqual(
entered,
["active", "normal-1", "startup", "normal-2"],
)
self.assertEqual(controller.background_active, 0)
self.assertEqual(controller.background_waiting, 0)
self.assertEqual(controller.background_startup_active, 0)
self.assertEqual(controller.background_startup_waiting, 0)
class TrafficControllerLoopIsolationTests(unittest.TestCase):
def test_get_traffic_controller_does_not_reuse_asyncio_primitives(self):