|
16 | 16 | from __future__ import annotations
|
17 | 17 |
|
18 | 18 | import unittest
|
| 19 | +from test import PyMongoTestCase |
19 | 20 |
|
20 | 21 | from mockupdb import MockupDB, OpMsg, going
|
21 | 22 |
|
22 | 23 | from bson.objectid import ObjectId
|
23 | 24 | from pymongo import MongoClient
|
| 25 | +from pymongo.errors import OperationFailure |
24 | 26 |
|
25 | 27 |
|
26 | 28 | class TestCursor(unittest.TestCase):
|
@@ -57,5 +59,31 @@ def test_getmore_load_balanced(self):
|
57 | 59 | request.replies({"cursor": {"id": cursor_id, "nextBatch": [{}]}})
|
58 | 60 |
|
59 | 61 |
|
| 62 | +class TestRetryableErrorCodeCatch(PyMongoTestCase): |
| 63 | + def _test_fail_on_operation_failure_with_code(self, code): |
| 64 | + """Test reads on error codes that should not be retried""" |
| 65 | + server = MockupDB() |
| 66 | + server.run() |
| 67 | + self.addCleanup(server.stop) |
| 68 | + server.autoresponds("ismaster", maxWireVersion=6) |
| 69 | + |
| 70 | + client = MongoClient(server.uri) |
| 71 | + |
| 72 | + with going(lambda: server.receives(OpMsg({"find": "collection"})).command_err(code=code)): |
| 73 | + cursor = client.db.collection.find() |
| 74 | + with self.assertRaises(OperationFailure) as ctx: |
| 75 | + cursor.next() |
| 76 | + self.assertEqual(ctx.exception.code, code) |
| 77 | + |
| 78 | + def test_fail_on_operation_failure_none(self): |
| 79 | + self._test_fail_on_operation_failure_with_code(None) |
| 80 | + |
| 81 | + def test_fail_on_operation_failure_zero(self): |
| 82 | + self._test_fail_on_operation_failure_with_code(0) |
| 83 | + |
| 84 | + def test_fail_on_operation_failure_one(self): |
| 85 | + self._test_fail_on_operation_failure_with_code(1) |
| 86 | + |
| 87 | + |
60 | 88 | if __name__ == "__main__":
|
61 | 89 | unittest.main()
|
0 commit comments