|
1 | 1 | import pytest |
| 2 | +import asyncio |
2 | 3 | import itertools |
| 4 | +import logging |
| 5 | +import time |
3 | 6 | from unittest.mock import ( |
4 | 7 | AsyncMock, |
5 | 8 | ) |
|
14 | 17 | PersistentConnectionProvider, |
15 | 18 | ) |
16 | 19 | from web3.exceptions import ( |
| 20 | + SubscriptionHandlerTaskException, |
17 | 21 | Web3ValueError, |
18 | 22 | ) |
| 23 | +from web3.providers.persistent.request_processor import ( |
| 24 | + TaskReliantQueue, |
| 25 | +) |
19 | 26 | from web3.providers.persistent.subscription_manager import ( |
20 | 27 | SubscriptionManager, |
21 | 28 | ) |
@@ -213,3 +220,161 @@ async def test_unsubscribe_with_subscriptions_reference_does_not_mutate_the_list |
213 | 220 |
|
214 | 221 | await subscription_manager.unsubscribe_all() |
215 | 222 | assert subscription_manager.subscriptions == [] |
| 223 | + |
| 224 | + |
| 225 | +@pytest.mark.asyncio |
| 226 | +async def test_high_throughput_subscription_task_based( |
| 227 | + subscription_manager, |
| 228 | +) -> None: |
| 229 | + provider = subscription_manager._w3.provider |
| 230 | + num_msgs = 5_000 |
| 231 | + |
| 232 | + provider._request_processor._handler_subscription_queue = TaskReliantQueue( |
| 233 | + maxsize=num_msgs |
| 234 | + ) |
| 235 | + |
| 236 | + # Turn on task-based processing. This test should fail the time constraint if this |
| 237 | + # is not set to ``True`` (not task-based processing). |
| 238 | + subscription_manager.task_based = True |
| 239 | + |
| 240 | + class Counter: |
| 241 | + val: int = 0 |
| 242 | + |
| 243 | + counter = Counter() |
| 244 | + |
| 245 | + async def high_throughput_handler(handler_context) -> None: |
| 246 | + # if we awaited all `num_msgs`, we would sleep at least 5 seconds total |
| 247 | + await asyncio.sleep(5 / num_msgs) |
| 248 | + |
| 249 | + handler_context.counter.val += 1 |
| 250 | + if handler_context.counter.val == num_msgs: |
| 251 | + await handler_context.subscription.unsubscribe() |
| 252 | + |
| 253 | + # build a meaningless subscription since we are fabricating the messages |
| 254 | + sub_id = await subscription_manager.subscribe( |
| 255 | + NewHeadsSubscription( |
| 256 | + handler=high_throughput_handler, handler_context={"counter": counter} |
| 257 | + ), |
| 258 | + ) |
| 259 | + provider._request_processor.cache_request_information( |
| 260 | + request_id=sub_id, |
| 261 | + method="eth_subscribe", |
| 262 | + params=[], |
| 263 | + response_formatters=((), (), ()), |
| 264 | + ) |
| 265 | + |
| 266 | + # put `num_msgs` messages in the queue |
| 267 | + for _ in range(num_msgs): |
| 268 | + provider._request_processor._handler_subscription_queue.put_nowait( |
| 269 | + { |
| 270 | + "jsonrpc": "2.0", |
| 271 | + "method": "eth_subscription", |
| 272 | + "params": {"subscription": sub_id, "result": "0x0"}, |
| 273 | + } |
| 274 | + ) |
| 275 | + |
| 276 | + start = time.time() |
| 277 | + await subscription_manager.handle_subscriptions() |
| 278 | + stop = time.time() |
| 279 | + |
| 280 | + assert counter.val == num_msgs |
| 281 | + |
| 282 | + assert subscription_manager.total_handler_calls == num_msgs |
| 283 | + assert stop - start < 3, "subscription handling took too long!" |
| 284 | + |
| 285 | + |
| 286 | +@pytest.mark.asyncio |
| 287 | +async def test_task_based_subscription_handling_error_propagation( |
| 288 | + subscription_manager, |
| 289 | +) -> None: |
| 290 | + provider = subscription_manager._w3.provider |
| 291 | + subscription_manager.task_based = True |
| 292 | + |
| 293 | + async def high_throughput_handler(_handler_context) -> None: |
| 294 | + raise ValueError("Test error msg.") |
| 295 | + |
| 296 | + # build a meaningless subscription since we are fabricating the messages |
| 297 | + sub_id = await subscription_manager.subscribe( |
| 298 | + NewHeadsSubscription(handler=high_throughput_handler) |
| 299 | + ) |
| 300 | + provider._request_processor.cache_request_information( |
| 301 | + request_id=sub_id, |
| 302 | + method="eth_subscribe", |
| 303 | + params=[], |
| 304 | + response_formatters=((), (), ()), |
| 305 | + ) |
| 306 | + provider._request_processor._handler_subscription_queue.put_nowait( |
| 307 | + { |
| 308 | + "jsonrpc": "2.0", |
| 309 | + "method": "eth_subscription", |
| 310 | + "params": {"subscription": sub_id, "result": "0x0"}, |
| 311 | + } |
| 312 | + ) |
| 313 | + |
| 314 | + with pytest.raises( |
| 315 | + SubscriptionHandlerTaskException, |
| 316 | + match="Test error msg.", |
| 317 | + ): |
| 318 | + await subscription_manager.handle_subscriptions() |
| 319 | + |
| 320 | + |
| 321 | +@pytest.mark.asyncio |
| 322 | +async def test_task_based_subscription_handling_ignore_errors( |
| 323 | + subscription_manager, caplog |
| 324 | +) -> None: |
| 325 | + provider = subscription_manager._w3.provider |
| 326 | + subscription_manager.task_based = True |
| 327 | + subscription_manager.ignore_task_exceptions = True |
| 328 | + |
| 329 | + class TestObject: |
| 330 | + exception = None |
| 331 | + |
| 332 | + async def sub_handler(handler_context) -> None: |
| 333 | + if handler_context.obj.exception: |
| 334 | + # on the second call, yield to loop so we log, unsubscribe, and return |
| 335 | + await asyncio.sleep(0.01) |
| 336 | + await handler_context.subscription.unsubscribe() |
| 337 | + return |
| 338 | + |
| 339 | + e = ValueError("Test error msg.") |
| 340 | + handler_context.obj.exception = e |
| 341 | + raise e |
| 342 | + |
| 343 | + # build a meaningless subscription since we are fabricating the messages |
| 344 | + test_obj = TestObject() |
| 345 | + sub_id = await subscription_manager.subscribe( |
| 346 | + NewHeadsSubscription(handler=sub_handler, handler_context={"obj": test_obj}) |
| 347 | + ) |
| 348 | + provider._request_processor.cache_request_information( |
| 349 | + request_id=sub_id, |
| 350 | + method="eth_subscribe", |
| 351 | + params=[], |
| 352 | + response_formatters=((), (), ()), |
| 353 | + ) |
| 354 | + for _ in range(2): |
| 355 | + provider._request_processor._handler_subscription_queue.put_nowait( |
| 356 | + { |
| 357 | + "jsonrpc": "2.0", |
| 358 | + "method": "eth_subscription", |
| 359 | + "params": {"subscription": sub_id, "result": "0x0"}, |
| 360 | + } |
| 361 | + ) |
| 362 | + |
| 363 | + with caplog.at_level( |
| 364 | + logging.WARNING, logger="web3.providers.persistent.subscription_manager" |
| 365 | + ): |
| 366 | + await subscription_manager.handle_subscriptions() |
| 367 | + |
| 368 | + # find the warning so and assert it was logged |
| 369 | + warning_records = [r for r in caplog.records if r.levelname == "WARNING"] |
| 370 | + assert len(warning_records) == 1 |
| 371 | + record = warning_records[0] |
| 372 | + assert ( |
| 373 | + "An exception occurred in a subscription handler task but was ignored, `" |
| 374 | + "`ignore_task_exceptions==True``." in record.message |
| 375 | + ) |
| 376 | + await subscription_manager.handle_subscriptions() |
| 377 | + |
| 378 | + assert subscription_manager.total_handler_calls == 2 |
| 379 | + assert subscription_manager.subscriptions == [] |
| 380 | + assert subscription_manager._tasks == set() |
0 commit comments