Exemple #1
0
def test_fire():
    # using a databox to keep state and make assertions
    box = Box()
    r1 = ReactorFactory.call_function(box, 'test_eventtype')
    r2 = ReactorFactory.call_function(box, 'any_eventtype')
    brq = Baroque()
    eventtype = GenericEventType()
    brq.on(eventtype).run(r1)
    brq.on_any_event_run(r2)
    evt = Event(eventtype)

    assert not box.reacted_on_test_eventtype
    assert not box.reacted_on_any_eventtype
    assert brq.events.count_all() == 0
    brq.fire(evt)
    assert box.reacted_on_test_eventtype
    assert box.reacted_on_any_eventtype
    assert brq.events.count_all() == 1
def test_publish_on_topic():
    reg = TopicsRegistry()
    t = Topic('test', eventtypes=[GenericEventType(), MetricEventType()])
    evt = Event(MetricEventType())

    # failures
    with pytest.raises(AssertionError):
        reg.publish_on_topic(None, t)
    with pytest.raises(AssertionError):
        reg.publish_on_topic(123, t)
    with pytest.raises(AssertionError):
        reg.publish_on_topic(evt, None)
    with pytest.raises(AssertionError):
        reg.publish_on_topic(evt, 123)

    # using a databox class to keep state and make assertions
    class Box:
        def __init__(self):
            self.called = False

        def mark_called(self):
            self.called = True

    # publish an event on a topic that doesn't have its eventtype registered
    t = Topic('aaa', eventtypes=[GenericEventType()])
    evt = Event(MetricEventType())
    box = Box()
    r = ReactorFactory.call_function(box, 'mark_called')
    reg.register(t)
    reg.on_topic_run(t, r)
    reg.publish_on_topic(evt, t)
    assert not box.called

    # publish an event on a topic that has its eventtype registered
    reg = TopicsRegistry()
    t = Topic('aaa', eventtypes=[MetricEventType()])
    box = Box()
    r = ReactorFactory.call_function(box, 'mark_called')
    reg.register(t)
    reg.on_topic_run(t, r)
    evt = Event(MetricEventType())
    reg.publish_on_topic(evt, t)
    assert box.called
Exemple #3
0
def test_publish():
    # using a databox to keep state and make assertions
    box = Box()
    r1 = ReactorFactory.call_function(box, 'test_eventtype')
    r2 = ReactorFactory.call_function(box, 'any_eventtype')
    brq = Baroque()
    eventtype = GenericEventType()
    brq.on(eventtype).run(r1)
    brq.on_any_event_run(r2)
    evt = Event(eventtype)

    assert not box.reacted_on_test_eventtype
    assert not box.reacted_on_any_eventtype
    assert brq.events.count_all() == 0
    brq.publish(evt)
    assert box.reacted_on_test_eventtype
    assert box.reacted_on_any_eventtype
    assert brq.events.count_all() == 1
    assert evt.status == EventStatus.PUBLISHED

    with pytest.raises(AssertionError):
        brq.publish('not-an-event')
        pytest.fail()
Exemple #4
0
def test_publish_on_topic():
    brq = Baroque()
    t = brq.topics.new('test-topic1',
                       eventtypes=[MetricEventType(),
                                   GenericEventType()])

    evt = Event(MetricEventType())

    # trying to publish events to an unregistered topic
    with pytest.raises(UnregisteredTopicError):
        brq.publish_on_topic(evt, Topic('unregistered'))
        pytest.fail()

    # using a box to keep state and make assertions
    box = Box()
    brq.on_topic_run(t, ReactorFactory.call_function(box, 'mark_called'))
    assert not box.called
    assert brq.events.count_all() == 0
    brq.publish_on_topic(evt, t)
    assert box.called
    assert brq.events.count_all() == 1
    assert evt.status == EventStatus.PUBLISHED