Esempio n. 1
0
def test_debug(circuit):
    """Test set_debug()."""
    def blocks_set():
        return [i for i, blk in enumerate(blocks) if blk.debug]

    blocks = [
        edzed.FuncBlock('F1', func=lambda: None),  # CBlock
        edzed.FuncBlock('F2', func=lambda: None),  # CBlock
        edzed.Timer('T1'),  # SBlock
        edzed.Timer('T2'),  # SBlock
    ]

    assert blocks_set() == []
    assert circuit.set_debug(True, '*') == 4
    # check real block count without duplicates
    assert circuit.set_debug(True, '*', *blocks, 'F1', 'F1') == 4
    assert blocks_set() == [0, 1, 2, 3]
    assert circuit.set_debug(False, blocks[0]) == 1
    assert blocks_set() == [1, 2, 3]
    assert circuit.set_debug(False, 'F2') == 1
    assert blocks_set() == [2, 3]
    assert circuit.set_debug(True, edzed.CBlock) == 2
    assert blocks_set() == [0, 1, 2, 3]
    assert circuit.set_debug(False, '?2') == 2
    assert blocks_set() == [0, 2]
Esempio n. 2
0
def test_getblocks(circuit):
    """Test getblocks() and findblock()."""
    # 1 Const, 2 CBlocks and 2 SBlocks
    edzed.Const(0)
    f1 = edzed.FuncBlock('F1', func=lambda: None)  # CBlock
    f2 = edzed.FuncBlock('F2', func=lambda: None)  # CBlock
    t1 = edzed.Timer('T1')  # SBlock
    t2 = edzed.Timer('T2')  # SBlock
    assert circuit.findblock('F1') is f1
    assert circuit.findblock('T2') is t2
    assert set(circuit.getblocks()) == {f1, f2, t1, t2}
    assert not set(circuit.getblocks(
        btype=edzed.Const))  # Const blocks are not registered
    assert set(circuit.getblocks(btype=edzed.CBlock)) == {f1, f2}
    assert set(circuit.getblocks(btype=edzed.SBlock)) == {t1, t2}
Esempio n. 3
0
def test_resolver_type_checking3(circuit):
    """Unsuccessful type check without resolving."""
    tmr = edzed.Timer(None)

    t = types.SimpleNamespace(c=tmr)
    with pytest.raises(TypeError, match='should be Counter'):
        circuit.resolve_name(t, 'c', edzed.Counter)
Esempio n. 4
0
def test_resolver_noop(circuit):
    """Only names need to be resolved."""
    tmr = edzed.Timer('tmr_name')

    t = types.SimpleNamespace(a=tmr)
    circuit.resolve_name(t, 'a')
    assert len(circuit._resolver._unresolved) == 0
Esempio n. 5
0
def test_resolver_type_checking2(circuit):
    """Unsuccessful type check."""
    tmr = edzed.Timer('tmr_name')

    t = types.SimpleNamespace(c='tmr_name')
    circuit.resolve_name(t, 'c', edzed.Input)
    with pytest.raises(TypeError, match='should be Input'):
        circuit._resolver.resolve()
Esempio n. 6
0
def test_resolver_type_checking1(circuit):
    """Successful type check."""
    tmr = edzed.Timer('tmr_name')

    t = types.SimpleNamespace(a='tmr_name', b='tmr_name')
    circuit.resolve_name(t, 'a', edzed.SBlock)
    circuit.resolve_name(t, 'b', edzed.Timer)
    circuit._resolver.resolve()
    assert t.a is t.b is tmr
Esempio n. 7
0
def test_resolver_basic(circuit):
    """Test the basic function."""
    tmr = edzed.Timer('tmr_name')

    resolver = circuit._resolver

    t = types.SimpleNamespace(a='tmr_name')
    circuit.resolve_name(t, 'a')
    assert len(resolver._unresolved) == 1
    resolver.resolve()
    assert len(resolver._unresolved) == 0
    assert t.a is tmr
Esempio n. 8
0
async def test_restartable(circuit):
    """Test restartable vs. not restartable."""
    rlogger = TimeLogger('rlogger')
    rmono = edzed.Timer('rtimer', t_on=0.12, on_output=edzed.Event(rlogger))
    nlogger = TimeLogger('nlogger')
    nmono = edzed.Timer('ntimer',
                        t_on=0.12,
                        on_output=edzed.Event(nlogger),
                        restartable=False)

    asyncio.create_task(circuit.run_forever())
    await asyncio.sleep(0.05)
    assert rmono.event('start')  # start OK
    assert nmono.event('start')  # start OK
    await asyncio.sleep(0.05)
    assert rmono.event('start')  # re-start OK
    assert not nmono.event('start')  # re-start not ok!
    await asyncio.sleep(0.1)
    assert rmono.event('start')  # re-start OK
    assert nmono.event('start')  # start OK
    await asyncio.sleep(0.05)
    rmono.event('start')
    nmono.event('start')
    await asyncio.sleep(0.25)
    await circuit.shutdown()

    RLOG = [
        (0, False),
        (50, True),
        (370, False),
    ]
    rlogger.compare(RLOG)
    NLOG = [
        (0, False),
        (50, True),
        (170, False),  # 120 ms
        (200, True),
        (320, False),  # 120 ms
    ]
    nlogger.compare(NLOG)
Esempio n. 9
0
def timelimit(limit, error):
    """Create a timer stopping the circuit after some time limit."""
    edzed.Timer(
        'test_utils_timelimit',
        t_off=limit,
        on_output=edzed.Event(
            '_ctrl',  # automatic edzed.ControlBlock('_ctrl')
            'abort' if error else 'shutdown',
            efilter=(
                edzed.Edge(rise=True), lambda data: {
                    **data, 'error': 'time limit exceeded'
                }
                # shutdown event type will ignore the 'error' item
            )))
Esempio n. 10
0
async def test_output(circuit):
    """Output is properly set when on_output is called."""
    def test_timer(value):
        assert value == timer.output
        assert timer.state == ('on' if value else 'off')

    testfunc = edzed.OutputFunc('testfunc', func=test_timer, on_error=None)
    timer = edzed.Timer('timer', on_output=edzed.Event(testfunc))

    asyncio.create_task(circuit.run_forever())
    await asyncio.sleep(0.0)
    timer.event('start')
    timer.event('stop')
    timer.event('start')
    timer.event('stop')
    await circuit.shutdown()
Esempio n. 11
0
async def test_clock(circuit):
    """Test a trivial clock signal generator."""
    timelimit(0.8, error=False)
    logger = TimeLogger('logger')
    clock = edzed.Timer('timer',
                        t_on=0.05,
                        t_off=0.1,
                        on_output=edzed.Event(logger))

    try:
        await circuit.run_forever()
    except asyncio.CancelledError:
        pass
    LOG = [(0, False), (100, True), (150, False), (250, True), (300, False),
           (400, True), (450, False), (550, True), (600, False), (700, True),
           (750, False)]
    logger.compare(LOG)
Esempio n. 12
0
def test_static(circuit):
    """Test states, events, output."""
    bis = edzed.Timer('timer')
    init(circuit)

    def trans(event, state):
        bis.event(event)
        assert bis.state == 'on' if state else 'off'
        assert bis.output is bool(state)

    assert bis.output is False
    trans('start', True)
    trans('stop', False)
    trans('stop', False)
    trans('toggle', True)
    trans('start', True)
    trans('start', True)
    trans('toggle', False)
    trans('toggle', True)
    trans('start', True)
    trans('toggle', False)
    trans('start', True)
Esempio n. 13
0
async def test_duration(circuit):
    """Test variable timer duration."""
    logger = TimeLogger('logger')
    mono = edzed.Timer('timer', t_on=0.2, on_output=edzed.Event(logger))

    asyncio.create_task(circuit.run_forever())
    await asyncio.sleep(0.0)
    mono.event('start')
    await asyncio.sleep(0.25)
    mono.event('start', duration=0.05)
    await asyncio.sleep(0.1)
    mono.event('start', duration=None)
    await asyncio.sleep(0.25)
    await circuit.shutdown()
    LOG = [
        (0, True),
        (200, False),  # 200 ms
        (250, True),
        (300, False),  # 50 ms
        (350, True),
        (550, False),  # 200 ms
    ]
    logger.compare(LOG)
Esempio n. 14
0
def test_keys(circuit):
    """Test keys identifying the persistent block data."""
    assert edzed.Input('ipers').key == "<Input 'ipers'>"
    assert edzed.Timer('tpers').key == "<Timer 'tpers'>"
Esempio n. 15
0
async def test_no_busy_loop(circuit):
    """Busy loop (zero period clock) is detected as chained events."""
    timer = edzed.Timer('timer', t_on=0, t_off=0)

    with pytest.raises(edzed.EdzedCircuitError, match="infinite loop?"):
        await circuit.run_forever()
Esempio n. 16
0
import asyncio
import edzed

edzed.Timer('clk',
            comment="clock generator",
            t_on=0.5,
            t_off=0.5,
            on_output=edzed.Event('out'))
edzed.OutputFunc('out',
                 func=lambda value: print('..tock' if value else 'tick..'),
                 on_error=None)

if __name__ == '__main__':
    print('Press ctrl-C to stop')
    circuit = edzed.get_circuit()
    asyncio.run(circuit.run_forever())