|
57 | 57 | from pymongo.driver_info import DriverInfo
|
58 | 58 | from pymongo.pool import SocketInfo, _METADATA
|
59 | 59 | from pymongo.read_preferences import ReadPreference
|
| 60 | +from pymongo.server_description import ServerDescription |
60 | 61 | from pymongo.server_selectors import (any_server_selector,
|
61 | 62 | writable_server_selector)
|
62 | 63 | from pymongo.server_type import SERVER_TYPE
|
@@ -1614,6 +1615,33 @@ def test_direct_connection(self):
|
1614 | 1615 | with self.assertRaises(ConfigurationError):
|
1615 | 1616 | MongoClient(['host1', 'host2'], directConnection=True)
|
1616 | 1617 |
|
| 1618 | + def test_continuous_network_errors(self): |
| 1619 | + def server_description_count(): |
| 1620 | + i = 0 |
| 1621 | + for obj in gc.get_objects(): |
| 1622 | + try: |
| 1623 | + if isinstance(obj, ServerDescription): |
| 1624 | + i += 1 |
| 1625 | + except ReferenceError: |
| 1626 | + pass |
| 1627 | + return i |
| 1628 | + gc.collect() |
| 1629 | + with client_knobs(min_heartbeat_interval=0.003): |
| 1630 | + client = MongoClient( |
| 1631 | + 'invalid:27017', |
| 1632 | + heartbeatFrequencyMS=3, |
| 1633 | + serverSelectionTimeoutMS=100) |
| 1634 | + initial_count = server_description_count() |
| 1635 | + self.addCleanup(client.close) |
| 1636 | + with self.assertRaises(ServerSelectionTimeoutError): |
| 1637 | + client.test.test.find_one() |
| 1638 | + gc.collect() |
| 1639 | + final_count = server_description_count() |
| 1640 | + # If a bug like PYTHON-2433 is reintroduced then too many |
| 1641 | + # ServerDescriptions will be kept alive and this test will fail: |
| 1642 | + # AssertionError: 4 != 22 within 5 delta (18 difference) |
| 1643 | + self.assertAlmostEqual(initial_count, final_count, delta=5) |
| 1644 | + |
1617 | 1645 |
|
1618 | 1646 | class TestExhaustCursor(IntegrationTest):
|
1619 | 1647 | """Test that clients properly handle errors from exhaust cursors."""
|
|
0 commit comments