def test_message_task(): X = [[1,2],[3,4],[5,6]] y = [0,1,0] label_name = ['Y', 'N'] with fake_uuid(): t = dto.Message(const_msg.TYPE_TASK, dto.Data(dto.get_id(), X, None)) assert json.dumps(t.to_dict(), sort_keys=True) == MSG_TASK1
def parse_msg(data) -> dto.Message: try: data_ = json.loads(data) return dto.Message(**data_) except TypeError as e: raise e except Exception as e: raise e
def client_msg(self, msg): res = dto.Message(const_msg.TYPE_ERROR, dto.Error("Nothing to do", None)) try: data = json.loads(msg.data) client_msg = dto.Message(**data) except TypeError as e: return dto.Message(const_msg.TYPE_ERROR, dto.Error(str(e), None), reply_id=data.get(const_msg.ID, None)) except Exception as e: logger.exception("Invalid message from client") raise e #Get task request if client_msg.type == const_msg.TYPE_TASK: return self.msg_push_task(client_msg.msg_id) #Client post solution if client_msg.type == const_msg.TYPE_SOLUTION: #create solution try: sol = dto.Solution(**data[const_msg.PAYLOAD]) except TypeError as e: return dto.Message(const_msg.TYPE_ERROR, dto.Error(str(e), None), reply_id=data[const_msg.ID]) except Exception as e: logger.exception("Can't decode solution from client") return dto.Message(const_msg.TYPE_ERROR, dto.Error("An exception occured", None), reply_id=data[const_msg.ID]) if sol: task = self.app["task"] if task and task.task_id == sol.task_id: self.app["solution"] = sol assert not self.app["solution_event"].is_set() self.app["solution_event"].set() return dto.Message(const_msg.TYPE_ACK, reply_id=data[const_msg.ID]) error_msg = "{} task not found".format(sol.task_id) # TODO This case requires support on client side. return dto.Message(const_msg.TYPE_ERROR, dto.Error(error_msg, None), reply_id=data[const_msg.ID]) return res
async def test_push_task(aiohttp_client, loop): ws = WebServer(loop=loop) task1 = create_task() #await ws.add_task(task1) ws.app["task"] = task1 app = ws.app client = await aiohttp_client(app) async with client.ws_connect(WS_URL) as ws: solution_id = dto.get_id() #wait for task meassage msg1 = await read_msg(ws, const_msg.TYPE_TASK) task2 = dto.Data(**msg1.payload) assert task1.task_id == task2.task_id assert len(task1.x) == len(task2.x) #wait for update meassage msg2 = await read_msg(ws, const_msg.TYPE_UPDATE) #send solution await ws.send_str(jd(dto.Message(const_msg.TYPE_SOLUTION, create_solution(), msg_id=solution_id))) #wait for ask msg3 = await read_msg(ws, const_msg.TYPE_ACK) assert msg3.reply_id == solution_id await ws.close() #?
def msg_push_update(self): data = None if self.app["update"]: data = self.app["update"] return dto.Message(const_msg.TYPE_UPDATE, data)
def msg_push_task(self, reply_id=None): task = None if self.app["task"]: task = self.app["task"] return dto.Message(const_msg.TYPE_TASK, task, reply_id=reply_id)
async def stop(self): for ws in self.app['sockets']: await ws.send_str(self.jd(dto.Message(const_msg.TYPE_STOP))) await self.app_runner.cleanup()