def test_schedule_sensor(self): """In this version, we pass the sensor directly to the scheduler and use a functional style to compose the filters""" s = ValueListSensor(1, value_stream) vo = ValidationSubscriber(expected_stream, self.test_schedule_sensor) scheduler = Scheduler(asyncio.get_event_loop()) scheduler.schedule_sensor(s, 0.5, where(predicate), passthrough(vo), output()) scheduler.run_forever() self.assertTrue( vo.completed, "Schedule exited before validation observer completed") print("That's all folks")
def test_thunk_builder_handling(self): """We have logic where we can pass a thunk builder into a combinator and it will do the right thing. Check that it works""" scheduler = Scheduler(asyncio.get_event_loop()) lux = ValueListSensor('lux-2', lux_data) vs = ValidationSubscriber(lux_data, self) scheduler.schedule_sensor( lux, 0.5, passthrough(output()), # output() evaluates to a thunk passthrough(output), # output is a thunk builder passthrough(output(sys.stdout)), # output can take an arg vs, print_downstream=True) scheduler.run_forever() self.assertTrue(vs.completed)
def test_publish_and_subscribe(self): sensor = ValueListSensor(1, sensor_values) scheduler = Scheduler(asyncio.get_event_loop()) pg = PostgresWriter(scheduler, self.connect_string, self.mapping) capture = CaptureSubscriber() scheduler.schedule_sensor(sensor, 0.5, parallel(pg, output, capture)) scheduler.run_forever() print("finish writing to the database") row_source = PostgresReader(self.connect_string, self.mapping) row_source.output() validate = SensorEventValidationSubscriber(capture.seq, self) row_source.subscribe(validate) scheduler.schedule_recurring(row_source) scheduler.run_forever() self.assertTrue(validate.completed) print("finished reading rows")
class TestCase(unittest.TestCase): def setUp(self): self.scheduler = Scheduler(asyncio.get_event_loop()) self.sensor = ValueListSensor(1, value_stream) def test_sensor_event_sliding_window(self): vs = ValidationSubscriber(mean_stream, self) self.scheduler.schedule_sensor(self.sensor, 0.1, transduce(SensorSlidingMean(4)), parallel(vs, output())) self.scheduler.run_forever() self.assertTrue(vs.completed) def test_periodic_median_transducer(self): vs = ValidationSubscriber(periodic_median_stream, self) self.scheduler.schedule_sensor(self.sensor, 0.1, transduce(PeriodicMedianTransducer(3)), parallel(vs, output())) self.scheduler.run_forever() self.assertTrue(vs.completed)
def test_complex_workflow(self): THRESHOLD = 300 lux = ValueListSensor('lux-1', lux_data) scheduler = Scheduler(asyncio.get_event_loop()) vs1 = ValidationSubscriber(lux_data, self) vs2 = ValidationSubscriber( [False, False, False, True, False, False, True], self, extract_value_fn=lambda v: v) scheduler.schedule_sensor( lux, 0.5, passthrough(output()), passthrough(vs1), map(lambda event: event.val > THRESHOLD), passthrough(lambda v: print('ON' if v else 'OFF')), vs2, print_downstream=True) scheduler.run_forever() self.assertTrue(vs1.completed) self.assertTrue(vs2.completed) print("That's all folks")
return "DummyLuxSensor(%s, %s, %s)" % \ (self.sensor_id, self.mean, self.stddev) class DummyLed(DefaultSubscriber): def on_next(seelf, x): if x: print("LED ON") else: print("LED OFF") def __repr__(self): return 'DummyLed' THRESHOLD = 300 # Instantiate the sensor and use the functional API to build a flow lux = DummyLuxSensor("lux-1") scheduler = Scheduler(asyncio.get_event_loop()) scheduler.schedule_sensor(lux, 1.0, passthrough(output()), passthrough(csv_writer('/tmp/lux.csv')), map(lambda event: event.val > THRESHOLD), passthrough(lambda v: print('ON' if v else 'OFF')), DummyLed(), print_downstream=True) scheduler.run_forever() print("That's all folks")