Skip to content

Commit c85310e

Browse files
committed
More test fixes
1 parent 6a3c026 commit c85310e

File tree

5 files changed

+36
-20
lines changed

5 files changed

+36
-20
lines changed

tests/datastore_elasticsearch/test_async_elasticsearch.py

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414
import elasticsearch._async.client as client
15-
import pytest
1615
from conftest import ES_SETTINGS, ES_VERSION
1716
from testing_support.fixture.event_loop import event_loop as loop # noqa: F401
1817
from testing_support.fixtures import override_application_settings

tests/datastore_elasticsearch/test_async_instrumented_methods.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ def test_method_on_async_client_datastore_trace_inputs(
6666
@background_task()
6767
async def _test():
6868
if not sub_module:
69-
getattr(async_client, method)(*args, **kwargs)
69+
await getattr(async_client, method)(*args, **kwargs)
7070
else:
71-
getattr(getattr(async_client, sub_module), method)(*args, **kwargs)
71+
await getattr(getattr(async_client, sub_module), method)(*args, **kwargs)
7272

7373
loop.run_until_complete(_test())
7474

tests/datastore_elasticsearch/test_async_mget.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -73,17 +73,19 @@
7373

7474

7575
@pytest.fixture(scope="module")
76-
def client():
76+
def client(loop):
7777
urls = [f"http://{db['host']}:{db['port']}" for db in ES_MULTIPLE_SETTINGS]
7878
# When selecting a connection from the pool, use the round robin method.
7979
# This is actually the default already. Using round robin will ensure that
8080
# doing two db calls will mean elastic search is talking to two different
8181
# dbs.
8282
if ES_VERSION >= (8,):
83-
client = AsyncElasticsearch(urls, node_selector_class=RoundRobinSelector, randomize_hosts=False)
83+
_client = AsyncElasticsearch(urls, node_selector_class=RoundRobinSelector, randomize_nodes_in_pool=False)
8484
else:
85-
client = AsyncElasticsearch(urls, selector_class=RoundRobinSelector, randomize_hosts=False)
86-
return client
85+
_client = AsyncElasticsearch(urls, selector_class=RoundRobinSelector, randomize_nodes_in_pool=False)
86+
87+
yield _client
88+
loop.run_until_complete(_client.close())
8789

8890

8991
# Query

tests/datastore_elasticsearch/test_async_multiple_dbs.py

+23-10
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,19 @@ async def _exercise_es(es):
7878
# Test
7979

8080

81+
@pytest.fixture(scope="session")
82+
def async_clients(loop):
83+
clients = []
84+
for db in ES_MULTIPLE_SETTINGS:
85+
es_url = f"http://{db['host']}:{db['port']}"
86+
clients.append(AsyncElasticsearch(es_url))
87+
88+
yield clients
89+
90+
for client in clients:
91+
loop.run_until_complete(client.close())
92+
93+
8194
@pytest.mark.skipif(len(ES_MULTIPLE_SETTINGS) < 2, reason="Test environment not configured with multiple databases.")
8295
@override_application_settings(_enable_instance_settings)
8396
@validate_transaction_metrics(
@@ -87,11 +100,11 @@ async def _exercise_es(es):
87100
background_task=True,
88101
)
89102
@background_task()
90-
def test_async_multiple_dbs_enabled(loop):
91-
for db in ES_MULTIPLE_SETTINGS:
92-
es_url = f"http://{db['host']}:{db['port']}"
93-
client = AsyncElasticsearch(es_url)
94-
loop.run_until_complete(_exercise_es(client))
103+
def test_async_multiple_dbs_enabled(loop, async_clients):
104+
import asyncio
105+
106+
# Run multiple queries in parallel
107+
loop.run_until_complete(asyncio.gather(*(_exercise_es(client) for client in async_clients)))
95108

96109

97110
@pytest.mark.skipif(len(ES_MULTIPLE_SETTINGS) < 2, reason="Test environment not configured with multiple databases.")
@@ -103,8 +116,8 @@ def test_async_multiple_dbs_enabled(loop):
103116
background_task=True,
104117
)
105118
@background_task()
106-
def test_async_multiple_dbs_disabled(loop):
107-
for db in ES_MULTIPLE_SETTINGS:
108-
es_url = f"http://{db['host']}:{db['port']}"
109-
client = AsyncElasticsearch(es_url)
110-
loop.run_until_complete(_exercise_es(client))
119+
def test_async_multiple_dbs_disabled(loop, async_clients):
120+
import asyncio
121+
122+
# Run multiple queries in parallel
123+
loop.run_until_complete(asyncio.gather(*(_exercise_es(client) for client in async_clients)))

tests/datastore_elasticsearch/test_mget.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,12 @@ def client():
7878
# doing two db calls will mean elastic search is talking to two different
7979
# dbs.
8080
if ES_VERSION >= (8,):
81-
client = Elasticsearch(urls, node_selector_class=RoundRobinSelector, randomize_hosts=False)
81+
_client = Elasticsearch(urls, node_selector_class=RoundRobinSelector, randomize_nodes_in_pool=False)
8282
else:
83-
client = Elasticsearch(urls, selector_class=RoundRobinSelector, randomize_hosts=False)
84-
return client
83+
_client = Elasticsearch(urls, selector_class=RoundRobinSelector, randomize_nodes_in_pool=False)
84+
85+
yield _client
86+
_client.close()
8587

8688

8789
# Query

0 commit comments

Comments
 (0)