def test_zero(args, caps): c = Accum('zero://;' + args, size='1kb', name='server', context=ctx) assert c.dcaps == 0 c.open() assert c.dcaps == caps | c.DCaps.Process if caps & c.DCaps.PollIn: assert c.fd != None
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 == []
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
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']
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
def test_simple(t, v): if isinstance(v, tuple): v, s = v else: s = str(v) scheme = f'''yamls:// - name: msg id: 10 fields: - {{name: f0, type: {t} }} ''' url = Config.load(f'''yamls:// tll.proto: yaml name: yaml dump: scheme config.0: name: msg ''') url['scheme'] = scheme url['config.0.data.f0'] = s c = Accum(url) c.open() c.process() assert [(m.msgid, m.seq) for m in c.result] == [(10, 0)] m = c.unpack(c.result[0]) assert m.as_dict() == {'f0': v}
def test_enum(): scheme = '''yamls:// - name: msg id: 10 enums: Enum: {type: int32, options.type: enum, enum: {A: 10, B: 20}} fields: - {name: f0, type: Enum} - {name: f1, type: Enum} ''' url = Config.load(f'''yamls:// tll.proto: yaml name: yaml dump: scheme config.0: name: msg data: f0: A f1: 20 ''') url['scheme'] = scheme c = Accum(url) c.open() c.process() assert [(m.msgid, m.seq) for m in c.result] == [(10, 0)] m = c.unpack(c.result[0]) assert m.f0 == m.f0.A assert m.f1 == m.f1.B
def test_union(data, r): scheme = '''yamls:// - name: sub fields: - {name: s0, type: int8} - name: msg id: 10 fields: - {name: f0, type: union, union: [{name: i8, type: int8}, {name: string, type: string}, {name: sub, type: sub}, {name: array, type: 'int8[4]'}]} ''' url = Config.load(f'''yamls:// tll.proto: yaml name: yaml dump: scheme config.0: name: msg data.f0: %s ''' % data) url['scheme'] = scheme c = Accum(url) c.open() if r is None: with pytest.raises(TLLError): c.process() assert c.state == c.State.Error return c.process() assert [(m.msgid, m.seq) for m in c.result] == [(10, 0)] m = c.unpack(c.result[0]) assert m.as_dict() == {'f0': r}
def test_many(): scheme = '''yamls:// - name: msg id: 10 fields: - {name: f0, type: int8} ''' url = Config.load('''yamls:// tll.proto: yaml name: yaml dump: scheme autoclose: no config: - {name: msg, seq: 0, data.f0: 0} - {name: msg, seq: 1, data.f0: 1} - {name: msg, seq: 2, data.f0: 2} - {name: msg, seq: 3, data.f0: 3} ''') url['scheme'] = scheme c = Accum(url) c.open() for i in range(4): c.process() assert [(m.msgid, m.seq) for m in c.result] == [(10, j) for j in range(i + 1)] c.process() assert [(m.msgid, m.seq) for m in c.result] == [(10, j) for j in range(4)] for i in range(4): assert c.unpack(c.result[i]).as_dict() == {'f0': i}
def test_message(): scheme = '''yamls:// - name: sub fields: - {name: s0, type: int8} - name: msg id: 10 fields: - {name: f0, type: sub} ''' url = Config.load(f'''yamls:// tll.proto: yaml name: yaml dump: scheme config.0: name: msg data.f0.s0: 123 ''') url['scheme'] = scheme c = Accum(url) c.open() c.process() assert [(m.msgid, m.seq) for m in c.result] == [(10, 0)] m = c.unpack(c.result[0]) assert m.as_dict() == {'f0': {'s0': 123}}
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")
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)]
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
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")
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
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
def test_binary(): url = Config.load(br'''yamls:// tll.proto: yaml name: yaml dump: frame config: - msgid: 1 seq: 10 data: "abc\x01def" ''') c = Accum(url) c.open() c.process() assert c.state == c.State.Active assert [(m.msgid, m.seq, m.data.tobytes()) for m in c.result] == [(1, 10, b'abc\x01def')] c.process() assert c.state == c.State.Closed
def test_list(t, v): scheme = f'''yamls:// - name: msg id: 10 fields: - {{name: f0, type: {t} }} ''' url = Config.load(f'''yamls:// tll.proto: yaml name: yaml dump: scheme config.0: name: msg data.f0: {v} ''') url['scheme'] = scheme c = Accum(url) c.open() c.process() assert [(m.msgid, m.seq) for m in c.result] == [(10, 0)] m = c.unpack(c.result[0]) assert m.as_dict() == {'f0': v}