Пример #1
0
    def test_subscription_constructor(self):
        callbacks = SubscriptionEventCallbacks()
        liveliness_callback = Mock()
        deadline_callback = Mock()
        message_callback = Mock()
        incompatible_qos_callback = Mock()
        expected_num_event_handlers = 1

        # No arg
        subscription = self.node.create_subscription(EmptyMsg, self.topic_name,
                                                     message_callback, 10)
        self.assertEqual(len(subscription.event_handlers),
                         expected_num_event_handlers)
        self.node.destroy_subscription(subscription)

        # Arg with no callbacks
        subscription = self.node.create_subscription(EmptyMsg,
                                                     self.topic_name,
                                                     message_callback,
                                                     10,
                                                     event_callbacks=callbacks)
        self.assertEqual(len(subscription.event_handlers),
                         expected_num_event_handlers)
        self.node.destroy_subscription(subscription)

        # Arg with one of the callbacks
        callbacks.deadline = deadline_callback
        expected_num_event_handlers += 1
        subscription = self.node.create_subscription(EmptyMsg,
                                                     self.topic_name,
                                                     message_callback,
                                                     10,
                                                     event_callbacks=callbacks)
        self.assertEqual(len(subscription.event_handlers),
                         expected_num_event_handlers)
        self.node.destroy_subscription(subscription)

        # Arg with two callbacks
        callbacks.liveliness = liveliness_callback
        expected_num_event_handlers += 1
        subscription = self.node.create_subscription(EmptyMsg,
                                                     self.topic_name,
                                                     message_callback,
                                                     10,
                                                     event_callbacks=callbacks)
        self.assertEqual(len(subscription.event_handlers),
                         expected_num_event_handlers)
        self.node.destroy_subscription(subscription)

        # Arg with three callbacks
        callbacks.incompatible_qos = incompatible_qos_callback
        subscription = self.node.create_subscription(EmptyMsg,
                                                     self.topic_name,
                                                     message_callback,
                                                     10,
                                                     event_callbacks=callbacks)
        self.assertEqual(len(subscription.event_handlers), 3)
        self.node.destroy_subscription(subscription)
Пример #2
0
    def __init__(self):

        qos = QoSProfile(depth=10)
        qos.durability = DurabilityPolicy.RMW_QOS_POLICY_DURABILITY_TRANSIENT_LOCAL

        custom_callback = lambda event: print("Custom Incompatible Callback!")
        callbacks = SubscriptionEventCallbacks()
        callbacks.incompatible_qos = custom_callback

        super().__init__('minimal_subscriber')
        self.subscription = self.create_subscription(
            String,
            'topic',
            self.listener_callback,
            qos,
            event_callbacks=callbacks)