async def test_message_to_topic(self): """Test converting a message to a topic.""" async with async_protocol_manager() as protocol: tests = await import_commands() for test_info in tests: self._topic = None address = repr(random_address()) curr_test = tests[test_info] if curr_test.get("address") is not None: curr_test["address"] = address msgs = [create_message(curr_test)] curr_topic = curr_test["topic"].format(address) subscribe_topic(self.capture_topic, curr_topic) send_data(msgs, protocol.read_queue) try: await asyncio.sleep(0.1) assert self._topic.name == curr_topic except asyncio.TimeoutError: raise AssertionError( "Failed timed out {} with test topic {}".format( test_info, curr_test.get("topic") ) ) except (AssertionError, AttributeError): raise AssertionError( "Failed test {} with test topic {}".format( test_info, curr_test.get("topic") ) ) finally: unsubscribe_topic(self.capture_topic, curr_topic)
async def test_receive_on_msg(self): """Test receiving an ON message.""" async with async_protocol_manager() as protocol: last_topic = None def topic_received(cmd1, cmd2, target, user_data, hops_left, topic=pub.AUTO_TOPIC): """Receive the OFF topic for a device.""" nonlocal last_topic last_topic = topic.name address = random_address() byte_data = create_std_ext_msg(address, 0x80, 0x11, 0xFF, target=Address("000001")) expected_topic = "{}.{}.on.broadcast".format(address.id, 1) pub.subscribe(topic_received, expected_topic) on_cmd = DataItem(byte_data, 0) data = [on_cmd] send_data(data, protocol.read_queue) await asyncio.sleep(0.05) assert last_topic == expected_topic
async def test_inbound(self): """Test direct command.""" async with async_protocol_manager(auto_ack=False) as protocol: tests = await import_commands() subscribe_topic(self.async_validate_values, pub.ALL_TOPICS) for test_info in tests: self._current_test = test_info test_command = tests[test_info] cmd_class = test_command.get("command") params = test_command.get("params") inbound_message = test_command.get("message") if params.get("address") == "": params["address"] = random_address() inbound_message["address"] = params["address"] self._assert_tests = test_command.get("assert_tests") obj = get_class_or_method(commands, cmd_class) cmd = obj(**params) inbound_data_item = create_message(inbound_message, 0) self._call_count = 0 send_data([inbound_data_item], protocol.read_queue) await sleep(0.1) assert self._call_count == 1 assert self._assert_result
async def test_modem_inbound(self): """Test direct command.""" async with async_protocol_manager(auto_ack=False) as protocol: tests = await import_modem_commands() pub.subscribe(self.validate_values, pub.ALL_TOPICS) for test_info in tests: self._current_test = test_info test_command = tests[test_info] command = test_command.get("command") cmd_class = command.get("class") inbound_message = command.get("inbound_message")["data"] self._assert_tests = test_command.get("assert_tests") obj = get_class_or_method(commands, cmd_class) cmd = obj() inbound_data = unhexlify(f"02{inbound_message}") ack_response_item = DataItem(inbound_data, 0) send_data([ack_response_item], protocol.read_queue) await sleep(0.1)
async def test_receive_on_msg(self): """Test receiving an ON message.""" topic_lock = asyncio.Lock() async with async_protocol_manager() as protocol: last_topic = None def topic_received(cmd1, cmd2, target, user_data, hops_left, topic=pub.AUTO_TOPIC): """Receive the OFF topic for a device.""" nonlocal last_topic last_topic = topic.name if topic_lock.locked(): topic_lock.release() address = random_address() byte_data = create_std_ext_msg(address, 0x80, 0x11, 0xFF, target=Address("000001")) expected_topic = "{}.{}.on.broadcast".format(address.id, 1) pub.subscribe(topic_received, expected_topic) on_cmd = DataItem(byte_data, 0) data = [on_cmd] await topic_lock.acquire() send_data(data, protocol.read_queue) try: await asyncio.wait_for(topic_lock.acquire(), 2) assert last_topic == expected_topic except asyncio.TimeoutError: assert expected_topic is None if topic_lock.locked(): topic_lock.release()
def listen_for_ack(): send_data(msgs, protocol.read_queue)