Skip to content

Commit 8094d0d

Browse files
authored
test: Add pytest-xdist to execute tests in parallel on CI (and/or locally) (#2672)
* Add pytest-xdist to execute tests in parallel on CI ; Update workflow * add _get_unique_index_name to handle tests with same name * Simplified _get_unique_index_name ; Removed some redundant comments * Lint
1 parent 4f63197 commit 8094d0d

4 files changed

Lines changed: 23 additions & 38 deletions

File tree

.github/workflows/opensearch.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ jobs:
5555
if: matrix.python-version == '3.10' && runner.os == 'Linux'
5656
run: hatch run docs
5757

58-
- name: Run tests
59-
run: hatch run test:cov-retry
58+
- name: Run tests (in parallel, using 4 cores)
59+
run: hatch run test:cov-retry -n 4 # GA runner has 4 cores
6060

6161
- name: Run unit tests with lowest direct dependencies
6262
run: |

integrations/opensearch/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ dependencies = [
5858
"pytest-asyncio",
5959
"pytest-cov",
6060
"pytest-rerunfailures",
61+
"pytest-xdist",
6162
"mypy",
6263
"pip",
6364
"boto3",
@@ -164,4 +165,3 @@ exclude_lines = ["no cov", "if __name__ == .__main__.:", "if TYPE_CHECKING:"]
164165
minversion = "6.0"
165166
markers = ["unit: unit tests", "integration: integration tests"]
166167
asyncio_mode = "auto"
167-

integrations/opensearch/tests/conftest.py

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,29 @@
11
import asyncio
2+
import uuid
23

34
import pytest
45
from haystack import Document
56

67
from haystack_integrations.document_stores.opensearch.document_store import OpenSearchDocumentStore
78

89

10+
def _get_unique_index_name() -> str:
11+
"""
12+
Generate a unique, valid OpenSearch index name for test isolation.
13+
14+
Each test gets its own index to enable parallel test execution without conflicts.
15+
"""
16+
return f"test_{uuid.uuid4().hex}"
17+
18+
919
@pytest.fixture
10-
def document_store(request):
20+
def document_store():
1121
"""
1222
We use this document store for basic tests and for testing filters.
1323
`return_embedding` is set to True because in filters tests we compare embeddings.
1424
"""
1525
hosts = ["https://localhost:9200"]
16-
# Use a different index for each test so we can run them in parallel
17-
index = f"{request.node.name}"
26+
index = _get_unique_index_name()
1827

1928
store = OpenSearchDocumentStore(
2029
hosts=hosts,
@@ -36,9 +45,9 @@ def document_store(request):
3645

3746

3847
@pytest.fixture
39-
def document_store_2(request):
48+
def document_store_2():
4049
hosts = ["https://localhost:9200"]
41-
index = f"test_index_2_{request.node.name}"
50+
index = f"test_index_2_{_get_unique_index_name()}"
4251

4352
store = OpenSearchDocumentStore(
4453
hosts=hosts,
@@ -61,13 +70,13 @@ def document_store_2(request):
6170

6271

6372
@pytest.fixture
64-
def document_store_readonly(request):
73+
def document_store_readonly():
6574
"""
6675
A document store that does not automatically create the underlying index.
6776
"""
6877
hosts = ["https://localhost:9200"]
6978
# Use a different index for each test so we can run them in parallel
70-
index = f"{request.node.name}"
79+
index = _get_unique_index_name()
7180

7281
store = OpenSearchDocumentStore(
7382
hosts=hosts,
@@ -90,13 +99,12 @@ def document_store_readonly(request):
9099

91100

92101
@pytest.fixture
93-
def document_store_embedding_dim_4_no_emb_returned(request):
102+
def document_store_embedding_dim_4_no_emb_returned():
94103
"""
95104
A document store with embedding dimension 4 that does not return embeddings.
96105
"""
97106
hosts = ["https://localhost:9200"]
98-
# Use a different index for each test so we can run them in parallel
99-
index = f"{request.node.name}"
107+
index = _get_unique_index_name()
100108

101109
store = OpenSearchDocumentStore(
102110
hosts=hosts,
@@ -113,15 +121,14 @@ def document_store_embedding_dim_4_no_emb_returned(request):
113121

114122

115123
@pytest.fixture
116-
def document_store_embedding_dim_4_no_emb_returned_faiss(request):
124+
def document_store_embedding_dim_4_no_emb_returned_faiss():
117125
"""
118126
A document store with embedding dimension 4 that uses a FAISS engine with HNSW algorithm for vector search.
119127
We use this document store for testing efficient k-NN filtering according to
120128
https://opensearch.org/docs/latest/vector-search/filter-search-knn/efficient-knn-filtering/.
121129
"""
122130
hosts = ["https://localhost:9200"]
123-
# Use a different index for each test so we can run them in parallel
124-
index = f"{request.node.name}"
131+
index = _get_unique_index_name()
125132

126133
store = OpenSearchDocumentStore(
127134
hosts=hosts,

integrations/opensearch/tests/test_document_store.py

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -443,28 +443,6 @@ def test_write_documents_max_chunk_bytes(self, mock_bulk, document_store):
443443

444444
assert mock_bulk.call_args.kwargs["max_chunk_bytes"] == DEFAULT_MAX_CHUNK_BYTES
445445

446-
@pytest.fixture
447-
def document_store_embedding_dim_4_no_emb_returned(self, request):
448-
"""
449-
This is the most basic requirement for the child class: provide
450-
an instance of this document store so the base class can use it.
451-
"""
452-
hosts = ["https://localhost:9200"]
453-
# Use a different index for each test so we can run them in parallel
454-
index = f"{request.node.name}"
455-
456-
store = OpenSearchDocumentStore(
457-
hosts=hosts,
458-
index=index,
459-
http_auth=("admin", "admin"),
460-
verify_certs=False,
461-
embedding_dim=4,
462-
return_embedding=False,
463-
method={"space_type": "cosinesimil", "engine": "nmslib", "name": "hnsw"},
464-
)
465-
yield store
466-
store._client.indices.delete(index=index, params={"ignore": [400, 404]})
467-
468446
def test_embedding_retrieval_but_dont_return_embeddings_for_embedding_retrieval(
469447
self, document_store_embedding_dim_4_no_emb_returned: OpenSearchDocumentStore
470448
):

0 commit comments

Comments
 (0)