Skip to content

Commit f61bdee

Browse files
chore: remove event_loop fixture (#891)
1 parent 2a95532 commit f61bdee

File tree

3 files changed

+17
-32
lines changed

3 files changed

+17
-32
lines changed

tests/conftest.py

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -57,19 +57,6 @@ def pytest_collection_modifyitems(config: Any, items: Any) -> None:
5757
item.add_marker(skip_private_ip)
5858

5959

60-
@pytest.fixture
61-
def event_loop() -> Generator:
62-
"""
63-
Creates an event loop to use for testing.
64-
"""
65-
loop: asyncio.AbstractEventLoop = asyncio.new_event_loop()
66-
yield loop
67-
# close loop gracefully
68-
if loop.is_running():
69-
loop.call_soon_threadsafe(loop.stop)
70-
loop.close()
71-
72-
7360
@pytest.fixture
7461
def connect_string() -> str:
7562
"""
@@ -149,13 +136,13 @@ def mock_instance() -> FakeCSQLInstance:
149136
async def instance(
150137
mock_instance: FakeCSQLInstance,
151138
fake_credentials: Credentials,
152-
event_loop: asyncio.AbstractEventLoop,
153139
) -> AsyncGenerator[Instance, None]:
154140
"""
155141
Instance with mocked API calls.
156142
"""
143+
loop = asyncio.get_running_loop()
157144
# generate client key pair
158-
keys = event_loop.create_task(generate_keys())
145+
keys = asyncio.create_task(generate_keys())
159146
_, client_key = await keys
160147

161148
with patch("google.cloud.sql.connector.utils.default") as mock_auth:
@@ -179,7 +166,7 @@ async def instance(
179166
f"{mock_instance.project}:{mock_instance.region}:{mock_instance.name}",
180167
"pg8000",
181168
keys,
182-
event_loop,
169+
loop,
183170
)
184171

185172
yield instance

tests/unit/test_instance.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,19 @@
3838

3939

4040
@pytest.fixture
41-
def test_rate_limiter(event_loop: asyncio.AbstractEventLoop) -> AsyncRateLimiter:
42-
return AsyncRateLimiter(max_capacity=1, rate=1 / 2, loop=event_loop)
41+
def test_rate_limiter() -> AsyncRateLimiter:
42+
return AsyncRateLimiter(max_capacity=1, rate=1 / 2)
4343

4444

4545
@pytest.mark.asyncio
4646
async def test_Instance_init(
47-
fake_credentials: Credentials, event_loop: asyncio.AbstractEventLoop
47+
fake_credentials: Credentials,
4848
) -> None:
4949
"""
5050
Test to check whether the __init__ method of Instance
5151
can tell if the connection string that's passed in is formatted correctly.
5252
"""
53-
53+
event_loop = asyncio.get_running_loop()
5454
connect_string = "test-project:test-region:test-instance"
5555
keys = asyncio.wrap_future(
5656
asyncio.run_coroutine_threadsafe(generate_keys(), event_loop), loop=event_loop
@@ -71,13 +71,12 @@ async def test_Instance_init(
7171

7272

7373
@pytest.mark.asyncio
74-
async def test_Instance_init_bad_credentials(
75-
event_loop: asyncio.AbstractEventLoop,
76-
) -> None:
74+
async def test_Instance_init_bad_credentials() -> None:
7775
"""
7876
Test to check whether the __init__ method of Instance
7977
throws proper error for bad credentials arg type.
8078
"""
79+
event_loop = asyncio.get_running_loop()
8180
connect_string = "test-project:test-region:test-instance"
8281
keys = asyncio.wrap_future(
8382
asyncio.run_coroutine_threadsafe(generate_keys(), event_loop), loop=event_loop
@@ -331,11 +330,12 @@ async def test_get_preferred_ip_CloudSQLIPTypeError(instance: Instance) -> None:
331330

332331
@pytest.mark.asyncio
333332
async def test_ClientResponseError(
334-
fake_credentials: Credentials, event_loop: asyncio.AbstractEventLoop
333+
fake_credentials: Credentials,
335334
) -> None:
336335
"""
337336
Test that detailed error message is applied to ClientResponseError.
338337
"""
338+
event_loop = asyncio.get_running_loop()
339339
# mock Cloud SQL Admin API calls with exceptions
340340
keys = asyncio.wrap_future(
341341
asyncio.run_coroutine_threadsafe(generate_keys(), event_loop), loop=event_loop

tests/unit/test_rate_limiter.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,12 @@
2323

2424

2525
@pytest.mark.asyncio
26-
async def test_rate_limiter_throttles_requests(
27-
event_loop: asyncio.AbstractEventLoop,
28-
) -> None:
26+
async def test_rate_limiter_throttles_requests() -> None:
2927
"""Test to check whether rate limiter will throttle incoming requests."""
28+
event_loop = asyncio.get_running_loop()
3029
counter = 0
3130
# allow 2 requests to go through every 5 seconds
32-
rate_limiter = AsyncRateLimiter(max_capacity=2, rate=1 / 5, loop=event_loop)
31+
rate_limiter = AsyncRateLimiter(max_capacity=2, rate=1 / 5)
3332

3433
async def increment() -> None:
3534
await rate_limiter.acquire()
@@ -53,13 +52,12 @@ async def increment() -> None:
5352

5453

5554
@pytest.mark.asyncio
56-
async def test_rate_limiter_completes_all_tasks(
57-
event_loop: asyncio.AbstractEventLoop,
58-
) -> None:
55+
async def test_rate_limiter_completes_all_tasks() -> None:
5956
"""Test to check all requests will go through rate limiter successfully."""
57+
event_loop = asyncio.get_running_loop()
6058
counter = 0
6159
# allow 1 request to go through per second
62-
rate_limiter = AsyncRateLimiter(max_capacity=1, rate=1, loop=event_loop)
60+
rate_limiter = AsyncRateLimiter(max_capacity=1, rate=1)
6361

6462
async def increment() -> None:
6563
await rate_limiter.acquire()

0 commit comments

Comments
 (0)