mirror of https://github.com/libp2p/py-libp2p.git
stuckinaboot
6 years ago
committed by
GitHub
11 changed files with 127 additions and 124 deletions
@ -1,30 +1,39 @@ |
|||||
|
import pytest |
||||
|
|
||||
from libp2p.libp2p import Libp2p |
from libp2p.libp2p import Libp2p |
||||
|
|
||||
|
@pytest.mark.asyncio |
||||
|
async def test_simple_messages(): |
||||
|
libA = Libp2p(transportOpt=["/ip4/127.0.0.1/tcp/8001/ipfs/hostA"]) |
||||
|
libB = Libp2p(transportOpt=["/ip4/127.0.0.1/tcp/8000/ipfs/hostB"]) |
||||
|
|
||||
|
hostA = await libA.new_node() |
||||
|
hostB = await libB.new_node() |
||||
|
|
||||
# TODO: are these connections async? how do we wait on responses? |
async def stream_handler(stream): |
||||
def test_simple_messages(): |
while True: |
||||
lib = Libp2p() |
read_string = (await stream.read()).decode() |
||||
|
print("host B received:" + read_string) |
||||
|
|
||||
hostA = lib.new_node() |
response = "ack:" + read_string |
||||
hostB = lib.new_node() |
print("sending response:" + response) |
||||
|
await stream.write(response.encode()) |
||||
|
|
||||
def stream_handler(stream): |
hostB.set_stream_handler("/echo/1.0.0", stream_handler) |
||||
print("stream received in host B") |
|
||||
|
|
||||
read_string = stream.read().decode() |
# Associate the peer with local ip address (see default parameters of Libp2p()) |
||||
print("host B received: " + read_string) |
hostA.get_peerstore().add_addr("hostB", "/ip4/127.0.0.1/tcp/8000", 10) |
||||
|
|
||||
response = "ack: " + read_string |
stream = await hostA.new_stream("hostB", "/echo/1.0.0") |
||||
stream.write(response.encode()) |
messages = ["hello" + str(x) for x in range(10)] |
||||
|
|
||||
hostB.set_stream_handler("/echo/1.0.0", stream_handler) |
for message in messages: |
||||
|
await stream.write(message.encode()) |
||||
|
|
||||
# associate the peer with local ip address (see default parameters of Libp2p()) |
response = (await stream.read()).decode() |
||||
hostA.get_peerstore().add_addr("hostB", "/ip4/127.0.0.1/tcp/10000") |
|
||||
|
|
||||
stream = hostA.new_stream("hostB", "/app/1.0.0") |
print("res: " + response) |
||||
message = "hello" |
assert response == ("ack:" + message) |
||||
stream.write(message.encode()) |
|
||||
|
|
||||
response = stream.read().decode() |
# Success, terminate pending tasks. |
||||
assert response == ("ack: " + message) |
return |
||||
|
Loading…
Reference in new issue