-
Notifications
You must be signed in to change notification settings - Fork 1.2k
PYTHON-5724 Add integration test exercising compression logic #2935
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
aclark4life
wants to merge
9
commits into
mongodb:main
Choose a base branch
from
aclark4life:PYTHON-5724
base: main
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
9 commits
Select commit
Hold shift + click to select a range
443ba23
PYTHON-5724 Add integration test exercising compression logic
aclark4life 2000224
Merge branch 'main' into PYTHON-5724
aclark4life ca58ec1
Merge branch 'main' into PYTHON-5724
aclark4life 35bd9b0
PYTHON-5724 Assert response decompression and skip when compression u…
aclark4life 5b24079
PYTHON-5724 Rename spy sink args to _recorded
aclark4life 621d1eb
PYTHON-5724 Bind original decompress via default arg for consistency
aclark4life b14b5cc
PYTHON-5724 Close each client per subtest to isolate patched decompress
aclark4life b7af159
Merge branch 'main' into PYTHON-5724
aclark4life 39ff087
Merge branch 'main' into PYTHON-5724
aclark4life 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -58,7 +58,7 @@ | |
| ) | ||
| from bson.son import SON | ||
| from bson.tz_util import utc | ||
| from pymongo import event_loggers, message, monitoring | ||
| from pymongo import event_loggers, message, monitoring, network_layer | ||
| from pymongo.asynchronous.command_cursor import AsyncCommandCursor | ||
| from pymongo.asynchronous.cursor import AsyncCursor, CursorType | ||
| from pymongo.asynchronous.database import AsyncDatabase | ||
|
|
@@ -71,7 +71,13 @@ | |
| from pymongo.asynchronous.topology import _ErrorContext | ||
| from pymongo.client_options import ClientOptions | ||
| from pymongo.common import _UUID_REPRESENTATIONS, CONNECT_TIMEOUT, MIN_SUPPORTED_WIRE_VERSION, has_c | ||
| from pymongo.compression_support import _have_snappy, _have_zstd | ||
| from pymongo.compression_support import ( | ||
| SnappyContext, | ||
| ZlibContext, | ||
| ZstdContext, | ||
| _have_snappy, | ||
| _have_zstd, | ||
| ) | ||
| from pymongo.driver_info import DriverInfo | ||
| from pymongo.errors import ( | ||
| AutoReconnect, | ||
|
|
@@ -1897,6 +1903,75 @@ def compression_settings(client): | |
| # No error | ||
| await client.pymongo_test.test.find_one() | ||
|
|
||
| async def test_compression_commands(self): | ||
| # Ensure the compression logic is actually exercised end-to-end by | ||
| # sending commands with each available compressor negotiated. | ||
| candidates: list[tuple[str, type]] = [("zlib", ZlibContext)] | ||
| if _have_snappy(): | ||
| candidates.append(("snappy", SnappyContext)) | ||
| if _have_zstd(): | ||
| candidates.append(("zstd", ZstdContext)) | ||
|
|
||
| negotiated = [] | ||
| for name, ctx_type in candidates: | ||
| with self.subTest(compressor=name): | ||
| # maxPoolSize=1 ensures the operations below reuse the same | ||
| # connection the spy is installed on, unless it is replaced. | ||
| client = await self.async_single_client(compressors=name, maxPoolSize=1) | ||
| # Close each client before moving on: decompress() is patched | ||
| # globally below, so app traffic from a client left over from an | ||
| # earlier subtest could otherwise pollute the recorded ids. | ||
| try: | ||
| # Trigger the connection handshake so the compressor is negotiated. | ||
| await client.admin.command("ping") | ||
| pool = await async_get_pool(client) | ||
| async with pool.checkout() as conn: | ||
| if conn.compression_context is None: | ||
| continue | ||
| negotiated.append(name) | ||
| self.assertIsInstance(conn.compression_context, ctx_type) | ||
|
|
||
| # Spy on the compress method to confirm the outgoing message | ||
| # is actually compressed. | ||
| compressed = [] | ||
| original = conn.compression_context.compress | ||
|
|
||
| # Default args bind the current iteration's values so the | ||
| # closure does not late-bind the loop variables. | ||
| def spy(data, _original=original, _recorded=compressed): | ||
| _recorded.append(data) | ||
| return _original(data) | ||
|
|
||
| conn.compression_context.compress = spy | ||
|
|
||
| # Spy on the read path's decompress() to confirm the server's | ||
| # replies are actually compressed too. | ||
| decompressed = [] | ||
| original_decompress = network_layer.decompress | ||
|
|
||
| def decompress_spy( | ||
| data, compressor_id, _original=original_decompress, _recorded=decompressed | ||
| ): | ||
| _recorded.append(compressor_id) | ||
| return _original(data, compressor_id) | ||
|
|
||
| # Round-trip a command large enough to compress. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All non-sensitive commands are compressed, we don't have a size limit. |
||
| coll = client.pymongo_test.test_compression | ||
| await coll.drop() | ||
| with patch.object(network_layer, "decompress", decompress_spy): | ||
| await coll.insert_one({"x": "y" * 1024}) | ||
| doc = await coll.find_one({}, {"_id": 0}) | ||
| self.assertEqual(doc, {"x": "y" * 1024}) | ||
| self.assertTrue(compressed, "compress() was never called") | ||
| self.assertTrue(decompressed, "decompress() was never called") | ||
| self.assertEqual(set(decompressed), {ctx_type.compressor_id}) | ||
| await coll.drop() | ||
| finally: | ||
| await client.close() | ||
|
|
||
| if not negotiated: | ||
| self.skipTest("server did not negotiate compression for any compressor") | ||
|
|
||
| @async_client_context.require_sync | ||
| async def test_reset_during_update_pool(self): | ||
| client = await self.async_rs_or_single_client(minPoolSize=10) | ||
|
|
||
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.
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.