Skip to content

PYTHON-5488 append_metadata should not add duplicates #2461

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
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pymongo/pool_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,8 @@ def __init__(

def _update_metadata(self, driver: DriverInfo) -> None:
"""Updates the client's metadata"""
if driver.name in self.__metadata["driver"]["name"].split("|"):
return
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm thinking we should be case-insensitive. "LangChain" vs. "langchain" should not make a difference.

I cannot remember if we already lowercase everything in name (I don't think we do).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good call, changed!


metadata = copy.deepcopy(self.__metadata)
if driver.name:
Expand Down
40 changes: 40 additions & 0 deletions test/asynchronous/test_client_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,46 @@ async def test_doesnt_update_established_connections(self):
self.assertIsNone(self.handshake_req)
self.assertEqual(listener.event_count(ConnectionClosedEvent), 0)

async def test_duplicate_driver_name_no_op(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we refactor check_metadata_added to support checking that the new metadata is the same as the old metadata? Most of this method is just duplicate code from check_metadata_added.

client = await self.async_rs_or_single_client(
"mongodb://" + self.server.address_string,
maxIdleTimeMS=1,
)
client.append_metadata(DriverInfo("library", "1.2", "Library Platform"))
# 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("framework", None, None))
new_name, new_version, new_platform, new_metadata = await self.send_ping_and_get_metadata(
client, True
)
self.assertEqual(new_name, f"{name}|framework")
self.assertEqual(new_version, version)
self.assertEqual(new_platform, platform)

metadata.pop("driver")
metadata.pop("platform")
new_metadata.pop("driver")
new_metadata.pop("platform")
self.assertEqual(metadata, new_metadata)

# wait for connection to become idle
await asyncio.sleep(0.005)
# add same metadata again
client.append_metadata(DriverInfo("framework", None, None))
(
same_name,
same_version,
same_platform,
same_metadata,
) = await self.send_ping_and_get_metadata(client, True)
self.assertEqual(new_name, same_name)
self.assertEqual(new_version, same_version)
self.assertEqual(new_platform, same_platform)


if __name__ == "__main__":
unittest.main()
40 changes: 40 additions & 0 deletions test/test_client_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,46 @@ def test_doesnt_update_established_connections(self):
self.assertIsNone(self.handshake_req)
self.assertEqual(listener.event_count(ConnectionClosedEvent), 0)

def test_duplicate_driver_name_no_op(self):
client = self.rs_or_single_client(
"mongodb://" + self.server.address_string,
maxIdleTimeMS=1,
)
client.append_metadata(DriverInfo("library", "1.2", "Library Platform"))
# send initial metadata
name, version, platform, metadata = self.send_ping_and_get_metadata(client, True)
# wait for connection to become idle
time.sleep(0.005)

# add new metadata
client.append_metadata(DriverInfo("framework", None, None))
new_name, new_version, new_platform, new_metadata = self.send_ping_and_get_metadata(
client, True
)
self.assertEqual(new_name, f"{name}|framework")
self.assertEqual(new_version, version)
self.assertEqual(new_platform, platform)

metadata.pop("driver")
metadata.pop("platform")
new_metadata.pop("driver")
new_metadata.pop("platform")
self.assertEqual(metadata, new_metadata)

# wait for connection to become idle
time.sleep(0.005)
# add same metadata again
client.append_metadata(DriverInfo("framework", None, None))
(
same_name,
same_version,
same_platform,
same_metadata,
) = self.send_ping_and_get_metadata(client, True)
self.assertEqual(new_name, same_name)
self.assertEqual(new_version, same_version)
self.assertEqual(new_platform, same_platform)


if __name__ == "__main__":
unittest.main()
Loading