Skip to content

Commit

Permalink
Fix node tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lassilaiho committed Nov 6, 2023
1 parent c65f2bc commit 8b10dbc
Showing 1 changed file with 113 additions and 122 deletions.
235 changes: 113 additions & 122 deletions pkg/rclgo/node_test.go
Original file line number Diff line number Diff line change
@@ -1,135 +1,126 @@
package rclgo_test

import (
"context"
"testing"
"time"

. "github.com/smartystreets/goconvey/convey" //nolint:revive
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
std_msgs_msg "github.com/tiiuae/rclgo/internal/msgs/std_msgs/msg"
"github.com/tiiuae/rclgo/pkg/rclgo"
)

func TestNodeGetTopicNamesAndTypes(t *testing.T) {
setNewDomainID()
var (
rclctx1, rclctx2 *rclgo.Context
node1, node2 *rclgo.Node
intpub *std_msgs_msg.Int64Publisher
err error
)
defer func() {
if rclctx1 != nil {
rclctx1.Close()
func requireTopicNamesAndTypes(t *testing.T, node *rclgo.Node, expected map[string][]string) {
t.Helper()
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
for {
actual, err := node.GetTopicNamesAndTypes(true)
require.NoError(t, err)
if assert.ObjectsAreEqualValues(expected, actual) {
return
}
if rclctx2 != nil {
rclctx2.Close()
select {
case <-ctx.Done():
require.EqualValues(t, expected, actual)
case <-time.After(100 * time.Millisecond):
}
}()
Convey("Scenario: Node.GetTopicNamesAndTypes works correctly", t, func() {
Convey("Create a rcl context and node", func() {
rclctx1, err = newDefaultRCLContext()
So(err, ShouldBeNil)
node1, err = rclctx1.NewNode("node1", "topic_names_and_types_test")
So(err, ShouldBeNil)
rclctx2, err = newDefaultRCLContext()
So(err, ShouldBeNil)
node2, err = rclctx2.NewNode("node2", "topic_names_and_types_test")
So(err, ShouldBeNil)
})
Convey("Check that topic names and types are correct", func() {
Convey("node1 in empty network", func() {
namesAndTypes, err := node1.GetTopicNamesAndTypes(true)
So(err, ShouldBeNil)
So(namesAndTypes, ShouldResemble, map[string][]string{
"/rosout": {"rcl_interfaces/msg/Log"},
})
})
Convey("node2 in empty network", func() {
namesAndTypes, err := node2.GetTopicNamesAndTypes(true)
So(err, ShouldBeNil)
So(namesAndTypes, ShouldResemble, map[string][]string{
"/rosout": {"rcl_interfaces/msg/Log"},
})
})
Convey("new publisher", func() {
_, err = std_msgs_msg.NewBoolPublisher(node1, "test_topic", nil)
So(err, ShouldBeNil)
})
Convey("node1 after publisher", func() {
namesAndTypes, err := node1.GetTopicNamesAndTypes(true)
So(err, ShouldBeNil)
So(namesAndTypes, ShouldResemble, map[string][]string{
"/rosout": {"rcl_interfaces/msg/Log"},
"/topic_names_and_types_test/test_topic": {"std_msgs/msg/Bool"},
})
})
Convey("node2 after publisher", func() {
namesAndTypes, err := node2.GetTopicNamesAndTypes(true)
So(err, ShouldBeNil)
So(namesAndTypes, ShouldResemble, map[string][]string{
"/rosout": {"rcl_interfaces/msg/Log"},
})
})
Convey("new int publisher", func() {
intpub, err = std_msgs_msg.NewInt64Publisher(node1, "test_topic2", nil)
So(err, ShouldBeNil)
})
Convey("node1 after creating int publisher", func() {
namesAndTypes, err := node1.GetTopicNamesAndTypes(true)
So(err, ShouldBeNil)
So(namesAndTypes, ShouldResemble, map[string][]string{
"/rosout": {"rcl_interfaces/msg/Log"},
"/topic_names_and_types_test/test_topic": {"std_msgs/msg/Bool"},
"/topic_names_and_types_test/test_topic2": {"std_msgs/msg/Int64"},
})
})
Convey("node2 after creating int publisher", func() {
namesAndTypes, err := node2.GetTopicNamesAndTypes(true)
So(err, ShouldBeNil)
So(namesAndTypes, ShouldResemble, map[string][]string{
"/rosout": {"rcl_interfaces/msg/Log"},
})
})
Convey("publish int", func() {
err = intpub.Publish(std_msgs_msg.NewInt64())
So(err, ShouldBeNil)
})
Convey("node1 after publishing int", func() {
namesAndTypes, err := node1.GetTopicNamesAndTypes(true)
So(err, ShouldBeNil)
So(namesAndTypes, ShouldResemble, map[string][]string{
"/rosout": {"rcl_interfaces/msg/Log"},
"/topic_names_and_types_test/test_topic": {"std_msgs/msg/Bool"},
"/topic_names_and_types_test/test_topic2": {"std_msgs/msg/Int64"},
})
})
Convey("node2 after publishing int", func() {
namesAndTypes, err := node2.GetTopicNamesAndTypes(true)
So(err, ShouldBeNil)
So(namesAndTypes, ShouldResemble, map[string][]string{
"/rosout": {"rcl_interfaces/msg/Log"},
})
})
Convey("new string publisher", func() {
_, err = std_msgs_msg.NewStringPublisher(node2, "test_topic", nil)
So(err, ShouldBeNil)
})
Convey("node1 after second publisher", func() {
namesAndTypes, err := node1.GetTopicNamesAndTypes(true)
So(err, ShouldBeNil)
So(namesAndTypes, ShouldResemble, map[string][]string{
"/rosout": {"rcl_interfaces/msg/Log"},
"/topic_names_and_types_test/test_topic": {"std_msgs/msg/Bool", "std_msgs/msg/String"},
"/topic_names_and_types_test/test_topic2": {"std_msgs/msg/Int64"},
})
})
Convey("node2 after second publisher", func() {
namesAndTypes, err := node2.GetTopicNamesAndTypes(true)
So(err, ShouldBeNil)
So(namesAndTypes, ShouldResemble, map[string][]string{
"/rosout": {"rcl_interfaces/msg/Log"},
"/topic_names_and_types_test/test_topic": {"std_msgs/msg/String"},
})
})
})
}
}

func TestNodeGetTopicNamesAndTypes(t *testing.T) {
setNewDomainID()

rclctx1, err := newDefaultRCLContext()
require.NoError(t, err)
defer rclctx1.Close()
node1, err := rclctx1.NewNode("node1", "topic_names_and_types_test")
require.NoError(t, err)

rclctx2, err := newDefaultRCLContext()
require.NoError(t, err)
defer rclctx2.Close()
node2, err := rclctx2.NewNode("node2", "topic_names_and_types_test")
require.NoError(t, err)

t.Log("node1 in empty network")
requireTopicNamesAndTypes(t, node1, map[string][]string{
"/rosout": {"rcl_interfaces/msg/Log"},
})

t.Log("node2 in empty network")
requireTopicNamesAndTypes(t, node2, map[string][]string{
"/rosout": {"rcl_interfaces/msg/Log"},
})

t.Log("new publisher")
_, err = std_msgs_msg.NewBoolPublisher(node1, "test_topic", nil)
require.NoError(t, err)

t.Log("node1 after publisher")
requireTopicNamesAndTypes(t, node1, map[string][]string{
"/rosout": {"rcl_interfaces/msg/Log"},
"/topic_names_and_types_test/test_topic": {"std_msgs/msg/Bool"},
})

t.Log("node2 after publisher")
requireTopicNamesAndTypes(t, node2, map[string][]string{
"/rosout": {"rcl_interfaces/msg/Log"},
"/topic_names_and_types_test/test_topic": {"std_msgs/msg/Bool"},
})

t.Log("new int publisher")
intpub, err := std_msgs_msg.NewInt64Publisher(node1, "test_topic2", nil)
require.NoError(t, err)

t.Log("node1 after creating int publisher")
requireTopicNamesAndTypes(t, node1, map[string][]string{
"/rosout": {"rcl_interfaces/msg/Log"},
"/topic_names_and_types_test/test_topic": {"std_msgs/msg/Bool"},
"/topic_names_and_types_test/test_topic2": {"std_msgs/msg/Int64"},
})

t.Log("node2 after creating int publisher")
requireTopicNamesAndTypes(t, node2, map[string][]string{
"/rosout": {"rcl_interfaces/msg/Log"},
"/topic_names_and_types_test/test_topic": {"std_msgs/msg/Bool"},
"/topic_names_and_types_test/test_topic2": {"std_msgs/msg/Int64"},
})

t.Log("publish int")
err = intpub.Publish(std_msgs_msg.NewInt64())
require.NoError(t, err)

t.Log("node1 after publishing int")
requireTopicNamesAndTypes(t, node1, map[string][]string{
"/rosout": {"rcl_interfaces/msg/Log"},
"/topic_names_and_types_test/test_topic": {"std_msgs/msg/Bool"},
"/topic_names_and_types_test/test_topic2": {"std_msgs/msg/Int64"},
})

t.Log("node2 after publishing int")
requireTopicNamesAndTypes(t, node2, map[string][]string{
"/rosout": {"rcl_interfaces/msg/Log"},
"/topic_names_and_types_test/test_topic": {"std_msgs/msg/Bool"},
"/topic_names_and_types_test/test_topic2": {"std_msgs/msg/Int64"},
})

t.Log("new string publisher")
_, err = std_msgs_msg.NewStringPublisher(node2, "test_topic", nil)
require.NoError(t, err)

t.Log("node1 after second publisher")
requireTopicNamesAndTypes(t, node1, map[string][]string{
"/rosout": {"rcl_interfaces/msg/Log"},
"/topic_names_and_types_test/test_topic": {"std_msgs/msg/Bool", "std_msgs/msg/String"},
"/topic_names_and_types_test/test_topic2": {"std_msgs/msg/Int64"},
})

t.Log("node2 after second publisher")
requireTopicNamesAndTypes(t, node2, map[string][]string{
"/rosout": {"rcl_interfaces/msg/Log"},
"/topic_names_and_types_test/test_topic": {"std_msgs/msg/Bool", "std_msgs/msg/String"},
"/topic_names_and_types_test/test_topic2": {"std_msgs/msg/Int64"},
})
}

0 comments on commit 8b10dbc

Please sign in to comment.