Skip to content

Commit 67384f0

Browse files
authored
PYTHON-5550 Add a test that uses uvloop as the event loop (mongodb#2543)
1 parent b291807 commit 67384f0

File tree

9 files changed

+140
-0
lines changed

9 files changed

+140
-0
lines changed

.github/workflows/test-python.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ defaults:
1414
run:
1515
shell: bash -eux {0}
1616

17+
permissions:
18+
contents: read
19+
1720
jobs:
1821

1922
static:
@@ -163,6 +166,36 @@ jobs:
163166
run: |
164167
just typing
165168
169+
integration_tests:
170+
runs-on: ubuntu-latest
171+
name: Integration Tests
172+
steps:
173+
- uses: actions/checkout@v5
174+
with:
175+
persist-credentials: false
176+
- name: Install uv
177+
uses: astral-sh/setup-uv@557e51de59eb14aaaba2ed9621916900a91d50c6 # v5
178+
with:
179+
enable-cache: true
180+
python-version: "3.10"
181+
- name: Install just
182+
run: uv tool install rust-just
183+
- name: Install dependencies
184+
run: |
185+
just install
186+
- id: setup-mongodb
187+
uses: mongodb-labs/drivers-evergreen-tools@master
188+
- name: Run tests
189+
run: |
190+
just integration-tests
191+
- id: setup-mongodb-ssl
192+
uses: mongodb-labs/drivers-evergreen-tools@master
193+
with:
194+
ssl: true
195+
- name: Run tests
196+
run: |
197+
just integration-tests
198+
166199
make_sdist:
167200
runs-on: ubuntu-latest
168201
name: "Make an sdist"

CONTRIBUTING.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,14 @@ a use the ticket number as the "reason" parameter to the decorator, e.g. `@flaky
411411
When running tests locally (not in CI), the `flaky` decorator will be disabled unless `ENABLE_FLAKY` is set.
412412
To disable the `flaky` decorator in CI, you can use `evergreen patch --param DISABLE_FLAKY=1`.
413413

414+
## Integration Tests
415+
416+
The `integration_tests` directory has a set of scripts that verify the usage of PyMongo with downstream packages or frameworks. See the [README](./integration_tests/README.md) for more information.
417+
418+
To run the tests, use `just integration_tests`.
419+
420+
The tests should be able to run with and without SSL enabled.
421+
414422
## Specification Tests
415423

416424
The MongoDB [specifications repository](https://github.com/mongodb/specifications)

integration_tests/README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Integration Tests
2+
3+
A set of tests that verify the usage of PyMongo with downstream packages or frameworks.
4+
5+
Each test uses [PEP 723 inline metadata](https://packaging.python.org/en/latest/specifications/inline-script-metadata/) and can be run using `pipx` or `uv`.
6+
7+
The `run.sh` convenience script can be used to run all of the files using `uv`.
8+
9+
Here is an example header for the script with the inline dependencies:
10+
11+
```python
12+
# /// script
13+
# dependencies = [
14+
# "uvloop>=0.18"
15+
# ]
16+
# requires-python = ">=3.10"
17+
# ///
18+
```
19+
20+
Here is an example of using the test helper function to create a configured client for the test:
21+
22+
23+
```python
24+
import asyncio
25+
import sys
26+
from pathlib import Path
27+
28+
# Use pymongo from parent directory.
29+
root = Path(__file__).parent.parent
30+
sys.path.insert(0, str(root))
31+
32+
from test.asynchronous import async_simple_test_client # noqa: E402
33+
34+
35+
async def main():
36+
async with async_simple_test_client() as client:
37+
result = await client.admin.command("ping")
38+
assert result["ok"]
39+
40+
41+
asyncio.run(main())
42+
```

integration_tests/run.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/bash
2+
# Run all of the integration test files using `uv run`.
3+
set -eu
4+
5+
for file in integration_tests/test_*.py ; do
6+
echo "-----------------"
7+
echo "Running $file..."
8+
uv run $file
9+
echo "Running $file...done."
10+
echo "-----------------"
11+
done

integration_tests/test_uv_loop.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# /// script
2+
# dependencies = [
3+
# "uvloop>=0.18"
4+
# ]
5+
# requires-python = ">=3.10"
6+
# ///
7+
from __future__ import annotations
8+
9+
import sys
10+
from pathlib import Path
11+
12+
import uvloop
13+
14+
# Use pymongo from parent directory.
15+
root = Path(__file__).parent.parent
16+
sys.path.insert(0, str(root))
17+
18+
from test.asynchronous import async_simple_test_client # noqa: E402
19+
20+
21+
async def main():
22+
async with async_simple_test_client() as client:
23+
result = await client.admin.command("ping")
24+
assert result["ok"]
25+
26+
27+
uvloop.run(main())

justfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ setup-tests *args="":
7272
teardown-tests:
7373
bash .evergreen/scripts/teardown-tests.sh
7474

75+
[group('test')]
76+
integration-tests:
77+
bash integration_tests/run.sh
78+
7579
[group('server')]
7680
run-server *args="":
7781
bash .evergreen/scripts/run-server.sh {{args}}

test/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1227,6 +1227,13 @@ def teardown():
12271227
print_running_clients()
12281228

12291229

1230+
@contextmanager
1231+
def simple_test_client():
1232+
client_context.init()
1233+
yield client_context.client
1234+
client_context.client.close()
1235+
1236+
12301237
def test_cases(suite):
12311238
"""Iterator over all TestCases within a TestSuite."""
12321239
for suite_or_case in suite._tests:

test/asynchronous/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1243,6 +1243,13 @@ async def async_teardown():
12431243
print_running_clients()
12441244

12451245

1246+
@asynccontextmanager
1247+
async def async_simple_test_client():
1248+
await async_client_context.init()
1249+
yield async_client_context.client
1250+
await async_client_context.client.close()
1251+
1252+
12461253
def test_cases(suite):
12471254
"""Iterator over all TestCases within a TestSuite."""
12481255
for suite_or_case in suite._tests:

tools/synchro.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@
131131
"async_create_barrier": "create_barrier",
132132
"async_barrier_wait": "barrier_wait",
133133
"async_joinall": "joinall",
134+
"async_simple_test_client": "simple_test_client",
134135
"_async_create_connection": "_create_connection",
135136
"pymongo.asynchronous.srv_resolver._SrvResolver.get_hosts": "pymongo.synchronous.srv_resolver._SrvResolver.get_hosts",
136137
"dns.asyncresolver.resolve": "dns.resolver.resolve",

0 commit comments

Comments
 (0)