コード例 #1
0
    def test_mqtt(self):
        loop = asyncio.get_event_loop()
        s = Scheduler(loop)
        sensor = make_test_publisher_from_vallist(1, sensor_data)
        mqtt_writer = MQTTWriter('localhost', topics=[
            ('bogus/bogus', 0),
        ])
        sensor.to_json().subscribe(mqtt_writer)
        s.schedule_periodic(sensor, 0.5)

        mqtt_reader = MQTTReader("localhost", topics=[
            ('bogus/bogus', 0),
        ])
        vs = ValidationSubscriber(sensor_data, self)
        mqtt_reader.take(5).select(mqtt_msg_to_unicode).from_json(constructor=SensorEvent) \
                       .output().subscribe(vs)
        c = s.schedule_on_private_event_loop(mqtt_reader)
        stop = StopLoopAfter(5, c)
        mqtt_reader.subscribe(stop)
        mqtt_reader.print_downstream()
        sensor.print_downstream()
        s.run_forever()
        loop.stop()
        self.assertTrue(vs.completed)
        print("that's it")
コード例 #2
0
 def test_case(self):
     """Just run the reader in its own event loop. We stop everything after 4
     events.
     """
     s = Scheduler(asyncio.get_event_loop())
     m = MQTTReader("localhost",
                    topics=[
                        ('bogus/bogus', 0),
                    ],
                    mock_class=MockMQTTClient)
     m.output()
     c = s.schedule_on_private_event_loop(m)
     m.subscribe(StopLoopAfter(4, c))
     m.print_downstream()
     s.run_forever()
     print("that's it")