Skip to content

Commit 983a4a0

Browse files
committed
Fix pre-commit checks
1 parent 1fb3f9c commit 983a4a0

File tree

2 files changed

+45
-42
lines changed

2 files changed

+45
-42
lines changed

pyproject.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
[build-system]
32
requires = ["setuptools>=42", "wheel"]
43
build-backend = "setuptools.build_meta"
@@ -23,7 +22,7 @@ dependencies = [
2322
"multiaddr>=0.0.9",
2423
"mypy-protobuf>=3.0.0",
2524
"noiseprotocol>=0.3.0",
26-
"protobuf>=3.20.1,<4.0.0",
25+
"protobuf>=4.21.0,<5.0.0",
2726
"pycryptodome>=3.9.2",
2827
"pymultihash>=0.8.2",
2928
"pynacl>=1.3.0",

tests/core/identity/identify_push/test_identify_push.py

Lines changed: 44 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,6 @@
3939
logger = logging.getLogger("libp2p.identity.identify-push-test")
4040

4141

42-
CONCURRENCY_LIMIT = 10
43-
LIMIT = trio.Semaphore(CONCURRENCY_LIMIT)
44-
concurrency_counter = 0
45-
max_observed = 0
46-
lock = trio.Lock()
47-
48-
4942
@pytest.mark.trio
5043
async def test_identify_push_protocol(security_protocol):
5144
"""
@@ -221,7 +214,7 @@ async def test_identify_push_to_peers(security_protocol):
221214
# Test for push_identify to only connected peers and not all peers
222215
# Disconnect a from c.
223216
await host_c.disconnect(host_a.get_id())
224-
#
217+
225218
await push_identify_to_peers(host_c)
226219

227220
# Wait a bit for the push to complete
@@ -456,6 +449,46 @@ async def test_push_identify_to_peers_respects_concurrency_limit():
456449
It mocks `push_identify_to_peer` to simulate delay using sleep,
457450
allowing the test to measure and assert actual concurrency behavior.
458451
"""
452+
CONCURRENCY_LIMIT = 10
453+
LIMIT = trio.Semaphore(CONCURRENCY_LIMIT)
454+
state = {
455+
"concurrency_counter": 0,
456+
"max_observed": 0,
457+
}
458+
lock = trio.Lock()
459+
460+
async def mock_push_identify_to_peer(
461+
host, peer_id, observed_multiaddr=None
462+
) -> bool:
463+
"""
464+
Mock function to test concurrency by simulating an identify message.
465+
466+
This function patches push_identify_to_peer for testing purpose
467+
468+
Returns
469+
-------
470+
bool
471+
True if the push was successful, False otherwise.
472+
473+
"""
474+
async with LIMIT:
475+
async with lock:
476+
state["concurrency_counter"] += 1
477+
if state["concurrency_counter"] > CONCURRENCY_LIMIT:
478+
raise RuntimeError(
479+
f"Concurrency limit exceeded: {state['concurrency_counter']}"
480+
)
481+
state["max_observed"] = max(
482+
state["max_observed"], state["concurrency_counter"]
483+
)
484+
485+
logger.debug("Successfully pushed identify to peer %s", peer_id)
486+
await trio.sleep(0.05)
487+
488+
async with lock:
489+
state["concurrency_counter"] -= 1
490+
491+
return True
459492

460493
# Create a mock host.
461494
key_pair_host = create_new_key_pair()
@@ -468,35 +501,6 @@ async def test_push_identify_to_peers_respects_concurrency_limit():
468501
new=mock_push_identify_to_peer,
469502
):
470503
await push_identify_to_peers(host)
471-
assert (
472-
max_observed <= CONCURRENCY_LIMIT
473-
), f"Max concurrency observed: {max_observed}"
474-
475-
476-
async def mock_push_identify_to_peer(host, peer_id, observed_multiaddr=None) -> bool:
477-
"""
478-
Mock function to test concurrency by simulating an identify message.
479-
480-
This function patches push_identify_to_peer for testing purpose
481-
482-
Returns
483-
-------
484-
bool
485-
True if the push was successful, False otherwise.
486-
"""
487-
global concurrency_counter, max_observed
488-
489-
async with LIMIT:
490-
async with lock:
491-
concurrency_counter += 1
492-
if concurrency_counter > CONCURRENCY_LIMIT:
493-
raise RuntimeError(f"Concurrency limit exceeded: {concurrency_counter}")
494-
max_observed = max(max_observed, concurrency_counter)
495-
496-
logger.debug("Successfully pushed identify to peer %s", peer_id)
497-
await trio.sleep(0.05)
498-
499-
async with lock:
500-
concurrency_counter -= 1
501-
502-
return True
504+
assert state["max_observed"] <= CONCURRENCY_LIMIT, (
505+
f"Max concurrency observed: {state['max_observed']}"
506+
)

0 commit comments

Comments
 (0)