|
14 | 14 |
|
15 | 15 |
|
16 | 16 | import unittest
|
| 17 | +import mock |
17 | 18 |
|
18 | 19 |
|
19 | 20 | class Test_merge_query_options(unittest.TestCase):
|
@@ -671,6 +672,83 @@ def test(self):
|
671 | 672 | self.assertEqual(metadata, [("google-cloud-resource-prefix", prefix)])
|
672 | 673 |
|
673 | 674 |
|
| 675 | +class Test_retry(unittest.TestCase): |
| 676 | + class test_class: |
| 677 | + def test_fxn(self): |
| 678 | + return True |
| 679 | + |
| 680 | + def test_retry_on_error(self): |
| 681 | + from google.api_core.exceptions import InternalServerError, NotFound |
| 682 | + from google.cloud.spanner_v1._helpers import _retry |
| 683 | + import functools |
| 684 | + |
| 685 | + test_api = mock.create_autospec(self.test_class) |
| 686 | + test_api.test_fxn.side_effect = [ |
| 687 | + InternalServerError("testing"), |
| 688 | + NotFound("testing"), |
| 689 | + True, |
| 690 | + ] |
| 691 | + |
| 692 | + _retry(functools.partial(test_api.test_fxn)) |
| 693 | + |
| 694 | + self.assertEqual(test_api.test_fxn.call_count, 3) |
| 695 | + |
| 696 | + def test_retry_allowed_exceptions(self): |
| 697 | + from google.api_core.exceptions import InternalServerError, NotFound |
| 698 | + from google.cloud.spanner_v1._helpers import _retry |
| 699 | + import functools |
| 700 | + |
| 701 | + test_api = mock.create_autospec(self.test_class) |
| 702 | + test_api.test_fxn.side_effect = [ |
| 703 | + NotFound("testing"), |
| 704 | + InternalServerError("testing"), |
| 705 | + True, |
| 706 | + ] |
| 707 | + |
| 708 | + with self.assertRaises(InternalServerError): |
| 709 | + _retry( |
| 710 | + functools.partial(test_api.test_fxn), |
| 711 | + allowed_exceptions={NotFound: None}, |
| 712 | + ) |
| 713 | + |
| 714 | + self.assertEqual(test_api.test_fxn.call_count, 2) |
| 715 | + |
| 716 | + def test_retry_count(self): |
| 717 | + from google.api_core.exceptions import InternalServerError |
| 718 | + from google.cloud.spanner_v1._helpers import _retry |
| 719 | + import functools |
| 720 | + |
| 721 | + test_api = mock.create_autospec(self.test_class) |
| 722 | + test_api.test_fxn.side_effect = [ |
| 723 | + InternalServerError("testing"), |
| 724 | + InternalServerError("testing"), |
| 725 | + ] |
| 726 | + |
| 727 | + with self.assertRaises(InternalServerError): |
| 728 | + _retry(functools.partial(test_api.test_fxn), retry_count=1) |
| 729 | + |
| 730 | + self.assertEqual(test_api.test_fxn.call_count, 2) |
| 731 | + |
| 732 | + def test_check_rst_stream_error(self): |
| 733 | + from google.api_core.exceptions import InternalServerError |
| 734 | + from google.cloud.spanner_v1._helpers import _retry, _check_rst_stream_error |
| 735 | + import functools |
| 736 | + |
| 737 | + test_api = mock.create_autospec(self.test_class) |
| 738 | + test_api.test_fxn.side_effect = [ |
| 739 | + InternalServerError("Received unexpected EOS on DATA frame from server"), |
| 740 | + InternalServerError("RST_STREAM"), |
| 741 | + True, |
| 742 | + ] |
| 743 | + |
| 744 | + _retry( |
| 745 | + functools.partial(test_api.test_fxn), |
| 746 | + allowed_exceptions={InternalServerError: _check_rst_stream_error}, |
| 747 | + ) |
| 748 | + |
| 749 | + self.assertEqual(test_api.test_fxn.call_count, 3) |
| 750 | + |
| 751 | + |
674 | 752 | class Test_metadata_with_leader_aware_routing(unittest.TestCase):
|
675 | 753 | def _call_fut(self, *args, **kw):
|
676 | 754 | from google.cloud.spanner_v1._helpers import _metadata_with_leader_aware_routing
|
|
0 commit comments