fix tests

This commit is contained in:
zixuanzh 2019-04-28 13:49:10 -04:00
parent fdaa4f56b9
commit 3b1a3fb198
2 changed files with 45 additions and 24 deletions

View File

@ -19,15 +19,14 @@ class KadmeliaPeerRouter(IPeerRouting):
# switching peer_id to xor_id used by kademlia as node_id
xor_id = peer_id.get_xor_id()
value = await self.server.get(xor_id)
return decode_peerinfo(value)
return value
def decode_peerinfo(encoded):
if isinstance(encoded, bytes):
encoded = encoded.decode()
lines = encoded.splitlines()
peer_id = lines[0]
addrs = lines[1:]
peer_data = PeerData()
peer_data.add_addrs(addrs)
return PeerInfo(peer_id, addrs)
# def decode_peerinfo(encoded):
# if isinstance(encoded, bytes):
# encoded = encoded.decode()
# lines = encoded.splitlines()
# peer_id = lines[0]
# addrs = lines[1:]
# peer_data = PeerData()
# peer_data.add_addrs(addrs)
# return PeerInfo(peer_id, addrs)

View File

@ -1,25 +1,47 @@
import asyncio
import pytest
from libp2p.kademlia.network import KademliaServer
from libp2p.routing.kademlia.kademlia_peer_router import KadmeliaPeerRouter
from libp2p.peer.id import id_b58_encode
@pytest.mark.asyncio
async def test_example():
async def test_simple_two_nodes():
node_a = KademliaServer()
await node_a.listen(5678)
node_b = KademliaServer()
await node_b.listen(5679)
value = await node_b.bootstrap([("127.0.0.1", 5678)])
peer_info = value[0]
peer_id = peer_info.peer_id_obj
print(id_b58_encode(peer_id))
# await node_a.set(peer_info.xor_id, str(peer_info.ip) + "/" + str(peer_info.port))
# router = KadmeliaPeerRouter(node_b)
# value = await router.find_peer(peer_id)
# print("value vvvv")
# print(value.xor_)
# assert value == str(peer_info.ip) + "/" + str(peer_info.port)
node_a_value = await node_b.bootstrap([("127.0.0.1", 5678)])
node_a_kad_peerinfo = node_a_value[0]
await node_a.set(node_a_kad_peerinfo.xor_id,
str(node_a_kad_peerinfo.ip)\
+ "/" + str(node_a_kad_peerinfo.port))
router = KadmeliaPeerRouter(node_b)
returned_info = await router.find_peer(node_a_kad_peerinfo.peer_id_obj)
assert returned_info == str(node_a_kad_peerinfo.ip)\
+ "/" + str(node_a_kad_peerinfo.port)
@pytest.mark.asyncio
async def test_simple_three_nodes():
node_a = KademliaServer()
await node_a.listen(5701)
node_b = KademliaServer()
await node_b.listen(5702)
node_c = KademliaServer()
await node_c.listen(5703)
node_a_value = await node_b.bootstrap([("127.0.0.1", 5701)])
node_a_kad_peerinfo = node_a_value[0]
await node_c.bootstrap([("127.0.0.1", 5702)])
await node_a.set(node_a_kad_peerinfo.xor_id,
str(node_a_kad_peerinfo.ip)\
+ "/" + str(node_a_kad_peerinfo.port))
router = KadmeliaPeerRouter(node_c)
returned_info = await router.find_peer(node_a_kad_peerinfo.peer_id_obj)
assert returned_info == str(node_a_kad_peerinfo.ip) + "/" + str(node_a_kad_peerinfo.port)