def test_send_msg_when_server_recv_not_time(self): expected = b'Enter time' import threading thread_1 = threading.Thread(target=tcp_server.TcpServer().run) thread_1.start() recv_msg, send_msg = tcp_client.TcpClient().run(b'not_time') thread_1.join() self.assertEqual(expected, recv_msg)
def test_send_msg_when_server_recv_time(self): import datetime expectedFormat = "%Y-%m-%dT%H:%M:%S.%f" import threading thread_1 = threading.Thread(target=tcp_server.TcpServer().run) thread_1.start() recv_msg, send_msg = tcp_client.TcpClient().run() # expectedFormat以外の文字列が来るとValueError Exceptionが発生してテストがFailする actual = datetime.datetime.strptime(recv_msg.decode('utf-8'), expectedFormat) thread_1.join()
def __init__(self): tcp_server.add_on_connet(self.client_on_connect) tcp_server.add_on_disconnet(self.client_on_disconnect) self.client_list = [] self.dev_list = [] self.dev_reg = False self.dev_logout = False self.state_report = False self.connect = True self.tcp_server = tcp_server.TcpServer() self.server_thread = threading.Thread(target=self.tcp_server.run) self.server_thread.daemon = True self.server_thread.start()
def thread_function(): loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) ex = exchanger.Exchanger() try: # start http server for browser hh = http_handler.GhostTextHttpHandlerFactory(8765) http_svr = tcp_server.TcpServer(loop, hh) try: http_svr.start() except: print('fail to start tcp svr on localhost:4001') http_svr = None raise ServerStartException # start tcp server for vim channel to connect ch = vim_channel_handler.Channel(ex.channel_rx_coro()) ch_svr = tcp_server.TcpServer(loop, ch) try: ch_svr.start(host='localhost', port='4002') except: print('fail to start channel svr on localhost:4002') ch_svr = None raise ServerStartException # exchanger needs channel to send message ex.channel = ch # start websockets server ws_manager = vim_websocket_handler.Manager( ex.websocket_rx_coro()) ws_svr = websocket_server.WebsocketServer(loop, ws_manager) try: ws_svr.start() except: print('fail to start channel svr on localhost:4002') ws_svr = None raise ServerStartException # exchanger needs ws_manager to send message ex.ws_manager = ws_manager except ServerStartException: self.running = False self.start_event.set() else: # run until the stop request event will be set self.running = True self.start_event.set() loop.run_until_complete(wait_for_stop(loop)) # teardown try: ws_svr.close() ch_svr.close() http_svr.close() loop.close() except: pass
def test_send_msg_when_recv_not_time(self, mock_socket): expected = b'Enter time' server = tcp_server.TcpServer() recv_msg, send_msg = server.run() self.assertEqual(expected, send_msg)
def test_send_msg_when_recv_time(self, mock_socket, mock_datetime): expected = b'2019-11-13 00:00:00.000000' server = tcp_server.TcpServer() recv_msg, send_msg = server.run() self.assertEqual(expected, send_msg)
sys.path.insert(0, parent_path) import asyncio import http_handler import tcp_server # wakeup hack for windows async def wakeup(): while True: await asyncio.sleep(1) print('test_http_server') loop = asyncio.get_event_loop() h = http_handler.GhostTextHttpHandlerFactory(8765) print('start server') s = tcp_server.TcpServer(loop, h) s.start() # add wakeup HACK for windows loop.create_task(wakeup()) try: loop.run_forever() except KeyboardInterrupt as e: pass s.close() loop.close()
} async def console_coro(): while True: line = await ainput(">>> ") if line == 'q' or line == 'quit' or line == 'exit': break if channel == None: print('channel not connected, ignore the input') continue line = commands.get(line, line) await channel.send(line) print('test_channel_server') loop = asyncio.get_event_loop() channel = vim_channel_handler.Channel(rx_coro) s = tcp_server.TcpServer(loop, channel) s.start() print('input json message sent to vim') print('try input following command :') for i, c in commands.items(): print('press "{}" for: {}'.format(i, c)) loop.run_until_complete(console_coro()) s.close() loop.close()