Skip to content

Commit e7aedbf

Browse files
committed
Test cleanup
1 parent 8e56a93 commit e7aedbf

28 files changed

+166
-166
lines changed

pymongo/asynchronous/pool.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ async def command(
445445
raise
446446
# Catch socket.error, KeyboardInterrupt, CancelledError, etc. and close ourselves.
447447
except BaseException as error:
448-
await _raise_connection_failure(error)
448+
await self._raise_connection_failure(error)
449449

450450
async def send_message(self, message: bytes, max_doc_size: int) -> None:
451451
"""Send a raw BSON message or raise ConnectionFailure.
@@ -462,7 +462,7 @@ async def send_message(self, message: bytes, max_doc_size: int) -> None:
462462
await async_sendall(self.conn.get_conn, message)
463463
# Catch KeyboardInterrupt, CancelledError, etc. and cleanup.
464464
except BaseException as error:
465-
await _raise_connection_failure(error)
465+
await self._raise_connection_failure(error)
466466

467467
async def receive_message(self, request_id: Optional[int]) -> Union[_OpReply, _OpMsg]:
468468
"""Receive a raw BSON message or raise ConnectionFailure.
@@ -473,7 +473,7 @@ async def receive_message(self, request_id: Optional[int]) -> Union[_OpReply, _O
473473
return await async_receive_message(self, request_id, self.max_message_size)
474474
# Catch KeyboardInterrupt, CancelledError, etc. and cleanup.
475475
except BaseException as error:
476-
await _raise_connection_failure(error)
476+
await self._raise_connection_failure(error)
477477

478478
def _raise_if_not_writable(self, unacknowledged: bool) -> None:
479479
"""Raise NotPrimaryError on unacknowledged write if this socket is not

pymongo/synchronous/pool.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ def command(
445445
raise
446446
# Catch socket.error, KeyboardInterrupt, CancelledError, etc. and close ourselves.
447447
except BaseException as error:
448-
_raise_connection_failure(error)
448+
self._raise_connection_failure(error)
449449

450450
def send_message(self, message: bytes, max_doc_size: int) -> None:
451451
"""Send a raw BSON message or raise ConnectionFailure.
@@ -462,7 +462,7 @@ def send_message(self, message: bytes, max_doc_size: int) -> None:
462462
sendall(self.conn.get_conn, message)
463463
# Catch KeyboardInterrupt, CancelledError, etc. and cleanup.
464464
except BaseException as error:
465-
_raise_connection_failure(error)
465+
self._raise_connection_failure(error)
466466

467467
def receive_message(self, request_id: Optional[int]) -> Union[_OpReply, _OpMsg]:
468468
"""Receive a raw BSON message or raise ConnectionFailure.
@@ -473,7 +473,7 @@ def receive_message(self, request_id: Optional[int]) -> Union[_OpReply, _OpMsg]:
473473
return receive_message(self, request_id, self.max_message_size)
474474
# Catch KeyboardInterrupt, CancelledError, etc. and cleanup.
475475
except BaseException as error:
476-
_raise_connection_failure(error)
476+
self._raise_connection_failure(error)
477477

478478
def _raise_if_not_writable(self, unacknowledged: bool) -> None:
479479
"""Raise NotPrimaryError on unacknowledged write if this socket is not

test/asynchronous/test_bulk.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ async def test_single_ordered_batch(self):
505505

506506
async def test_single_error_ordered_batch(self):
507507
await self.coll.create_index("a", unique=True)
508-
self.addToCleanup(self.coll.drop_index, [("a", 1)])
508+
self.addAsyncCleanup(self.coll.drop_index, [("a", 1)])
509509
requests: list = [
510510
InsertOne({"b": 1, "a": 1}),
511511
UpdateOne({"b": 2}, {"$set": {"a": 1}}, upsert=True),
@@ -547,7 +547,7 @@ async def test_single_error_ordered_batch(self):
547547

548548
async def test_multiple_error_ordered_batch(self):
549549
await self.coll.create_index("a", unique=True)
550-
self.addToCleanup(self.coll.drop_index, [("a", 1)])
550+
self.addAsyncCleanup(self.coll.drop_index, [("a", 1)])
551551
requests: list = [
552552
InsertOne({"b": 1, "a": 1}),
553553
UpdateOne({"b": 2}, {"$set": {"a": 1}}, upsert=True),
@@ -616,7 +616,7 @@ async def test_single_unordered_batch(self):
616616

617617
async def test_single_error_unordered_batch(self):
618618
await self.coll.create_index("a", unique=True)
619-
self.addToCleanup(self.coll.drop_index, [("a", 1)])
619+
self.addAsyncCleanup(self.coll.drop_index, [("a", 1)])
620620
requests: list = [
621621
InsertOne({"b": 1, "a": 1}),
622622
UpdateOne({"b": 2}, {"$set": {"a": 1}}, upsert=True),
@@ -659,7 +659,7 @@ async def test_single_error_unordered_batch(self):
659659

660660
async def test_multiple_error_unordered_batch(self):
661661
await self.coll.create_index("a", unique=True)
662-
self.addToCleanup(self.coll.drop_index, [("a", 1)])
662+
self.addAsyncCleanup(self.coll.drop_index, [("a", 1)])
663663
requests: list = [
664664
InsertOne({"b": 1, "a": 1}),
665665
UpdateOne({"b": 2}, {"$set": {"a": 3}}, upsert=True),
@@ -1002,7 +1002,7 @@ async def test_write_concern_failure_ordered(self):
10021002

10031003
await self.coll.delete_many({})
10041004
await self.coll.create_index("a", unique=True)
1005-
self.addToCleanup(self.coll.drop_index, [("a", 1)])
1005+
self.addAsyncCleanup(self.coll.drop_index, [("a", 1)])
10061006

10071007
# Fail due to write concern support as well
10081008
# as duplicate key error on ordered batch.
@@ -1077,7 +1077,7 @@ async def test_write_concern_failure_unordered(self):
10771077

10781078
await self.coll.delete_many({})
10791079
await self.coll.create_index("a", unique=True)
1080-
self.addToCleanup(self.coll.drop_index, [("a", 1)])
1080+
self.addAsyncCleanup(self.coll.drop_index, [("a", 1)])
10811081

10821082
# Fail due to write concern support as well
10831083
# as duplicate key error on unordered batch.

test/asynchronous/test_change_stream.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ async def test_try_next(self):
165165
coll = self.watched_collection().with_options(write_concern=WriteConcern("majority"))
166166
await coll.drop()
167167
await coll.insert_one({})
168-
self.addToCleanup(coll.drop)
168+
self.addAsyncCleanup(coll.drop)
169169
async with await self.change_stream(max_await_time_ms=250) as stream:
170170
self.assertIsNone(await stream.try_next()) # No changes initially.
171171
await coll.insert_one({}) # Generate a change.
@@ -191,7 +191,7 @@ async def test_try_next_runs_one_getmore(self):
191191
# Create the watched collection before starting the change stream to
192192
# skip any "create" events.
193193
await coll.insert_one({"_id": 1})
194-
self.addToCleanup(coll.drop)
194+
self.addAsyncCleanup(coll.drop)
195195
async with await self.change_stream_with_client(client, max_await_time_ms=250) as stream:
196196
self.assertEqual(listener.started_command_names(), ["aggregate"])
197197
listener.reset()
@@ -249,7 +249,7 @@ async def test_batch_size_is_honored(self):
249249
# Create the watched collection before starting the change stream to
250250
# skip any "create" events.
251251
await coll.insert_one({"_id": 1})
252-
self.addToCleanup(coll.drop)
252+
self.addAsyncCleanup(coll.drop)
253253
# Expected batchSize.
254254
expected = {"batchSize": 23}
255255
async with await self.change_stream_with_client(
@@ -489,7 +489,7 @@ async def _client_with_listener(self, *commands):
489489
client = await AsyncPyMongoTestCase.unmanaged_async_rs_or_single_client(
490490
event_listeners=[listener]
491491
)
492-
self.addToCleanup(client.close)
492+
self.addAsyncCleanup(client.close)
493493
return client, listener
494494

495495
@no_type_check
@@ -1156,7 +1156,7 @@ async def setFailPoint(self, scenario_dict):
11561156
fail_cmd = SON([("configureFailPoint", "failCommand")])
11571157
fail_cmd.update(fail_point)
11581158
await async_client_context.client.admin.command(fail_cmd)
1159-
self.addToCleanup(
1159+
self.addAsyncCleanup(
11601160
async_client_context.client.admin.command,
11611161
"configureFailPoint",
11621162
fail_cmd["configureFailPoint"],

test/asynchronous/test_client.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,8 +1102,8 @@ def test_bad_uri(self):
11021102
async def test_auth_from_uri(self):
11031103
host, port = await async_client_context.host, await async_client_context.port
11041104
await async_client_context.create_user("admin", "admin", "pass")
1105-
self.addToCleanup(async_client_context.drop_user, "admin", "admin")
1106-
self.addToCleanup(remove_all_users, self.client.pymongo_test)
1105+
self.addAsyncCleanup(async_client_context.drop_user, "admin", "admin")
1106+
self.addAsyncCleanup(remove_all_users, self.client.pymongo_test)
11071107

11081108
await async_client_context.create_user(
11091109
"pymongo_test", "user", "pass", roles=["userAdmin", "readWrite"]
@@ -1149,7 +1149,7 @@ async def test_auth_from_uri(self):
11491149
@async_client_context.require_auth
11501150
async def test_username_and_password(self):
11511151
await async_client_context.create_user("admin", "ad min", "pa/ss")
1152-
self.addToCleanup(async_client_context.drop_user, "admin", "ad min")
1152+
self.addAsyncCleanup(async_client_context.drop_user, "admin", "ad min")
11531153

11541154
c = await self.async_rs_or_single_client_noauth(username="ad min", password="pa/ss")
11551155

@@ -1258,7 +1258,6 @@ async def test_socket_timeout(self):
12581258
no_timeout = self.client
12591259
timeout_sec = 1
12601260
timeout = await self.async_rs_or_single_client(socketTimeoutMS=1000 * timeout_sec)
1261-
self.addToCleanup(timeout.close)
12621261

12631262
await no_timeout.pymongo_test.drop_collection("test")
12641263
await no_timeout.pymongo_test.test.insert_one({"x": 1})
@@ -1325,7 +1324,7 @@ async def test_tz_aware(self):
13251324
self.assertRaises(ValueError, AsyncMongoClient, tz_aware="foo")
13261325

13271326
aware = await self.async_rs_or_single_client(tz_aware=True)
1328-
self.addToCleanup(aware.close)
1327+
self.addAsyncCleanup(aware.close)
13291328
naive = self.client
13301329
await aware.pymongo_test.drop_collection("test")
13311330

@@ -1477,7 +1476,7 @@ async def test_lazy_connect_w0(self):
14771476
# Use a separate collection to avoid races where we're still
14781477
# completing an operation on a collection while the next test begins.
14791478
await async_client_context.client.drop_database("test_lazy_connect_w0")
1480-
self.addToCleanup(async_client_context.client.drop_database, "test_lazy_connect_w0")
1479+
self.addAsyncCleanup(async_client_context.client.drop_database, "test_lazy_connect_w0")
14811480

14821481
client = await self.async_rs_or_single_client(connect=False, w=0)
14831482
await client.test_lazy_connect_w0.test.insert_one({})
@@ -2160,7 +2159,7 @@ async def test_exhaust_getmore_server_error(self):
21602159
await collection.drop()
21612160

21622161
await collection.insert_many([{} for _ in range(200)])
2163-
self.addToCleanup(async_client_context.client.pymongo_test.test.drop)
2162+
self.addAsyncCleanup(async_client_context.client.pymongo_test.test.drop)
21642163

21652164
pool = await async_get_pool(client)
21662165
pool._check_interval_seconds = None # Never check.
@@ -2407,7 +2406,7 @@ async def test_discover_primary(self):
24072406
replicaSet="rs",
24082407
heartbeatFrequencyMS=500,
24092408
)
2410-
self.addToCleanup(c.close)
2409+
self.addAsyncCleanup(c.close)
24112410

24122411
await async_wait_until(lambda: len(c.nodes) == 3, "connect")
24132412

@@ -2434,7 +2433,7 @@ async def test_reconnect(self):
24342433
retryReads=False,
24352434
serverSelectionTimeoutMS=1000,
24362435
)
2437-
self.addToCleanup(c.close)
2436+
self.addAsyncCleanup(c.close)
24382437

24392438
await async_wait_until(lambda: len(c.nodes) == 3, "connect")
24402439

@@ -2472,7 +2471,7 @@ async def _test_network_error(self, operation_callback):
24722471
serverSelectionTimeoutMS=1000,
24732472
)
24742473

2475-
self.addToCleanup(c.close)
2474+
self.addAsyncCleanup(c.close)
24762475

24772476
# Set host-specific information so we can test whether it is reset.
24782477
c.set_wire_version_range("a:1", 2, MIN_SUPPORTED_WIRE_VERSION)
@@ -2548,7 +2547,7 @@ async def test_rs_client_does_not_maintain_pool_to_arbiters(self):
25482547
minPoolSize=1, # minPoolSize
25492548
event_listeners=[listener],
25502549
)
2551-
self.addToCleanup(c.close)
2550+
self.addAsyncCleanup(c.close)
25522551

25532552
await async_wait_until(lambda: len(c.nodes) == 3, "connect")
25542553
self.assertEqual(await c.address, ("a", 1))
@@ -2578,7 +2577,7 @@ async def test_direct_client_maintains_pool_to_arbiter(self):
25782577
minPoolSize=1, # minPoolSize
25792578
event_listeners=[listener],
25802579
)
2581-
self.addToCleanup(c.close)
2580+
self.addAsyncCleanup(c.close)
25822581

25832582
await async_wait_until(lambda: len(c.nodes) == 1, "connect")
25842583
self.assertEqual(await c.address, ("c", 3))

test/asynchronous/test_client_bulk_write.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ async def test_batch_splits_if_num_operations_too_large(self):
102102
models = []
103103
for _ in range(self.max_write_batch_size + 1):
104104
models.append(InsertOne(namespace="db.coll", document={"a": "b"}))
105-
self.addToCleanup(client.db["coll"].drop)
105+
self.addAsyncCleanup(client.db["coll"].drop)
106106

107107
result = await client.bulk_write(models=models)
108108
self.assertEqual(result.inserted_count, self.max_write_batch_size + 1)
@@ -134,7 +134,7 @@ async def test_batch_splits_if_ops_payload_too_large(self):
134134
document={"a": b_repeated},
135135
)
136136
)
137-
self.addToCleanup(client.db["coll"].drop)
137+
self.addAsyncCleanup(client.db["coll"].drop)
138138

139139
result = await client.bulk_write(models=models)
140140
self.assertEqual(result.inserted_count, num_models)
@@ -177,7 +177,7 @@ async def test_collects_write_concern_errors_across_batches(self):
177177
document={"a": "b"},
178178
)
179179
)
180-
self.addToCleanup(client.db["coll"].drop)
180+
self.addAsyncCleanup(client.db["coll"].drop)
181181

182182
with self.assertRaises(ClientBulkWriteException) as context:
183183
await client.bulk_write(models=models)
@@ -200,7 +200,7 @@ async def test_collects_write_errors_across_batches_unordered(self):
200200
client = await self.async_rs_or_single_client(event_listeners=[listener])
201201

202202
collection = client.db["coll"]
203-
self.addToCleanup(collection.drop)
203+
self.addAsyncCleanup(collection.drop)
204204
await collection.drop()
205205
await collection.insert_one(document={"_id": 1})
206206

@@ -230,7 +230,7 @@ async def test_collects_write_errors_across_batches_ordered(self):
230230
client = await self.async_rs_or_single_client(event_listeners=[listener])
231231

232232
collection = client.db["coll"]
233-
self.addToCleanup(collection.drop)
233+
self.addAsyncCleanup(collection.drop)
234234
await collection.drop()
235235
await collection.insert_one(document={"_id": 1})
236236

@@ -260,7 +260,7 @@ async def test_handles_cursor_requiring_getMore(self):
260260
client = await self.async_rs_or_single_client(event_listeners=[listener])
261261

262262
collection = client.db["coll"]
263-
self.addToCleanup(collection.drop)
263+
self.addAsyncCleanup(collection.drop)
264264
await collection.drop()
265265

266266
models = []
@@ -301,7 +301,7 @@ async def test_handles_cursor_requiring_getMore_within_transaction(self):
301301
client = await self.async_rs_or_single_client(event_listeners=[listener])
302302

303303
collection = client.db["coll"]
304-
self.addToCleanup(collection.drop)
304+
self.addAsyncCleanup(collection.drop)
305305
await collection.drop()
306306

307307
async with client.start_session() as session:
@@ -344,7 +344,7 @@ async def test_handles_getMore_error(self):
344344
client = await self.async_rs_or_single_client(event_listeners=[listener])
345345

346346
collection = client.db["coll"]
347-
self.addToCleanup(collection.drop)
347+
self.addAsyncCleanup(collection.drop)
348348
await collection.drop()
349349

350350
fail_command = {
@@ -464,7 +464,7 @@ async def test_no_batch_splits_if_new_namespace_is_not_too_large(self):
464464
document={"a": "b"},
465465
)
466466
)
467-
self.addToCleanup(client.db["coll"].drop)
467+
self.addAsyncCleanup(client.db["coll"].drop)
468468

469469
# No batch splitting required.
470470
result = await client.bulk_write(models=models)
@@ -497,8 +497,8 @@ async def test_batch_splits_if_new_namespace_is_too_large(self):
497497
document={"a": "b"},
498498
)
499499
)
500-
self.addToCleanup(client.db["coll"].drop)
501-
self.addToCleanup(client.db[c_repeated].drop)
500+
self.addAsyncCleanup(client.db["coll"].drop)
501+
self.addAsyncCleanup(client.db[c_repeated].drop)
502502

503503
# Batch splitting required.
504504
result = await client.bulk_write(models=models)
@@ -561,7 +561,7 @@ async def test_upserted_result(self):
561561
client = await self.async_rs_or_single_client()
562562

563563
collection = client.db["coll"]
564-
self.addToCleanup(collection.drop)
564+
self.addAsyncCleanup(collection.drop)
565565
await collection.drop()
566566

567567
models = []
@@ -602,7 +602,7 @@ async def test_15_unacknowledged_write_across_batches(self):
602602
client = await self.async_rs_or_single_client(event_listeners=[listener])
603603

604604
collection = client.db["coll"]
605-
self.addToCleanup(collection.drop)
605+
self.addAsyncCleanup(collection.drop)
606606
await collection.drop()
607607
await client.db.command({"create": "db.coll"})
608608

@@ -651,10 +651,9 @@ async def test_timeout_in_multi_batch_bulk_write(self):
651651
_OVERHEAD = 500
652652

653653
internal_client = await self.async_rs_or_single_client(timeoutMS=None)
654-
self.addToCleanup(internal_client.close)
655654

656655
collection = internal_client.db["coll"]
657-
self.addToCleanup(collection.drop)
656+
self.addAsyncCleanup(collection.drop)
658657
await collection.drop()
659658

660659
fail_command = {

0 commit comments

Comments
 (0)