|
1 | 1 | import asyncio |
2 | 2 | import socket |
3 | 3 | import unittest |
| 4 | +from contextlib import asynccontextmanager |
| 5 | +from types import SimpleNamespace |
| 6 | +from unittest.mock import AsyncMock, patch |
4 | 7 |
|
5 | 8 | from aiohttp import ClientSession, TCPConnector |
6 | 9 |
|
7 | 10 | from utils.speed import create_speed_test_session, _install_aiohttp_dns_error_filter |
8 | 11 |
|
9 | 12 |
|
| 13 | +class MemoryArtifactWriter: |
| 14 | + def __init__(self, *_args, **_kwargs): |
| 15 | + self.count = 0 |
| 16 | + |
| 17 | + def write(self, _record): |
| 18 | + self.count += 1 |
| 19 | + |
| 20 | + def close(self): |
| 21 | + pass |
| 22 | + |
| 23 | + |
| 24 | +@asynccontextmanager |
| 25 | +async def speed_session(_concurrency): |
| 26 | + yield object() |
| 27 | + |
| 28 | + |
| 29 | +def speed_test_config(): |
| 30 | + return SimpleNamespace( |
| 31 | + open_ipv6=False, |
| 32 | + speed_test_mode="full", |
| 33 | + open_full_speed_test=True, |
| 34 | + open_filter_resolution=False, |
| 35 | + open_stream_screenshot=False, |
| 36 | + performance_settings=SimpleNamespace( |
| 37 | + speed_test_concurrency=1, |
| 38 | + probe_concurrency=1, |
| 39 | + ), |
| 40 | + speed_test_target=1, |
| 41 | + speed_test_timeout=1, |
| 42 | + ) |
| 43 | + |
| 44 | + |
10 | 45 | class FailingResolver: |
11 | 46 | async def resolve(self, host, port=0, family=socket.AF_INET): |
12 | 47 | await asyncio.sleep(0.02) |
@@ -59,3 +94,64 @@ async def test_unrelated_asyncio_exception_uses_previous_handler(self): |
59 | 94 | loop.call_exception_handler(context) |
60 | 95 |
|
61 | 96 | self.assertEqual(contexts, [context]) |
| 97 | + |
| 98 | + async def test_isolated_request_cancellation_does_not_cancel_batch(self): |
| 99 | + from utils.channel import test_speed |
| 100 | + |
| 101 | + channel_data = { |
| 102 | + "Test": { |
| 103 | + "Channel": [{ |
| 104 | + "url": "http://unresolvable.invalid/live", |
| 105 | + "host": "unresolvable.invalid", |
| 106 | + "resolution": None, |
| 107 | + "ipv_type": "ipv4", |
| 108 | + "origin": "subscribe", |
| 109 | + }] |
| 110 | + } |
| 111 | + } |
| 112 | + with ( |
| 113 | + patch("utils.channel.config", speed_test_config()), |
| 114 | + patch("utils.channel.ArtifactWriter", MemoryArtifactWriter), |
| 115 | + patch("utils.channel.create_speed_test_session", speed_session), |
| 116 | + patch("utils.channel.get_speed", AsyncMock(side_effect=asyncio.CancelledError)), |
| 117 | + patch("utils.channel.mark_url_bad"), |
| 118 | + patch("utils.channel.mark_url_good"), |
| 119 | + ): |
| 120 | + result = await test_speed(channel_data) |
| 121 | + |
| 122 | + item = result["Test"]["Channel"][0] |
| 123 | + self.assertEqual(item["test_status"], "request_error") |
| 124 | + self.assertEqual(item["error_type"], "CancelledError") |
| 125 | + |
| 126 | + async def test_real_batch_cancellation_still_propagates(self): |
| 127 | + from utils.channel import test_speed |
| 128 | + |
| 129 | + started = asyncio.Event() |
| 130 | + |
| 131 | + async def wait_for_cancellation(*_args, **_kwargs): |
| 132 | + started.set() |
| 133 | + await asyncio.Event().wait() |
| 134 | + |
| 135 | + channel_data = { |
| 136 | + "Test": { |
| 137 | + "Channel": [{ |
| 138 | + "url": "http://example.invalid/live", |
| 139 | + "host": "example.invalid", |
| 140 | + "resolution": None, |
| 141 | + "ipv_type": "ipv4", |
| 142 | + "origin": "subscribe", |
| 143 | + }] |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + with ( |
| 148 | + patch("utils.channel.config", speed_test_config()), |
| 149 | + patch("utils.channel.ArtifactWriter", MemoryArtifactWriter), |
| 150 | + patch("utils.channel.create_speed_test_session", speed_session), |
| 151 | + patch("utils.channel.get_speed", wait_for_cancellation), |
| 152 | + ): |
| 153 | + task = asyncio.create_task(test_speed(channel_data)) |
| 154 | + await started.wait() |
| 155 | + task.cancel() |
| 156 | + with self.assertRaises(asyncio.CancelledError): |
| 157 | + await task |
0 commit comments