mirror of
https://github.com/varun-r-mallya/py-libp2p.git
synced 2025-12-31 20:36:24 +00:00
ft. modernise py-libp2p (#618)
* fix pyproject.toml , add ruff * rm lock * make progress * add poetry lock ignore * fix type issues * fix tcp type errors * fix text example - type error - wrong args * add setuptools to dev * test ci * fix docs build * fix type issues for new_swarm & new_host * fix types in gossipsub * fix type issues in noise * wip: factories * revert factories * fix more type issues * more type fixes * fix: add null checks for noise protocol initialization and key handling * corrected argument-errors in peerId and Multiaddr in peer tests * fix: Noice - remove redundant type casts in BaseNoiseMsgReadWriter * fix: update test_notify.py to use SwarmFactory.create_batch_and_listen, fix type hints, and comment out ClosedStream assertions * Fix type checks for pubsub module Signed-off-by: sukhman <sukhmansinghsaluja@gmail.com> * Fix type checks for pubsub module-tests Signed-off-by: sukhman <sukhmansinghsaluja@gmail.com> * noise: add checks for uninitialized protocol and key states in PatternXX Signed-off-by: varun-r-mallya <varunrmallya@gmail.com> * pubsub: add None checks for optional fields in FloodSub and Pubsub Signed-off-by: varun-r-mallya <varunrmallya@gmail.com> * Fix type hints and improve testing Signed-off-by: varun-r-mallya <varunrmallya@gmail.com> * remove redundant checks Signed-off-by: varun-r-mallya <varunrmallya@gmail.com> * fix build issues * add optional to trio service * fix types * fix type errors * Fix type errors Signed-off-by: varun-r-mallya <varunrmallya@gmail.com> * fixed more-type checks in crypto and peer_data files * wip: factories * replaced union with optional * fix: type-error in interp-utils and peerinfo * replace pyright with pyrefly * add pyrefly.toml * wip: fix multiselect issues * try typecheck * base check * mcache test fixes , typecheck ci update * fix ci * will this work * minor fix * use poetry * fix wokflow * use cache,fix err * fix pyrefly.toml * fix pyrefly.toml * fix cache in ci * deploy commit * add main baseline * update to v5 * improve typecheck ci (#14) * fix typo * remove holepunching code (#16) * fix gossipsub typeerrors (#17) * fix: ensure initiator user includes remote peer id in handshake (#15) * fix ci (#19) * typefix: custom_types | core/peerinfo/test_peer_info | io/abc | pubsub/floodsub | protocol_muxer/multiselect (#18) * fix: Typefixes in PeerInfo (#21) * fix minor type issue (#22) * fix type errors in pubsub (#24) * fix: Minor typefixes in tests (#23) * Fix failing tests for type-fixed test/pubsub (#8) * move pyrefly & ruff to pyproject.toml & rm .project-template (#28) * move the async_context file to tests/core * move crypto test to crypto folder * fix: some typefixes (#25) * fix type errors * fix type issues * fix: update gRPC API usage in autonat_pb2_grpc.py (#31) * md: typecheck ci * rm comments * clean up : from review suggestions * use | None over Optional as per new python standards * drop supporto for py3.9 * newsfragments --------- Signed-off-by: sukhman <sukhmansinghsaluja@gmail.com> Signed-off-by: varun-r-mallya <varunrmallya@gmail.com> Co-authored-by: acul71 <luca.pisani@birdo.net> Co-authored-by: kaneki003 <sakshamchauhan707@gmail.com> Co-authored-by: sukhman <sukhmansinghsaluja@gmail.com> Co-authored-by: varun-r-mallya <varunrmallya@gmail.com> Co-authored-by: varunrmallya <100590632+varun-r-mallya@users.noreply.github.com> Co-authored-by: lla-dane <abhinavagarwalla6@gmail.com> Co-authored-by: Collins <ArtemisfowlX@protonmail.com> Co-authored-by: Abhinav Agarwalla <120122716+lla-dane@users.noreply.github.com> Co-authored-by: guha-rahul <52607971+guha-rahul@users.noreply.github.com> Co-authored-by: Sukhman Singh <63765293+sukhman-sukh@users.noreply.github.com> Co-authored-by: acul71 <34693171+acul71@users.noreply.github.com> Co-authored-by: pacrob <5199899+pacrob@users.noreply.github.com>
This commit is contained in:
@ -11,13 +11,8 @@ else:
|
||||
|
||||
import pytest
|
||||
import trio
|
||||
from trio.testing import (
|
||||
Matcher,
|
||||
RaisesGroup,
|
||||
)
|
||||
|
||||
from libp2p.tools.async_service import (
|
||||
DaemonTaskExit,
|
||||
LifecycleError,
|
||||
Service,
|
||||
TrioManager,
|
||||
@ -134,11 +129,7 @@ async def test_trio_service_lifecycle_run_and_exception():
|
||||
manager = TrioManager(service)
|
||||
|
||||
async def do_service_run():
|
||||
with RaisesGroup(
|
||||
Matcher(RuntimeError, match="Service throwing error"),
|
||||
allow_unwrapped=True,
|
||||
flatten_subgroups=True,
|
||||
):
|
||||
with pytest.raises(ExceptionGroup):
|
||||
await manager.run()
|
||||
|
||||
await do_service_lifecycle_check(
|
||||
@ -165,11 +156,7 @@ async def test_trio_service_lifecycle_run_and_task_exception():
|
||||
manager = TrioManager(service)
|
||||
|
||||
async def do_service_run():
|
||||
with RaisesGroup(
|
||||
Matcher(RuntimeError, match="Service throwing error"),
|
||||
allow_unwrapped=True,
|
||||
flatten_subgroups=True,
|
||||
):
|
||||
with pytest.raises(ExceptionGroup):
|
||||
await manager.run()
|
||||
|
||||
await do_service_lifecycle_check(
|
||||
@ -230,11 +217,7 @@ async def test_trio_service_lifecycle_run_and_daemon_task_exit():
|
||||
manager = TrioManager(service)
|
||||
|
||||
async def do_service_run():
|
||||
with RaisesGroup(
|
||||
Matcher(DaemonTaskExit, match="Daemon task"),
|
||||
allow_unwrapped=True,
|
||||
flatten_subgroups=True,
|
||||
):
|
||||
with pytest.raises(ExceptionGroup):
|
||||
await manager.run()
|
||||
|
||||
await do_service_lifecycle_check(
|
||||
@ -395,11 +378,7 @@ async def test_trio_service_manager_run_task_reraises_exceptions():
|
||||
with trio.fail_after(1):
|
||||
await trio.sleep_forever()
|
||||
|
||||
with RaisesGroup(
|
||||
Matcher(Exception, match="task exception in run_task"),
|
||||
allow_unwrapped=True,
|
||||
flatten_subgroups=True,
|
||||
):
|
||||
with pytest.raises(ExceptionGroup):
|
||||
async with background_trio_service(RunTaskService()):
|
||||
task_event.set()
|
||||
with trio.fail_after(1):
|
||||
@ -419,13 +398,7 @@ async def test_trio_service_manager_run_daemon_task_cancels_if_exits():
|
||||
with trio.fail_after(1):
|
||||
await trio.sleep_forever()
|
||||
|
||||
with RaisesGroup(
|
||||
Matcher(
|
||||
DaemonTaskExit, match=r"Daemon task daemon_task_fn\[daemon=True\] exited"
|
||||
),
|
||||
allow_unwrapped=True,
|
||||
flatten_subgroups=True,
|
||||
):
|
||||
with pytest.raises(ExceptionGroup):
|
||||
async with background_trio_service(RunTaskService()):
|
||||
task_event.set()
|
||||
with trio.fail_after(1):
|
||||
@ -443,11 +416,7 @@ async def test_trio_service_manager_propogates_and_records_exceptions():
|
||||
|
||||
assert manager.did_error is False
|
||||
|
||||
with RaisesGroup(
|
||||
Matcher(RuntimeError, match="this is the error"),
|
||||
allow_unwrapped=True,
|
||||
flatten_subgroups=True,
|
||||
):
|
||||
with pytest.raises(ExceptionGroup):
|
||||
await manager.run()
|
||||
|
||||
assert manager.did_error is True
|
||||
@ -641,7 +610,7 @@ async def test_trio_service_with_try_finally_cleanup_with_shielded_await():
|
||||
ready_cancel.set()
|
||||
await self.manager.wait_finished()
|
||||
finally:
|
||||
with trio.CancelScope(shield=True):
|
||||
with trio.CancelScope(shield=True): # type: ignore[call-arg]
|
||||
await trio.lowlevel.checkpoint()
|
||||
self.cleanup_up = True
|
||||
|
||||
@ -660,7 +629,7 @@ async def test_error_in_service_run():
|
||||
self.manager.run_daemon_task(self.manager.wait_finished)
|
||||
raise ValueError("Exception inside run()")
|
||||
|
||||
with RaisesGroup(ValueError, allow_unwrapped=True, flatten_subgroups=True):
|
||||
with pytest.raises(ExceptionGroup):
|
||||
await TrioManager.run_service(ServiceTest())
|
||||
|
||||
|
||||
@ -679,5 +648,5 @@ async def test_daemon_task_finishes_leaving_children():
|
||||
async def run(self):
|
||||
self.manager.run_daemon_task(self.buggy_daemon)
|
||||
|
||||
with RaisesGroup(DaemonTaskExit, allow_unwrapped=True, flatten_subgroups=True):
|
||||
with pytest.raises(ExceptionGroup):
|
||||
await TrioManager.run_service(ServiceTest())
|
||||
|
||||
@ -1,9 +1,15 @@
|
||||
# Copied from https://github.com/ethereum/async-service
|
||||
import sys
|
||||
|
||||
import pytest
|
||||
import trio
|
||||
from trio.testing import (
|
||||
RaisesGroup,
|
||||
)
|
||||
|
||||
if sys.version_info >= (3, 11):
|
||||
from builtins import (
|
||||
ExceptionGroup,
|
||||
)
|
||||
else:
|
||||
from exceptiongroup import ExceptionGroup
|
||||
|
||||
from libp2p.tools.async_service import (
|
||||
LifecycleError,
|
||||
@ -50,7 +56,7 @@ async def test_trio_service_external_api_raises_when_cancelled():
|
||||
service = ExternalAPIService()
|
||||
|
||||
async with background_trio_service(service) as manager:
|
||||
with RaisesGroup(LifecycleError, allow_unwrapped=True, flatten_subgroups=True):
|
||||
with pytest.raises(ExceptionGroup):
|
||||
async with trio.open_nursery() as nursery:
|
||||
# an event to ensure that we are indeed within the body of the
|
||||
is_within_fn = trio.Event()
|
||||
|
||||
@ -3,8 +3,8 @@ import trio
|
||||
|
||||
from libp2p.tools.async_service import (
|
||||
Service,
|
||||
background_trio_service,
|
||||
)
|
||||
from libp2p.tools.async_service.trio_service import TrioManager
|
||||
|
||||
|
||||
@pytest.mark.trio
|
||||
@ -33,24 +33,31 @@ async def test_trio_manager_stats():
|
||||
self.manager.run_task(trio.lowlevel.checkpoint)
|
||||
|
||||
service = StatsTest()
|
||||
async with background_trio_service(service) as manager:
|
||||
service.run_external_root()
|
||||
assert len(manager._root_tasks) == 2
|
||||
with trio.fail_after(1):
|
||||
await ready.wait()
|
||||
async with trio.open_nursery() as nursery:
|
||||
manager = TrioManager(service)
|
||||
nursery.start_soon(manager.run)
|
||||
await manager.wait_started()
|
||||
|
||||
# we need to yield to the event loop a few times to allow the various
|
||||
# tasks to schedule themselves and get running.
|
||||
for _ in range(50):
|
||||
await trio.lowlevel.checkpoint()
|
||||
try:
|
||||
service.run_external_root()
|
||||
assert len(manager._root_tasks) == 2
|
||||
with trio.fail_after(1):
|
||||
await ready.wait()
|
||||
|
||||
assert manager.stats.tasks.total_count == 10
|
||||
assert manager.stats.tasks.finished_count == 3
|
||||
assert manager.stats.tasks.pending_count == 7
|
||||
# we need to yield to the event loop a few times to allow the various
|
||||
# tasks to schedule themselves and get running.
|
||||
for _ in range(50):
|
||||
await trio.lowlevel.checkpoint()
|
||||
|
||||
# This is a simple test to ensure that finished tasks are removed from
|
||||
# tracking to prevent unbounded memory growth.
|
||||
assert len(manager._root_tasks) == 1
|
||||
assert manager.stats.tasks.total_count == 10
|
||||
assert manager.stats.tasks.finished_count == 3
|
||||
assert manager.stats.tasks.pending_count == 7
|
||||
|
||||
# This is a simple test to ensure that finished tasks are removed from
|
||||
# tracking to prevent unbounded memory growth.
|
||||
assert len(manager._root_tasks) == 1
|
||||
finally:
|
||||
await manager.stop()
|
||||
|
||||
# now check after exiting
|
||||
assert manager.stats.tasks.total_count == 10
|
||||
@ -67,18 +74,26 @@ async def test_trio_manager_stats_does_not_count_main_run_method():
|
||||
self.manager.run_task(trio.sleep_forever)
|
||||
ready.set()
|
||||
|
||||
async with background_trio_service(StatsTest()) as manager:
|
||||
with trio.fail_after(1):
|
||||
await ready.wait()
|
||||
service = StatsTest()
|
||||
async with trio.open_nursery() as nursery:
|
||||
manager = TrioManager(service)
|
||||
nursery.start_soon(manager.run)
|
||||
await manager.wait_started()
|
||||
|
||||
# we need to yield to the event loop a few times to allow the various
|
||||
# tasks to schedule themselves and get running.
|
||||
for _ in range(10):
|
||||
await trio.lowlevel.checkpoint()
|
||||
try:
|
||||
with trio.fail_after(1):
|
||||
await ready.wait()
|
||||
|
||||
assert manager.stats.tasks.total_count == 1
|
||||
assert manager.stats.tasks.finished_count == 0
|
||||
assert manager.stats.tasks.pending_count == 1
|
||||
# we need to yield to the event loop a few times to allow the various
|
||||
# tasks to schedule themselves and get running.
|
||||
for _ in range(10):
|
||||
await trio.lowlevel.checkpoint()
|
||||
|
||||
assert manager.stats.tasks.total_count == 1
|
||||
assert manager.stats.tasks.finished_count == 0
|
||||
assert manager.stats.tasks.pending_count == 1
|
||||
finally:
|
||||
await manager.stop()
|
||||
|
||||
# now check after exiting
|
||||
assert manager.stats.tasks.total_count == 1
|
||||
|
||||
Reference in New Issue
Block a user