|
| 1 | +# Copyright 2026-present MongoDB, Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Async-only unit tests for AsyncPeriodicExecutor.""" |
| 16 | + |
| 17 | +from __future__ import annotations |
| 18 | + |
| 19 | +import sys |
| 20 | + |
| 21 | +sys.path[0:0] = [""] |
| 22 | + |
| 23 | +from test.asynchronous import AsyncUnitTest, unittest |
| 24 | + |
| 25 | +from pymongo.periodic_executor import AsyncPeriodicExecutor |
| 26 | + |
| 27 | + |
| 28 | +class TestAsyncPeriodicExecutorExceptions(AsyncUnitTest): |
| 29 | + async def test_target_exception_stops_executor(self): |
| 30 | + call_count = 0 |
| 31 | + |
| 32 | + async def target(): |
| 33 | + nonlocal call_count |
| 34 | + call_count += 1 |
| 35 | + raise RuntimeError("error") |
| 36 | + |
| 37 | + executor = AsyncPeriodicExecutor( |
| 38 | + interval=30.0, min_interval=0.01, target=target, name="test" |
| 39 | + ) |
| 40 | + executor.open() |
| 41 | + await executor.join(timeout=2) |
| 42 | + if executor._task is not None and executor._task.done(): |
| 43 | + executor._task.exception() |
| 44 | + self.assertEqual(call_count, 1, "target should stop after exception") |
| 45 | + |
| 46 | + |
| 47 | +if __name__ == "__main__": |
| 48 | + unittest.main() |
0 commit comments