1
1
import asyncio
2
- import importlib .util
3
2
import sys
4
3
from collections .abc import AsyncGenerator
5
- from unittest .mock import AsyncMock , call , patch
4
+ from unittest .mock import AsyncMock , patch
6
5
7
6
import pytest
8
7
@@ -177,10 +176,11 @@ def test_create_event_loop_default_config() -> None:
177
176
with zarr .config .set ({"async.use_uvloop" : True }):
178
177
loop = _create_event_loop ()
179
178
if sys .platform != "win32" :
180
- if importlib .util .find_spec ("uvloop" ) is not None :
181
- # uvloop is available, should use it
182
- assert "uvloop" in str (type (loop ))
183
- else :
179
+ try :
180
+ import uvloop
181
+
182
+ assert isinstance (loop , uvloop .Loop )
183
+ except ImportError :
184
184
# uvloop not available, should use asyncio
185
185
assert isinstance (loop , asyncio .AbstractEventLoop )
186
186
assert "uvloop" not in str (type (loop ))
@@ -203,13 +203,13 @@ def test_create_event_loop_uvloop_disabled() -> None:
203
203
204
204
205
205
@pytest .mark .skipif (sys .platform == "win32" , reason = "uvloop is not supported on Windows" )
206
- @pytest .mark .skipif (importlib .util .find_spec ("uvloop" ) is None , reason = "uvloop is not installed" )
207
206
def test_create_event_loop_uvloop_enabled_non_windows () -> None :
208
207
"""Test uvloop usage on non-Windows platforms when uvloop is installed."""
208
+ uvloop = pytest .importorskip ("uvloop" )
209
+
209
210
with zarr .config .set ({"async.use_uvloop" : True }):
210
211
loop = _create_event_loop ()
211
- # uvloop is available and should be used
212
- assert "uvloop" in str (type (loop ))
212
+ assert isinstance (loop , uvloop .Loop )
213
213
loop .close ()
214
214
215
215
@@ -224,96 +224,6 @@ def test_create_event_loop_windows_no_uvloop() -> None:
224
224
loop .close ()
225
225
226
226
227
- def test_uvloop_config_environment_variable () -> None :
228
- """Test that uvloop can be controlled via environment variable."""
229
- # This test verifies the config system works with uvloop setting
230
- # We test both True and False values
231
- with zarr .config .set ({"async.use_uvloop" : False }):
232
- assert zarr .config .get ("async.use_uvloop" ) is False
233
-
234
- with zarr .config .set ({"async.use_uvloop" : True }):
235
- assert zarr .config .get ("async.use_uvloop" ) is True
236
-
237
-
238
- def test_uvloop_integration_with_zarr_operations (clean_state ) -> None :
239
- """Test that uvloop integration doesn't break zarr operations."""
240
- # Test with uvloop enabled (default)
241
- with zarr .config .set ({"async.use_uvloop" : True }):
242
- arr = zarr .zeros ((10 , 10 ), chunks = (5 , 5 ))
243
- arr [0 , 0 ] = 42.0
244
- result = arr [0 , 0 ]
245
- assert result == 42.0
246
-
247
- # Test with uvloop disabled
248
- with zarr .config .set ({"async.use_uvloop" : False }):
249
- arr2 = zarr .zeros ((10 , 10 ), chunks = (5 , 5 ))
250
- arr2 [0 , 0 ] = 24.0
251
- result2 = arr2 [0 , 0 ]
252
- assert result2 == 24.0
253
-
254
-
255
- @patch ("zarr.core.sync.logger.debug" )
256
- def test_uvloop_logging_availability (mock_debug , clean_state ) -> None :
257
- """Test that appropriate debug messages are logged."""
258
- # Test with uvloop enabled
259
- with zarr .config .set ({"async.use_uvloop" : True }):
260
- loop = _create_event_loop ()
261
-
262
- if sys .platform != "win32" :
263
- if importlib .util .find_spec ("uvloop" ) is not None :
264
- # Should log that uvloop is being used
265
- mock_debug .assert_called_with ("Creating Zarr event loop with uvloop" )
266
- else :
267
- # Should log fallback to asyncio
268
- mock_debug .assert_called_with ("uvloop not available, falling back to asyncio" )
269
- else :
270
- # Should log that uvloop is not supported on Windows
271
- mock_debug .assert_called_with ("uvloop not supported on Windows, using asyncio" )
272
-
273
- loop .close ()
274
-
275
-
276
- @pytest .mark .skipif (sys .platform == "win32" , reason = "uvloop is not supported on Windows" )
277
- @pytest .mark .skipif (importlib .util .find_spec ("uvloop" ) is None , reason = "uvloop is not installed" )
278
- @patch ("zarr.core.sync.logger.debug" )
279
- def test_uvloop_logging_with_uvloop_installed (mock_debug , clean_state ) -> None :
280
- """Test that uvloop is logged when installed and enabled."""
281
- with zarr .config .set ({"async.use_uvloop" : True }):
282
- loop = _create_event_loop ()
283
- # Should log that uvloop is being used
284
- mock_debug .assert_called_with ("Creating Zarr event loop with uvloop" )
285
- loop .close ()
286
-
287
-
288
- @pytest .mark .skipif (importlib .util .find_spec ("uvloop" ) is not None , reason = "uvloop is installed" )
289
- @patch ("zarr.core.sync.logger.debug" )
290
- def test_uvloop_logging_without_uvloop_installed (mock_debug , clean_state ) -> None :
291
- """Test that fallback to asyncio is logged when uvloop is not installed."""
292
- with zarr .config .set ({"async.use_uvloop" : True }):
293
- loop = _create_event_loop ()
294
- if sys .platform != "win32" :
295
- # Should log fallback to asyncio
296
- mock_debug .assert_called_with ("uvloop not available, falling back to asyncio" )
297
- else :
298
- # Should log that uvloop is not supported on Windows
299
- mock_debug .assert_called_with ("uvloop not supported on Windows, using asyncio" )
300
- loop .close ()
301
-
302
-
303
- @patch ("zarr.core.sync.logger.debug" )
304
- def test_uvloop_logging_disabled (mock_debug , clean_state ) -> None :
305
- """Test that appropriate debug message is logged when uvloop is disabled."""
306
- with zarr .config .set ({"async.use_uvloop" : False }):
307
- loop = _create_event_loop ()
308
- # Should log both that uvloop is disabled and the final loop creation
309
- expected_calls = [
310
- call ("uvloop disabled via config, using asyncio" ),
311
- call ("Creating Zarr event loop with asyncio" ),
312
- ]
313
- mock_debug .assert_has_calls (expected_calls )
314
- loop .close ()
315
-
316
-
317
227
def test_uvloop_mock_import_error (clean_state ) -> None :
318
228
"""Test graceful handling when uvloop import fails."""
319
229
with zarr .config .set ({"async.use_uvloop" : True }):
0 commit comments