Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a60ed71
commit bfd3709
Showing
2 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -146,6 +146,7 @@ | |
], | ||
extras_require={ | ||
"dev": [ | ||
"asyncio", | ||
"pytest>=6.2", | ||
"pytest-cov", | ||
"pytest-mock", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import asyncio | ||
import pytest | ||
|
||
import cyclonedds.idl as idl | ||
import cyclonedds.idl.annotations as annotate | ||
|
||
from dataclasses import dataclass | ||
from cyclonedds.domain import DomainParticipant | ||
from cyclonedds.topic import Topic | ||
from cyclonedds.pub import DataWriter | ||
from cyclonedds.sub import DataReader | ||
from cyclonedds.util import duration | ||
|
||
|
||
@dataclass | ||
@annotate.mutable | ||
@annotate.autoid("sequential") | ||
class Base(idl.IdlStruct, typename="Hierarchy.Base"): | ||
fieldA: str | ||
|
||
|
||
@dataclass | ||
@annotate.mutable | ||
@annotate.autoid("sequential") | ||
class Derived(Base, typename="Hierarchy.Derived"): | ||
fieldB: str | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_base_topic(): | ||
''' | ||
Creates a publisher and a subscriber of the Base topic and checks if the | ||
update sent by the publisher is received by the subscriber. | ||
''' | ||
base = Base(fieldA='Lorem') | ||
tasks = [ | ||
_subscriber(Base), | ||
_publisher(Base, base), | ||
] | ||
results = await asyncio.gather(*tasks) | ||
assert results[0] == results[1] | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_derived_topic(): | ||
''' | ||
Creates a publisher and a subscriber of the Derived topic and checks if the | ||
update sent by the publisher is received by the subscriber. | ||
''' | ||
#base = Base(fieldA='Lorem') | ||
derived = Derived( | ||
fieldA='Ipsum', | ||
fieldB='Dolor') | ||
|
||
tasks = [ | ||
# This should not be necessary. I suspect there is a bug in CycloneDDS | ||
#_subscriber(Base), | ||
#_publisher(Base, base), | ||
|
||
# This is the part of the test that really matters | ||
_subscriber(Derived), | ||
_publisher(Derived, derived), | ||
] | ||
|
||
results = await asyncio.gather(*tasks) | ||
assert results[0] == results[1] | ||
assert results[2] == results[3] | ||
|
||
|
||
async def _publisher(topicClass, value, timeout=2): | ||
''' | ||
Sends a given value. | ||
''' | ||
participant = DomainParticipant(0) | ||
topic = Topic(participant, topicClass.__name__, topicClass) | ||
writer = DataWriter(participant, topic) | ||
writer.write(value) | ||
await asyncio.sleep(timeout) | ||
return value | ||
|
||
|
||
async def _subscriber(topicClass, timeout=2): | ||
''' | ||
Receives an update. Returns None if no update is not received within a | ||
given timeout. | ||
''' | ||
participant = DomainParticipant(0) | ||
topic = Topic(participant, topicClass.__name__, topicClass) | ||
reader = DataReader(participant, topic) | ||
async for update in reader.read_aiter(timeout=duration(seconds=timeout)): | ||
return update |