|
| 1 | +# Copyright 2025-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 | +"""Test that AsyncPeriodicExecutors do not copy ContextVars from their parents.""" |
| 16 | +from __future__ import annotations |
| 17 | + |
| 18 | +import asyncio |
| 19 | +import sys |
| 20 | +from test.asynchronous.utils import async_get_pool |
| 21 | +from test.utils_shared import delay, one |
| 22 | + |
| 23 | +sys.path[0:0] = [""] |
| 24 | + |
| 25 | +from test.asynchronous import AsyncIntegrationTest |
| 26 | + |
| 27 | + |
| 28 | +class TestAsyncContextVarsReset(AsyncIntegrationTest): |
| 29 | + async def test_context_vars_are_reset_in_executor(self): |
| 30 | + client = self.simple_client() |
| 31 | + |
| 32 | + await client.db.test.insert_one({"x": 1}) |
| 33 | + for server in client._topology._servers.values(): |
| 34 | + for context in [ |
| 35 | + c |
| 36 | + for c in server._monitor._executor._task.get_context() |
| 37 | + if c.name in ["TIMEOUT", "RTT", "DEADLINE"] |
| 38 | + ]: |
| 39 | + self.assertIn(context.get(), [None, float("inf"), 0.0]) |
| 40 | + await client.db.test.delete_many({}) |
0 commit comments