Пример #1
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 == []
Пример #2
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)]
Пример #3
0
def test(init, open, wait):
    c = Accum('timer://;{}'.format(init),
              name='timer',
              dump='yes',
              context=ctx)
    MSGID = 2

    c.open(open)

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

    assert c.result == []
    assert poll.poll(0) == []

    for w in wait:
        if w is None:
            return
        ts = str2ms(w)
        print("Check {}: {:.3f}ms".format(w, ts))
        assert c.dcaps == c.DCaps.Process | c.DCaps.PollIn
        assert poll.poll(0) == []
        c.process()
        assert c.dcaps == c.DCaps.Process | c.DCaps.PollIn
        assert [m.msgid for m in c.result] == []
        dt = time.time()
        assert poll.poll(2 * ts), [(c.fd == select.POLLIN)]
        dt = 1000 * (time.time() - dt)
        assert 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] == [MSGID]
        c.result = []
    assert poll.poll(0) == []
    assert c.dcaps == 0
Пример #4
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")