Skip to content

Commit e8c9c54

Browse files
authored
Merge branch 'master' into PYTHON-5103
2 parents 8e1605b + 097a853 commit e8c9c54

15 files changed

+1010
-57
lines changed

test/asynchronous/helpers.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"""Shared constants and helper methods for pymongo, bson, and gridfs test suites."""
1616
from __future__ import annotations
1717

18+
import asyncio
1819
import base64
1920
import gc
2021
import multiprocessing
@@ -30,6 +31,8 @@
3031
import warnings
3132
from asyncio import iscoroutinefunction
3233

34+
from pymongo._asyncio_task import create_task
35+
3336
try:
3437
import ipaddress
3538

@@ -369,3 +372,37 @@ def disable(self):
369372
os.environ.pop("SSL_CERT_FILE")
370373
else:
371374
os.environ["SSL_CERT_FILE"] = self.original_certs
375+
376+
377+
if _IS_SYNC:
378+
PARENT = threading.Thread
379+
else:
380+
PARENT = object
381+
382+
383+
class ConcurrentRunner(PARENT):
384+
def __init__(self, name, *args, **kwargs):
385+
if _IS_SYNC:
386+
super().__init__(*args, **kwargs)
387+
self.name = name
388+
self.stopped = False
389+
self.task = None
390+
if "target" in kwargs:
391+
self.target = kwargs["target"]
392+
393+
if not _IS_SYNC:
394+
395+
async def start(self):
396+
self.task = create_task(self.run(), name=self.name)
397+
398+
async def join(self, timeout: float | None = 0): # type: ignore[override]
399+
if self.task is not None:
400+
await asyncio.wait([self.task], timeout=timeout)
401+
402+
def is_alive(self):
403+
return not self.stopped
404+
405+
async def run(self):
406+
if self.target:
407+
await self.target()
408+
self.stopped = True

test/asynchronous/test_run_command.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Copyright 2024-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+
"""Run Command unified tests."""
16+
from __future__ import annotations
17+
18+
import os
19+
import unittest
20+
from pathlib import Path
21+
from test.asynchronous.unified_format import generate_test_classes
22+
23+
_IS_SYNC = False
24+
25+
# Location of JSON test specifications.
26+
if _IS_SYNC:
27+
TEST_PATH = os.path.join(Path(__file__).resolve().parent, "run_command")
28+
else:
29+
TEST_PATH = os.path.join(Path(__file__).resolve().parent.parent, "run_command")
30+
31+
32+
globals().update(
33+
generate_test_classes(
34+
os.path.join(TEST_PATH, "unified"),
35+
module=__name__,
36+
)
37+
)
38+
39+
40+
if __name__ == "__main__":
41+
unittest.main()
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Copyright 2021-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 the Sessions unified spec tests."""
16+
from __future__ import annotations
17+
18+
import os
19+
import sys
20+
from pathlib import Path
21+
22+
sys.path[0:0] = [""]
23+
24+
from test import unittest
25+
from test.asynchronous.unified_format import generate_test_classes
26+
27+
_IS_SYNC = False
28+
29+
# Location of JSON test specifications.
30+
if _IS_SYNC:
31+
TEST_PATH = os.path.join(Path(__file__).resolve().parent, "sessions")
32+
else:
33+
TEST_PATH = os.path.join(Path(__file__).resolve().parent.parent, "sessions")
34+
35+
36+
# Generate unified tests.
37+
globals().update(generate_test_classes(TEST_PATH, module=__name__))
38+
39+
if __name__ == "__main__":
40+
unittest.main()

0 commit comments

Comments
 (0)