-
Notifications
You must be signed in to change notification settings - Fork 1.1k
PYTHON-5344 and PYTHON-5403 Allow Instantiated MongoClients to Send Client Metadata On-Demand #2358
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sleepyStick
wants to merge
15
commits into
mongodb:master
Choose a base branch
from
sleepyStick:PYTHON-5344
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
b1899d6
WIP - sync test pass but not async..
sleepyStick e18737f
move test to correct folder
sleepyStick a17f6ef
Merge branch 'master' into PYTHON-5344
sleepyStick f9a097a
fix typing
sleepyStick 21c4eef
add another test
sleepyStick 9f9c9d8
update/add the new prose tests
sleepyStick 265ef3b
make public
sleepyStick 8a9c4cb
add async tests (PYTHON-5403)
sleepyStick a4d1116
fix import?
sleepyStick 42e4b81
Merge branch 'master' into PYTHON-5344
sleepyStick 92b3046
Merge branch 'master' into PYTHON-5344
sleepyStick 08dbbf7
add unified format support
sleepyStick c54a7da
pull unified test from spec repo
sleepyStick 1c835ae
address review
sleepyStick 082c4ab
add comment
sleepyStick File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,225 @@ | ||
# Copyright 2013-present MongoDB, Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
from __future__ import annotations | ||
|
||
import asyncio | ||
import os | ||
import pathlib | ||
import time | ||
import unittest | ||
from test.asynchronous import AsyncIntegrationTest | ||
from test.asynchronous.unified_format import generate_test_classes | ||
from test.utils_shared import CMAPListener | ||
from typing import Any, Optional | ||
|
||
import pytest | ||
|
||
from pymongo import AsyncMongoClient, MongoClient | ||
from pymongo.driver_info import DriverInfo | ||
from pymongo.monitoring import ConnectionClosedEvent | ||
|
||
try: | ||
from mockupdb import MockupDB, OpMsgReply | ||
|
||
_HAVE_MOCKUPDB = True | ||
except ImportError: | ||
_HAVE_MOCKUPDB = False | ||
|
||
pytestmark = pytest.mark.mockupdb | ||
|
||
_IS_SYNC = False | ||
|
||
# Location of JSON test specifications. | ||
if _IS_SYNC: | ||
_TEST_PATH = os.path.join(pathlib.Path(__file__).resolve().parent, "handshake", "unified") | ||
else: | ||
_TEST_PATH = os.path.join( | ||
pathlib.Path(__file__).resolve().parent.parent, "handshake", "unified" | ||
) | ||
|
||
# Generate unified tests. | ||
globals().update(generate_test_classes(_TEST_PATH, module=__name__)) | ||
|
||
|
||
def _get_handshake_driver_info(request): | ||
assert "client" in request | ||
return request["client"] | ||
|
||
|
||
class TestClientMetadataProse(AsyncIntegrationTest): | ||
async def asyncSetUp(self): | ||
await super().asyncSetUp() | ||
self.server = MockupDB() | ||
self.handshake_req = None | ||
|
||
def respond(r): | ||
if "ismaster" in r: | ||
# then this is a handshake request | ||
self.handshake_req = r | ||
return r.reply(OpMsgReply(minWireVersion=0, maxWireVersion=13)) | ||
|
||
self.server.autoresponds(respond) | ||
self.server.run() | ||
self.addAsyncCleanup(self.server.stop) | ||
|
||
async def send_ping_and_get_metadata( | ||
self, client: AsyncMongoClient, is_handshake: bool | ||
) -> tuple[str, Optional[str], Optional[str], dict[str, Any]]: | ||
# reset if handshake request | ||
if is_handshake: | ||
self.handshake_req: Optional[dict] = None | ||
|
||
await client.admin.command("ping") | ||
metadata = _get_handshake_driver_info(self.handshake_req) | ||
driver_metadata = metadata["driver"] | ||
name, version, platform = ( | ||
driver_metadata["name"], | ||
driver_metadata["version"], | ||
metadata["platform"], | ||
) | ||
return name, version, platform, metadata | ||
|
||
async def check_metadata_added( | ||
self, | ||
client: AsyncMongoClient, | ||
add_name: str, | ||
add_version: Optional[str], | ||
add_platform: Optional[str], | ||
) -> None: | ||
# send initial metadata | ||
name, version, platform, metadata = await self.send_ping_and_get_metadata(client, True) | ||
# wait for connection to become idle | ||
await asyncio.sleep(0.005) | ||
|
||
# add new metadata | ||
client.append_metadata(DriverInfo(add_name, add_version, add_platform)) | ||
new_name, new_version, new_platform, new_metadata = await self.send_ping_and_get_metadata( | ||
client, True | ||
) | ||
self.assertEqual(new_name, f"{name}|{add_name}" if add_name is not None else name) | ||
self.assertEqual( | ||
new_version, | ||
f"{version}|{add_version}" if add_version is not None else version, | ||
) | ||
self.assertEqual( | ||
new_platform, | ||
f"{platform}|{add_platform}" if add_platform is not None else platform, | ||
) | ||
|
||
metadata.pop("driver") | ||
metadata.pop("platform") | ||
new_metadata.pop("driver") | ||
new_metadata.pop("platform") | ||
self.assertEqual(metadata, new_metadata) | ||
|
||
async def test_append_metadata(self): | ||
client = AsyncMongoClient( | ||
"mongodb://" + self.server.address_string, | ||
maxIdleTimeMS=1, | ||
driver=DriverInfo("library", "1.2", "Library Platform"), | ||
) | ||
await self.check_metadata_added(client, "framework", "2.0", "Framework Platform") | ||
await client.close() | ||
|
||
async def test_append_metadata_platform_none(self): | ||
client = AsyncMongoClient( | ||
"mongodb://" + self.server.address_string, | ||
maxIdleTimeMS=1, | ||
driver=DriverInfo("library", "1.2", "Library Platform"), | ||
) | ||
await self.check_metadata_added(client, "framework", "2.0", None) | ||
await client.close() | ||
|
||
async def test_append_metadata_version_none(self): | ||
client = AsyncMongoClient( | ||
"mongodb://" + self.server.address_string, | ||
maxIdleTimeMS=1, | ||
driver=DriverInfo("library", "1.2", "Library Platform"), | ||
) | ||
await self.check_metadata_added(client, "framework", None, "Framework Platform") | ||
await client.close() | ||
|
||
async def test_append_metadata_platform_version_none(self): | ||
client = AsyncMongoClient( | ||
"mongodb://" + self.server.address_string, | ||
maxIdleTimeMS=1, | ||
driver=DriverInfo("library", "1.2", "Library Platform"), | ||
) | ||
await self.check_metadata_added(client, "framework", None, None) | ||
await client.close() | ||
|
||
async def test_multiple_successive_metadata_updates(self): | ||
client = AsyncMongoClient( | ||
"mongodb://" + self.server.address_string, maxIdleTimeMS=1, connect=False | ||
) | ||
client.append_metadata(DriverInfo("library", "1.2", "Library Platform")) | ||
await self.check_metadata_added(client, "framework", "2.0", "Framework Platform") | ||
await client.close() | ||
|
||
async def test_multiple_successive_metadata_updates_platform_none(self): | ||
client = AsyncMongoClient( | ||
"mongodb://" + self.server.address_string, | ||
maxIdleTimeMS=1, | ||
) | ||
client.append_metadata(DriverInfo("library", "1.2", "Library Platform")) | ||
await self.check_metadata_added(client, "framework", "2.0", None) | ||
await client.close() | ||
|
||
async def test_multiple_successive_metadata_updates_version_none(self): | ||
client = AsyncMongoClient( | ||
"mongodb://" + self.server.address_string, | ||
maxIdleTimeMS=1, | ||
) | ||
client.append_metadata(DriverInfo("library", "1.2", "Library Platform")) | ||
await self.check_metadata_added(client, "framework", None, "Framework Platform") | ||
await client.close() | ||
|
||
async def test_multiple_successive_metadata_updates_platform_version_none(self): | ||
client = AsyncMongoClient( | ||
"mongodb://" + self.server.address_string, | ||
maxIdleTimeMS=1, | ||
) | ||
client.append_metadata(DriverInfo("library", "1.2", "Library Platform")) | ||
await self.check_metadata_added(client, "framework", None, None) | ||
await client.close() | ||
|
||
async def test_doesnt_update_established_connections(self): | ||
listener = CMAPListener() | ||
client = AsyncMongoClient( | ||
"mongodb://" + self.server.address_string, | ||
maxIdleTimeMS=1, | ||
driver=DriverInfo("library", "1.2", "Library Platform"), | ||
event_listeners=[listener], | ||
) | ||
|
||
# send initial metadata | ||
name, version, platform, metadata = await self.send_ping_and_get_metadata(client, True) | ||
self.assertIsNotNone(name) | ||
self.assertIsNotNone(version) | ||
self.assertIsNotNone(platform) | ||
|
||
# add data | ||
add_name, add_version, add_platform = "framework", "2.0", "Framework Platform" | ||
client.append_metadata(DriverInfo(add_name, add_version, add_platform)) | ||
# check new data isn't sent | ||
self.handshake_req: Optional[dict] = None | ||
await client.admin.command("ping") | ||
self.assertIsNone(self.handshake_req) | ||
self.assertEqual(listener.event_count(ConnectionClosedEvent), 0) | ||
|
||
await client.close() | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.