Skip to content

PYTHON-3260 Improve test_transaction_starts_with_batched_write and test_continuous_network_errors #945

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

Merged
merged 2 commits into from
May 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 3 additions & 4 deletions test/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1596,7 +1596,6 @@ def test_direct_connection(self):
with self.assertRaises(ConfigurationError):
MongoClient(["host1", "host2"], directConnection=True)

@unittest.skipIf(sys.platform.startswith("java"), "Jython does not support gc.get_objects")
@unittest.skipIf("PyPy" in sys.version, "PYTHON-2927 fails often on PyPy")
def test_continuous_network_errors(self):
def server_description_count():
Expand All @@ -1612,7 +1611,7 @@ def server_description_count():
gc.collect()
with client_knobs(min_heartbeat_interval=0.003):
client = MongoClient(
"invalid:27017", heartbeatFrequencyMS=3, serverSelectionTimeoutMS=100
"invalid:27017", heartbeatFrequencyMS=3, serverSelectionTimeoutMS=150
)
initial_count = server_description_count()
self.addCleanup(client.close)
Expand All @@ -1622,8 +1621,8 @@ def server_description_count():
final_count = server_description_count()
# If a bug like PYTHON-2433 is reintroduced then too many
# ServerDescriptions will be kept alive and this test will fail:
# AssertionError: 4 != 22 within 5 delta (18 difference)
self.assertAlmostEqual(initial_count, final_count, delta=10)
# AssertionError: 19 != 46 within 15 delta (27 difference)
self.assertAlmostEqual(initial_count, final_count, delta=15)

@client_context.require_failCommand_fail_point
def test_network_error_message(self):
Expand Down
10 changes: 6 additions & 4 deletions test/test_transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
)
from test.utils_spec_runner import SpecRunner

from bson import encode
from bson.raw_bson import RawBSONDocument
from gridfs import GridFS, GridFSBucket
from pymongo import WriteConcern, client_session
from pymongo.client_session import TransactionOptions
Expand Down Expand Up @@ -330,14 +332,14 @@ def test_transaction_starts_with_batched_write(self):
listener.reset()
self.addCleanup(client.close)
self.addCleanup(coll.drop)
large_str = "\0" * (10 * 1024 * 1024)
ops = [InsertOne({"a": large_str}) for _ in range(10)]
large_str = "\0" * (1 * 1024 * 1024)
ops = [InsertOne(RawBSONDocument(encode({"a": large_str}))) for _ in range(48)]
with client.start_session() as session:
with session.start_transaction():
coll.bulk_write(ops, session=session)
# Assert commands were constructed properly.
self.assertEqual(
["insert", "insert", "insert", "commitTransaction"], listener.started_command_names()
["insert", "insert", "commitTransaction"], listener.started_command_names()
)
first_cmd = listener.results["started"][0].command
self.assertTrue(first_cmd["startTransaction"])
Expand All @@ -347,7 +349,7 @@ def test_transaction_starts_with_batched_write(self):
self.assertNotIn("startTransaction", event.command)
self.assertEqual(lsid, event.command["lsid"])
self.assertEqual(txn_number, event.command["txnNumber"])
self.assertEqual(10, coll.count_documents({}))
self.assertEqual(48, coll.count_documents({}))


class PatchSessionTimeout(object):
Expand Down