Peer tests and minor peer features/bug fixes.

This commit is contained in:
Robert Zajac 2018-11-02 16:16:46 -04:00
parent 330ed8363a
commit db6b846251
11 changed files with 221 additions and 28 deletions

View File

@ -8,4 +8,12 @@ py-libp2p requires Python 3.6 and the best way to guarantee a clean Python 3.6 e
virtualenv -p python3.6 venv virtualenv -p python3.6 venv
. venv/bin/activate . venv/bin/activate
pip install -r requirements.txt pip install -r requirements.txt
```
## Testing
After installing our requirements (see above), you can:
```sh
cd tests
pytest
``` ```

View File

@ -13,6 +13,9 @@ class PeerData(IPeerData):
def add_protocols(self, protocols): def add_protocols(self, protocols):
self.protocols.extend(protocols) self.protocols.extend(protocols)
def set_protocols(self, protocols):
self.protocols = protocols
def add_addrs(self, addrs): def add_addrs(self, addrs):
self.addrs.extend(addrs) self.addrs.extend(addrs)

View File

@ -2,48 +2,62 @@ from abc import ABC, abstractmethod
class IPeerData(ABC): class IPeerData(ABC):
""" @abstractmethod
:return: all protocols associated with given peer
"""
def get_protocols(self): def get_protocols(self):
"""
:return: all protocols associated with given peer
"""
pass pass
""" @abstractmethod
:param protocols: protocols to add
"""
def add_protocols(self, protocols): def add_protocols(self, protocols):
"""
:param protocols: protocols to add
"""
pass pass
""" @abstractmethod
:param addrs: multiaddresses to add def set_protocols(self, protocols):
""" """
:param protocols: protocols to add
"""
pass
@abstractmethod
def add_addrs(self, addrs): def add_addrs(self, addrs):
"""
:param addrs: multiaddresses to add
"""
pass pass
""" @abstractmethod
:return: all multiaddresses
"""
def get_addrs(self): def get_addrs(self):
"""
:return: all multiaddresses
"""
pass pass
""" @abstractmethod
Clear all addresses
"""
def clear_addrs(self): def clear_addrs(self):
"""
Clear all addresses
"""
pass pass
""" @abstractmethod
:param key: key in KV pair
:param val: val to associate with key
:raise Exception: unsuccesful put
"""
def put_metadata(self, key, val): def put_metadata(self, key, val):
"""
:param key: key in KV pair
:param val: val to associate with key
:raise Exception: unsuccesful put
"""
pass pass
""" @abstractmethod
:param key: key in KV pair
:return: val for key
:raise Exception: key not found
"""
def get_metadata(self, key): def get_metadata(self, key):
"""
:param key: key in KV pair
:return: val for key
:raise Exception: key not found
"""
pass pass

View File

@ -38,6 +38,10 @@ class PeerStore(IPeerStore):
peer = self.__create_or_get_peer(peer_id) peer = self.__create_or_get_peer(peer_id)
peer.add_protocols(protocols) peer.add_protocols(protocols)
def set_protocols(self, peer_id, protocols):
peer = self.__create_or_get_peer(peer_id)
peer.set_protocols(protocols)
def peers(self): def peers(self):
return self.peer_map.keys() return self.peer_map.keys()
@ -55,7 +59,7 @@ class PeerStore(IPeerStore):
peer.put_metadata(key, val) peer.put_metadata(key, val)
def add_addr(self, peer_id, addr, ttl): def add_addr(self, peer_id, addr, ttl):
self.add_addrs(self, peer_id, [addr]) self.add_addrs(peer_id, [addr], ttl)
def add_addrs(self, peer_id, addrs, ttl): def add_addrs(self, peer_id, addrs, ttl):
# Ignore ttl for now # Ignore ttl for now

View File

@ -2,7 +2,7 @@ from abc import ABC, abstractmethod
from .addrbook_interface import IAddrBook from .addrbook_interface import IAddrBook
from .peermetadata_interface import IPeerMetadata from .peermetadata_interface import IPeerMetadata
class IPeerStore(ABC, IAddrBook, IPeerMetadata): class IPeerStore(IAddrBook, IPeerMetadata):
def __init__(self): def __init__(self):
IPeerMetadata.__init__(self) IPeerMetadata.__init__(self)

0
tests/peer/__init__.py Normal file
View File

View File

@ -0,0 +1,59 @@
import pytest
from peer.peerstore import PeerStoreError
from peer.peerstore import PeerStore
# Testing methods from IAddrBook base class.
def test_addrs_empty():
with pytest.raises(PeerStoreError):
store = PeerStore()
val = store.addrs("peer")
assert not val
def test_add_addr_single():
store = PeerStore()
store.add_addr("peer1", "/foo", 10)
store.add_addr("peer1", "/bar", 10)
store.add_addr("peer2", "/baz", 10)
assert store.addrs("peer1") == ["/foo", "/bar"]
assert store.addrs("peer2") == ["/baz"]
def test_add_addrs_multiple():
store = PeerStore()
store.add_addrs("peer1", ["/foo1", "/bar1"], 10)
store.add_addrs("peer2", ["/foo2"], 10)
assert store.addrs("peer1") == ["/foo1", "/bar1"]
assert store.addrs("peer2") == ["/foo2"]
def test_clear_addrs():
store = PeerStore()
store.add_addrs("peer1", ["/foo1", "/bar1"], 10)
store.add_addrs("peer2", ["/foo2"], 10)
store.clear_addrs("peer1")
assert store.addrs("peer1") == []
assert store.addrs("peer2") == ["/foo2"]
store.add_addrs("peer1", ["/foo1", "/bar1"], 10)
assert store.addrs("peer1") == ["/foo1", "/bar1"]
def test_peers_with_addrs():
store = PeerStore()
store.add_addrs("peer1", [], 10)
store.add_addrs("peer2", ["/foo"], 10)
store.add_addrs("peer3", ["/bar"], 10)
assert set(store.peers_with_addrs()) == set(["peer2", "peer3"])
store.clear_addrs("peer2")
assert set(store.peers_with_addrs()) == set(["peer3"])

View File

@ -0,0 +1,44 @@
import pytest
from peer.peerstore import PeerStoreError
from peer.peerstore import PeerStore
# Testing methods from IPeerMetadata base class.
def test_get_empty():
with pytest.raises(PeerStoreError):
store = PeerStore()
val = store.get("peer", "key")
assert not val
def test_put_get_simple():
store = PeerStore()
store.put("peer", "key", "val")
assert store.get("peer", "key") == "val"
def test_put_get_update():
store = PeerStore()
store.put("peer", "key1", "val1")
store.put("peer", "key2", "val2")
store.put("peer", "key2", "new val2")
assert store.get("peer", "key1") == "val1"
assert store.get("peer", "key2") == "new val2"
def test_put_get_two_peers():
store = PeerStore()
store.put("peer1", "key1", "val1")
store.put("peer2", "key1", "val1 prime")
assert store.get("peer1", "key1") == "val1"
assert store.get("peer2", "key1") == "val1 prime"
# Try update
store.put("peer2", "key1", "new val1")
assert store.get("peer1", "key1") == "val1"
assert store.get("peer2", "key1") == "new val1"

View File

@ -0,0 +1,61 @@
import pytest
from peer.peerstore import PeerStoreError
from peer.peerstore import PeerStore
# Testing methods from IPeerStore base class.
def test_peer_info_empty():
store = PeerStore()
info = store.peer_info("peer")
assert not info
def test_peer_info_basic():
store = PeerStore()
store.add_addr("peer", "/foo", 10)
info = store.peer_info("peer")
assert info.peer_id == "peer"
assert info.addrs == ["/foo"]
def test_add_get_protocols_basic():
store = PeerStore()
store.add_protocols("peer1", ["p1", "p2"])
store.add_protocols("peer2", ["p3"])
assert set(store.get_protocols("peer1")) == set(["p1", "p2"])
assert set(store.get_protocols("peer2")) == set(["p3"])
def test_add_get_protocols_extend():
store = PeerStore()
store.add_protocols("peer1", ["p1", "p2"])
store.add_protocols("peer1", ["p3"])
assert set(store.get_protocols("peer1")) == set(["p1", "p2", "p3"])
def test_set_protocols():
store = PeerStore()
store.add_protocols("peer1", ["p1", "p2"])
store.add_protocols("peer2", ["p3"])
store.set_protocols("peer1", ["p4"])
store.set_protocols("peer2", [])
assert set(store.get_protocols("peer1")) == set(["p4"])
assert set(store.get_protocols("peer2")) == set([])
# Test with methods from other Peer interfaces.
def test_peers():
store = PeerStore()
store.add_protocols("peer1", [])
store.put("peer2", "key", "val")
store.add_addr("peer3", "/foo", 10)
assert set(store.peers()) == set(["peer1", "peer2", "peer3"])

View File

@ -3,7 +3,7 @@ import pytest
@pytest.mark.parametrize("test_input,expected", [ @pytest.mark.parametrize("test_input,expected", [
("3+5", 8), ("3+5", 8),
("2+4", 6), ("2+4", 6),
("6*9", 42), ("6*9", 54),
]) ])
def test_eval(test_input, expected): def test_eval(test_input, expected):
assert eval(test_input) == expected assert eval(test_input) == expected