예제 #1
0
def test_emit_validation():
    """
    Anything but a callable that accepts two arguments should raise ValueError.
    """

    # Not callable
    with pytest.raises(ValueError):
        event_model.DocumentRouter(emit=1)

    # Wrong callback signature
    with pytest.raises(ValueError):
        event_model.DocumentRouter(emit=lambda: None)

    # Wrong callback signature
    with pytest.raises(ValueError):
        event_model.DocumentRouter(emit=lambda a: None)

    # Wrong callback signature
    with pytest.raises(ValueError):
        event_model.DocumentRouter(emit=lambda a, b, c: None)

    # Right callback signature
    event_model.DocumentRouter(emit=lambda a, b: None)
예제 #2
0
def test_emit():
    collector = []

    def cb(name, doc):
        collector.append((name, doc))

    router = event_model.DocumentRouter(emit=cb)
    name = 'start'
    doc = {'uid': 'asdf', 'time': 0}
    router.emit(name, doc)
    assert len(collector) == 1
    assert collector == [(name, doc)]

    # Test that we hold a weakref only.
    del cb
    gc.collect()
    router.emit(name, doc)
    assert len(collector) == 1
예제 #3
0
def test_emit_with_method():
    collector = []

    class Thing:
        def cb(self, name, doc):
            collector.append((name, doc))

    thing = Thing()

    router = event_model.DocumentRouter(emit=thing.cb)
    name = 'start'
    doc = {'uid': 'asdf', 'time': 0}
    router.emit(name, doc)
    assert len(collector) == 1
    assert collector == [(name, doc)]

    # Test that we hold a weakref only.
    del thing
    gc.collect()
    router.emit(name, doc)
    assert len(collector) == 1
예제 #4
0
def test_document_router_smoke_test():
    dr = event_model.DocumentRouter()
    run_bundle = event_model.compose_run()
    dr('start', run_bundle.start_doc)
    desc_bundle = run_bundle.compose_descriptor(
        data_keys={'motor': {'shape': [], 'dtype': 'number', 'source': '...'},
                   'image': {'shape': [512, 512], 'dtype': 'number',
                             'source': '...', 'external': 'FILESTORE:'}},
        name='primary')
    dr('descriptor', desc_bundle.descriptor_doc)
    desc_bundle_baseline = run_bundle.compose_descriptor(
        data_keys={'motor': {'shape': [], 'dtype': 'number', 'source': '...'}},
        name='baseline')
    dr('descriptor', desc_bundle_baseline.descriptor_doc)
    res_bundle = run_bundle.compose_resource(
        spec='TIFF', root='/tmp', resource_path='stack.tiff',
        resource_kwargs={})
    dr('resource', res_bundle.resource_doc)
    datum_doc1 = res_bundle.compose_datum(datum_kwargs={'slice': 5})
    datum_doc2 = res_bundle.compose_datum(datum_kwargs={'slice': 10})
    dr('datum', datum_doc1)
    dr('datum', datum_doc2)
    event1 = desc_bundle.compose_event(
        data={'motor': 0, 'image': datum_doc1['datum_id']},
        timestamps={'motor': 0, 'image': 0}, filled={'image': False},
        seq_num=1)
    dr('event', event1)
    event2 = desc_bundle.compose_event(
        data={'motor': 0, 'image': datum_doc2['datum_id']},
        timestamps={'motor': 0, 'image': 0}, filled={'image': False},
        seq_num=2)
    dr('event', event2)
    event3 = desc_bundle_baseline.compose_event(
        data={'motor': 0},
        timestamps={'motor': 0},
        seq_num=1)
    dr('event', event3)
    dr('stop', run_bundle.compose_stop())