Пример #1
0
    def test(self):
        import termios
        p = termios.tcgetattr(self.m)
        print(p)
        p[0] = p[1] = p[3] = 0
        p[4] = p[5] = termios.B9600
        termios.tcsetattr(self.m, 0, p)

        c = Accum('serial://{}'.format(self.tty), context=ctx)
        c.open()
        assert c.fd != -1

        poll = select.poll()
        poll.register(self.m, select.POLLIN)
        poll.register(c.fd, select.POLLIN)

        assert poll.poll(0) == []

        c.post(b'xxx');
        assert poll.poll(0) == [(self.m, select.POLLIN)]
        assert os.read(self.m, 100) == b'xxx'

        os.write(self.m, b'data')
        assert poll.poll(0) == [(c.fd, select.POLLIN)]

        c.process()
        assert [x.data.tobytes() for x in c.result] == [b'data']
Пример #2
0
def test_post(clock, msg, wait):
    ms = str2ms(wait)

    c = Accum('timer://', name='timer', clock=clock, dump='yes', context=ctx)

    c.open()

    poll = select.poll()
    poll.register(c.fd, select.POLLIN)

    assert c.result == []
    assert c.dcaps == 0

    ts = ms / 1000
    if msg == 'absolute':
        ts += time.time()
    c.post({'ts': ts}, name=msg)  # nanoseconds

    assert c.dcaps == c.DCaps.Process | c.DCaps.PollIn
    assert poll.poll(0) == []

    dt = time.time()
    assert poll.poll(2 * ms) == [(c.fd, select.POLLIN)]
    dt = 1000 * (time.time() - dt)
    assert not (
        ts / 2 < dt < 2 *
        ts), "Sleep time {:.3f}ms not in range {:.3f}ms < {:.3f}ms)".format(
            dt, ts / 2, 2 * ts)

    c.process()
    assert [m.msgid for m in c.result] == [2]
    assert c.dcaps == 0
Пример #3
0
def test():
    ctx = C.Context()

    with pytest.raises(TLLError):
        ctx.Channel("echo://;name=echo")
    ctx.register(Echo)
    c = Accum("echo://;name=echo", context=ctx)
    cfg = c.config

    pyc = C.channel_cast(c)
    assert isinstance(pyc, Echo)

    pyc = C.channel_cast(c, Echo)
    assert isinstance(pyc, Echo)

    with pytest.raises(TypeError):
        C.channel_cast(c, TestPrefix)

    assert c.state == c.State.Closed
    assert cfg.get("state", "") == "Closed"
    assert [x.name for x in c.children] == []

    c.open()

    assert [x.name for x in c.children] == ['child', 'orphan']

    with pytest.raises(TypeError):
        C.channel_cast(c.children[0])

    assert c.state == c.State.Opening
    assert cfg.get("state", "") == "Opening"

    c.process()

    assert c.state == c.State.Active
    assert cfg.get("state", "") == "Active"

    assert c.result == []
    c.post(b'xxx', seq=100)
    assert [(m.seq, m.data.tobytes()) for m in c.result], [(100 == b'xxx')]

    c.close()
    assert [x.name for x in c.children] == ['orphan']
    assert c.state == c.State.Closing
    c.process()
    assert c.state == c.State.Closed
    del c

    assert ctx.get('orphan') == None

    ctx.unregister(Echo)
    with pytest.raises(TLLError):
        ctx.Channel("echo://;name=echo")
Пример #4
0
def test_direct():
    s = Accum('direct://', name='server', context=ctx)
    c = Accum('direct://', name='client', master=s, context=ctx)

    assert s.dcaps == 0

    s.open()
    assert s.dcaps == 0

    s.post(b'xxx', seq=10)

    assert c.result == []
    assert s.result == []
    assert s.dcaps == 0

    c.open()
    assert c.dcaps == 0

    s.post(b'yyy', seq=20)
    assert [(m.data.tobytes(), m.seq) for m in c.result] == [(b'yyy', 20)]
    assert s.result == []

    c.post(b'yyy', seq=21)
    assert [(m.data.tobytes(), m.seq) for m in c.result] == [(b'yyy', 20)]
    assert [(m.data.tobytes(), m.seq) for m in s.result] == [(b'yyy', 21)]

    c.close()
    c.result = []
    s.result = []

    s.post(b'yyy', seq=20)
    assert c.result == []
    assert s.result == []
Пример #5
0
def test_post_clear(clock, msg, fail):
    c = Accum('timer://', name='timer', clock=clock, dump='yes', context=ctx)

    c.open(initial='10ms')

    assert c.result == []
    assert c.dcaps == c.DCaps.Process | c.DCaps.PollIn

    if fail == 'fail':
        with pytest.raises(TLLError):
            c.post({'ts': 0}, name=msg)
    else:
        c.post({'ts': 0}, name=msg)
        assert c.dcaps == 0
Пример #6
0
def test_prefix():
    ctx = C.Context()

    with pytest.raises(TLLError):
        ctx.Channel("prefix+null://;name=channel")
    ctx.register(Echo)
    ctx.register(TestPrefix)
    c = Accum("prefix+echo://;name=channel", context=ctx)
    cfg = c.config

    pyc = C.channel_cast(c)
    assert isinstance(pyc, TestPrefix)

    assert c.state == c.State.Closed
    assert cfg.get("state", "") == "Closed"
    assert [x.name for x in c.children] == ['channel/prefix']

    c.open()

    assert [x.name for x in c.children] == ['channel/prefix']

    assert c.state == c.State.Opening
    assert cfg.get("state", "") == "Opening"

    c.process()

    assert c.state == c.State.Opening
    assert cfg.get("state", "") == "Opening"

    c.children[0].process()

    assert c.state == c.State.Active
    assert cfg.get("state", "") == "Active"

    assert c.result == []
    c.post(b'xxx', seq=100)
    assert [(m.seq, m.data.tobytes()) for m in c.result] == [(100, b'xxx')]

    c.result = []
    c.post(b'zzz', seq=200, type=C.Type.Control, addr=0xbeef)
    assert [(m.type, m.seq, m.addr, m.data.tobytes())
            for m in c.result], [(C.Type.Control, 200, 0xbeef, b'zzz')]

    c.close()
    assert [x.name for x in c.children] == ['channel/prefix']
    del c

    ctx.unregister(TestPrefix)
    with pytest.raises(TLLError):
        ctx.Channel("prefix+null://;name=channel")
Пример #7
0
def test_logic():
    ctx = C.Context()
    sl = ctx.stat_list

    with pytest.raises(TLLError):
        ctx.Channel("logic://;name=logic")
    ctx.register(TestLogic)

    with pytest.raises(TLLError):
        ctx.Channel(
            "logic://;name=logic;tll.channel.input=input;tll.channel.output=input"
        )

    i = ctx.Channel('mem://;name=input;dump=yes')
    o = Accum('mem://;name=output;dump=yes', master=i, context=ctx)
    l = ctx.Channel(
        "logic://;name=logic;tll.channel.input=input;tll.channel.output=input;stat=yes"
    )

    assert len(list(sl)) == 1
    assert [x.name for x in sl] == ['logic']
    fields = iter(sl).swap()
    assert [(f.name, f.value) for f in fields[:-1]] == [('rx', 0), ('rx', 0),
                                                        ('tx', 0), ('tx', 0)]
    assert (fields[-1].name, fields[-1].count) == ('time', 0)

    l.open()

    i.open()
    o.open()

    o.post(b'xxx')

    assert [m.data.tobytes() for m in o.result] == []

    i.process()

    assert [m.data.tobytes() for m in o.result] == []

    o.process()

    assert [m.data.tobytes() for m in o.result] == [b'xxx']

    fields = iter(sl).swap()
    assert [(f.name, f.value) for f in fields[:-1]] == [('rx', 1), ('rx', 3),
                                                        ('tx', 0), ('tx', 0)]
    assert (fields[-1].name, fields[-1].count) == ('time', 1)
    assert fields[-1].sum > 1000
Пример #8
0
def test_mem(fd=True, **kw):
    s = Accum('mem://;size=1kb', name='server', context=ctx, fd='yes' if fd else 'no', **kw)

    s.open()
    with pytest.raises(TLLError): s.post(b'x' * 1024)
    s.post(b'xxx', seq=10)

    c = Accum('mem://', name='client', master=s, context=ctx)

    assert c.result == []
    assert s.result == []

    c.open()

    if sys.platform.startswith('linux') and fd:
        assert c.fd != None
    else:
        assert c.fd == None

    if c.fd is not None:
        poll = select.poll()
        poll.register(c.fd, select.POLLIN)
        poll.register(s.fd, select.POLLIN)
        assert poll.poll(0), [(c.fd == select.POLLIN)]

    s.post(b'yyy', seq=20)
    c.process()
    assert s.result == []
    assert [(m.data.tobytes(), m.seq) for m in c.result] == [(b'xxx', 10)]
    if c.fd is not None:
        assert poll.poll(0), [(c.fd == select.POLLIN)]
    assert c.dcaps & c.DCaps.Pending == c.DCaps.Pending
    c.result = []

    c.process()
    assert s.result == []
    assert [(m.data.tobytes(), m.seq) for m in c.result] == [(b'yyy', 20)]
    c.result = []
    if c.fd is not None:
        assert poll.poll(0) == []

    c.process()
    assert c.result == []
    if c.fd is not None:
        assert poll.poll(0) == []

    c.post(b'yyy', seq=21)
    if c.fd is not None:
        assert poll.poll(0) == [(s.fd, select.POLLIN)]
    s.process()
    assert [(m.data.tobytes(), m.seq) for m in s.result] == [(b'yyy', 21)]
Пример #9
0
def test_lz4():
    s = Accum('lz4+direct://', name='server', context=ctx)
    c = Accum('direct://', name='client', master=s, context=ctx)

    s.open()
    c.open()

    s.post(b'xxx', seq=10)

    assert [(m.data.tobytes(), m.seq) for m in c.result] == [(lz4.block.compress(b'xxx', store_size=False), 10)]
    assert s.result == []

    c.post(lz4.block.compress(b'yyy', store_size=False), seq=21)
    assert [(m.data.tobytes(), m.seq) for m in s.result] == [(b'yyy', 21)]

    c.post(b'xxx')
    assert s.state == s.State.Error