Ejemplo n.º 1
0
def test_complex_event_unpack(event_bus: EventBus):
    """Test that the ComplexEventValue get properly unpacked"""
    m = MagicMock()
    assert not m.called

    item = Item.get_create_item('test_complex')
    listener = EventBusListener(
        item.name, wrappedfunction.WrappedFunction(m, name='test'))
    EventBus.add_listener(listener)

    with SyncWorker():
        item.post_value(ComplexEventValue('ValOld'))
        item.post_value(ComplexEventValue('ValNew'))

    # assert that we have been called with exactly one arg
    for k in m.call_args_list:
        assert len(k[0]) == 1

    arg0 = m.call_args_list[0][0][0]
    arg1 = m.call_args_list[1][0][0]
    arg2 = m.call_args_list[2][0][0]
    arg3 = m.call_args_list[3][0][0]

    # Events for first post_value
    assert vars(arg0) == vars(ValueUpdateEvent(item.name, 'ValOld'))
    assert vars(arg1) == vars(ValueChangeEvent(item.name, 'ValOld', None))

    # Events for second post_value
    assert vars(arg2) == vars(ValueUpdateEvent(item.name, 'ValNew'))
    assert vars(arg3) == vars(ValueChangeEvent(item.name, 'ValNew', 'ValOld'))
Ejemplo n.º 2
0
    def item_events(self, changes=False, secs=5, values=[]):
        self.secs = secs
        self.watch_item = Item.get_create_item(get_random_name())
        (self.watch_item.watch_change
         if changes else self.watch_item.watch_update)(secs)

        event = ItemNoUpdateEvent if not changes else ItemNoChangeEvent

        self.ts_set = 0
        listener = self.listen_event(self.watch_item, self.check_event, event)

        for step, value in enumerate(values):
            if step:
                time.sleep(0.2)
            self.ts_set = time.time()
            self.watch_item.set_value(value)
            with EventWaiter(self.watch_item.name,
                             event,
                             secs + 2,
                             check_value=False) as w:
                w.wait_for_event(value)
                if not w.events_ok:
                    listener.cancel()
                    return w.events_ok

        listener.cancel()
        return True
Ejemplo n.º 3
0
    def item_events(self, changes=False, secs=5, values=[]):
        item_name = get_random_name()
        self.secs = secs
        self.watch_item = Item.get_create_item(item_name)
        watcher = (self.watch_item.watch_change
                   if changes else self.watch_item.watch_update)(secs)

        event = ItemNoUpdateEvent if not changes else ItemNoChangeEvent
        listener = self.listen_event(self.watch_item, self.check_event, event)

        def _run():
            self.ts_set = 0
            for step, value in enumerate(values):
                if step:
                    time.sleep(0.2)
                self.ts_set = time.time()
                self.watch_item.set_value(value)
                with EventWaiter(self.watch_item.name,
                                 event,
                                 secs + 2,
                                 check_value=False) as w:
                    w.wait_for_event(value)
                    if not w.events_ok:
                        listener.cancel()
                        return w.events_ok
            return True

        if not _run():
            return False

        HABApp.core.Items.pop_item(item_name)
        assert not HABApp.core.Items.item_exists(item_name)
        time.sleep(1)
        self.watch_item = Item.get_create_item(item_name)

        if not _run():
            return False

        listener.cancel()
        watcher.cancel()
        return True
Ejemplo n.º 4
0
    def item_events(self, changes=False, secs=5, values=[]):
        item_name = get_random_name('HABApp')
        self.watch_item = Item.get_create_item(item_name)
        self.secs = secs

        watcher = (self.watch_item.watch_change
                   if changes else self.watch_item.watch_update)(secs)
        event = ItemNoUpdateEvent if not changes else ItemNoChangeEvent
        listener = self.listen_event(self.watch_item, self.check_event, event)

        try:
            self._run(values, event)

            HABApp.core.Items.pop_item(item_name)
            assert not HABApp.core.Items.item_exists(item_name)

            time.sleep(0.5)

            self.watch_item = Item.get_create_item(item_name)
            self._run(values, event)
        finally:
            listener.cancel()
            watcher.cancel()
        return None
Ejemplo n.º 5
0
    def item_events(self, changes=False, secs=5, values=[]):
        watch_item = Item.get_create_item('watch_item', values[0])
        event = ValueNoUpdateEvent if not changes else ValueNoChangeEvent

        def cb(event: ValueNoUpdateEvent):
            assert event.name == watch_item.name, f'Wrong name: {event.name} != {watch_item.name}'
            assert event.seconds == secs, f'Wrong seconds: {event.seconds} != {secs}'

        self.item_watch_and_listen(watch_item.name, secs, cb, changes)

        for step, value in enumerate(values):
            watch_item.set_value(value)
            with EventWaiter(watch_item.name, event, secs + 2) as w:
                w.wait_for_event(value)
                if not w.events_ok:
                    return w.events_ok

        return True
Ejemplo n.º 6
0
    def trigger_event(self):
        self.watch_item = Item.get_create_item(get_random_name())
        listener = self.watch_item.listen_event(self.check_event,
                                                ValueUpdateEvent)

        self.run.at(1, HABApp.core.EventBus.post_event, self.watch_item.name,
                    ValueUpdateEvent(self.watch_item.name, 123))

        with EventWaiter(self.watch_item.name,
                         ValueUpdateEvent,
                         2,
                         check_value=True) as w:
            w.wait_for_event(123)
            if not w.events_ok:
                listener.cancel()
                return w.events_ok

        listener.cancel()
        return True
Ejemplo n.º 7
0
 def set_up(self):
     self.watch_item = Item.get_create_item(get_random_name('HABApp'))
     self.listener = self.watch_item.listen_event(self.check_event,
                                                  ValueUpdateEvent)