@@ -83,103 +83,61 @@ 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 ('is_worker_process' , [True , False ])
87+ async def test_async_context_manager_enter (
88+ * ,
89+ is_worker_process : bool ,
90+ ) -> None :
91+ """Test that `__aenter__` and `__aexit__` calls work."""
8892 broker = _TestBroker ()
93+ broker .is_worker_process = is_worker_process
8994 startup_called = False
95+ shutdown_called = False
9096
9197 @broker .on_event (TaskiqEvents .CLIENT_STARTUP )
9298 async def track_startup (state : TaskiqState ) -> None :
9399 nonlocal startup_called
94100 startup_called = True
95101
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-
106102 @broker .on_event (TaskiqEvents .CLIENT_SHUTDOWN )
107103 async def track_shutdown (state : TaskiqState ) -> None :
108104 nonlocal shutdown_called
109105 shutdown_called = True
110106
111- async with broker :
112- pass
107+ async with broker as ctx :
108+ assert ctx is None
109+ assert startup_called is True
110+ assert shutdown_called is False
113111
114112 assert shutdown_called is True
115113
116114
117115@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."""
116+ @pytest .mark .parametrize ('is_worker_process' , [True , False ])
117+ async def test_async_context_manager_exit_on_exception (
118+ * ,
119+ is_worker_process : bool ,
120+ ) -> None :
121+ """Test that __aexit__ calls shutdown even if exception is raised."""
120122 broker = _TestBroker ()
121- broker .is_worker_process = True
123+ broker .is_worker_process = is_worker_process
122124 startup_called = False
125+ shutdown_called = False
123126
124- @broker .on_event (TaskiqEvents .WORKER_STARTUP )
127+ @broker .on_event (TaskiqEvents .CLIENT_STARTUP )
125128 async def track_startup (state : TaskiqState ) -> None :
126129 nonlocal startup_called
127130 startup_called = True
128131
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-
157132 @broker .on_event (TaskiqEvents .CLIENT_SHUTDOWN )
158133 async def track_shutdown (state : TaskiqState ) -> None :
159134 nonlocal shutdown_called
160135 shutdown_called = True
161136
162137 with pytest .raises (ValueError , match = "Test exception" ):
163138 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 )
177- async def track_shutdown (state : TaskiqState ) -> None :
178- nonlocal shutdown_called
179- shutdown_called = True
180-
181- with pytest .raises (ValueError , match = "Test exception" ):
182- async with broker :
139+ assert startup_called is True
140+ assert shutdown_called is False
183141 raise ValueError ("Test exception" )
184142
185143 assert shutdown_called is True
0 commit comments