Skip to content

Commit 4e8c0e3

Browse files
committed
test(sftkit): reenable and fix dbhook test
1 parent 6b3010e commit 4e8c0e3

File tree

2 files changed

+12
-26
lines changed

2 files changed

+12
-26
lines changed

sftkit/tests/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ async def test_db(db_config: DatabaseConfig, static_test_db_pool: Pool) -> Datab
4949

5050
@pytest_asyncio.fixture(loop_scope="session", scope="function")
5151
async def test_db_pool(test_db: Database) -> Pool:
52-
pool = await test_db.create_pool(n_connections=2)
52+
pool = await test_db.create_pool(n_connections=10)
5353
yield pool
5454
await pool.close()
5555

sftkit/tests/test_dbhook.py

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
11
import asyncio
2-
import threading
32

4-
import asyncpg
5-
import pytest
3+
from sftkit.database import DatabaseHook, Pool
64

7-
from sftkit.database import DatabaseConfig, DatabaseHook, create_db_pool
85

9-
10-
@pytest.mark.skip("currently does not work as test setup is not yet finalized")
11-
async def test_hook(config: DatabaseConfig, setup_test_db_pool: asyncpg.Pool):
6+
async def test_hook(test_db_pool: Pool):
127
initial_run: bool = False
138
received_payload: str = ""
149

@@ -23,25 +18,19 @@ async def trigger(payload):
2318

2419
hook: DatabaseHook | None = None
2520

26-
def hook_thread(**hook_args):
21+
async def hook_coro(**hook_args):
2722
nonlocal hook
28-
hook_loop = asyncio.new_event_loop()
29-
asyncio.set_event_loop(hook_loop)
30-
db_pool = asyncio.run(create_db_pool(config, 2))
31-
hook = DatabaseHook(pool=db_pool, **hook_args)
32-
asyncio.run(hook.run())
23+
hook = DatabaseHook(pool=test_db_pool, **hook_args)
24+
await hook.run()
3325

3426
# first round - with initial run
35-
ht = threading.Thread(
36-
target=hook_thread, kwargs={"channel": "testchannel", "event_handler": trigger, "initial_run": True}
37-
)
38-
ht.start()
27+
ht = asyncio.create_task(hook_coro(channel="testchannel", event_handler=trigger, initial_run=True))
3928
await asyncio.sleep(0.5) # wait for connection listener to be set up
40-
await setup_test_db_pool.execute("select pg_notify('testchannel', 'rolf');")
29+
await test_db_pool.execute("select pg_notify('testchannel', 'rolf');")
4130
assert hook is not None
4231
await asyncio.sleep(0.2) # wait for the notification to arrive
4332
hook.stop()
44-
ht.join()
33+
await ht
4534

4635
assert initial_run
4736
assert received_payload == "rolf"
@@ -50,16 +39,13 @@ def hook_thread(**hook_args):
5039
initial_run = False
5140
received_payload = ""
5241

53-
ht = threading.Thread(
54-
target=hook_thread, kwargs={"channel": "testchannel", "event_handler": trigger, "initial_run": False}
55-
)
56-
ht.start()
42+
ht = asyncio.create_task(hook_coro(channel="testchannel", event_handler=trigger, initial_run=False))
5743
await asyncio.sleep(0.5) # wait for connection listener to be set up
58-
await setup_test_db_pool.execute("select pg_notify('testchannel', 'lol');")
44+
await test_db_pool.execute("select pg_notify('testchannel', 'lol');")
5945
assert hook is not None
6046
await asyncio.sleep(0.2) # wait for the notification to arrive
6147
hook.stop()
62-
ht.join()
48+
await ht
6349

6450
assert not initial_run
6551
assert received_payload == "lol"

0 commit comments

Comments
 (0)