async def ping_observing_task(address):
    logger = logging.getLogger('moler.user.app-code')

    # Lowest layer of Moler's usage (you manually glue all elements):
    # 1. create observer
    net_down_detector = NetworkDownDetector('10.0.2.15')
    # 2. ObservableConnection is a proxy-glue between observer (speaks str)
    #                                   and asyncio-connection (speaks bytes)
    moler_conn = ObservableConnection(
        decoder=lambda data: data.decode("utf-8"))
    # 3a. glue from proxy to observer
    moler_conn.subscribe(net_down_detector.data_received)

    logger.debug('waiting for data to observe')
    async for connection_data in tcp_connection(address):
        # 3b. glue to proxy from external-IO (asyncio tcp client connection)
        #   (client code has to pass it's received data into Moler's connection)
        moler_conn.data_received(connection_data)
        # 4. Moler's client code must manually check status of observer ...
        if net_down_detector.done():
            # 5. ... to know when it can ask for result
            net_down_time = net_down_detector.result()
            timestamp = time.strftime("%H:%M:%S",
                                      time.localtime(net_down_time))
            logger.debug('Network is down from {}'.format(timestamp))
            break
Esempio n. 2
0
def test_events_true_any_one():
    connection = ObservableConnection()
    events = list()
    patterns = ("aaa", "bbb")
    for pattern in patterns:
        event = Wait4prompt(connection=connection,
                            till_occurs_times=1,
                            prompt=pattern)
        event.start()
        events.append(event)
    connection.data_received(patterns[0])
    assert EventAwaiter.wait_for_any(timeout=0.1, events=events) is True
    done, not_done = EventAwaiter.separate_done_events(events)
    assert 1 == len(done)
    assert 1 == len(not_done)
    EventAwaiter.cancel_all_events(events)
Esempio n. 3
0
def test_garbage_collected_subscriber_is_not_notified():
    from moler.observable_connection import ObservableConnection

    moler_conn = ObservableConnection()
    received_data = []

    class Subscriber(object):
        def __call__(self, data):
            received_data.append(data)

    subscr1 = Subscriber()
    subscr2 = Subscriber()
    moler_conn.subscribe(observer=subscr1,
                         connection_closed_handler=do_nothing_func)
    moler_conn.subscribe(observer=subscr2,
                         connection_closed_handler=do_nothing_func)

    del subscr1
    gc.collect()

    moler_conn.data_received("data")
    assert len(received_data) == 1
Esempio n. 4
0
def test_single_unsubscription_doesnt_impact_other_subscribers():
    from moler.observable_connection import ObservableConnection

    class TheObserver(object):
        def __init__(self):
            self.received_data = []

        def on_new_data(self, data):
            self.received_data.append(data)

    observer1 = TheObserver()
    observer2 = TheObserver()

    function_received_data = []

    def raw_fun1(data):
        function_received_data.append(data)

    def raw_fun2(data):
        function_received_data.append(data)

    class TheCallableClass(object):
        def __init__(self):
            self.received_data = []

        def __call__(self, data):
            self.received_data.append(data)

    callable1 = TheCallableClass()
    callable2 = TheCallableClass()

    moler_conn = ObservableConnection()
    moler_conn.subscribe(observer=observer1.on_new_data,
                         connection_closed_handler=do_nothing_func)
    moler_conn.subscribe(observer=observer2.on_new_data,
                         connection_closed_handler=do_nothing_func)
    moler_conn.subscribe(observer=observer2.on_new_data,
                         connection_closed_handler=do_nothing_func)
    moler_conn.unsubscribe(observer=observer1.on_new_data,
                           connection_closed_handler=do_nothing_func)
    moler_conn.unsubscribe(observer=observer1.on_new_data,
                           connection_closed_handler=do_nothing_func)

    moler_conn.subscribe(observer=raw_fun1,
                         connection_closed_handler=do_nothing_func)
    moler_conn.subscribe(observer=raw_fun2,
                         connection_closed_handler=do_nothing_func)
    moler_conn.subscribe(observer=raw_fun2,
                         connection_closed_handler=do_nothing_func)
    moler_conn.unsubscribe(observer=raw_fun1,
                           connection_closed_handler=do_nothing_func)

    moler_conn.subscribe(observer=callable1,
                         connection_closed_handler=do_nothing_func)
    moler_conn.subscribe(observer=callable2,
                         connection_closed_handler=do_nothing_func)
    moler_conn.subscribe(observer=callable2,
                         connection_closed_handler=do_nothing_func)
    moler_conn.unsubscribe(observer=callable1,
                           connection_closed_handler=do_nothing_func)

    moler_conn.data_received("incoming data")

    assert observer1.received_data == []
    assert observer2.received_data == ["incoming data"]

    assert function_received_data == ["incoming data"]

    assert callable1.received_data == []
    assert callable2.received_data == ["incoming data"]