mirror of
https://github.com/varun-r-mallya/py-libp2p.git
synced 2025-12-31 20:36:24 +00:00
libp2p end to end test
This commit is contained in:
0
tests/libp2p/__init__.py
Normal file
0
tests/libp2p/__init__.py
Normal file
30
tests/libp2p/test_libp2p.py
Normal file
30
tests/libp2p/test_libp2p.py
Normal file
@ -0,0 +1,30 @@
|
||||
from libp2p.libp2p import Libp2p
|
||||
|
||||
|
||||
# TODO: are these connections async? how do we wait on responses?
|
||||
def test_simple_messages():
|
||||
lib = Libp2p()
|
||||
|
||||
hostA = lib.new_node()
|
||||
hostB = lib.new_node()
|
||||
|
||||
def stream_handler(stream):
|
||||
print("stream received in host B")
|
||||
|
||||
read_string = stream.read().decode()
|
||||
print("host B received: " + read_string)
|
||||
|
||||
response = "ack: " + read_string
|
||||
stream.write(response.encode())
|
||||
|
||||
hostB.set_stream_handler("/echo/1.0.0", stream_handler)
|
||||
|
||||
# associate the peer with local ip address (see default parameters of Libp2p())
|
||||
hostA.get_peerstore().add_addr("hostB", "/ip4/127.0.0.1/tcp/10000")
|
||||
|
||||
stream = hostA.new_stream("hostB", "/echo/1.0.0")
|
||||
message = "hello"
|
||||
stream.write(message.encode())
|
||||
|
||||
response = stream.read().decode()
|
||||
assert response == ("ack: " + message)
|
||||
Reference in New Issue
Block a user