Skip to content

Commit e87d3fd

Browse files
committed
Improve flakiness of persistent connection provider tests:
- Clear caches before each persistent cxn provider test to help ensure a clean state for each test.
1 parent 3fdf305 commit e87d3fd

File tree

2 files changed

+36
-29
lines changed

2 files changed

+36
-29
lines changed

tests/integration/conftest.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,14 @@ def event_loop():
6464
loop = asyncio.get_event_loop_policy().new_event_loop()
6565
yield loop
6666
loop.close()
67+
68+
69+
@pytest.fixture(autouse=True)
70+
def _async_test_setup(request) -> None:
71+
# Check if the 'async_w3' fixture is available in the current context
72+
if "async_w3" in request.fixturenames:
73+
async_w3 = request.getfixturevalue("async_w3")
74+
if hasattr(async_w3.provider, "_request_processor"):
75+
# Clear the request processor cache for persistent connection provider tests
76+
async_w3.provider._request_processor.clear_caches()
77+
yield

web3/_utils/module_testing/persistent_connection_provider.py

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -363,40 +363,11 @@ async def test_async_extradata_poa_middleware_on_eth_subscription(
363363
# clean up
364364
async_w3.middleware_onion.remove("poa_middleware")
365365

366-
@pytest.mark.asyncio
367-
async def test_public_socket_api(self, async_w3: "AsyncWeb3") -> None:
368-
async_w3.provider._request_processor.clear_caches()
369-
370-
# send a request over the socket
371-
await async_w3.socket.send(
372-
RPCEndpoint("eth_getBlockByNumber"), ["latest", True]
373-
)
374-
375-
# recv and validate the unprocessed response
376-
response = await async_w3.socket.recv()
377-
assert "id" in response, "Expected 'id' key in response."
378-
assert "jsonrpc" in response, "Expected 'jsonrpc' key in response."
379-
assert "result" in response, "Expected 'result' key in response."
380-
assert all(k in response["result"].keys() for k in SOME_BLOCK_KEYS)
381-
assert not isinstance(response["result"]["number"], int) # assert not processed
382-
383-
# make a request over the socket
384-
response = await async_w3.socket.make_request(
385-
RPCEndpoint("eth_getBlockByNumber"), ["latest", True]
386-
)
387-
assert "id" in response, "Expected 'id' key in response."
388-
assert "jsonrpc" in response, "Expected 'jsonrpc' key in response."
389-
assert "result" in response, "Expected 'result' key in response."
390-
assert all(k in response["result"].keys() for k in SOME_BLOCK_KEYS)
391-
assert not isinstance(response["result"]["number"], int) # assert not processed
392-
393366
@pytest.mark.asyncio
394367
async def test_asyncio_gather_for_multiple_requests_matches_the_responses(
395368
self,
396369
async_w3: "AsyncWeb3",
397370
) -> None:
398-
async_w3.provider._request_processor.clear_caches()
399-
400371
(
401372
latest,
402373
chain_id,
@@ -427,3 +398,28 @@ async def test_asyncio_gather_for_multiple_requests_matches_the_responses(
427398
assert isinstance(chain_id, int)
428399
assert isinstance(chain_id2, int)
429400
assert isinstance(chain_id3, int)
401+
402+
@pytest.mark.asyncio
403+
async def test_public_socket_api(self, async_w3: "AsyncWeb3") -> None:
404+
# send a request over the socket
405+
await async_w3.socket.send(
406+
RPCEndpoint("eth_getBlockByNumber"), ["latest", True]
407+
)
408+
409+
# recv and validate the unprocessed response
410+
response = await async_w3.socket.recv()
411+
assert "id" in response, "Expected 'id' key in response."
412+
assert "jsonrpc" in response, "Expected 'jsonrpc' key in response."
413+
assert "result" in response, "Expected 'result' key in response."
414+
assert all(k in response["result"].keys() for k in SOME_BLOCK_KEYS)
415+
assert not isinstance(response["result"]["number"], int) # assert not processed
416+
417+
# make a request over the socket
418+
response = await async_w3.socket.make_request(
419+
RPCEndpoint("eth_getBlockByNumber"), ["latest", True]
420+
)
421+
assert "id" in response, "Expected 'id' key in response."
422+
assert "jsonrpc" in response, "Expected 'jsonrpc' key in response."
423+
assert "result" in response, "Expected 'result' key in response."
424+
assert all(k in response["result"].keys() for k in SOME_BLOCK_KEYS)
425+
assert not isinstance(response["result"]["number"], int) # assert not processed

0 commit comments

Comments
 (0)