def invoke_heartbeat(self, interval=30): while True: yield from asyncio.sleep(interval) pkg = HeartbeatRequest.new_request() resp = yield from self.invoke(pkg) if resp.request_id != pkg.request_id: logger.error("heartbeat response request_id({}) mismatch with request({}).".format(resp.request_id, pkg.request_id)) continue if resp.respstatus != RESPSTATUS.SUCCESS: logger.error("heartbeat response status ({}) on request({}).".format(resp.respstatus, resp.request_id)) continue
def test_heartbeat(self): pkg = HeartbeatRequest.new_request() from socket import socket with socket() as s: s.connect(('127.0.0.1', 12200)) s.send(pkg.to_stream()) buf = b'' buf += s.recv(1024) resp = HeartbeatResponse.from_stream(buf) print(resp) self.assertEqual(resp.request_id, pkg.request_id) self.assertEqual(resp.respstatus, RESPSTATUS.SUCCESS)
def test_heartbeat(self): pkg = HeartbeatRequest.new_request() print(pkg) print(pkg.to_stream()) print(pkg.header) self.assertEqual(pkg.class_len, 0) self.assertEqual(pkg.header_len, 0) self.assertEqual(pkg.content_len, 0) self.assertEqual(pkg.cmdcode, CMDCODE.HEARTBEAT) resp = HeartbeatResponse.response_to(pkg.request_id) print(resp) print(resp.to_stream()) self.assertEqual(resp.class_len, 0) self.assertEqual(resp.header_len, 0) self.assertEqual(resp.content_len, 0) self.assertEqual(resp.cmdcode, CMDCODE.HEARTBEAT) self.assertEqual(resp.respstatus, RESPSTATUS.SUCCESS) self.assertEqual(pkg.request_id, resp.request_id)
async def invoke_heartbeat(self, address): """ Send heartbeat to server :return bool, if the server response properly. TODO: to break the connection if server response wrongly """ pkg = HeartbeatRequest.new_request() resp = await self.invoke(pkg, address=address) if resp.request_id != pkg.request_id: logger.error( "heartbeat response request_id({}) mismatch with request({}).". format(resp.request_id, pkg.request_id)) return False if resp.respstatus != RESPSTATUS.SUCCESS: logger.error( "heartbeat response status ({}) on request({}).".format( resp.respstatus, resp.request_id)) return False return True