Skip to content

PYTHON-5781 Coverage increase for network_layer.py#2774

Draft
aclark4life wants to merge 7 commits intomongodb:masterfrom
aclark4life:PYTHON-5781
Draft

PYTHON-5781 Coverage increase for network_layer.py#2774
aclark4life wants to merge 7 commits intomongodb:masterfrom
aclark4life:PYTHON-5781

Conversation

@aclark4life
Copy link
Copy Markdown
Contributor

@aclark4life aclark4life commented Apr 23, 2026

PYTHON-5781

Changes in this PR

Adds unit tests for pymongo/network_layer.py, split into two files:

  • test/asynchronous/test_network_layer.py (synchro'd to test/test_network_layer.py) — tests for code shared between the sync and async APIs.
  • test/asynchronous/test_async_network_layer.py (not synchro'd; added to async_only_test() in tools/synchro.py) — tests for asyncio-specific code (PyMongoProtocol, _async_socket_receive).

Tests cover:

Shared (NetworkingInterfaceBase)

  • NotImplementedError on gettimeout, settimeout, close, is_closing, get_conn, sock.

Async-only — wire-protocol parsing (PyMongoProtocol)

  • process_header() — OP_MSG, OP_REPLY, OP_COMPRESSED, and the three ProtocolError paths (compressed length ≤ 25, uncompressed length ≤ 16, length > max message size).
  • process_compression_header() — snappy and zlib compressor IDs.
  • buffer_updated() — completes pending future on full message.
  • close() / connection_lost() — abort transport, exception propagation to pending futures, idempotency on repeated connection_lost.
  • timeout init / gettimeout / settimeout.

Async-only — _async_socket_receive()

  • Multi-chunk accumulation (covers the read loop).
  • Raises OSError("connection closed") when the socket returns 0 bytes.

Test Plan

Added unit tests using MagicMock transports/sockets and the AsyncUnitTest event-loop harness for the asyncio-dependent protocol code. No live socket or MongoDB server required.

uv run --extra test python -m pytest test/test_network_layer.py test/asynchronous/test_network_layer.py test/asynchronous/test_async_network_layer.py -v

Checklist

Checklist for Author

  • Did you update the changelog (if necessary)?
  • Is there test coverage?
  • Is any followup work tracked in a JIRA ticket? If so, add link(s).

Checklist for Reviewer

  • Does the title of the PR reference a JIRA Ticket?
  • Do you fully understand the implementation? (Would you be comfortable explaining how this code works to someone else?)
  • Is all relevant documentation (README or docstring) updated?

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds a dedicated unit test suite to increase coverage of pymongo/network_layer.py, focusing on logic that can be validated without live sockets or a running MongoDB server.

Changes:

  • Introduces test/test_network_layer.py with unit tests for PyMongoProtocol state transitions and message parsing helpers.
  • Adds delegation/behavior tests for NetworkingInterfaceBase, NetworkingInterface, and AsyncNetworkingInterface.
  • Adds tests for small helper functions (sendall, _async_socket_receive) using mocks.

Comment thread test/test_network_layer.py Outdated
@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Apr 23, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 88.01%. Comparing base (f4219bd) to head (33c8039).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2774      +/-   ##
==========================================
+ Coverage   82.68%   88.01%   +5.33%     
==========================================
  Files         141      141              
  Lines       24406    24406              
  Branches     4176     4176              
==========================================
+ Hits        20179    21480    +1301     
+ Misses       3328     2033    -1295     
+ Partials      899      893       -6     
Flag Coverage Δ
auth-aws-rhel8-test-auth-aws-rapid-web-identity-python3.14-cov 35.02% <ø> (+0.01%) ⬆️
auth-aws-win64-test-auth-aws-rapid-web-identity-python3.14-cov 35.01% <ø> (-0.03%) ⬇️
auth-enterprise-macos-test-standard-auth-latest-python3.11-auth-ssl-sharded-cluster-cov 43.85% <ø> (+0.10%) ⬆️
auth-enterprise-rhel8-test-standard-auth-latest-python3.11-auth-ssl-sharded-cluster-cov 43.75% <ø> (ø)
auth-enterprise-win64-test-standard-auth-latest-python3.11-auth-ssl-sharded-cluster-cov 43.77% <ø> (-0.01%) ⬇️
auth-oidc-local-ubuntu-22-test-auth-oidc-default 48.61% <ø> (?)
compression-snappy-rhel8-test-standard-latest-python3.11-async-noauth-nossl-standalone-cov 59.43% <ø> (+0.08%) ⬆️
compression-snappy-rhel8-test-standard-latest-python3.12-async-noauth-ssl-replica-set-cov 61.59% <ø> (+0.11%) ⬆️
compression-snappy-rhel8-test-standard-latest-python3.13-async-auth-ssl-sharded-cluster-cov 61.19% <ø> (+0.09%) ⬆️
compression-snappy-rhel8-test-standard-latest-python3.14-async-noauth-nossl-standalone-cov 59.09% <ø> (+0.08%) ⬆️
compression-zlib-rhel8-test-standard-latest-python3.11-async-noauth-nossl-standalone-cov 59.43% <ø> (+0.09%) ⬆️
compression-zlib-rhel8-test-standard-latest-python3.12-async-noauth-ssl-replica-set-cov 61.60% <ø> (+0.09%) ⬆️
compression-zlib-rhel8-test-standard-latest-python3.13-async-auth-ssl-sharded-cluster-cov 61.19% <ø> (+0.09%) ⬆️
compression-zlib-rhel8-test-standard-latest-python3.14-async-noauth-nossl-standalone-cov 59.10% <ø> (+0.08%) ⬆️
compression-zstd-rhel8-test-standard-latest-python3.11-async-noauth-nossl-standalone-cov 59.44% <ø> (+0.08%) ⬆️
compression-zstd-rhel8-test-standard-latest-python3.12-async-noauth-ssl-replica-set-cov 61.60% <ø> (+0.13%) ⬆️
compression-zstd-rhel8-test-standard-latest-python3.13-async-auth-ssl-sharded-cluster-cov 61.19% <ø> (+0.09%) ⬆️
compression-zstd-rhel8-test-standard-latest-python3.14-async-noauth-nossl-standalone-cov 59.09% <ø> (+0.07%) ⬆️
compression-zstd-ubuntu-22-test-standard-latest-python3.14-async-noauth-nossl-standalone-cov 59.07% <ø> (+0.09%) ⬆️
coverage-report-coverage-report 87.98% <ø> (+5.56%) ⬆️
disable-test-commands-rhel8-test-standard-latest-python3.11-async-noauth-nossl-standalone-cov 59.43% <ø> (+0.08%) ⬆️
disable-test-commands-rhel8-test-standard-latest-python3.12-async-noauth-ssl-replica-set-cov 61.56% <ø> (+0.05%) ⬆️
disable-test-commands-rhel8-test-standard-latest-python3.13-async-auth-ssl-sharded-cluster-cov 61.18% <ø> (+0.08%) ⬆️
disable-test-commands-rhel8-test-standard-latest-python3.14-async-noauth-nossl-standalone-cov 59.09% <ø> (+0.09%) ⬆️
encryption-crypt_shared-macos-test-non-standard-latest-python3.13-noauth-nossl-standalone-cov 52.72% <ø> (?)
encryption-crypt_shared-macos-test-non-standard-latest-python3.14-auth-ssl-sharded-cluster-cov 54.55% <ø> (?)
encryption-crypt_shared-macos-test-non-standard-latest-python3.14t-noauth-ssl-replica-set-cov 54.37% <ø> (?)
encryption-crypt_shared-rhel8-test-non-standard-latest-python3.13-noauth-nossl-standalone-cov 52.72% <ø> (?)
encryption-crypt_shared-rhel8-test-non-standard-latest-python3.14-auth-ssl-sharded-cluster-cov 54.48% <ø> (?)
encryption-crypt_shared-rhel8-test-non-standard-latest-python3.14t-noauth-ssl-replica-set-cov 54.32% <ø> (?)
encryption-crypt_shared-win64-test-non-standard-latest-python3.13-noauth-nossl-standalone-cov 52.66% <ø> (?)
encryption-crypt_shared-win64-test-non-standard-latest-python3.14-auth-ssl-sharded-cluster-cov 54.45% <ø> (?)
encryption-crypt_shared-win64-test-non-standard-latest-python3.14t-noauth-ssl-replica-set-cov 54.44% <ø> (?)
encryption-macos-test-non-standard-latest-python3.13-noauth-nossl-standalone-cov 52.72% <ø> (?)
encryption-macos-test-non-standard-latest-python3.14-auth-ssl-sharded-cluster-cov 54.54% <ø> (?)
encryption-macos-test-non-standard-latest-python3.14t-noauth-ssl-replica-set-cov 54.36% <ø> (?)
encryption-pyopenssl-rhel8-test-non-standard-latest-python3.13-noauth-nossl-standalone-cov 53.38% <ø> (?)
encryption-pyopenssl-rhel8-test-non-standard-latest-python3.14-auth-ssl-sharded-cluster-cov 55.10% <ø> (?)
encryption-pyopenssl-rhel8-test-non-standard-latest-python3.14t-noauth-ssl-replica-set-cov 55.03% <ø> (?)
encryption-rhel8-test-non-standard-latest-python3.13-noauth-nossl-standalone-cov 52.72% <ø> (?)
encryption-rhel8-test-non-standard-latest-python3.14-auth-ssl-sharded-cluster-cov 54.41% <ø> (?)
encryption-rhel8-test-non-standard-latest-python3.14t-noauth-ssl-replica-set-cov 54.35% <ø> (?)
encryption-win64-test-non-standard-latest-python3.13-noauth-nossl-standalone-cov 52.67% <ø> (?)
encryption-win64-test-non-standard-latest-python3.14-auth-ssl-sharded-cluster-cov 54.45% <ø> (?)
encryption-win64-test-non-standard-latest-python3.14t-noauth-ssl-replica-set-cov 54.41% <ø> (?)
load-balancer-test-non-standard-latest-python3.14-auth-ssl-sharded-cluster-cov 48.40% <ø> (?)
mongodb-latest-test-server-version-python3.10-async-auth-ssl-sharded-cluster-min-deps-cov 61.56% <ø> (+0.09%) ⬆️
mongodb-latest-test-server-version-python3.10-async-noauth-nossl-standalone-min-deps-cov 59.42% <ø> (+0.07%) ⬆️
mongodb-latest-test-server-version-python3.10-sync-auth-ssl-sharded-cluster-min-deps-cov 59.51% <ø> (ø)
mongodb-latest-test-server-version-python3.10-sync-noauth-nossl-replica-set-min-deps-cov 59.60% <ø> (ø)
mongodb-latest-test-server-version-python3.11-async-noauth-nossl-replica-set-cov 61.50% <ø> (+0.08%) ⬆️
mongodb-rapid-test-server-version-python3.10-async-auth-ssl-sharded-cluster-min-deps-cov 61.56% <ø> (+0.08%) ⬆️
mongodb-rapid-test-server-version-python3.10-async-noauth-nossl-standalone-min-deps-cov 59.44% <ø> (+0.08%) ⬆️
mongodb-rapid-test-server-version-python3.10-sync-auth-ssl-sharded-cluster-min-deps-cov 59.50% <ø> (ø)
mongodb-rapid-test-server-version-python3.10-sync-noauth-nossl-replica-set-min-deps-cov 59.59% <ø> (ø)
mongodb-rapid-test-server-version-python3.11-async-noauth-nossl-replica-set-cov 61.50% <ø> (+0.07%) ⬆️
mongodb-v4.2-test-server-version-python3.10-async-auth-ssl-sharded-cluster-min-deps-cov 57.28% <ø> (+0.10%) ⬆️
mongodb-v4.2-test-server-version-python3.10-async-noauth-nossl-standalone-min-deps-cov 55.70% <ø> (+0.09%) ⬆️
mongodb-v4.2-test-server-version-python3.10-sync-auth-ssl-sharded-cluster-min-deps-cov 57.06% <ø> (ø)
mongodb-v4.2-test-server-version-python3.10-sync-noauth-nossl-replica-set-min-deps-cov 57.17% <ø> (ø)
mongodb-v4.2-test-server-version-python3.11-async-noauth-nossl-replica-set-cov 57.44% <ø> (+0.09%) ⬆️
mongodb-v4.4-test-server-version-python3.10-async-auth-ssl-sharded-cluster-min-deps-cov 59.65% <ø> (+0.08%) ⬆️
mongodb-v4.4-test-server-version-python3.10-async-noauth-nossl-standalone-min-deps-cov 57.66% <ø> (+0.07%) ⬆️
mongodb-v4.4-test-server-version-python3.10-sync-auth-ssl-sharded-cluster-min-deps-cov 57.57% <ø> (ø)
mongodb-v4.4-test-server-version-python3.10-sync-noauth-nossl-replica-set-min-deps-cov 57.65% <ø> (ø)
mongodb-v4.4-test-server-version-python3.11-async-noauth-nossl-replica-set-cov 59.44% <ø> (+0.10%) ⬆️
mongodb-v5.0-test-server-version-python3.10-async-auth-ssl-sharded-cluster-min-deps-cov 59.84% <ø> (+0.07%) ⬆️
mongodb-v5.0-test-server-version-python3.10-async-noauth-nossl-standalone-min-deps-cov 57.83% <ø> (+0.09%) ⬆️
mongodb-v5.0-test-server-version-python3.10-sync-auth-ssl-sharded-cluster-min-deps-cov 57.77% <ø> (ø)
mongodb-v5.0-test-server-version-python3.10-sync-noauth-nossl-replica-set-min-deps-cov 57.88% <ø> (ø)
mongodb-v5.0-test-server-version-python3.11-async-noauth-nossl-replica-set-cov 59.67% <ø> (+0.09%) ⬆️
mongodb-v6.0-test-server-version-python3.10-async-auth-ssl-sharded-cluster-min-deps-cov 59.87% <ø> (+0.10%) ⬆️
mongodb-v6.0-test-server-version-python3.10-async-noauth-nossl-standalone-min-deps-cov 57.84% <ø> (+0.09%) ⬆️
mongodb-v6.0-test-server-version-python3.10-sync-auth-ssl-sharded-cluster-min-deps-cov 57.79% <ø> (ø)
mongodb-v6.0-test-server-version-python3.10-sync-noauth-nossl-replica-set-min-deps-cov 57.93% <ø> (ø)
mongodb-v6.0-test-server-version-python3.11-async-noauth-nossl-replica-set-cov 59.83% <ø> (+0.08%) ⬆️
mongodb-v7.0-test-server-version-python3.10-async-auth-ssl-sharded-cluster-min-deps-cov 59.89% <ø> (+0.09%) ⬆️
mongodb-v7.0-test-server-version-python3.10-async-noauth-nossl-standalone-min-deps-cov 57.82% <ø> (+0.08%) ⬆️
mongodb-v7.0-test-server-version-python3.10-sync-auth-ssl-sharded-cluster-min-deps-cov 57.84% <ø> (ø)
mongodb-v7.0-test-server-version-python3.10-sync-noauth-nossl-replica-set-min-deps-cov 57.93% <ø> (ø)
mongodb-v7.0-test-server-version-python3.11-async-noauth-nossl-replica-set-cov 59.81% <ø> (+0.07%) ⬆️
mongodb-v8.0-test-server-version-python3.10-async-auth-ssl-sharded-cluster-min-deps-cov 61.57% <ø> (+0.09%) ⬆️
mongodb-v8.0-test-server-version-python3.10-async-noauth-nossl-standalone-min-deps-cov 59.43% <ø> (+0.10%) ⬆️
mongodb-v8.0-test-server-version-python3.10-sync-auth-ssl-sharded-cluster-min-deps-cov 59.50% <ø> (ø)
mongodb-v8.0-test-server-version-python3.10-sync-noauth-nossl-replica-set-min-deps-cov 59.59% <ø> (ø)
mongodb-v8.0-test-server-version-python3.11-async-noauth-nossl-replica-set-cov 61.50% <ø> (+0.09%) ⬆️
no-c-ext-rhel8-test-standard-latest-python3.11-async-noauth-nossl-standalone-cov 60.65% <ø> (+0.10%) ⬆️
no-c-ext-rhel8-test-standard-latest-python3.12-async-noauth-ssl-replica-set-cov 62.80% <ø> (+0.12%) ⬆️
no-c-ext-rhel8-test-standard-latest-python3.13-async-auth-ssl-sharded-cluster-cov 62.39% <ø> (+0.09%) ⬆️
no-c-ext-rhel8-test-standard-latest-python3.14-async-noauth-nossl-standalone-cov 60.29% <ø> (+0.07%) ⬆️
ocsp-rhel8-test-ocsp-ecdsa-valid-cert-server-staples-latest-python3.14-cov 34.21% <ø> (ø)
ocsp-rhel8-test-ocsp-rsa-valid-cert-server-staples-latest-python3.14-cov 34.24% <ø> (+0.03%) ⬆️
pyopenssl-macos-test-standard-latest-python3.12-async-noauth-ssl-replica-set-cov 61.81% <ø> (?)
pyopenssl-rhel8-test-standard-latest-python3.12-async-noauth-ssl-replica-set-cov 61.81% <ø> (?)
pyopenssl-win64-test-standard-latest-python3.12-async-noauth-ssl-replica-set-cov 61.75% <ø> (?)
stable-api-accept-v2-rhel8-auth-test-standard-latest-python3.11-async-noauth-nossl-standalone-cov 59.43% <ø> (+0.08%) ⬆️
stable-api-accept-v2-rhel8-auth-test-standard-latest-python3.14-async-noauth-nossl-standalone-cov 59.09% <ø> (+0.07%) ⬆️
stable-api-require-v1-rhel8-auth-test-standard-latest-python3.11-async-noauth-nossl-standalone-cov 59.41% <ø> (+0.09%) ⬆️
stable-api-require-v1-rhel8-auth-test-standard-latest-python3.13-async-auth-ssl-sharded-cluster-cov 61.04% <ø> (+0.08%) ⬆️
stable-api-require-v1-rhel8-auth-test-standard-latest-python3.14-async-noauth-nossl-standalone-cov 59.05% <ø> (+0.09%) ⬆️
storage-inmemory-rhel8-test-standard-latest-python3.11-async-noauth-nossl-standalone-cov 59.43% <ø> (+0.09%) ⬆️
storage-inmemory-rhel8-test-standard-latest-python3.14-async-noauth-nossl-standalone-cov 59.09% <ø> (+0.07%) ⬆️
test-macos-arm64-test-standard-latest-python3.11-async-noauth-nossl-standalone-cov 59.42% <ø> (+0.09%) ⬆️
test-macos-arm64-test-standard-latest-python3.12-async-noauth-ssl-replica-set-cov 61.61% <ø> (+0.09%) ⬆️
test-macos-arm64-test-standard-latest-python3.13-async-auth-ssl-sharded-cluster-cov 61.17% <ø> (+0.08%) ⬆️
test-macos-arm64-test-standard-latest-python3.14-async-noauth-nossl-standalone-cov 59.05% <ø> (+0.07%) ⬆️
test-macos-test-standard-latest-python3.11-async-noauth-nossl-standalone-cov 59.42% <ø> (+0.11%) ⬆️
test-macos-test-standard-latest-python3.12-async-noauth-ssl-replica-set-cov 61.58% <ø> (+0.09%) ⬆️
test-macos-test-standard-latest-python3.13-async-auth-ssl-sharded-cluster-cov 61.18% <ø> (+0.09%) ⬆️
test-macos-test-standard-latest-python3.14-async-noauth-nossl-standalone-cov 59.07% <ø> (+0.08%) ⬆️
test-numpy-macos-arm64-test-numpy-python3.14-python3.14-cov 32.61% <ø> (-0.02%) ⬇️
test-numpy-macos-test-numpy-python3.14-python3.14-cov 32.61% <ø> (ø)
test-numpy-rhel8-test-numpy-python3.14-python3.14-cov 32.61% <ø> (ø)
test-numpy-win32-test-numpy-python3.14-python3.14-cov 32.57% <ø> (-0.02%) ⬇️
test-numpy-win64-test-numpy-python3.14-python3.14-cov 32.59% <ø> (ø)
test-win32-test-standard-latest-python3.11-async-noauth-nossl-standalone-cov 59.30% <ø> (+0.09%) ⬆️
test-win32-test-standard-latest-python3.12-async-noauth-ssl-replica-set-cov 61.52% <ø> (+0.08%) ⬆️
test-win32-test-standard-latest-python3.13-async-auth-ssl-sharded-cluster-cov 61.11% <ø> (+0.04%) ⬆️
test-win32-test-standard-latest-python3.14-async-noauth-nossl-standalone-cov 58.95% <ø> (+0.07%) ⬆️
test-win64-test-standard-latest-python3.11-async-noauth-nossl-standalone-cov 59.30% <ø> (+0.10%) ⬆️
test-win64-test-standard-latest-python3.12-async-noauth-ssl-replica-set-cov 61.52% <ø> (+0.07%) ⬆️
test-win64-test-standard-latest-python3.13-async-auth-ssl-sharded-cluster-cov 61.13% <ø> (+0.06%) ⬆️
test-win64-test-standard-latest-python3.14-async-noauth-nossl-standalone-cov 58.95% <ø> (+0.08%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@aclark4life aclark4life changed the title PYTHON-5781 Increase coverage for network_layer.py PYTHON-5781 Increase code coverage for network_layer.py Apr 24, 2026
caseyclements
caseyclements previously approved these changes Apr 29, 2026
Copy link
Copy Markdown
Contributor

@caseyclements caseyclements left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

Comment thread tools/synchro.py
@aclark4life aclark4life force-pushed the PYTHON-5781 branch 7 times, most recently from c6991e0 to 72fd1a4 Compare May 6, 2026 00:42
Comment thread test/asynchronous/test_network_layer.py Outdated


class TestSendall(AsyncUnitTest):
def test_delegates_to_sock_sendall(self):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What's the purpose of this test? Testing that our wrapper calls what it wraps doesn't seem to test useful behavior.

Comment thread test/asynchronous/test_network_layer.py Outdated


class TestNetworkingInterfaceBase(AsyncUnitTest):
def setUp(self):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
def setUp(self):
def asyncSetUp(self):

mock_socket.sendall.assert_called_once_with(b"hello")


class TestNetworkingInterfaceBase(AsyncUnitTest):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This class doesn't need to be synchro'd since it's testing code common to both APIs.

Comment thread test/asynchronous/test_network_layer.py Outdated
_ = self.base.sock


class TestNetworkingInterface(AsyncUnitTest):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Are we gaining useful coverage by testing that all of NetworkingInterface's methods delegate to the underlying socket instead of testing the actual result of these calls?

Comment thread test/asynchronous/test_network_layer.py Outdated
self.assertIs(self.network_interface.sock, self.mock_socket)


if not _IS_SYNC:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

All of this code will be duplicated but unused in the synchronized version of this file. A better pattern is to put all of the async-specific tests that don't produce a meaningful synchronous version into a test/asynchronous/test_async_network_layer.py file that is not synchro'd.

This file would then become tests common to both APIs (like the NetworkingInterfaceBase tests) as well as the synchronous tests. Then there would be no code duplication and more isolated test modules.

Comment thread test/asynchronous/test_network_layer.py Outdated
self.assertIsNone(protocol.gettimeout)

async def test_normal_op_msg(self):
header = _make_header(32, 1, 99, 2013)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Using keyword arguments for these _make_header calls would be more readable (less magic numbers).

Comment thread test/asynchronous/test_network_layer.py Outdated
self.assertEqual(op_code, 1)
self.assertFalse(expecting_compression)

async def test_compression_header_returns_op_code_and_compressor_id(self):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should this be named something like test_compression_header_snappy_compressor_id for consistency with the following test?

Comment thread test/asynchronous/test_network_layer.py Outdated
Comment on lines +280 to +282
with self.assertRaises(OSError) as ctx:
await future
self.assertIn("connection reset", str(ctx.exception))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
with self.assertRaises(OSError) as ctx:
await future
self.assertIn("connection reset", str(ctx.exception))
with self.assertRaisesRegex(OSError, "connection reset"):
await future

Comment thread test/asynchronous/test_network_layer.py Outdated
self.assertIn("connection reset", str(ctx.exception))

class TestAsyncSocketReceive(AsyncUnitTest):
async def test_reads_full_data_in_one_call(self):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This seems to be primarily testing the behavior of loop.sock_recv_into(), which is a Python built-in method. Are we gaining useful coverage with this test?

Comment thread test/asynchronous/test_network_layer.py Outdated
result = await _async_socket_receive(mock_socket, length, loop)
self.assertEqual(bytes(result), data)

async def test_reads_data_in_multiple_chunks(self):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Same question here.

@aclark4life aclark4life marked this pull request as draft May 6, 2026 18:18
Add test/test_network_layer.py with 56 unit tests covering:
- PyMongoProtocol: process_header (including ProtocolError paths),
  process_compression_header, get_buffer (all state branches),
  buffer_updated (state machine), close/connection_lost lifecycle
- NetworkingInterfaceBase: abstract method NotImplementedError raises
- NetworkingInterface: socket delegation methods
- AsyncNetworkingInterface: transport/protocol delegation
- sendall: trivial delegation
- _async_socket_receive: success and connection-closed paths
- Fix mypy type error in test_network_layer.py
- Use 'from test import unittest', remove self-evident docstrings from helpers
- Fix inaccurate comment: second field in compression header is uncompressed_size
- Add test/asynchronous/test_network_layer.py as the async source of truth
  with AsyncUnitTest and _IS_SYNC = False
- Regenerate test/test_network_layer.py via synchro from the async source
- Register test_network_layer.py in synchro converted_tests (alpha order)
- Add "AsyncMock": "MagicMock" replacement after AsyncMockPool to avoid
  substring collision in translate_docstrings
Applying feedback from PYTHON-5784 to all open codecov PRs
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

Comment thread test/test_network_layer.py Outdated
@aclark4life aclark4life requested a review from Copilot May 7, 2026 02:01
@aclark4life aclark4life changed the title PYTHON-5781 Increase code coverage for network_layer.py PYTHON-5781 Improve code coverage for network_layer.py May 7, 2026
@aclark4life aclark4life changed the title PYTHON-5781 Improve code coverage for network_layer.py PYTHON-5781 Improve coverage for network_layer.py May 7, 2026
@aclark4life aclark4life changed the title PYTHON-5781 Improve coverage for network_layer.py PYTHON-5781 Improve coverage for network_layer.py May 7, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

Comment thread test/test_network_layer.py
@aclark4life aclark4life changed the title PYTHON-5781 Improve coverage for network_layer.py PYTHON-5781 Coverage improvements for network_layer.py May 7, 2026
@aclark4life aclark4life changed the title PYTHON-5781 Coverage improvements for network_layer.py PYTHON-5781 Coverage improvement for network_layer.py May 7, 2026
@aclark4life aclark4life requested a review from Copilot May 7, 2026 02:27
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

Comment thread test/test_network_layer.py Outdated
Comment thread test/asynchronous/test_network_layer.py Outdated
@aclark4life aclark4life changed the title PYTHON-5781 Coverage improvement for network_layer.py PYTHON-5781 Coverage increase for network_layer.py May 7, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

Comment thread test/test_network_layer.py
Comment thread test/asynchronous/test_network_layer.py
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated no new comments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants