|
| 1 | +# Copyright 2026 The Dapr Authors |
| 2 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +# you may not use this file except in compliance with the License. |
| 4 | +# You may obtain a copy of the License at |
| 5 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +# Unless required by applicable law or agreed to in writing, software |
| 7 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 8 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 9 | +# See the License for the specific language governing permissions and |
| 10 | +# limitations under the License. |
| 11 | + |
| 12 | +import logging |
| 13 | +import sys |
| 14 | +from unittest.mock import patch |
| 15 | + |
| 16 | +from dapr.ext.workflow._durabletask.aio.internal import shared |
| 17 | + |
| 18 | +HOST_ADDRESS = 'localhost:50051' |
| 19 | + |
| 20 | + |
| 21 | +def _record(msg: str, exc: BaseException | None) -> logging.LogRecord: |
| 22 | + exc_info = None |
| 23 | + if exc is not None: |
| 24 | + try: |
| 25 | + raise exc |
| 26 | + except BaseException: |
| 27 | + exc_info = sys.exc_info() |
| 28 | + return logging.LogRecord('asyncio', logging.ERROR, __file__, 1, msg, (), exc_info) |
| 29 | + |
| 30 | + |
| 31 | +def test_filter_drops_poller_eagain_record(): |
| 32 | + record = _record( |
| 33 | + 'Exception in callback PollerCompletionQueue._handle_events()', |
| 34 | + BlockingIOError(11, 'Resource temporarily unavailable'), |
| 35 | + ) |
| 36 | + assert shared._GrpcAioPollerNoiseFilter().filter(record) is False |
| 37 | + |
| 38 | + |
| 39 | +def test_filter_keeps_record_without_exception(): |
| 40 | + assert shared._GrpcAioPollerNoiseFilter().filter(_record('some other error', None)) is True |
| 41 | + |
| 42 | + |
| 43 | +def test_filter_keeps_blockingioerror_without_marker(): |
| 44 | + record = _record('unrelated message', BlockingIOError(11, 'nope')) |
| 45 | + assert shared._GrpcAioPollerNoiseFilter().filter(record) is True |
| 46 | + |
| 47 | + |
| 48 | +def test_get_grpc_aio_channel_installs_filter_on_asyncio_logger(): |
| 49 | + asyncio_logger = logging.getLogger('asyncio') |
| 50 | + for existing in [ |
| 51 | + f for f in asyncio_logger.filters if isinstance(f, shared._GrpcAioPollerNoiseFilter) |
| 52 | + ]: |
| 53 | + asyncio_logger.removeFilter(existing) |
| 54 | + |
| 55 | + with patch('dapr.ext.workflow._durabletask.aio.internal.shared.grpc_aio.insecure_channel'): |
| 56 | + shared.get_grpc_aio_channel(HOST_ADDRESS, False) |
| 57 | + |
| 58 | + installed = [ |
| 59 | + f for f in asyncio_logger.filters if isinstance(f, shared._GrpcAioPollerNoiseFilter) |
| 60 | + ] |
| 61 | + assert len(installed) == 1 # installed once, not duplicated |
| 62 | + |
| 63 | + |
| 64 | +def test_install_is_idempotent(): |
| 65 | + asyncio_logger = logging.getLogger('asyncio') |
| 66 | + shared._silence_grpc_aio_poller_noise() |
| 67 | + shared._silence_grpc_aio_poller_noise() |
| 68 | + installed = [ |
| 69 | + f for f in asyncio_logger.filters if isinstance(f, shared._GrpcAioPollerNoiseFilter) |
| 70 | + ] |
| 71 | + assert len(installed) == 1 |
0 commit comments