From fcd453fbdc15c7bd070e0eb6874eca62343e26a6 Mon Sep 17 00:00:00 2001 From: Lokman Rahmani Date: Tue, 14 Jul 2020 10:34:21 +0100 Subject: [PATCH 1/6] Increase p2p_libp2p connection code coverage to 100%: with 29/351 statement excluded --- .../connections/p2p_libp2p/connection.py | 49 ++-- .../connections/p2p_libp2p/connection.yaml | 2 +- packages/hashes.csv | 2 +- tests/conftest.py | 6 +- .../test_p2p_libp2p/test_communication.py | 9 + .../test_p2p_libp2p/test_errors.py | 220 ++++++++++++++++++ 6 files changed, 257 insertions(+), 31 deletions(-) create mode 100644 tests/test_packages/test_connections/test_p2p_libp2p/test_errors.py diff --git a/packages/fetchai/connections/p2p_libp2p/connection.py b/packages/fetchai/connections/p2p_libp2p/connection.py index 6dc31f155e..d1f52cff77 100644 --- a/packages/fetchai/connections/p2p_libp2p/connection.py +++ b/packages/fetchai/connections/p2p_libp2p/connection.py @@ -25,6 +25,7 @@ import logging import os import shutil +from shutil import ExecError import struct import subprocess # nosec import tempfile @@ -62,7 +63,10 @@ async def _golang_module_build_async( - path: str, log_file_desc: IO[str], loop: Optional[asyncio.AbstractEventLoop] = None + path: str, + log_file_desc: IO[str], + loop: Optional[asyncio.AbstractEventLoop] = None, + timeout: Optional[float] = LIBP2P_NODE_DEPS_DOWNLOAD_TIMEOUT, ) -> int: """ Builds go module located at `path`, downloads necessary dependencies @@ -72,7 +76,7 @@ async def _golang_module_build_async( cmd = ["go", "build"] env = os.environ - + proc = AwaitableProc( cmd, env=env, cwd=path, stdout=log_file_desc, stderr=log_file_desc, shell=False, ) @@ -80,9 +84,7 @@ async def _golang_module_build_async( golang_build = asyncio.ensure_future(proc.start()) try: - returncode = await asyncio.wait_for( - golang_build, LIBP2P_NODE_DEPS_DOWNLOAD_TIMEOUT - ) + returncode = await asyncio.wait_for(golang_build, timeout) except asyncio.TimeoutError: e = Exception( "Failed to download libp2p dependencies within timeout({})".format( @@ -126,7 +128,6 @@ def _golang_module_run( return proc - class AwaitableProc: """ Async-friendly subprocess.Popen @@ -186,7 +187,7 @@ def __init__( def __str__(self): return "{}:{}".format(self._host, self._port) - def __repr__(self): + def __repr__(self): # pragma: no cover return self.__str__() @property @@ -293,10 +294,6 @@ async def start(self) -> None: :return: None """ - which = shutil.which("go") - if which is None: - raise Exception("Libp2p Go should me installed") - if self._loop is None: self._loop = asyncio.get_event_loop() @@ -325,9 +322,9 @@ async def start(self) -> None: out_path = self.aea_to_libp2p_path logger.debug("Creating pipes ({}, {})...".format(in_path, out_path)) if os.path.exists(in_path): - os.remove(in_path) + os.remove(in_path) # pragma: no cover if os.path.exists(out_path): - os.remove(out_path) + os.remove(out_path) # pragma: no cover os.mkfifo(in_path) os.mkfifo(out_path) @@ -407,7 +404,7 @@ async def _connect(self) -> None: await self._connect() return else: - raise e + raise e # pragma: no cover # setup reader assert ( @@ -450,11 +447,11 @@ async def read(self) -> Optional[bytes]: try: logger.debug("Waiting for messages...") buf = await self._stream_reader.readexactly(4) - if not buf: + if not buf: # pragma: no cover return None size = struct.unpack("!I", buf)[0] data = await self._stream_reader.readexactly(size) - if not data: + if not data: # pragma: no cover return None return data except asyncio.streams.IncompleteReadError as e: @@ -510,7 +507,7 @@ def stop(self) -> None: ) self.proc.wait() else: - logger.debug("Called stop when process not set!") + logger.debug("Called stop when process not set!") # pragma: no cover if os.path.exists(LIBP2P_NODE_ENV_FILE): os.remove(LIBP2P_NODE_ENV_FILE) @@ -545,7 +542,7 @@ def __init__(self, **kwargs): if ( self.has_crypto_store and self.crypto_store.crypto_objects.get("fetchai", None) is not None - ): + ): # pragma: no cover key = cast(FetchAICrypto, self.crypto_store.crypto_objects["fetchai"]) elif libp2p_key_file is not None: key = FetchAICrypto(libp2p_key_file) @@ -577,7 +574,7 @@ def __init__(self, **kwargs): raise ValueError( "At least one Entry Peer should be provided when node can not be publically reachable" ) - if delegate_uri is not None: + if delegate_uri is not None: # pragma: no cover logger.warning( "Ignoring Delegate Uri configuration as node can not be publically reachable" ) @@ -611,12 +608,12 @@ def __init__(self, **kwargs): self._receive_from_node_task = None # type: Optional[asyncio.Future] @property - def libp2p_address(self) -> str: + def libp2p_address(self) -> str: # pragma: no cover """The address used by the node.""" return self.node.pub @property - def libp2p_address_id(self) -> str: + def libp2p_address_id(self) -> str: # pragma: no cover """The identifier for the address.""" return LIBP2P @@ -626,7 +623,7 @@ async def connect(self) -> None: :return: None """ - if self.connection_status.is_connected: + if self.connection_status.is_connected: # pragma: no cover return try: # start libp2p node @@ -683,7 +680,7 @@ async def receive(self, *args, **kwargs) -> Optional["Envelope"]: # TOFIX(LR) attempt restarting the node? logger.debug("Received data: {}".format(data)) return Envelope.decode(data) - except CancelledError: + except CancelledError: # pragma: no cover logger.debug("Receive cancelled.") return None except Exception as e: # pragma: nocover # pylint: disable=broad-except @@ -706,17 +703,17 @@ async def _receive_from_node(self) -> None: """ while True: data = await self.node.read() - if data is None: - break assert self._in_queue is not None, "Input queue not initialized." self._in_queue.put_nowait(data) + if data is None: + break @staticmethod def _check_go_installed() -> None: """Checks if go is installed. Sys.exits if not""" res = shutil.which("go") if res is None: - raise AEAException( + raise AEAException( # pragma: nocover "Please install go before running the `fetchai/p2p_libp2p:0.1.0` connection. " "Go is available for download here: https://golang.org/doc/install" ) diff --git a/packages/fetchai/connections/p2p_libp2p/connection.yaml b/packages/fetchai/connections/p2p_libp2p/connection.yaml index 945bbde0ff..0cc09f54fe 100644 --- a/packages/fetchai/connections/p2p_libp2p/connection.yaml +++ b/packages/fetchai/connections/p2p_libp2p/connection.yaml @@ -11,7 +11,7 @@ fingerprint: aea/api.go: QmW5fUpVZmV3pxgoakm3RvsvCGC6FwT2XprcqXHM8rBXP5 aea/envelope.pb.go: QmRfUNGpCeVJfsW3H1MzCN4pwDWgumfyWufVFp6xvUjjug aea/envelope.proto: QmSC8EGCKiNFR2vf5bSWymSzYDFMipQW9aQVMwPzQoKb4n - connection.py: QmUJ3Bsg9ungBjgzyCTNwxhesJnpw4gB8Tz8y3Bg4w7og9 + connection.py: QmeCLqLZWz3R7NQdNsQj4upHT8s5fA46uA57EjUXsFbev8 dht/dhtclient/dhtclient.go: QmNnU1pVCUtj8zJ1Pz5eMk9sznsjPFSJ9qDkzbrNwzEecV dht/dhtclient/dhtclient_test.go: QmPfnHSHXtbaW5VYuq1QsKQWey64pUEvLEaKKkT9eAcmws dht/dhtclient/options.go: QmPorj38wNrxGrzsbFe5wwLmiHzxbTJ2VsgvSd8tLDYS8s diff --git a/packages/hashes.csv b/packages/hashes.csv index 80aadbbe4a..913735f5b7 100644 --- a/packages/hashes.csv +++ b/packages/hashes.csv @@ -25,7 +25,7 @@ fetchai/connections/ledger,QmezMgaJkk9wbQ4nzURERnNJdrzkQyvV5PiieH6uGbVzc3 fetchai/connections/local,QmVcTEJxGbWbtXi2fLN5eJA6XuEAneaNd83UJPugrtb9xU fetchai/connections/oef,QmfX6fF2CqruwVc46Tqogb7SyyLEQa2t5J6SpN5wkj2tQw fetchai/connections/p2p_client,QmbwCDuAB1eq6JikqeAAqpqjVhxevGNeWCLqRD67Uvqiaz -fetchai/connections/p2p_libp2p,QmZpWkEKYUJ9kcf1fVuZp7uPhRTc1fL3cbNPTNpqwnPz7e +fetchai/connections/p2p_libp2p,QmZeKK72PMs4K9cns7L8gcMFm2Ts4HSHDdYD8B5Lq4DkHT fetchai/connections/p2p_libp2p_client,QmVhsh863k3ws4HeDpkZm7GQkrW3aMREu5sLkHATmwCddC fetchai/connections/p2p_stub,QmSBRr26YELdbYk9nAurw3XdQ3Myj7cVgCDZZMv7DMrsdg fetchai/connections/scaffold,QmTzEeEydjohZNTsAJnoGMtzTgCyzMBQCYgbTBLfqWtw5w diff --git a/tests/conftest.py b/tests/conftest.py index 32fce2e6f4..23cf5d1b5f 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -732,7 +732,7 @@ def _make_libp2p_connection( identity = Identity("", address=FetchAICrypto().address) if relay and delegate: configuration = ConnectionConfig( - libp2p_key_file=None, + node_key_file=None, local_uri="{}:{}".format(host, port), public_uri="{}:{}".format(host, port), entry_peers=entry_peers, @@ -742,7 +742,7 @@ def _make_libp2p_connection( ) elif relay and not delegate: configuration = ConnectionConfig( - libp2p_key_file=None, + node_key_file=None, local_uri="{}:{}".format(host, port), public_uri="{}:{}".format(host, port), entry_peers=entry_peers, @@ -751,7 +751,7 @@ def _make_libp2p_connection( ) else: configuration = ConnectionConfig( - libp2p_key_file=None, + node_key_file=None, local_uri="{}:{}".format(host, port), entry_peers=entry_peers, log_file=log_file, diff --git a/tests/test_packages/test_connections/test_p2p_libp2p/test_communication.py b/tests/test_packages/test_connections/test_p2p_libp2p/test_communication.py index 1058d89bff..376ef99293 100644 --- a/tests/test_packages/test_connections/test_p2p_libp2p/test_communication.py +++ b/tests/test_packages/test_connections/test_p2p_libp2p/test_communication.py @@ -29,6 +29,8 @@ from aea.multiplexer import Multiplexer from aea.protocols.default.message import DefaultMessage +from packages.fetchai.connections.p2p_libp2p.connection import Uri + from tests.conftest import ( _make_libp2p_connection, libp2p_log_on_failure, @@ -508,3 +510,10 @@ def teardown_class(cls): shutil.rmtree(cls.t) except (OSError, IOError): pass + + +@skip_test_windows +def test_libp2pconnection_uri(): + uri = Uri(host="127.0.0.1") + uri = Uri(host="127.0.0.1", port=10000) + assert uri.host == "127.0.0.1" and uri.port == 10000 diff --git a/tests/test_packages/test_connections/test_p2p_libp2p/test_errors.py b/tests/test_packages/test_connections/test_p2p_libp2p/test_errors.py new file mode 100644 index 0000000000..601e927c1c --- /dev/null +++ b/tests/test_packages/test_connections/test_p2p_libp2p/test_errors.py @@ -0,0 +1,220 @@ +# -*- coding: utf-8 -*- +# ------------------------------------------------------------------------------ +# +# Copyright 2018-2019 Fetch.AI Limited +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# ------------------------------------------------------------------------------ + +"""This test module contains Negative tests for Libp2p connection.""" + +from os import close +from aea.configurations.base import ConnectionConfig +from aea.crypto.fetchai import FetchAICrypto +from aea.identity.base import Identity +import asyncio +import os +import shutil +import tempfile +import time + +import pytest + +from aea.multiplexer import Multiplexer + +from packages.fetchai.connections.p2p_libp2p.connection import ( + P2PLibp2pConnection, _golang_module_build_async, + _golang_module_run, + LIBP2P_NODE_MODULE_NAME, + AwaitableProc, +) + +from tests.conftest import ( + _make_libp2p_connection, + skip_test_windows, +) + +DEFAULT_PORT = 10234 +DEFAULT_NET_SIZE = 4 + + +@skip_test_windows +@pytest.mark.asyncio +class TestP2PLibp2pConnectionFailureGolangBuild: + """Test that golang build async fails if timeout exceeded or wrong path""" + + @classmethod + def setup_class(cls): + """Set the test up""" + cls.cwd = os.getcwd() + cls.t = tempfile.mkdtemp() + os.chdir(cls.t) + + cls.connection = _make_libp2p_connection() + cls.wrong_path = tempfile.mkdtemp() + + @pytest.mark.asyncio + async def test_timeout(self): + log_file_desc = open("log", "a", 1) + with pytest.raises(Exception): + await _golang_module_build_async( + self.connection.node.source, log_file_desc, timeout=0 + ) + + @pytest.mark.asyncio + async def test_wrong_path(self): + self.connection.node.source = self.wrong_path + with pytest.raises(Exception): + await self.connection.connect() + + + @classmethod + def teardown_class(cls): + """Tear down the test""" + os.chdir(cls.cwd) + try: + shutil.rmtree(cls.t) + shutil.rmtree(cls.wrong_path) + except (OSError, IOError): + pass + + +@skip_test_windows +class TestP2PLibp2pConnectionFailureGolangRun: + """Test that golang run fails if wrong path or timeout""" + + @classmethod + def setup_class(cls): + """Set the test up""" + cls.cwd = os.getcwd() + cls.t = tempfile.mkdtemp() + os.chdir(cls.t) + + cls.connection = _make_libp2p_connection() + cls.wrong_path = tempfile.mkdtemp() + + def test_wrong_path(self): + log_file_desc = open("log", "a", 1) + with pytest.raises(Exception): + _golang_module_run( + self.wrong_path, LIBP2P_NODE_MODULE_NAME, [], log_file_desc + ) + + def test_timeout(self): + self.connection.node._connection_timeout = 0 + self.connection.node._connection_attempts = 2 + with pytest.raises(Exception): + muxer = Multiplexer([self.connection]) + muxer.connect() + + + @classmethod + def teardown_class(cls): + """Tear down the test""" + os.chdir(cls.cwd) + try: + shutil.rmtree(cls.t) + shutil.rmtree(cls.wrong_path) + except (OSError, IOError): + pass + +@skip_test_windows +class TestP2PLibp2pConnectionFailureNodeDisconnect: + """Test that connection handles node disconnecting properly""" + + @classmethod + def setup_class(cls): + """Set the test up""" + cls.cwd = os.getcwd() + cls.t = tempfile.mkdtemp() + os.chdir(cls.t) + + cls.connection = _make_libp2p_connection() + + def test_node_disconnect(self): + muxer = Multiplexer([self.connection]) + muxer.connect() + self.connection.node.proc.terminate() + self.connection.node.proc.wait() + muxer.disconnect() + + @classmethod + def teardown_class(cls): + """Tear down the test""" + os.chdir(cls.cwd) + try: + shutil.rmtree(cls.t) + except (OSError, IOError): + pass + +@skip_test_windows +class TestP2PLibp2pConnectionFailureSetupNewConnection: + """Test that connection constructor ensures proper configuration""" + + @classmethod + def setup_class(cls): + """Set the test up""" + cls.cwd = os.getcwd() + cls.t = tempfile.mkdtemp() + os.chdir(cls.t) + + cls.identity = Identity("", address=FetchAICrypto().address) + cls.host = "localhost" + cls.port = "10000" + + cls.key_file = os.path.join(cls.t, "keyfile") + key_file_desc = open(cls.key_file, "ab") + FetchAICrypto().dump(key_file_desc) + key_file_desc.close() + + def test_entry_peers_when_no_public_uri_provided(self): + configuration = ConnectionConfig( + libp2p_key_file=None, + local_uri="{}:{}".format(self.host, self.port), + delegate_uri="{}:{}".format(self.host, self.port), + entry_peers=None, + log_file=None, + connection_id=P2PLibp2pConnection.connection_id, + ) + with pytest.raises(ValueError): + P2PLibp2pConnection(configuration=configuration, identity=self.identity) + + def test_local_uri_provided_when_public_uri_provided(self): + + configuration = ConnectionConfig( + node_key_file=self.key_file, + public_uri="{}:{}".format(self.host, self.port), + entry_peers=None, + log_file=None, + connection_id=P2PLibp2pConnection.connection_id, + ) + with pytest.raises(ValueError): + P2PLibp2pConnection(configuration=configuration, identity=self.identity) + + @classmethod + def teardown_class(cls): + """Tear down the test""" + os.chdir(cls.cwd) + try: + shutil.rmtree(cls.t) + except (OSError, IOError): + pass + + +@skip_test_windows +def test_libp2pconnection_awaitable_proc_cancelled(): + proc = AwaitableProc(["sleep", "100"], shell=False) + proc_task = asyncio.ensure_future(proc.start()) + proc_task.cancel() + From 80af5026a2a35bd9702139e184f5aebfc0b4f805 Mon Sep 17 00:00:00 2001 From: Lokman Rahmani Date: Tue, 14 Jul 2020 13:21:52 +0100 Subject: [PATCH 2/6] Address ci comments --- .../connections/p2p_libp2p/connection.py | 8 +++--- .../test_p2p_libp2p/test_errors.py | 28 +++++++++---------- 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/packages/fetchai/connections/p2p_libp2p/connection.py b/packages/fetchai/connections/p2p_libp2p/connection.py index 0c6879bd86..f0d4655cce 100644 --- a/packages/fetchai/connections/p2p_libp2p/connection.py +++ b/packages/fetchai/connections/p2p_libp2p/connection.py @@ -25,7 +25,6 @@ import logging import os import shutil -from shutil import ExecError import struct import subprocess # nosec import tempfile @@ -76,7 +75,7 @@ async def _golang_module_build_async( cmd = ["go", "build"] env = os.environ - + proc = AwaitableProc( cmd, env=env, cwd=path, stdout=log_file_desc, stderr=log_file_desc, shell=False, ) @@ -128,6 +127,7 @@ def _golang_module_run( return proc + class AwaitableProc: """ Async-friendly subprocess.Popen @@ -322,9 +322,9 @@ async def start(self) -> None: out_path = self.aea_to_libp2p_path logger.debug("Creating pipes ({}, {})...".format(in_path, out_path)) if os.path.exists(in_path): - os.remove(in_path) # pragma: no cover + os.remove(in_path) # pragma: no cover if os.path.exists(out_path): - os.remove(out_path) # pragma: no cover + os.remove(out_path) # pragma: no cover os.mkfifo(in_path) os.mkfifo(out_path) diff --git a/tests/test_packages/test_connections/test_p2p_libp2p/test_errors.py b/tests/test_packages/test_connections/test_p2p_libp2p/test_errors.py index 601e927c1c..aa20f03980 100644 --- a/tests/test_packages/test_connections/test_p2p_libp2p/test_errors.py +++ b/tests/test_packages/test_connections/test_p2p_libp2p/test_errors.py @@ -19,25 +19,24 @@ """This test module contains Negative tests for Libp2p connection.""" -from os import close -from aea.configurations.base import ConnectionConfig -from aea.crypto.fetchai import FetchAICrypto -from aea.identity.base import Identity import asyncio import os import shutil import tempfile -import time import pytest +from aea.configurations.base import ConnectionConfig +from aea.crypto.fetchai import FetchAICrypto +from aea.identity.base import Identity from aea.multiplexer import Multiplexer from packages.fetchai.connections.p2p_libp2p.connection import ( - P2PLibp2pConnection, _golang_module_build_async, - _golang_module_run, - LIBP2P_NODE_MODULE_NAME, AwaitableProc, + LIBP2P_NODE_MODULE_NAME, + P2PLibp2pConnection, + _golang_module_build_async, + _golang_module_run, ) from tests.conftest import ( @@ -71,13 +70,12 @@ async def test_timeout(self): await _golang_module_build_async( self.connection.node.source, log_file_desc, timeout=0 ) - + @pytest.mark.asyncio async def test_wrong_path(self): self.connection.node.source = self.wrong_path with pytest.raises(Exception): await self.connection.connect() - @classmethod def teardown_class(cls): @@ -118,7 +116,6 @@ def test_timeout(self): muxer = Multiplexer([self.connection]) muxer.connect() - @classmethod def teardown_class(cls): """Tear down the test""" @@ -129,6 +126,7 @@ def teardown_class(cls): except (OSError, IOError): pass + @skip_test_windows class TestP2PLibp2pConnectionFailureNodeDisconnect: """Test that connection handles node disconnecting properly""" @@ -158,6 +156,7 @@ def teardown_class(cls): except (OSError, IOError): pass + @skip_test_windows class TestP2PLibp2pConnectionFailureSetupNewConnection: """Test that connection constructor ensures proper configuration""" @@ -168,11 +167,11 @@ def setup_class(cls): cls.cwd = os.getcwd() cls.t = tempfile.mkdtemp() os.chdir(cls.t) - + cls.identity = Identity("", address=FetchAICrypto().address) cls.host = "localhost" cls.port = "10000" - + cls.key_file = os.path.join(cls.t, "keyfile") key_file_desc = open(cls.key_file, "ab") FetchAICrypto().dump(key_file_desc) @@ -191,7 +190,7 @@ def test_entry_peers_when_no_public_uri_provided(self): P2PLibp2pConnection(configuration=configuration, identity=self.identity) def test_local_uri_provided_when_public_uri_provided(self): - + configuration = ConnectionConfig( node_key_file=self.key_file, public_uri="{}:{}".format(self.host, self.port), @@ -217,4 +216,3 @@ def test_libp2pconnection_awaitable_proc_cancelled(): proc = AwaitableProc(["sleep", "100"], shell=False) proc_task = asyncio.ensure_future(proc.start()) proc_task.cancel() - From 19eefd6e6088aff2b51b9812c1d43b2186104307 Mon Sep 17 00:00:00 2001 From: Lokman Rahmani Date: Tue, 14 Jul 2020 13:30:57 +0100 Subject: [PATCH 3/6] Update fingerprints --- packages/fetchai/connections/p2p_libp2p/connection.yaml | 2 +- packages/hashes.csv | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/fetchai/connections/p2p_libp2p/connection.yaml b/packages/fetchai/connections/p2p_libp2p/connection.yaml index 1d6f073a97..6633129498 100644 --- a/packages/fetchai/connections/p2p_libp2p/connection.yaml +++ b/packages/fetchai/connections/p2p_libp2p/connection.yaml @@ -11,7 +11,7 @@ fingerprint: aea/api.go: QmW5fUpVZmV3pxgoakm3RvsvCGC6FwT2XprcqXHM8rBXP5 aea/envelope.pb.go: QmRfUNGpCeVJfsW3H1MzCN4pwDWgumfyWufVFp6xvUjjug aea/envelope.proto: QmSC8EGCKiNFR2vf5bSWymSzYDFMipQW9aQVMwPzQoKb4n - connection.py: QmZWMbvSDmL5AqPU5hKfgg8NcfrEx1Sgne4FAHQJoem8dr + connection.py: Qma9S5jWdTzuz93Y7WQsABai9xPGchfvnWoz2u5RRXwYU9 dht/dhtclient/dhtclient.go: QmNnU1pVCUtj8zJ1Pz5eMk9sznsjPFSJ9qDkzbrNwzEecV dht/dhtclient/dhtclient_test.go: QmPfnHSHXtbaW5VYuq1QsKQWey64pUEvLEaKKkT9eAcmws dht/dhtclient/options.go: QmPorj38wNrxGrzsbFe5wwLmiHzxbTJ2VsgvSd8tLDYS8s diff --git a/packages/hashes.csv b/packages/hashes.csv index 11ec415990..316c261960 100644 --- a/packages/hashes.csv +++ b/packages/hashes.csv @@ -25,7 +25,7 @@ fetchai/connections/ledger,QmVXceMJCioA1Hro9aJgBwrF9yLgToaVXifDz6EVo6vTXn fetchai/connections/local,QmVcTEJxGbWbtXi2fLN5eJA6XuEAneaNd83UJPugrtb9xU fetchai/connections/oef,QmdkQ9hUbJ8HsJD5qxSPRae9s2G9LZXFhfJabeHBVVYMJi fetchai/connections/p2p_client,QmbwCDuAB1eq6JikqeAAqpqjVhxevGNeWCLqRD67Uvqiaz -fetchai/connections/p2p_libp2p,QmbMaCh3NPapo4YnP7BrjifQt5gbCRvo5viJ8bRCF8Z97g +fetchai/connections/p2p_libp2p,QmZM7xR551DCLhF37VemXyk8sRoRhQAS9GXJe3CvT2foi8 fetchai/connections/p2p_libp2p_client,QmVhsh863k3ws4HeDpkZm7GQkrW3aMREu5sLkHATmwCddC fetchai/connections/p2p_stub,QmSBRr26YELdbYk9nAurw3XdQ3Myj7cVgCDZZMv7DMrsdg fetchai/connections/scaffold,QmTzEeEydjohZNTsAJnoGMtzTgCyzMBQCYgbTBLfqWtw5w From 6fc63c78be8a36af989705a84bc79fc3122f3d7e Mon Sep 17 00:00:00 2001 From: Lokman Rahmani Date: Tue, 14 Jul 2020 15:46:21 +0100 Subject: [PATCH 4/6] Increase p2p_libp2p_client connection code coverage to 100%: with 18/145 statement excluded --- .../p2p_libp2p_client/connection.py | 26 +++++++++---------- .../p2p_libp2p_client/connection.yaml | 2 +- packages/hashes.csv | 2 +- tests/conftest.py | 2 +- .../test_communication.py | 9 +++++++ 5 files changed, 25 insertions(+), 16 deletions(-) diff --git a/packages/fetchai/connections/p2p_libp2p_client/connection.py b/packages/fetchai/connections/p2p_libp2p_client/connection.py index 1e612cbcf5..dae9a2f474 100644 --- a/packages/fetchai/connections/p2p_libp2p_client/connection.py +++ b/packages/fetchai/connections/p2p_libp2p_client/connection.py @@ -63,7 +63,7 @@ def __init__( def __str__(self): return "{}:{}".format(self._host, self._port) - def __repr__(self): + def __repr__(self): # pragma: no cover return self.__str__() @property @@ -106,12 +106,12 @@ def __init__(self, **kwargs): if ( self.has_crypto_store and self.crypto_store.crypto_objects.get("fetchai", None) is not None - ): + ): # pragma: no cover key = cast(FetchAICrypto, self.crypto_store.crypto_objects["fetchai"]) - elif key_file is None: - key = FetchAICrypto() - else: + elif key_file is not None: key = FetchAICrypto(key_file) + else: + key = FetchAICrypto() # client connection id self.key = key @@ -144,7 +144,7 @@ async def connect(self) -> None: :return: None """ - if self.connection_status.is_connected: + if self.connection_status.is_connected: # pragma: no cover return if self._loop is None: self._loop = asyncio.get_event_loop() @@ -211,7 +211,7 @@ async def disconnect(self) -> None: if self._in_queue is not None: self._in_queue.put_nowait(None) - else: + else: # pragma: no cover logger.debug("Called disconnect when input queue not initialized.") async def receive(self, *args, **kwargs) -> Optional["Envelope"]: @@ -234,10 +234,10 @@ async def receive(self, *args, **kwargs) -> Optional["Envelope"]: # TOFIX(LR) attempt restarting the node? logger.debug("Received data: {}".format(data)) return Envelope.decode(data) - except CancelledError: + except CancelledError: # pragma: no cover logger.debug("Receive cancelled.") return None - except Exception as e: # pragma: nocover # pylint: disable=broad-except + except Exception as e: # pragma: no cover # pylint: disable=broad-except logger.exception(e) return None @@ -257,10 +257,10 @@ async def _process_messages(self) -> None: """ while True: data = await self._receive() - if data is None: - break assert self._in_queue is not None, "Input queue not initialized." self._in_queue.put_nowait(data) + if data is None: + break async def _send(self, data: bytes) -> None: assert self._writer is not None @@ -274,11 +274,11 @@ async def _receive(self) -> Optional[bytes]: try: logger.debug("Waiting for messages...") buf = await self._reader.readexactly(4) - if not buf: + if not buf: # pragma: no cover return None size = struct.unpack("!I", buf)[0] data = await self._reader.readexactly(size) - if not data: + if not data: # pragma: no cover return None return data except asyncio.streams.IncompleteReadError as e: diff --git a/packages/fetchai/connections/p2p_libp2p_client/connection.yaml b/packages/fetchai/connections/p2p_libp2p_client/connection.yaml index 3b4ea52a50..e392759bbd 100644 --- a/packages/fetchai/connections/p2p_libp2p_client/connection.yaml +++ b/packages/fetchai/connections/p2p_libp2p_client/connection.yaml @@ -8,7 +8,7 @@ license: Apache-2.0 aea_version: '>=0.5.0, <0.6.0' fingerprint: __init__.py: QmT1FEHkPGMHV5oiVEfQHHr25N2qdZxydSNRJabJvYiTgf - connection.py: QmT9ncNDy27GXAqtmJJDFQep2M8Qn7ycih7E8tMT2PwS3i + connection.py: QmZfEw3G2LXEivmGu9UodJwhptcRCz3BYkRGfepuRfhGWU fingerprint_ignore_patterns: [] protocols: [] class_name: P2PLibp2pClientConnection diff --git a/packages/hashes.csv b/packages/hashes.csv index 316c261960..4f10c3c48d 100644 --- a/packages/hashes.csv +++ b/packages/hashes.csv @@ -26,7 +26,7 @@ fetchai/connections/local,QmVcTEJxGbWbtXi2fLN5eJA6XuEAneaNd83UJPugrtb9xU fetchai/connections/oef,QmdkQ9hUbJ8HsJD5qxSPRae9s2G9LZXFhfJabeHBVVYMJi fetchai/connections/p2p_client,QmbwCDuAB1eq6JikqeAAqpqjVhxevGNeWCLqRD67Uvqiaz fetchai/connections/p2p_libp2p,QmZM7xR551DCLhF37VemXyk8sRoRhQAS9GXJe3CvT2foi8 -fetchai/connections/p2p_libp2p_client,QmVhsh863k3ws4HeDpkZm7GQkrW3aMREu5sLkHATmwCddC +fetchai/connections/p2p_libp2p_client,QmY4vR6r4XqqWw25Q3bTmPcXMcaVAkAs3RJjEWyVEe81kv fetchai/connections/p2p_stub,QmSBRr26YELdbYk9nAurw3XdQ3Myj7cVgCDZZMv7DMrsdg fetchai/connections/scaffold,QmTzEeEydjohZNTsAJnoGMtzTgCyzMBQCYgbTBLfqWtw5w fetchai/connections/soef,QmcXsmtNeio1CPnZbZFvQLuvfQyZhX5K8mP624cB66DnjF diff --git a/tests/conftest.py b/tests/conftest.py index 23cf5d1b5f..9b357b73c6 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -761,7 +761,7 @@ def _make_libp2p_connection( def _make_libp2p_client_connection( - node_port: int = 11234, node_host: str = "127.0.0.1", + node_port: int = 11234, node_host: str = "127.0.0.1" ) -> P2PLibp2pClientConnection: identity = Identity("", address=FetchAICrypto().address) configuration = ConnectionConfig( diff --git a/tests/test_packages/test_connections/test_p2p_libp2p_client/test_communication.py b/tests/test_packages/test_connections/test_p2p_libp2p_client/test_communication.py index 3d6bf032b0..4e2ec63b29 100644 --- a/tests/test_packages/test_connections/test_p2p_libp2p_client/test_communication.py +++ b/tests/test_packages/test_connections/test_p2p_libp2p_client/test_communication.py @@ -30,6 +30,8 @@ from aea.protocols.default.message import DefaultMessage from aea.protocols.default.serialization import DefaultSerializer +from packages.fetchai.connections.p2p_libp2p_client.connection import Uri + from tests.conftest import ( _make_libp2p_client_connection, _make_libp2p_connection, @@ -489,3 +491,10 @@ def teardown_class(cls): shutil.rmtree(cls.t) except (OSError, IOError): pass + + +@skip_test_windows +def test_libp2pclientconnection_uri(): + uri = Uri(host="127.0.0.1") + uri = Uri(host="127.0.0.1", port=10000) + assert uri.host == "127.0.0.1" and uri.port == 10000 From 0975c53c680b15cfce990747582141ffa3cc5f26 Mon Sep 17 00:00:00 2001 From: Lokman Rahmani Date: Tue, 14 Jul 2020 15:57:08 +0100 Subject: [PATCH 5/6] Address reviewers comments --- packages/fetchai/connections/p2p_libp2p/connection.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/fetchai/connections/p2p_libp2p/connection.py b/packages/fetchai/connections/p2p_libp2p/connection.py index f0d4655cce..7923621cc0 100644 --- a/packages/fetchai/connections/p2p_libp2p/connection.py +++ b/packages/fetchai/connections/p2p_libp2p/connection.py @@ -65,7 +65,7 @@ async def _golang_module_build_async( path: str, log_file_desc: IO[str], loop: Optional[asyncio.AbstractEventLoop] = None, - timeout: Optional[float] = LIBP2P_NODE_DEPS_DOWNLOAD_TIMEOUT, + timeout: float = LIBP2P_NODE_DEPS_DOWNLOAD_TIMEOUT, ) -> int: """ Builds go module located at `path`, downloads necessary dependencies From 15b7ecf3cc0e850dc564a3ca243d6d07332506b3 Mon Sep 17 00:00:00 2001 From: Lokman Rahmani Date: Tue, 14 Jul 2020 16:20:52 +0100 Subject: [PATCH 6/6] Update fingerprints --- packages/fetchai/connections/p2p_libp2p/connection.yaml | 2 +- packages/hashes.csv | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/fetchai/connections/p2p_libp2p/connection.yaml b/packages/fetchai/connections/p2p_libp2p/connection.yaml index 6633129498..8372f510d4 100644 --- a/packages/fetchai/connections/p2p_libp2p/connection.yaml +++ b/packages/fetchai/connections/p2p_libp2p/connection.yaml @@ -11,7 +11,7 @@ fingerprint: aea/api.go: QmW5fUpVZmV3pxgoakm3RvsvCGC6FwT2XprcqXHM8rBXP5 aea/envelope.pb.go: QmRfUNGpCeVJfsW3H1MzCN4pwDWgumfyWufVFp6xvUjjug aea/envelope.proto: QmSC8EGCKiNFR2vf5bSWymSzYDFMipQW9aQVMwPzQoKb4n - connection.py: Qma9S5jWdTzuz93Y7WQsABai9xPGchfvnWoz2u5RRXwYU9 + connection.py: QmaGCXNSyuW5MP428XB1FxUt6sAYdsNrh13UWLDFeBoP7b dht/dhtclient/dhtclient.go: QmNnU1pVCUtj8zJ1Pz5eMk9sznsjPFSJ9qDkzbrNwzEecV dht/dhtclient/dhtclient_test.go: QmPfnHSHXtbaW5VYuq1QsKQWey64pUEvLEaKKkT9eAcmws dht/dhtclient/options.go: QmPorj38wNrxGrzsbFe5wwLmiHzxbTJ2VsgvSd8tLDYS8s diff --git a/packages/hashes.csv b/packages/hashes.csv index 4f10c3c48d..8fff14cd65 100644 --- a/packages/hashes.csv +++ b/packages/hashes.csv @@ -25,7 +25,7 @@ fetchai/connections/ledger,QmVXceMJCioA1Hro9aJgBwrF9yLgToaVXifDz6EVo6vTXn fetchai/connections/local,QmVcTEJxGbWbtXi2fLN5eJA6XuEAneaNd83UJPugrtb9xU fetchai/connections/oef,QmdkQ9hUbJ8HsJD5qxSPRae9s2G9LZXFhfJabeHBVVYMJi fetchai/connections/p2p_client,QmbwCDuAB1eq6JikqeAAqpqjVhxevGNeWCLqRD67Uvqiaz -fetchai/connections/p2p_libp2p,QmZM7xR551DCLhF37VemXyk8sRoRhQAS9GXJe3CvT2foi8 +fetchai/connections/p2p_libp2p,QmT5sbnNb4mz3EYbRJBatcGEPht8s2QWGsG98unVSfWuhJ fetchai/connections/p2p_libp2p_client,QmY4vR6r4XqqWw25Q3bTmPcXMcaVAkAs3RJjEWyVEe81kv fetchai/connections/p2p_stub,QmSBRr26YELdbYk9nAurw3XdQ3Myj7cVgCDZZMv7DMrsdg fetchai/connections/scaffold,QmTzEeEydjohZNTsAJnoGMtzTgCyzMBQCYgbTBLfqWtw5w