async def gen(sd, reference, **kwargs): assert await ( yield DeviceMessages.EchoRequest(echoing=b"hi", target=V.device.serial) ) assert not await ( yield DeviceMessages.EchoRequest(echoing=b"hi", target=V.device2.serial) ) for i in range(5): assert await ( yield DeviceMessages.EchoRequest(echoing=b"hi", target=V.device.serial) )
class AddressPlan(Plan): """ Return the ``(ip, port)`` for this device """ messages = [DeviceMessages.EchoRequest(echoing=b"get_remote_addr")] class Instance(Plan.Instance): def process(self, pkt): self.address = pkt.Information.remote_addr return True async def info(self): return self.address
with ServicesResponder.limited_services(device, Services.UDP, AnotherService): async for m in responder.respond(device, get_service, "UDP"): got.append(m) assert got == [state_service1, state_service2] got = [] with ServicesResponder.limited_services(device, MemoryService): async for m in responder.respond(device, get_service, "UDP"): got.append(m) assert got == [state_service3] got = [] with ServicesResponder.limited_services(device, mock.Mock(name="Service")): async for m in responder.respond(device, get_service, "UDP"): got.append(m) assert got == [] describe "EchoResponder": @pytest.fixture() def responder(self, device): return device.echo_responder async it "returns an EchoResponse", device, responder: pkt = DeviceMessages.EchoRequest(echoing=b"hello") got = [] async for m in responder.respond(device, pkt, "memory"): got.append(m) assert got == [DeviceMessages.EchoResponse(echoing=pkt.echoing)]
async with target.session() as sender: msg = DiscoveryMessages.GetService() got = defaultdict(list) async for pkt in sender(msg, device.serial): got[pkt.serial].append(pkt.payload.as_dict()) assert dict(got) == {"d073d5000001": [{"service": Services.UDP, "port": device_port}]} lantarget = LanTarget.create(options) async with lantarget.session() as sender: await sender.add_service( device.serial, Services.UDP, host="127.0.0.1", port=device_port ) msg = DeviceMessages.EchoRequest(echoing=b"hi") got = defaultdict(list) async for pkt in sender(msg, device.serial): got[pkt.serial].append(pkt.payload.as_dict()) assert dict(got) == {"d073d5000001": [{"echoing": b"hi" + b"\x00" * 62}]} async it "works without sockets": device = FakeDevice("d073d5000001", [], use_sockets=False) options = {"final_future": asyncio.Future(), "protocol_register": protocol_register} target = MemoryTarget.create(options, {"devices": device}) await device.start()
describe "Sending messages": @pytest.fixture def V(self, _setup): class V: target = _setup[0] device = _setup[1] device_port = device.services[0].state_service.port return V() describe "send api": async it "works with the sender as sender api", V: async with V.target.session() as sender: original = DeviceMessages.EchoRequest(echoing=b"hi") got = defaultdict(list) async for pkt in sender(original, V.device.serial): assert pkt.Information.remote_addr == ("127.0.0.1", V.device_port) assert pkt.Information.sender_message is original got[pkt.serial].append(pkt.payload.as_dict()) assert dict(got) == {V.device.serial: [{"echoing": b"hi" + b"\x00" * 62}]} async it "works with target.send api", V: original = DeviceMessages.EchoRequest(echoing=b"hi") got = defaultdict(list) async for pkt in V.target.send(original, V.device.serial): assert pkt.Information.remote_addr == ("127.0.0.1", V.device_port)
async def choose_transport(s, original, services): called.append("choose_transport") return services["SERV"] async def make_transport(s, serial, service, kwargs): called.append("make_transport") assert service == "SERV" return T(s) comms = C(V.transport_target) serial = "d073d5000001" await comms.add_service(serial, "SERV") assert called == ["make_transport", "__eq__"] original = DeviceMessages.EchoRequest(echoing=b"ping") packet = original.clone() packet.update(source=1, sequence=1, target=serial) res = await comms.send_single(original, packet, timeout=2) assert len(res) == 1 pkt, addr, orig = res[0] assert pkt | DeviceMessages.EchoResponse assert pkt.echoing[: pkt.echoing.find(b"\x00")] == b"pong" assert addr == ("fake://device", 56700) assert orig is original assert called == [ "make_transport", "__eq__",
describe "Sending messages": @pytest.fixture def V(self, sender): class V: target = sender.transport_target device = devices["one"] device2 = devices["two"] device_port = devices["one"].io["UDP"].options.port return V() describe "send api": async it "works with the sender as sender api", V, sender: original = DeviceMessages.EchoRequest(echoing=b"hi") got = defaultdict(list) async for pkt in sender(original, V.device.serial): assert pkt.Information.remote_addr == ("127.0.0.1", V.device_port) assert pkt.Information.sender_message is original got[pkt.serial].append(pkt.payload.as_dict()) assert dict(got) == {V.device.serial: [{"echoing": b"hi" + b"\x00" * 62}]} async it "works with target.send api", V: original = DeviceMessages.EchoRequest(echoing=b"hi") V.target.default_broadcast = ("127.0.0.1", V.device_port) got = defaultdict(list) async for pkt in V.target.send(original, V.device.serial):