def test_1_producer_1_consumer(self):
        """Test single producer single consumer."""
        def save_message(x):
            self.message = x

        def consume():
            """Function to consume messages."""
            consume_messages(self.EXCHANGE, save_message)

        consumer = threading.Thread(target=consume)
        consumer.name = '{}:consume_messages'.format(self.__class__.__name__)
        consumer.start()

        # Give the receiver some time to set up, see comment below
        time.sleep(0.05)
        self.assertIs(self.message, None)
        sent_message = 'banana'
        producer = MessageProducer(self.EXCHANGE)
        producer.publish(sent_message)
        producer.publish('QUIT')
        for _ in range(10):
            # Because of a race condition, if the message is sent before the
            # receiver has set up, the messages are never queued or something.
            # Keep resending until the thread exits.
            consumer.join(0.05)
            if consumer.is_alive():
                producer.publish(sent_message)
                producer.publish('QUIT')

        consumer.join(0.05)
        self.assertFalse(consumer.is_alive())
        producer.kill()
        self.assertEqual(self.message, sent_message)
Beispiel #2
0
def terminate(signal_number, stack_frame):  # pylint: disable=unused-argument
    """Terminates the program. Used when a signal is received."""
    print(
        'Received signal {signal_number}, quitting'.format(
            signal_number=signal_number
        )
    )
    if POPEN is not None and POPEN.poll() is None:
        print('Killing image capture')
        try:
            POPEN.kill()
        except OSError:
            pass

    DRIVER.drive(0.0, 0.0)
    time.sleep(0.2)
    try:
        with open('/dev/pi-blaster', 'w') as blaster:
            time.sleep(0.1)
            blaster.write(
                '{pin}={throttle}\n'.format(
                    pin=THROTTLE_GPIO_PIN,
                    throttle=THROTTLE_NEUTRAL_US
                )
            )
            time.sleep(0.1)
            blaster.write(
                '{pin}={steering}\n'.format(
                    pin=STEERING_GPIO_PIN,
                    steering=STEERING_NEUTRAL_US
                )
            )
            time.sleep(0.1)
    except IOError:
        pass

    for socket in os.listdir(os.sep.join(('.', 'messaging', 'sockets'))):
        MessageProducer(socket).kill()
    time.sleep(0.1)

    for thread in THREADS:
        thread.kill()
        thread.join()
    # Some threads should still be active
    expected = set(('MainThread', '_TimeoutMonitor'))
    actives = set((thread.name for thread in threading.enumerate()))
    if not (actives <= expected):
        print('Trying to exit while {} threads are still active!'.format(
            threading.active_count()
        ))
        for thread in threading.enumerate():
            print(thread.name)
    sys.exit(0)
 def __init__(self):
     super(AsyncLogger, self).__init__()
     self._producer = MessageProducer(config.LOGS_EXCHANGE)
     self.warning = self.warn
 def __init__(self):
     super(WaypointProducer, self).__init__()
     self._producer = MessageProducer(config.WAYPOINT_EXCHANGE)
 def __init__(self):
     super(CommandForwardProducer, self).__init__()
     self._producer = MessageProducer(config.COMMAND_FORWARDED_EXCHANGE)
 def __init__(self):
     super(TelemetryProducer, self).__init__()
     self._producer = MessageProducer(config.TELEMETRY_EXCHANGE)