Пример #1
0
def test_reset():
    brq = Baroque()

    class MyEventType1(EventType):
        def __init__(self, owner=None):
            EventType.__init__(self,
                               '''{
                                 "$schema": "http://json-schema.org/draft-04/schema#"
                               }''',
                               description='test',
                               owner=owner)

    class MyEventType2(EventType):
        def __init__(self, owner=None):
            EventType.__init__(self,
                               '''{
                                 "$schema": "http://json-schema.org/draft-04/schema#"
                               }''',
                               description='test',
                               owner=owner)

    eventtype1 = MyEventType1()
    eventtype2 = MyEventType2()
    brq.on(eventtype1).run(ReactorFactory.stdout())
    brq.on(eventtype2).run(ReactorFactory.stdout())
    brq.publish(Event(eventtype1))
    brq.publish(Event(eventtype2))
    assert brq.events.count_all() == 2
    assert brq.eventtypes.count() == 2 + len(
        DEFAULT_CONFIG['eventtypes']['pre_registered'])
    assert len(brq.reactors.registered_types) == 2
    brq.reset()
    assert brq.events.count_all() == 0
    assert brq.eventtypes.count() == 0
    assert len(brq.reactors.registered_types) == 0
Пример #2
0
def test_count():
    r1 = ReactorFactory.stdout()
    r2 = ReactorFactory.stdout()
    r3 = ReactorFactory.stdout()
    bag = ReactorsBag()
    bag.run(r1)
    bag.run(r2)
    bag.run(r3)
    assert bag.count() == 3
Пример #3
0
def test_magic_len():
    r1 = ReactorFactory.stdout()
    r2 = ReactorFactory.stdout()
    r3 = ReactorFactory.stdout()
    bag = ReactorsBag()
    bag.run(r1)
    bag.run(r2)
    bag.run(r3)
    assert len(bag) == 3
Пример #4
0
def test_magic_contains():
    r1 = ReactorFactory.stdout()
    r2 = ReactorFactory.stdout()
    r3 = ReactorFactory.stdout()
    bag = ReactorsBag()
    bag.run(r1)
    bag.run(r3)
    assert r1 in bag
    assert r2 not in bag
    assert r3 in bag
Пример #5
0
def test_magic_iter():
    r1 = ReactorFactory.stdout()
    r2 = ReactorFactory.stdout()
    r3 = ReactorFactory.stdout()
    bag = ReactorsBag()
    bag.run(r1)
    bag.run(r2)
    bag.run(r3)
    for item in bag:
        assert isinstance(item, Reactor)
Пример #6
0
def test_remove():
    r1 = ReactorFactory.stdout()
    r2 = ReactorFactory.stdout()
    r3 = ReactorFactory.stdout()
    bag = ReactorsBag()
    bag.run(r1)
    bag.run(r2)
    bag.run(r3)
    assert bag.count() == 3
    bag.remove(r2)
    assert bag.count() == 2
    assert r1 in bag.reactors
    assert r3 in bag.reactors
Пример #7
0
def test_on_any_event_run():
    brq = Baroque()
    assert len(brq.reactors.jolly_bag) == 0
    r = ReactorFactory.stdout()
    result = brq.on_any_event_run(r)
    assert isinstance(result, Reactor)
    assert r in brq.reactors.jolly_bag
Пример #8
0
def test_register_on_binding():
    # do register topics upon reactor binding
    cfg['topics']['register_on_binding'] = True
    brq = Baroque()
    brq.config = cfg
    t = Topic('test-topic')
    assert len(brq.topics) == 0
    brq.on_topic_run(t, ReactorFactory.stdout())
    assert len(brq.topics) == 1

    # do not register topics upon reactor binding: throw an exception
    cfg['topics']['register_on_binding'] = False
    brq = Baroque()
    brq.config = cfg
    with pytest.raises(UnregisteredTopicError):
        brq.on_topic_run(t, ReactorFactory.stdout())
Пример #9
0
def test_run():
    reg = TopicsRegistry()
    t = Topic('test-topic')
    r1 = ReactorFactory.stdout()

    # failures
    with pytest.raises(AssertionError):
        reg.on_topic_run(None, r1)
    with pytest.raises(AssertionError):
        reg.on_topic_run(123, r1)
    with pytest.raises(AssertionError):
        reg.on_topic_run(t, None)
    with pytest.raises(AssertionError):
        reg.on_topic_run(t, 123)

    # adding a reactor to a topic that is not yet registered is idempotent
    assert len(reg.topics) == 0
    reg.on_topic_run(t, r1)
    assert len(reg.topics) == 0

    # adding a reactor to a topic that is registered
    r1 = ReactorFactory.stdout()
    reg.register(t)
    reg.on_topic_run(t, r1)
    reactors = reg.topics[t]
    assert len(reactors) == 1
    assert r1 in reactors

    # adding another reactor on the same topic does not cause topic duplication
    r2 = ReactorFactory.stdout()
    reg.on_topic_run(t, r2)
    assert len(reg.topics) == 1
    reactors = reg.topics[t]
    assert len(reactors) == 2
    assert r2 in reactors

    # adding a reactor to an already registered topic
    reg = TopicsRegistry()
    reg.register(t)
    r3 = ReactorFactory.stdout()
    reg.on_topic_run(t, r3)
    assert len(reg.topics) == 1
    reactors = reg.topics[t]
    assert len(reactors) == 1
    assert r3 in reactors
Пример #10
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
Пример #11
0
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
Пример #12
0
def test_run():
    r = ReactorFactory.stdout()
    bag = ReactorsBag()
    result = bag.run(r)
    assert result == r
    assert len(bag.reactors) == 1
    assert bag.reactors[0] == r
    with pytest.raises(AssertionError):
        bag.run('not-a-reactor')
        pytest.fail()
Пример #13
0
def test_on_topic_run():
    brq = Baroque()
    t1 = Topic('test-topic1',
               eventtypes=[MetricEventType(),
                           GenericEventType()])
    assert len(brq.topics.topics) == 0
    brq.on_topic_run(t1, ReactorFactory.stdout())
    assert len(brq.topics.topics) == 1
    assert len(brq.topics.topics[t1]) == 1

    # one more reactor on the same topic
    brq.on_topic_run(t1, ReactorFactory.stdout())
    assert len(brq.topics.topics) == 1
    assert len(brq.topics.topics[t1]) == 2

    # let's register another topic
    t2 = Topic('test-topic2', eventtypes=[MetricEventType()])
    brq.on_topic_run(t2, ReactorFactory.stdout())
    assert len(brq.topics.topics) == 2
    assert len(brq.topics.topics[t1]) == 2
    assert len(brq.topics.topics[t2]) == 1
Пример #14
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()
Пример #15
0
def test_remove_all():
    reg = ReactorsRegistry()
    et1 = MetricEventType()
    et2 = GenericEventType()
    et3 = DataOperationEventType()
    reg.get_or_create_bag(et1)
    reg.get_or_create_bag(et2)
    reg.get_or_create_bag(et3)
    reg.get_jolly_bag().run(ReactorFactory.stdout())
    assert len(reg.registered_types) == 3
    assert len(reg.jolly_bag) == 1
    reg.remove_all()
    assert len(reg.registered_types) == 0
    assert len(reg.jolly_bag) == 0
Пример #16
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