@@ -83,103 +83,77 @@ async def test_task() -> None: ...
8383
8484
8585@pytest .mark .anyio
86- async def test_async_context_manager_enter () -> None :
87- """Test that __aenter__ calls startup."""
86+ @pytest .mark .parametrize (
87+ ("is_worker_process" , "startup" , "shutdown" ),
88+ [
89+ (True , TaskiqEvents .WORKER_STARTUP , TaskiqEvents .WORKER_SHUTDOWN ),
90+ (False , TaskiqEvents .CLIENT_STARTUP , TaskiqEvents .CLIENT_SHUTDOWN ),
91+ ],
92+ )
93+ async def test_async_context_manager_enter (
94+ * ,
95+ is_worker_process : bool ,
96+ startup : TaskiqEvents ,
97+ shutdown : TaskiqEvents ,
98+ ) -> None :
99+ """Test that `__aenter__` and `__aexit__` calls work."""
88100 broker = _TestBroker ()
101+ broker .is_worker_process = is_worker_process
89102 startup_called = False
103+ shutdown_called = False
90104
91- @broker .on_event (TaskiqEvents . CLIENT_STARTUP )
105+ @broker .on_event (startup )
92106 async def track_startup (state : TaskiqState ) -> None :
93107 nonlocal startup_called
94108 startup_called = True
95109
96- async with broker :
97- assert startup_called is True
98-
99-
100- @pytest .mark .anyio
101- async def test_async_context_manager_exit () -> None :
102- """Test that __aexit__ calls shutdown."""
103- broker = _TestBroker ()
104- shutdown_called = False
105-
106- @broker .on_event (TaskiqEvents .CLIENT_SHUTDOWN )
110+ @broker .on_event (shutdown )
107111 async def track_shutdown (state : TaskiqState ) -> None :
108112 nonlocal shutdown_called
109113 shutdown_called = True
110114
111- async with broker :
112- pass
115+ async with broker as ctx :
116+ assert ctx is None
117+ assert startup_called is True
118+ assert shutdown_called is False
113119
114120 assert shutdown_called is True
115121
116122
117123@pytest .mark .anyio
118- async def test_async_context_manager_enter_worker () -> None :
119- """Test that __aenter__ calls worker startup when is_worker_process is True."""
124+ @pytest .mark .parametrize (
125+ ("is_worker_process" , "startup" , "shutdown" ),
126+ [
127+ (True , TaskiqEvents .WORKER_STARTUP , TaskiqEvents .WORKER_SHUTDOWN ),
128+ (False , TaskiqEvents .CLIENT_STARTUP , TaskiqEvents .CLIENT_SHUTDOWN ),
129+ ],
130+ )
131+ async def test_async_context_manager_exit_on_exception (
132+ * ,
133+ is_worker_process : bool ,
134+ startup : TaskiqEvents ,
135+ shutdown : TaskiqEvents ,
136+ ) -> None :
137+ """Test that __aexit__ calls shutdown even if exception is raised."""
120138 broker = _TestBroker ()
121- broker .is_worker_process = True
139+ broker .is_worker_process = is_worker_process
122140 startup_called = False
141+ shutdown_called = False
123142
124- @broker .on_event (TaskiqEvents . WORKER_STARTUP )
143+ @broker .on_event (startup )
125144 async def track_startup (state : TaskiqState ) -> None :
126145 nonlocal startup_called
127146 startup_called = True
128147
129- async with broker :
130- assert startup_called is True
131-
132-
133- @pytest .mark .anyio
134- async def test_async_context_manager_exit_worker () -> None :
135- """Test that __aexit__ calls worker shutdown when is_worker_process is True."""
136- broker = _TestBroker ()
137- broker .is_worker_process = True
138- shutdown_called = False
139-
140- @broker .on_event (TaskiqEvents .WORKER_SHUTDOWN )
141- async def track_shutdown (state : TaskiqState ) -> None :
142- nonlocal shutdown_called
143- shutdown_called = True
144-
145- async with broker :
146- pass
147-
148- assert shutdown_called is True
149-
150-
151- @pytest .mark .anyio
152- async def test_async_context_manager_exit_on_exception () -> None :
153- """Test that __aexit__ calls shutdown even if exception is raised."""
154- broker = _TestBroker ()
155- shutdown_called = False
156-
157- @broker .on_event (TaskiqEvents .CLIENT_SHUTDOWN )
158- async def track_shutdown (state : TaskiqState ) -> None :
159- nonlocal shutdown_called
160- shutdown_called = True
161-
162- with pytest .raises (ValueError , match = "Test exception" ):
163- async with broker :
164- raise ValueError ("Test exception" )
165-
166- assert shutdown_called is True
167-
168-
169- @pytest .mark .anyio
170- async def test_async_context_manager_exit_worker_on_exception () -> None :
171- """Test that __aexit__ calls worker shutdown even if exception is raised."""
172- broker = _TestBroker ()
173- broker .is_worker_process = True
174- shutdown_called = False
175-
176- @broker .on_event (TaskiqEvents .WORKER_SHUTDOWN )
148+ @broker .on_event (shutdown )
177149 async def track_shutdown (state : TaskiqState ) -> None :
178150 nonlocal shutdown_called
179151 shutdown_called = True
180152
181153 with pytest .raises (ValueError , match = "Test exception" ):
182154 async with broker :
155+ assert startup_called is True
156+ assert shutdown_called is False
183157 raise ValueError ("Test exception" )
184158
185159 assert shutdown_called is True
0 commit comments