|
12 | 12 | # See the License for the specific language governing permissions and |
13 | 13 | # limitations under the License. |
14 | 14 |
|
| 15 | +import inspect |
| 16 | + |
15 | 17 | import aiomysql |
16 | 18 | from testing_support.db_settings import mysql_settings |
17 | 19 | from testing_support.util import instance_hostname |
@@ -150,3 +152,35 @@ async def _test(): |
150 | 152 | await pool.wait_closed() |
151 | 153 |
|
152 | 154 | loop.run_until_complete(_test()) |
| 155 | + |
| 156 | + |
| 157 | +@background_task() |
| 158 | +def test_connection_pool_no_double_wrap(loop): |
| 159 | + async def _test(): |
| 160 | + pool = await aiomysql.create_pool( |
| 161 | + db=DB_SETTINGS["name"], |
| 162 | + user=DB_SETTINGS["user"], |
| 163 | + password=DB_SETTINGS["password"], |
| 164 | + host=DB_SETTINGS["host"], |
| 165 | + port=DB_SETTINGS["port"], |
| 166 | + loop=loop, |
| 167 | + ) |
| 168 | + |
| 169 | + # Retrieve the same connection from the pool twice to see if it gets double wrapped |
| 170 | + async with pool.acquire() as first_connection: |
| 171 | + first_connection_unwrapped = inspect.unwrap(first_connection) |
| 172 | + async with pool.acquire() as second_connection: |
| 173 | + second_connection_unwrapped = inspect.unwrap(second_connection) |
| 174 | + |
| 175 | + # Ensure we actually retrieved the same underlying connection object from the pool twice |
| 176 | + assert first_connection_unwrapped is second_connection_unwrapped, "Did not get same connection from pool" |
| 177 | + |
| 178 | + # Check that wrapping occurred only once |
| 179 | + assert hasattr(first_connection, "__wrapped__"), "first_connection object was not wrapped" |
| 180 | + assert hasattr(second_connection, "__wrapped__"), "second_connection object was not wrapped" |
| 181 | + assert not hasattr(second_connection.__wrapped__, "__wrapped__"), "second_connection was double wrapped" |
| 182 | + |
| 183 | + pool.close() |
| 184 | + await pool.wait_closed() |
| 185 | + |
| 186 | + loop.run_until_complete(_test()) |
0 commit comments