test_live_to_console.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 """
4 Test: grep live topics to console output.
5 
6 ------------------------------------------------------------------------------
7 This file is part of grepros - grep for ROS bag files and live topics.
8 Released under the BSD License.
9 
10 @author Erki Suurjaak
11 @created 23.12.2021
12 @modified 24.12.2021
13 ------------------------------------------------------------------------------
14 """
15 import logging
16 import os
17 import sys
18 
19 sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
20 from test import testbase
21 
22 logger = logging.getLogger()
23 
24 
26  """Tests grepping from live topics and printing matches to console."""
27 
28 
29  NAME = os.path.splitext(os.path.basename(__file__))[0]
30 
31 
32  INPUT_LABEL = "live topics"
33 
34 
35  OUTPUT_LABEL = "console"
36 
37  def __init__(self, *args, **kwargs):
38  super().__init__(*args, **kwargs)
39  self.init_node()
40 
41  def setUp(self):
42  """Collects bags in data directory, assembles command."""
43  super().setUp()
44  self._cmd = self.CMD_BASE + ["--live"]
45 
46  def tearDown(self):
47  """Terminates subprocess and shuts down ROS2 node, if any."""
48  super().tearDown()
49  self.shutdown_node()
50 
51  def test_grepros(self):
52  """Runs grepros on live topics, verifies console output."""
53  self.verify_bags()
54  self.run_command(communicate=False)
55 
56  logger.info("Opening publishers.")
57  pubs = {} # {topic: ROS publisher}
58  for bagfile in self._bags:
59  bag = testbase.BagReader(bagfile)
60  for topic, msg, _ in bag.read_messages():
61  if topic not in pubs:
62  logger.info("Opening publisher to %r.", topic)
63  pubs[topic] = self.create_publisher(topic, type(msg))
64  self.spin_once(0.5)
65  bag.close()
66  self.spin_once(2)
67 
68  logger.info("Publishing messages to live.")
69  for bagfile in self._bags:
70  bag = testbase.BagReader(bagfile)
71  for topic, msg, _ in bag.read_messages():
72  pubs[topic].publish(msg)
73  self.spin_once(0.5)
74  bag.close()
75  self.spin_once(2)
76 
77  self._proc.terminate()
78  fulltext = self._proc.communicate()[0]
79  self.assertTrue(fulltext, "Command did not print to console.")
80  self.verify_topics(fulltext, fulltext)
81 
82 
83 if "__main__" == __name__:
84  TestLiveInputConsoleOutput.run_rostest()
test.testbase.TestBase.shutdown_node
def shutdown_node(self)
Definition: testbase.py:149
test.test_live_to_console.TestLiveInputConsoleOutput.test_grepros
def test_grepros(self)
Definition: test_live_to_console.py:51
test.testbase.TestBase
Definition: testbase.py:46
test.testbase.TestBase.CMD_BASE
list CMD_BASE
Base command for running grepros.
Definition: testbase.py:71
test.testbase.TestBase.create_publisher
def create_publisher(self, topic, cls)
Definition: testbase.py:131
test.testbase.TestBase.init_node
def init_node(self)
Definition: testbase.py:137
test.testbase.TestBase.verify_topics
def verify_topics(self, topics, messages=None)
Definition: testbase.py:176
test.test_live_to_console.TestLiveInputConsoleOutput.__init__
def __init__(self, *args, **kwargs)
Definition: test_live_to_console.py:37
test.test_live_to_console.TestLiveInputConsoleOutput.tearDown
def tearDown(self)
Definition: test_live_to_console.py:46
test.testbase.TestBase.spin_once
def spin_once(self, timeout)
Definition: testbase.py:163
test.test_live_to_console.TestLiveInputConsoleOutput._cmd
_cmd
Definition: test_live_to_console.py:44
test.test_live_to_console.TestLiveInputConsoleOutput.setUp
def setUp(self)
Definition: test_live_to_console.py:41
test.testbase.TestBase._proc
_proc
Definition: testbase.py:95
test.testbase.TestBase._bags
_bags
Definition: testbase.py:94
test.testbase.TestBase.verify_bags
def verify_bags(self)
Definition: testbase.py:169
test.testbase.TestBase.run_command
def run_command(self, communicate=True)
Definition: testbase.py:154
test.test_live_to_console.TestLiveInputConsoleOutput
Definition: test_live_to_console.py:25


grepros
Author(s): Erki Suurjaak
autogenerated on Sat Jan 6 2024 03:11:29