12
12
pytestmark = pytest .mark .anyio
13
13
14
14
15
+ @asynccontextmanager
16
+ async def create_client () -> AsyncGenerator [stompman .Client , None ]:
17
+ server = stompman .ConnectionParameters (
18
+ host = os .environ ["ARTEMIS_HOST" ], port = 61616 , login = "admin" , passcode = "%3D123"
19
+ )
20
+ async with stompman .Client (servers = [server ], read_timeout = 10 , connection_confirmation_timeout = 10 ) as client :
21
+ yield client
22
+
23
+
15
24
@pytest .fixture ()
16
- def server () -> stompman .ConnectionParameters :
17
- return stompman .ConnectionParameters (host = os .environ ["ARTEMIS_HOST" ], port = 61616 , login = "admin" , passcode = "%3D123" )
25
+ async def client () -> AsyncGenerator [stompman .Client , None ]:
26
+ async with create_client () as client :
27
+ yield client
18
28
19
29
20
- async def test_ok ( server : stompman . ConnectionParameters ) -> None :
21
- destination = "DLQ"
22
- messages = [ str ( uuid4 ()). encode () for _ in range ( 10000 )]
30
+ @ pytest . fixture ()
31
+ def destination () -> str :
32
+ return "DLQ"
23
33
34
+
35
+ async def test_ok (destination : str ) -> None :
24
36
async def produce () -> None :
25
37
async with producer .enter_transaction () as transaction :
26
38
for message in messages :
@@ -42,35 +54,62 @@ async def consume() -> None:
42
54
43
55
assert sorted (received_messages ) == sorted (messages )
44
56
45
- async with (
46
- stompman .Client (servers = [server ], read_timeout = 10 , connection_confirmation_timeout = 10 ) as consumer ,
47
- stompman .Client (servers = [server ], read_timeout = 10 , connection_confirmation_timeout = 10 ) as producer ,
48
- asyncio .TaskGroup () as task_group ,
49
- ):
57
+ messages = [str (uuid4 ()).encode () for _ in range (10000 )]
58
+
59
+ async with create_client () as consumer , create_client () as producer , asyncio .TaskGroup () as task_group :
50
60
task_group .create_task (consume ())
51
61
task_group .create_task (produce ())
52
62
53
63
54
- @asynccontextmanager
55
- async def closed_client (server : stompman .ConnectionParameters ) -> AsyncGenerator [stompman .Client , None ]:
56
- async with stompman .Client (servers = [server ], read_timeout = 10 , connection_confirmation_timeout = 10 ) as client :
64
+ async def test_not_raises_connection_lost_error_in_aexit (client : stompman .Client ) -> None :
65
+ await client ._connection .close ()
66
+
67
+
68
+ async def test_not_raises_connection_lost_error_in_write_frame (client : stompman .Client ) -> None :
69
+ await client ._connection .close ()
70
+
71
+ with pytest .raises (ConnectionLostError ):
72
+ await client ._connection .write_frame (stompman .ConnectFrame (headers = {"accept-version" : "" , "host" : "" }))
73
+
74
+
75
+ @pytest .mark .parametrize ("anyio_backend" , [("asyncio" , {"use_uvloop" : True })])
76
+ async def test_not_raises_connection_lost_error_in_write_heartbeat (client : stompman .Client ) -> None :
77
+ await client ._connection .close ()
78
+
79
+ with pytest .raises (ConnectionLostError ):
80
+ client ._connection .write_heartbeat ()
81
+
82
+
83
+ async def test_not_raises_connection_lost_error_in_subscription (client : stompman .Client , destination : str ) -> None :
84
+ async with client .subscribe (destination ):
57
85
await client ._connection .close ()
58
- yield client
59
86
60
87
61
- async def test_not_raises_connection_lost_error_in_aexit ( server : stompman .ConnectionParameters ) -> None :
62
- async with closed_client ( server ):
63
- pass
88
+ async def test_not_raises_connection_lost_error_in_transaction_without_send ( client : stompman .Client ) -> None :
89
+ async with client . enter_transaction ( ):
90
+ await client . _connection . close ()
64
91
65
92
66
- async def test_not_raises_connection_lost_error_in_write_frame (server : stompman .ConnectionParameters ) -> None :
67
- async with closed_client (server ) as client :
93
+ async def test_not_raises_connection_lost_error_in_transaction_with_send (
94
+ client : stompman .Client , destination : str
95
+ ) -> None :
96
+ async with client .enter_transaction () as transaction :
97
+ await client .send (b"first" , destination = destination , transaction = transaction )
98
+ await client ._connection .close ()
99
+
68
100
with pytest .raises (ConnectionLostError ):
69
- await client ._connection . write_frame ( stompman . ConnectFrame ( headers = { "accept-version" : "" , "host" : "" }) )
101
+ await client .send ( b"second" , destination = destination , transaction = transaction )
70
102
71
103
72
- @pytest .mark .parametrize ("anyio_backend" , [("asyncio" , {"use_uvloop" : True })])
73
- async def test_not_raises_connection_lost_error_in_write_heartbeat (server : stompman .ConnectionParameters ) -> None :
74
- async with closed_client (server ) as client :
75
- with pytest .raises (ConnectionLostError ):
76
- client ._connection .write_heartbeat ()
104
+ async def test_raises_connection_lost_error_in_send (client : stompman .Client , destination : str ) -> None :
105
+ await client ._connection .close ()
106
+
107
+ with pytest .raises (ConnectionLostError ):
108
+ await client .send (b"first" , destination = destination )
109
+
110
+
111
+ async def test_raises_connection_lost_error_in_listen (client : stompman .Client ) -> None :
112
+ await client ._connection .close ()
113
+ client .read_timeout = 0
114
+ with pytest .raises (ConnectionLostError ):
115
+ [event async for event in client .listen ()]
0 commit comments