Exemplo 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'))
Exemplo n.º 2
0
    def check(self, now):
        if self.is_canceled:
            return None

        try:
            item = Items.get_item(self.name)
        except Items.ItemNotFoundException:
            return None

        timestamp = item._last_change if self.__watch_only_changes else item._last_update
        duration = now - timestamp
        if duration < self.duration_const:
            self.executed = False
            return None

        if self.executed:
            return None

        EventBus.post_event(
            self.name,
            (ValueNoChangeEvent if self.__watch_only_changes else ValueNoUpdateEvent)(
                self.name, item.value, int(duration.total_seconds())
            )
        )
        self.executed = True
Exemplo n.º 3
0
def test_str_event(clean_event_bus: EventBus, sync_worker):
    event_history = []

    def append_event(event):
        event_history.append(event)

    func = wrappedfunction.WrappedFunction(append_event)

    listener = EventBusListener('str_test', func)
    EventBus.add_listener(listener)

    EventBus.post_event('str_test', 'str_event')
    assert event_history == ['str_event']
Exemplo n.º 4
0
def test_str_event(event_bus: EventBus):
    event_history = []

    def set(event):
        event_history.append(event)

    listener = EventBusListener('str_test',
                                wrappedfunction.WrappedFunction(set))
    EventBus.add_listener(listener)

    with SyncWorker():
        EventBus.post_event('str_test', 'str_event')

    assert event_history == ['str_event']
Exemplo n.º 5
0
def test_multiple_events(event_bus: EventBus):
    event_history = []
    target = ['str_event', TestEvent(), 'str_event2']

    def set(event):
        event_history.append(event)

    listener = EventBusListener('test', wrappedfunction.WrappedFunction(set),
                                (str, TestEvent))
    EventBus.add_listener(listener)

    with SyncWorker():
        for k in target:
            EventBus.post_event('test', k)

    assert event_history == target
Exemplo n.º 6
0
def test_multiple_events(clean_event_bus: EventBus, sync_worker):
    event_history = []
    target = ['str_event', TestEvent(), 'str_event2']

    def append_event(event):
        event_history.append(event)

    listener = EventBusListener('test',
                                wrappedfunction.WrappedFunction(append_event),
                                (str, TestEvent))
    EventBus.add_listener(listener)

    for k in target:
        EventBus.post_event('test', k)

    assert event_history == target
Exemplo n.º 7
0
 def tearDown(self) -> None:
     EventBus.remove_all_listeners()
Exemplo n.º 8
0
def event_bus():
    EventBus.remove_all_listeners()
    yield EventBus
    EventBus.remove_all_listeners()
Exemplo n.º 9
0
def test_event_filter(clean_event_bus: EventBus, sync_worker):
    events_all, events_filtered1, events_filtered2 , events_filtered3 = [], [], [], []

    def append_all(event):
        events_all.append(event)

    def append_filter1(event):
        events_filtered1.append(event)

    def append_filter2(event):
        events_filtered2.append(event)

    def append_filter3(event):
        events_filtered3.append(event)

    name = 'test_filter'
    func1 = wrappedfunction.WrappedFunction(append_filter1)
    func2 = wrappedfunction.WrappedFunction(append_filter2)
    func3 = wrappedfunction.WrappedFunction(append_filter3)

    # listener to all events
    EventBus.add_listener(
        EventBusListener(name, wrappedfunction.WrappedFunction(append_all)))

    listener = EventBusListener(name, func1, ValueUpdateEvent, 'value',
                                'test_value')
    EventBus.add_listener(listener)
    listener = EventBusListener(name, func2, ValueUpdateEvent, None, None,
                                'value', 1)
    EventBus.add_listener(listener)
    listener = EventBusListener(name, func3, ValueChangeEvent, 'old_value',
                                None, 'value', 1)
    EventBus.add_listener(listener)

    event0 = ValueUpdateEvent(name, None)
    event1 = ValueUpdateEvent(name, 'test_value')
    event2 = ValueUpdateEvent(name, 1)
    event3 = ValueChangeEvent(name, 1, None)

    EventBus.post_event(name, event0)
    EventBus.post_event(name, event1)
    EventBus.post_event(name, event2)
    EventBus.post_event(name, event3)

    assert len(events_all) == 4
    assert vars(events_all[0]) == vars(event0)
    assert vars(events_all[1]) == vars(event1)
    assert vars(events_all[2]) == vars(event2)
    assert vars(events_all[3]) == vars(event3)

    assert len(events_filtered1) == 1
    assert vars(events_filtered1[0]) == vars(event1)

    assert len(events_filtered2) == 1
    assert vars(events_filtered2[0]) == vars(event2)

    assert len(events_filtered3) == 1
    assert vars(events_filtered3[0]) == vars(event3)