Skip to content

Commit cca7589

Browse files
committed
test concurrent loading of multiple variables in one dataset
1 parent 48e4534 commit cca7589

File tree

1 file changed

+29
-9
lines changed

1 file changed

+29
-9
lines changed

xarray/tests/test_async.py

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@ async def measure(self):
120120
@requires_zarr_v3
121121
@pytest.mark.asyncio
122122
class TestAsyncLoad:
123-
N_LOADS: int = 5
124123
LATENCY: float = 1.0
125124

126125
@pytest.fixture(params=["ds", "da", "var"])
@@ -136,21 +135,42 @@ def xr_obj(self, request, memorystore) -> xr.Dataset | xr.DataArray | xr.Variabl
136135
case "ds":
137136
return ds
138137

139-
def assert_time_as_expected(self, total_time: float) -> None:
140-
assert total_time > self.LATENCY # Cannot possibly be quicker than this
138+
def assert_time_as_expected(
139+
self, total_time: float, latency: float, n_loads: int
140+
) -> None:
141+
assert total_time > latency # Cannot possibly be quicker than this
141142
assert (
142-
total_time < self.LATENCY * self.N_LOADS
143+
total_time < latency * n_loads
143144
) # If this isn't true we're gaining nothing from async
144145
assert (
145-
abs(total_time - self.LATENCY) < 2.0
146-
) # Should take approximately LATENCY seconds, but allow some buffer
146+
abs(total_time - latency) < 2.0
147+
) # Should take approximately `latency` seconds, but allow some buffer
148+
149+
async def test_concurrent_load_multiple_objects(self, xr_obj) -> None:
150+
N_OBJECTS = 5
147151

148-
async def test_async_load(self, xr_obj):
149152
async with AsyncTimer().measure() as timer:
150-
tasks = [xr_obj.load_async() for _ in range(self.N_LOADS)]
153+
tasks = [xr_obj.load_async() for _ in range(N_OBJECTS)]
151154
results = await asyncio.gather(*tasks)
152155

153156
for result in results:
154157
xrt.assert_identical(result, xr_obj.load())
155158

156-
self.assert_time_as_expected(timer.total_time)
159+
self.assert_time_as_expected(
160+
total_time=timer.total_time, latency=self.LATENCY, n_loads=N_OBJECTS
161+
)
162+
163+
async def test_concurrent_load_multiple_variables(self, memorystore) -> None:
164+
latencystore = LatencyStore(memorystore, latency=self.LATENCY)
165+
ds = xr.open_zarr(latencystore, zarr_format=3, consolidated=False, chunks=None)
166+
167+
# TODO up the number of variables in the dataset?
168+
async with AsyncTimer().measure() as timer:
169+
result_ds = await ds.load_async()
170+
171+
xrt.assert_identical(result_ds, ds.load())
172+
173+
# 2 because there are 2 lazy variables in the dataset
174+
self.assert_time_as_expected(
175+
total_time=timer.total_time, latency=self.LATENCY, n_loads=2
176+
)

0 commit comments

Comments
 (0)