Skip to content

Commit 09be0b5

Browse files
committed
PYTHON-2744 Run LB tests against non-LB clusters (#638)
Fix serviceId fallback to make spec test pass. Fix socket leak when SocketInfo connection handshake fails. (cherry picked from commit bf78a9b)
1 parent 61d11fe commit 09be0b5

13 files changed

+78
-42
lines changed

pymongo/pool.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,9 @@ def _set_keepalive_times(sock):
264264
# main thread, to avoid the deadlock. See PYTHON-607.
265265
u'foo'.encode('idna')
266266

267+
# Remove after PYTHON-2712
268+
_MOCK_SERVICE_ID = False
269+
267270

268271
def _raise_connection_failure(address, error, msg_prefix=None):
269272
"""Convert a socket.error to ConnectionFailure and raise it."""
@@ -618,7 +621,7 @@ def _ismaster(self, cluster_time, topology_version,
618621
doc = self.command('admin', cmd, publish_events=False,
619622
exhaust_allowed=awaitable)
620623
# PYTHON-2712 will remove this topologyVersion fallback logic.
621-
if self.opts.load_balanced:
624+
if self.opts.load_balanced and _MOCK_SERVICE_ID:
622625
process_id = doc.get('topologyVersion', {}).get('processId')
623626
doc.setdefault('serviceId', process_id)
624627
ismaster = IsMaster(doc, awaitable=awaitable)
@@ -645,7 +648,7 @@ def _ismaster(self, cluster_time, topology_version,
645648
if self.opts.load_balanced:
646649
if not ismaster.service_id:
647650
raise ConfigurationError(
648-
'Driver attempted to initialize in load balancing mode'
651+
'Driver attempted to initialize in load balancing mode,'
649652
' but the server does not support this mode')
650653
self.service_id = ismaster.service_id
651654
self.generation = self.pool_gen.get(self.service_id)
@@ -1311,11 +1314,11 @@ def connect(self, all_credentials=None):
13111314
raise
13121315

13131316
sock_info = SocketInfo(sock, self, self.address, conn_id)
1314-
if self.handshake:
1315-
sock_info.ismaster(all_credentials)
1316-
self.is_writable = sock_info.is_writable
1317-
13181317
try:
1318+
if self.handshake:
1319+
sock_info.ismaster(all_credentials)
1320+
self.is_writable = sock_info.is_writable
1321+
13191322
sock_info.check_auth(all_credentials)
13201323
except BaseException:
13211324
sock_info.close_socket(ConnectionClosedReason.ERROR)

test/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@
9797
SINGLE_MONGOS_LB_URI = os.environ.get("SINGLE_MONGOS_LB_URI")
9898
MULTI_MONGOS_LB_URI = os.environ.get("MULTI_MONGOS_LB_URI")
9999
if TEST_LOADBALANCER:
100+
# Remove after PYTHON-2712
101+
from pymongo import pool
102+
pool._MOCK_SERVICE_ID = True
100103
res = parse_uri(SINGLE_MONGOS_LB_URI)
101104
host, port = res['nodelist'][0]
102105
db_user = res['username'] or db_user

test/load_balancer/test_crud_unified.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
# limitations under the License.
1414

1515
import sys
16-
import unittest
1716

1817
sys.path[0:0] = [""]
1918

19+
from test import unittest
2020
from test.test_crud_unified import *
2121

2222
if __name__ == '__main__':

test/load_balancer/test_dns.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
# limitations under the License.
1414

1515
import sys
16-
import unittest
1716

1817
sys.path[0:0] = [""]
1918

19+
from test import unittest
2020
from test.test_dns import *
2121

2222
if __name__ == '__main__':

test/load_balancer/test_load_balancer.py

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,38 +14,12 @@
1414

1515
"""Test the Load Balancer unified spec tests."""
1616

17-
import os
1817
import sys
1918

2019
sys.path[0:0] = [""]
2120

22-
from test import unittest, IntegrationTest, client_context
23-
from test.utils import get_pool
24-
from test.unified_format import generate_test_classes
25-
26-
# Location of JSON test specifications.
27-
TEST_PATH = os.path.join(
28-
os.path.dirname(os.path.realpath(__file__)), 'unified')
29-
30-
# Generate unified tests.
31-
globals().update(generate_test_classes(TEST_PATH, module=__name__))
32-
33-
34-
class TestLB(IntegrationTest):
35-
@client_context.require_load_balancer
36-
def test_unpin_committed_transaction(self):
37-
pool = get_pool(self.client)
38-
with self.client.start_session() as session:
39-
with session.start_transaction():
40-
self.assertEqual(pool.active_sockets, 0)
41-
self.db.test.insert_one({}, session=session)
42-
self.assertEqual(pool.active_sockets, 1) # Pinned.
43-
self.assertEqual(pool.active_sockets, 1) # Still pinned.
44-
self.assertEqual(pool.active_sockets, 0) # Unpinned.
45-
46-
def test_client_can_be_reopened(self):
47-
self.client.close()
48-
self.db.test.find_one({})
21+
from test import unittest
22+
from test.test_load_balancer import *
4923

5024

5125
if __name__ == "__main__":

test/load_balancer/test_retryable_change_stream.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
# limitations under the License.
1414

1515
import sys
16-
import unittest
1716

1817
sys.path[0:0] = [""]
1918

19+
from test import unittest
2020
from test.test_change_stream import *
2121

2222
if __name__ == '__main__':

test/load_balancer/test_retryable_reads.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
# limitations under the License.
1414

1515
import sys
16-
import unittest
1716

1817
sys.path[0:0] = [""]
1918

19+
from test import unittest
2020
from test.test_retryable_reads import *
2121

2222
if __name__ == '__main__':

test/load_balancer/test_retryable_writes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
# limitations under the License.
1414

1515
import sys
16-
import unittest
1716

1817
sys.path[0:0] = [""]
1918

19+
from test import unittest
2020
from test.test_retryable_writes import *
2121

2222
if __name__ == '__main__':

test/load_balancer/test_transactions_unified.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
# limitations under the License.
1414

1515
import sys
16-
import unittest
1716

1817
sys.path[0:0] = [""]
1918

19+
from test import unittest
2020
from test.test_transactions_unified import *
2121

2222
if __name__ == '__main__':

test/load_balancer/test_uri_options.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
# limitations under the License.
1414

1515
import sys
16-
import unittest
1716

1817
sys.path[0:0] = [""]
1918

19+
from test import unittest
2020
from test.test_uri_spec import *
2121

2222
if __name__ == '__main__':

0 commit comments

Comments
 (0)