|
5 | 5 | import pytest
|
6 | 6 | from bson.tz_util import utc
|
7 | 7 | from pymongo import MongoClient, ReadPreference
|
8 |
| -from pymongo.errors import InvalidName, OperationFailure |
| 8 | +from pymongo.errors import ( |
| 9 | + InvalidName, |
| 10 | + InvalidOperation, |
| 11 | + OperationFailure, |
| 12 | +) |
9 | 13 |
|
10 | 14 | import mongoengine.connection
|
11 | 15 | from mongoengine import (
|
@@ -287,6 +291,30 @@ def test_disconnect_silently_pass_if_alias_does_not_exist(self):
|
287 | 291 | assert len(connections) == 0
|
288 | 292 | disconnect(alias="not_exist")
|
289 | 293 |
|
| 294 | + def test_disconnect_does_not_close_client_used_by_another_alias(self): |
| 295 | + client1 = connect(alias="disconnect_reused_client_test_1") |
| 296 | + client2 = connect(alias="disconnect_reused_client_test_2") |
| 297 | + client3 = connect(alias="disconnect_reused_client_test_3", maxPoolSize=10) |
| 298 | + assert client1 is client2 |
| 299 | + assert client1 is not client3 |
| 300 | + client1.admin.command("ping") |
| 301 | + disconnect("disconnect_reused_client_test_1") |
| 302 | + # The client is not closed because the second alias still exists. |
| 303 | + client2.admin.command("ping") |
| 304 | + disconnect("disconnect_reused_client_test_2") |
| 305 | + # The client is now closed: |
| 306 | + if PYMONGO_VERSION >= (4,): |
| 307 | + with pytest.raises(InvalidOperation): |
| 308 | + client2.admin.command("ping") |
| 309 | + # 3rd client connected to the same cluster with different options |
| 310 | + # is not closed either. |
| 311 | + client3.admin.command("ping") |
| 312 | + disconnect("disconnect_reused_client_test_3") |
| 313 | + # 3rd client is now closed: |
| 314 | + if PYMONGO_VERSION >= (4,): |
| 315 | + with pytest.raises(InvalidOperation): |
| 316 | + client3.admin.command("ping") |
| 317 | + |
290 | 318 | def test_disconnect_all(self):
|
291 | 319 | connections = mongoengine.connection._connections
|
292 | 320 | dbs = mongoengine.connection._dbs
|
|
0 commit comments