def test_sequential_writer(tmp_path): """ Test for sequential writer. :return: """ bag_path = str(tmp_path / 'tmp_write_test') storage_options, converter_options = get_rosbag_options(bag_path) writer = rosbag2_py.SequentialWriter() writer.open(storage_options, converter_options) # create topic topic_name = '/chatter' create_topic(writer, topic_name, 'std_msgs/msg/String') for i in range(10): msg = String() msg.data = f'Hello, world! {str(i)}' time_stamp = i * 100 writer.write(topic_name, serialize_message(msg), time_stamp) # close bag and create new storage instance del writer storage_options, converter_options = get_rosbag_options(bag_path) reader = rosbag2_py.SequentialReader() reader.open(storage_options, converter_options) topic_types = reader.get_all_topics_and_types() # Create a map for quicker lookup type_map = { topic_types[i].name: topic_types[i].type for i in range(len(topic_types)) } msg_counter = 0 while reader.has_next(): topic, data, t = reader.read_next() msg_type = get_message(type_map[topic]) msg_deserialized = deserialize_message(data, msg_type) assert isinstance(msg_deserialized, String) assert msg_deserialized.data == f'Hello, world! {msg_counter}' assert t == msg_counter * 100 msg_counter += 1
def test_seek_forward(): bag_path = str(RESOURCES_PATH / 'wbag') storage_options, converter_options = get_rosbag_options(bag_path) reader = rosbag2_py.SequentialReader() reader.open(storage_options, converter_options) # seek forward reader.seek(1822) (topic, data, t) = reader.read_next() assert topic == 'CCC' assert t == 1822 # set filter continues in same location storage_filter = rosbag2_py.StorageFilter(topics=['BBB', 'GGG']) reader.set_filter(storage_filter) (topic, data, t) = reader.read_next() assert topic == 'GGG' assert t == 1822 (topic, data, t) = reader.read_next() assert topic == 'GGG' assert t == 1822 (topic, data, t) = reader.read_next() assert topic == 'BBB' assert t == 1826
def test_seek_backward(): bag_path = str(RESOURCES_PATH / 'wbag') storage_options, converter_options = get_rosbag_options(bag_path) reader = rosbag2_py.SequentialReader() reader.open(storage_options, converter_options) # seek forward first reader.seek(1822) storage_filter = rosbag2_py.StorageFilter(topics=['BBB', 'GGG']) reader.set_filter(storage_filter) (topic, data, t) = reader.read_next() # seek backwards & filter preserved reader.seek(1408) (topic, data, t) = reader.read_next() assert topic == 'BBB' assert t == 1408 (topic, data, t) = reader.read_next() assert topic == 'GGG' assert t == 1408 (topic, data, t) = reader.read_next() assert topic == 'BBB' assert t == 1413
def test_reindexer_multiple_files(): bag_path = Path(__file__).parent.parent / 'resources' / 'reindex_test_bags' / 'multiple_files' result_path = bag_path / 'metadata.yaml' storage_options, converter_options = get_rosbag_options(str(bag_path)) reindexer = rosbag2_py.Reindexer() reindexer.reindex(storage_options) assert(result_path.exists()) result_path.unlink(missing_ok=True)
def test_reindexer_multiple_files(): bag_path = RESOURCES_PATH / 'reindex_test_bags' / 'multiple_files' result_path = bag_path / 'metadata.yaml' storage_options, converter_options = get_rosbag_options(str(bag_path)) reindexer = rosbag2_py.Reindexer() reindexer.reindex(storage_options) assert (result_path.exists()) try: result_path.unlink() except FileNotFoundError: pass
def test_sequential_reader(): bag_path = str(Path(__file__).parent.parent / 'resources' / 'talker') storage_options, converter_options = get_rosbag_options(bag_path) reader = rosbag2_py.SequentialReader() reader.open(storage_options, converter_options) topic_types = reader.get_all_topics_and_types() # Create a map for quicker lookup type_map = { topic_types[i].name: topic_types[i].type for i in range(len(topic_types)) } # Set filter for topic of string type storage_filter = rosbag2_py.StorageFilter(topics=['/topic']) reader.set_filter(storage_filter) msg_counter = 0 while reader.has_next(): (topic, data, t) = reader.read_next() msg_type = get_message(type_map[topic]) msg = deserialize_message(data, msg_type) assert isinstance(msg, String) assert msg.data == f'Hello, world! {msg_counter}' msg_counter += 1 # No filter reader.reset_filter() reader = rosbag2_py.SequentialReader() reader.open(storage_options, converter_options) msg_counter = 0 while reader.has_next(): (topic, data, t) = reader.read_next() msg_type = get_message(type_map[topic]) msg = deserialize_message(data, msg_type) assert isinstance(msg, Log) or isinstance(msg, String) if isinstance(msg, String): assert msg.data == f'Hello, world! {msg_counter}' msg_counter += 1
def test_reset_filter(): bag_path = str(RESOURCES_PATH / 'wbag') storage_options, converter_options = get_rosbag_options(bag_path) reader = rosbag2_py.SequentialReader() reader.open(storage_options, converter_options) # Set filter for topic of string type storage_filter = rosbag2_py.StorageFilter(topics=['AAA', 'CCC', 'DDD']) reader.set_filter(storage_filter) (topic, data, t) = reader.read_next() assert topic == 'AAA' assert t == 1001 (topic, data, t) = reader.read_next() assert topic == 'CCC' assert t == 1002 (topic, data, t) = reader.read_next() assert topic == 'AAA' assert t == 1004 # No filter and bag continues same location reader.reset_filter() (topic, data, t) = reader.read_next() assert topic == 'FFF' assert t == 1004 (topic, data, t) = reader.read_next() assert topic == 'BBB' assert t == 1004 (topic, data, t) = reader.read_next() assert topic == 'EEE' assert t == 1005
def test_record_cancel(tmp_path): bag_path = str(tmp_path / 'test_record_cancel') storage_options, converter_options = get_rosbag_options(bag_path) recorder = rosbag2_py.Recorder() record_options = rosbag2_py.RecordOptions() record_options.all = True record_options.is_discovery_disabled = False record_options.topic_polling_interval = datetime.timedelta( milliseconds=100) rclpy.init() record_thread = threading.Thread(target=recorder.record, args=(storage_options, record_options), daemon=True) record_thread.start() node = rclpy.create_node('test_record_cancel') executor = rclpy.executors.SingleThreadedExecutor() executor.add_node(node) pub = node.create_publisher(String, 'chatter', 10) i = 0 msg = String() while rclpy.ok() and i < 10: msg.data = 'Hello World: {0}'.format(i) i += 1 pub.publish(msg) recorder.cancel() metadata_path = Path(bag_path) / 'metadata.yaml' db3_path = Path(bag_path) / 'test_record_cancel_0.db3' assert wait_for(lambda: metadata_path.is_file() and db3_path.is_file(), timeout=rclpy.duration.Duration(seconds=3))
def test_sequential_reader_seek(): bag_path = str(RESOURCES_PATH / 'talker') storage_options, converter_options = get_rosbag_options(bag_path) reader = rosbag2_py.SequentialReader() reader.open(storage_options, converter_options) topic_types = reader.get_all_topics_and_types() # Create a map for quicker lookup type_map = { topic_types[i].name: topic_types[i].type for i in range(len(topic_types)) } # Seek No Filter reader = rosbag2_py.SequentialReader() reader.open(storage_options, converter_options) reader.seek(1585866237113147888) msg_counter = 5 (topic, data, t) = reader.read_next() msg_type = get_message(type_map[topic]) msg = deserialize_message(data, msg_type) assert isinstance(msg, Log) (topic, data, t) = reader.read_next() msg_type = get_message(type_map[topic]) msg = deserialize_message(data, msg_type) isinstance(msg, String) assert msg.data == f'Hello, world! {msg_counter}' msg_counter += 1 # Set Filter will continue storage_filter = rosbag2_py.StorageFilter(topics=['/topic']) reader.set_filter(storage_filter) (topic, data, t) = reader.read_next() msg_type = get_message(type_map[topic]) msg = deserialize_message(data, msg_type) isinstance(msg, String) assert msg.data == f'Hello, world! {msg_counter}' # Seek will keep filter reader.seek(1585866239113147888) msg_counter = 8 (topic, data, t) = reader.read_next() msg_type = get_message(type_map[topic]) msg = deserialize_message(data, msg_type) isinstance(msg, String) assert msg.data == f'Hello, world! {msg_counter}' msg_counter += 1 (topic, data, t) = reader.read_next() msg_type = get_message(type_map[topic]) msg = deserialize_message(data, msg_type) isinstance(msg, String) assert msg.data == f'Hello, world! {msg_counter}'