def run_with_already_running_loop(): """ An already running loop is passed to ModbusClient Factory :return: """ log.debug("Running Async client with asyncio loop already started") log.debug("------------------------------------------------------") def done(future): log.info("Done !!!") def start_loop(loop): """ Start Loop :param loop: :return: """ asyncio.set_event_loop(loop) loop.run_forever() loop = asyncio.new_event_loop() t = Thread(target=start_loop, args=[loop]) t.daemon = True # Start the loop t.start() assert loop.is_running() asyncio.set_event_loop(loop) loop, client = ModbusClient(schedulers.ASYNC_IO, port=5020, loop=loop) print(type(loop)) print(type(client)) print(type(client.protocol)) future = asyncio.run_coroutine_threadsafe(start_async_test( client.protocol), loop=loop) future.add_done_callback(done) while not future.done(): time.sleep(0.1) loop.stop() log.debug("--------DONE RUN_WITH_ALREADY_RUNNING_LOOP-------------") log.debug("")
def testTcpTornadoClient(self, mock_iostream, mock_ioloop): """ Test the TCP tornado client client initialize """ protocol, future = AsyncModbusTCPClient(schedulers.IO_LOOP, framer=ModbusSocketFramer(ClientDecoder())) client = future.result() assert(isinstance(client, AsyncTornadoModbusTcpClient)) assert(0 == len(list(client.transaction))) assert(isinstance(client.framer, ModbusSocketFramer)) assert(client.port == 502) assert client._connected assert(client.stream.connect.call_count == 1) assert(client.stream.read_until_close.call_count == 1) def handle_failure(failure): assert(isinstance(failure.exception(), ConnectionException)) d = client._build_response(0x00) d.add_done_callback(handle_failure) assert(client._connected) client.close() protocol.stop() assert(not client._connected)
def run_with_already_running_loop(): """ An already running loop is passed to ModbusClient Factory :return: """ log.debug("Running Async client with asyncio loop already started") log.debug("------------------------------------------------------") def done(future): log.info("Done !!!") def start_loop(loop): """ Start Loop :param loop: :return: """ asyncio.set_event_loop(loop) loop.run_forever() loop = asyncio.new_event_loop() t = Thread(target=start_loop, args=[loop]) t.daemon = True # Start the loop t.start() assert loop.is_running() asyncio.set_event_loop(loop) loop, client = ModbusClient(schedulers.ASYNC_IO, port=5020, loop=loop) future = asyncio.run_coroutine_threadsafe( start_async_test(client.protocol), loop=loop) future.add_done_callback(done) while not future.done(): time.sleep(0.1) loop.stop() log.debug("--------DONE RUN_WITH_ALREADY_RUNNING_LOOP-------------") log.debug("")