Skip to content

Commit 845fc5f

Browse files
ShaneHarveyblink1073
authored andcommitted
PYTHON-3260 Improve test_transaction_starts_with_batched_write and test_continuous_network_errors (mongodb#945)
(cherry picked from commit a624197)
1 parent 3638a58 commit 845fc5f

File tree

2 files changed

+72
-6
lines changed

2 files changed

+72
-6
lines changed

test/test_client.py

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1621,7 +1621,6 @@ def test_direct_connection(self):
16211621
with self.assertRaises(ConfigurationError):
16221622
MongoClient(["host1", "host2"], directConnection=True)
16231623

1624-
@unittest.skipIf(sys.platform.startswith("java"), "Jython does not support gc.get_objects")
16251624
@unittest.skipIf("PyPy" in sys.version, "PYTHON-2927 fails often on PyPy")
16261625
def test_continuous_network_errors(self):
16271626
def server_description_count():
@@ -1637,7 +1636,7 @@ def server_description_count():
16371636
gc.collect()
16381637
with client_knobs(min_heartbeat_interval=0.003):
16391638
client = MongoClient(
1640-
"invalid:27017", heartbeatFrequencyMS=3, serverSelectionTimeoutMS=100
1639+
"invalid:27017", heartbeatFrequencyMS=3, serverSelectionTimeoutMS=150
16411640
)
16421641
initial_count = server_description_count()
16431642
self.addCleanup(client.close)
@@ -1651,6 +1650,70 @@ def server_description_count():
16511650
# On Python 3.11 we seem to get more of a delta.
16521651
self.assertAlmostEqual(initial_count, final_count, delta=20)
16531652

1653+
@client_context.require_failCommand_fail_point
1654+
def test_network_error_message(self):
1655+
client = single_client(retryReads=False)
1656+
self.addCleanup(client.close)
1657+
client.admin.command("ping") # connect
1658+
with self.fail_point(
1659+
{"mode": {"times": 1}, "data": {"closeConnection": True, "failCommands": ["find"]}}
1660+
):
1661+
expected = "%s:%s: " % client.address
1662+
with self.assertRaisesRegex(AutoReconnect, expected):
1663+
client.pymongo_test.test.find_one({})
1664+
1665+
@unittest.skipIf("PyPy" in sys.version, "PYTHON-2938 could fail on PyPy")
1666+
def test_process_periodic_tasks(self):
1667+
client = rs_or_single_client()
1668+
coll = client.db.collection
1669+
coll.insert_many([{} for _ in range(5)])
1670+
cursor = coll.find(batch_size=2)
1671+
cursor.next()
1672+
c_id = cursor.cursor_id
1673+
self.assertIsNotNone(c_id)
1674+
client.close()
1675+
# Add cursor to kill cursors queue
1676+
del cursor
1677+
wait_until(
1678+
lambda: client._MongoClient__kill_cursors_queue,
1679+
"waited for cursor to be added to queue",
1680+
)
1681+
client._process_periodic_tasks() # This must not raise or print any exceptions
1682+
with self.assertRaises(InvalidOperation):
1683+
coll.insert_many([{} for _ in range(5)])
1684+
1685+
@unittest.skipUnless(_HAVE_DNSPYTHON, "DNS-related tests need dnspython to be installed")
1686+
def test_service_name_from_kwargs(self):
1687+
client = MongoClient(
1688+
"mongodb+srv://user:[email protected]",
1689+
srvServiceName="customname",
1690+
connect=False,
1691+
)
1692+
self.assertEqual(client._topology_settings.srv_service_name, "customname")
1693+
client = MongoClient(
1694+
"mongodb+srv://user:[email protected]"
1695+
"/?srvServiceName=shouldbeoverriden",
1696+
srvServiceName="customname",
1697+
connect=False,
1698+
)
1699+
self.assertEqual(client._topology_settings.srv_service_name, "customname")
1700+
client = MongoClient(
1701+
"mongodb+srv://user:[email protected]/?srvServiceName=customname",
1702+
connect=False,
1703+
)
1704+
self.assertEqual(client._topology_settings.srv_service_name, "customname")
1705+
1706+
@unittest.skipUnless(_HAVE_DNSPYTHON, "DNS-related tests need dnspython to be installed")
1707+
def test_srv_max_hosts_kwarg(self):
1708+
client = MongoClient("mongodb+srv://test1.test.build.10gen.cc/")
1709+
self.assertGreater(len(client.topology_description.server_descriptions()), 1)
1710+
client = MongoClient("mongodb+srv://test1.test.build.10gen.cc/", srvmaxhosts=1)
1711+
self.assertEqual(len(client.topology_description.server_descriptions()), 1)
1712+
client = MongoClient(
1713+
"mongodb+srv://test1.test.build.10gen.cc/?srvMaxHosts=1", srvmaxhosts=2
1714+
)
1715+
self.assertEqual(len(client.topology_description.server_descriptions()), 2)
1716+
16541717
@unittest.skipIf(_HAVE_DNSPYTHON, "dnspython must not be installed")
16551718
def test_srv_no_dnspython_error(self):
16561719
with self.assertRaisesRegex(ConfigurationError, 'The "dnspython" module must be'):

test/test_transactions.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
from test.utils_spec_runner import SpecRunner
3131

3232
from bson.py3compat import StringIO
33+
from bson import encode
34+
from bson.raw_bson import RawBSONDocument
35+
3336
from gridfs import GridFS, GridFSBucket
3437
from pymongo import WriteConcern, client_session
3538
from pymongo.client_session import TransactionOptions
@@ -332,14 +335,14 @@ def test_transaction_starts_with_batched_write(self):
332335
listener.reset()
333336
self.addCleanup(client.close)
334337
self.addCleanup(coll.drop)
335-
large_str = "\0" * (10 * 1024 * 1024)
336-
ops = [InsertOne({"a": large_str}) for _ in range(10)]
338+
large_str = "\0" * (1 * 1024 * 1024)
339+
ops = [InsertOne(RawBSONDocument(encode({"a": large_str}))) for _ in range(48)]
337340
with client.start_session() as session:
338341
with session.start_transaction():
339342
coll.bulk_write(ops, session=session)
340343
# Assert commands were constructed properly.
341344
self.assertEqual(
342-
["insert", "insert", "insert", "commitTransaction"], listener.started_command_names()
345+
["insert", "insert", "commitTransaction"], listener.started_command_names()
343346
)
344347
first_cmd = listener.results["started"][0].command
345348
self.assertTrue(first_cmd["startTransaction"])
@@ -349,7 +352,7 @@ def test_transaction_starts_with_batched_write(self):
349352
self.assertNotIn("startTransaction", event.command)
350353
self.assertEqual(lsid, event.command["lsid"])
351354
self.assertEqual(txn_number, event.command["txnNumber"])
352-
self.assertEqual(10, coll.count_documents({}))
355+
self.assertEqual(48, coll.count_documents({}))
353356

354357

355358
class PatchSessionTimeout(object):

0 commit comments

Comments
 (0)