def test_decode_bad_message(): """Test decode bad message.""" msg = Message.decode("bad") assert not msg msg = Message.decode('["val1", "val2"]') assert not msg
async def hello_process( server: "CPIAServer", message: Message, planet: Optional[str] = None ) -> Message: """Run the process hello command. This command creates a process the first time it's run. """ if planet is None: planet = "Jupiter" if "hello_process" not in server.store: server.store["hello_process"] = create_process(server, create_state) recv, send = server.store["hello_process"] await send(planet) try: old_planet, new_planet = await recv() except ReceiveError: return message LOGGER.info( "Hello! The old planet was %s. The new planet is %s", old_planet, new_planet ) reply = message.copy() reply.data["old_planet"] = old_planet reply.data["new_planet"] = new_planet return reply
async def hello_persistent( server: "CPIAServer", message: Message, planet: Optional[str] = None ) -> Message: """Run the persistent hello command. This command creates a state the first time it's run. """ if planet is None: planet = "Jupiter" if "hello_persistent_state" not in server.store: server.store["hello_persistent_state"] = create_state() command_task = server.store["hello_persistent_state"] old_planet, new_planet = command_task(planet) LOGGER.info( "Hello! The old planet was %s. The new planet is %s", old_planet, new_planet ) reply = message.copy() reply.data["old_planet"] = old_planet reply.data["new_planet"] = new_planet return reply
def test_message_encode(): """Test message encode.""" msg_string = '{"cli": "client-1", "cmd": "hello", "dta": {"param1": "world"}}\n' msg = Message.decode(msg_string) msg_encoded = msg.encode() assert msg_encoded == msg_string
def test_message_decode(): """Test message decode.""" msg = Message.decode( '{"cli": "client-1", "cmd": "hello", "dta": {"param1": "world"}}' ) assert msg.client == "client-1" assert msg.command == "hello" assert msg.data == {"param1": "world"}
async def check_args(server, message, **data): # type: ignore """Check arguments.""" try: data = vol_schema(data) except vol.Invalid as exc: err = humanize_error(data, exc) LOGGER.error("Received invalid data for command %s: %s", message.command, err) return Message(client=message.client, command="invalid", data=data) return await func(server, message, **data)
async def hello_slow( server: "CPIAServer", message: Message, planet: Optional[str] = None ) -> Message: """Run the slow hello command.""" if planet is None: planet = "Jupiter" result = await server.run_process_job(do_cpu_work) LOGGER.info("Hello %s! The result is %s", planet, result) reply = message.copy() reply.data["result"] = result return reply