|
17 | 17 |
|
18 | 18 | import mock |
19 | 19 | from google.api_core import gapic_v1 |
20 | | - |
| 20 | +from google.cloud.exceptions import NotFound |
21 | 21 | from google.cloud.spanner_v1.param_types import INT64 |
22 | 22 | from google.api_core.retry import Retry |
23 | 23 |
|
@@ -1792,6 +1792,49 @@ class Testing(Exception): |
1792 | 1792 |
|
1793 | 1793 | self.assertIs(pool._session, session) |
1794 | 1794 |
|
| 1795 | + def test_context_mgr_session_not_found_error(self): |
| 1796 | + from google.cloud.spanner_v1.database import SnapshotCheckout |
| 1797 | + from google.cloud.exceptions import NotFound |
| 1798 | + |
| 1799 | + database = _Database(self.DATABASE_NAME) |
| 1800 | + session = _Session(database, name="session-1") |
| 1801 | + session.exists = mock.MagicMock(return_value=False) |
| 1802 | + pool = database._pool = _Pool() |
| 1803 | + new_session = _Session(database, name="session-2") |
| 1804 | + new_session.create = mock.MagicMock(return_value=[]) |
| 1805 | + pool._new_session = mock.MagicMock(return_value=new_session) |
| 1806 | + |
| 1807 | + pool.put(session) |
| 1808 | + checkout = self._make_one(database) |
| 1809 | + |
| 1810 | + self.assertEqual(pool._session, session) |
| 1811 | + with self.assertRaises(NotFound): |
| 1812 | + with checkout as snapshot: |
| 1813 | + raise NotFound(f"Session not found") |
| 1814 | + # Assert that session-1 was removed from pool and new session was added. |
| 1815 | + self.assertEqual(pool._session, new_session) |
| 1816 | + |
| 1817 | + def test_context_mgr_unknown_error(self): |
| 1818 | + from google.cloud.spanner_v1.database import SnapshotCheckout |
| 1819 | + |
| 1820 | + database = _Database(self.DATABASE_NAME) |
| 1821 | + session = _Session(database) |
| 1822 | + pool = database._pool = _Pool() |
| 1823 | + pool._new_session = mock.MagicMock(return_value=[]) |
| 1824 | + pool.put(session) |
| 1825 | + checkout = self._make_one(database) |
| 1826 | + |
| 1827 | + class Testing(Exception): |
| 1828 | + pass |
| 1829 | + |
| 1830 | + self.assertEqual(pool._session, session) |
| 1831 | + with self.assertRaises(Testing): |
| 1832 | + with checkout as snapshot: |
| 1833 | + raise Testing(f"Session not found") |
| 1834 | + # Assert that session-1 was not removed from pool. |
| 1835 | + self.assertEqual(pool._session, session) |
| 1836 | + pool._new_session.assert_not_called() |
| 1837 | + |
1795 | 1838 |
|
1796 | 1839 | class TestBatchSnapshot(_BaseTest): |
1797 | 1840 | TABLE = "table_name" |
|
0 commit comments