py-libp2p/tests/interop/test_pubsub.py

33 lines
948 B
Python
Raw Normal View History

2019-09-02 21:01:13 +08:00
import asyncio
2019-09-02 18:40:12 +08:00
import pytest
from libp2p.peer.id import ID
2019-09-02 21:01:13 +08:00
from .utils import connect
2019-09-02 21:01:13 +08:00
TOPIC = "TOPIC_0123"
DATA = b"DATA_0123"
2019-09-02 21:01:13 +08:00
2019-09-02 18:40:12 +08:00
@pytest.mark.parametrize("num_hosts", (1,))
@pytest.mark.asyncio
async def test_pubsub_subscribe(pubsubs_gsub, p2pds):
2019-09-02 21:01:13 +08:00
# await connect(pubsubs_gsub[0].host, p2pds[0])
await connect(p2pds[0], pubsubs_gsub[0].host)
peers = await p2pds[0].control.pubsub_list_peers("")
assert pubsubs_gsub[0].host.get_id() in peers
# FIXME:
assert p2pds[0].peer_id in pubsubs_gsub[0].peers
2019-09-02 21:01:13 +08:00
sub = await pubsubs_gsub[0].subscribe(TOPIC)
peers_topic = await p2pds[0].control.pubsub_list_peers(TOPIC)
await asyncio.sleep(0.1)
assert pubsubs_gsub[0].host.get_id() in peers_topic
await p2pds[0].control.pubsub_publish(TOPIC, DATA)
msg = await sub.get()
assert ID(msg.from_id) == p2pds[0].peer_id
assert msg.data == DATA
assert len(msg.topicIDs) == 1 and msg.topicIDs[0] == TOPIC