Skip to content

PYTHON-4542 Improved sessions API #2335

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 13 commits into
base: master
Choose a base branch
from
14 changes: 12 additions & 2 deletions test/asynchronous/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,12 +394,22 @@ async def test_cursor_clone(self):
await cursor.close()
await clone.close()

async def test_bind_session(self):
coll = self.client.pymongo_test.collection

# Explicit session via context variable.
async with self.client.start_session(bind=True) as s:
cursor = coll.find()
self.assertTrue(cursor.session is s)
clone = cursor.clone()
self.assertTrue(clone.session is s)

# Nested sessions.
session1 = self.client.start_session(bind=True)
with session1:
session2 = self.client.start_session(bind=True)
with session2:
coll.find_one() # uses session2
coll.find_one() # uses session1
coll.find_one() # uses implicit session
Copy link
Member

Choose a reason for hiding this comment

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

This test has to actually verify the correct sessions are used via command monitoring events.

Copy link
Contributor Author

@aclark4life aclark4life Jun 2, 2025

Choose a reason for hiding this comment

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

How about comparing with the cursor session ? E.g. c36e9df


async def test_cursor(self):
listener = self.listener
Expand Down
14 changes: 12 additions & 2 deletions test/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,12 +394,22 @@ def test_cursor_clone(self):
cursor.close()
clone.close()

def test_bind_session(self):
coll = self.client.pymongo_test.collection

# Explicit session via context variable.
with self.client.start_session(bind=True) as s:
cursor = coll.find()
self.assertTrue(cursor.session is s)
clone = cursor.clone()
self.assertTrue(clone.session is s)

# Nested sessions.
session1 = self.client.start_session(bind=True)
with session1:
session2 = self.client.start_session(bind=True)
with session2:
coll.find_one() # uses session2
coll.find_one() # uses session1
coll.find_one() # uses implicit session

def test_cursor(self):
listener = self.listener
Expand Down
Loading