1
1
import asyncio
2
- import threading
3
2
4
- import asyncpg
5
- import pytest
3
+ from sftkit .database import DatabaseHook , Pool
6
4
7
- from sftkit .database import DatabaseConfig , DatabaseHook , create_db_pool
8
5
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 ):
12
7
initial_run : bool = False
13
8
received_payload : str = ""
14
9
@@ -23,25 +18,19 @@ async def trigger(payload):
23
18
24
19
hook : DatabaseHook | None = None
25
20
26
- def hook_thread (** hook_args ):
21
+ async def hook_coro (** hook_args ):
27
22
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 ()
33
25
34
26
# 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 ))
39
28
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');" )
41
30
assert hook is not None
42
31
await asyncio .sleep (0.2 ) # wait for the notification to arrive
43
32
hook .stop ()
44
- ht . join ()
33
+ await ht
45
34
46
35
assert initial_run
47
36
assert received_payload == "rolf"
@@ -50,16 +39,13 @@ def hook_thread(**hook_args):
50
39
initial_run = False
51
40
received_payload = ""
52
41
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 ))
57
43
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');" )
59
45
assert hook is not None
60
46
await asyncio .sleep (0.2 ) # wait for the notification to arrive
61
47
hook .stop ()
62
- ht . join ()
48
+ await ht
63
49
64
50
assert not initial_run
65
51
assert received_payload == "lol"
0 commit comments