Exemplo n.º 1
0
def test_event_queue(kv_test_app):
    q = kv_test_app.kv.events(prefix='foo')
    assert len(q.filters) == 1
    e_foo = next(iter(q.filters))

    # smoketest
    repr(q)

    # Already subscribed
    assert q.subscribe(prefix='foo') == e_foo
    assert len(q.filters) == 1

    # subscribe/unsubscribe with keywords
    e_bar = q.subscribe(key='bar')
    assert len(q.filters) == 2
    assert e_bar in q.filters
    q.unsubscribe(key='bar')
    assert len(q.filters) == 1
    assert e_bar not in q.filters

    # subscribe/unsubscribe with explicit filter
    e_baz = kv.EventFilter(key='baz')
    assert q.subscribe(e_baz) == e_baz
    assert e_baz in q.filters
    assert q.unsubscribe(e_baz) == e_baz
    assert e_baz not in q.filters

    with pytest.raises(ValueError):
        q.unsubscribe(key='missing')

    with pytest.raises(ValueError):
        q.unsubscribe(event_filter=e_foo, prefix='bar')

    with pytest.raises(TypeError):
        q.unsubscribe(event_filter='incorrect type')

    # Context manager
    with q:
        q.subscribe(prefix='more')
        assert len(q.filters) == 2
    assert len(q.filters) == 0

    # Test queue methods
    q.put(1)
    assert q.get() == 1

    # Queue is iterable
    q.put(1)
    q.put(2)
    iq = iter(q)
    next(iq) == 1
    next(iq) == 2

    # Test queue methods with errors
    q.put(ValueError("foobar"))
    for _ in range(2):
        with pytest.raises(ValueError) as exc:
            q.get()
        assert str(exc.value) == 'foobar'
Exemplo n.º 2
0
def test_event_filter_type():
    x = kv.EventFilter()
    assert x.start is None
    assert x.end is None
    assert x.event_type == kv.EventType.ALL

    assert copy.copy(x) == x
    assert hash(x) == hash(x)

    # smoketest
    repr(x)

    x2 = kv.EventFilter(prefix='foo')
    assert x2.start == 'foo'
    assert x2.end == 'fop'
    assert kv.EventFilter(prefix='foo') == x2
    assert kv.EventFilter(prefix='bar') != x2

    x3 = kv.EventFilter(key='foo')
    assert x3.start == 'foo'
    assert x3.end == 'foo\x00'
    assert kv.EventFilter(key='foo') == x3
    assert kv.EventFilter(key='bar') != x3

    assert kv.EventFilter(event_type='delete').event_type == 'DELETE'

    for k in ['key', 'prefix', 'start', 'end', 'event_type']:
        with pytest.raises(TypeError):
            kv.EventFilter(**{k: 1})

    with pytest.raises(ValueError):
        kv.EventFilter(key='foo', prefix='bar')

    with pytest.raises(ValueError):
        kv.EventFilter(event_type='foo')