# Path: ogn/client/client.py
# class TelnetClient:
#     def __init__(self, settings=settings):
#         self.logger = logging.getLogger(__name__)
#         self.logger.info("Connect to local telnet server")
#         self.settings = settings
# 
#     def connect(self):
#         self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#         self.sock.connect((self.settings.TELNET_SERVER_HOST, self.settings.TELNET_SERVER_PORT))
# 
#     def run(self, callback, autoreconnect=False):
#         while True:
#             try:
#                 self.sock_file = self.sock.makefile(mode='rw', encoding='iso-8859-1')
#                 while True:
#                     packet_str = self.sock_file.readline().strip()
#                     callback(packet_str)
# 
#             except ConnectionRefusedError:
#                 self.logger.error('Telnet server not running', exc_info=True)
# 
#             if autoreconnect:
#                 sleep(1)
#                 self.connect()
#             else:
#                 return
# 
#     def disconnect(self):
#         self.logger.info('Disconnect')
#         self.sock.shutdown(0)
#         self.sock.close()


import unittest
import unittest.mock as mock
from ogn.client.client import TelnetClient



class TelnetClientTest(unittest.TestCase):
    @mock.patch('ogn.client.client.socket')
    def test_connect_disconnect(self, socket_mock):
        client = TelnetClient()
        client.connect()
        client.sock.connect.assert_called_once_with(('localhost', 50001))

        client.disconnect()
        client.sock.shutdown.assert_called_once_with(0)
        client.sock.close.assert_called_once_with()

    @mock.patch('ogn.client.client.socket')
    def test_run(self, socket_mock):
        def callback(raw_message):
            raise ConnectionRefusedError


# Path: ogn/client/client.py
# class TelnetClient:
#     def __init__(self, settings=settings):
#         self.logger = logging.getLogger(__name__)
#         self.logger.info("Connect to local telnet server")
#         self.settings = settings
# 
#     def connect(self):
#         self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#         self.sock.connect((self.settings.TELNET_SERVER_HOST, self.settings.TELNET_SERVER_PORT))
# 
#     def run(self, callback, autoreconnect=False):
#         while True:
#             try:
#                 self.sock_file = self.sock.makefile(mode='rw', encoding='iso-8859-1')
#                 while True:
#                     packet_str = self.sock_file.readline().strip()
#                     callback(packet_str)
# 
#             except ConnectionRefusedError:
#                 self.logger.error('Telnet server not running', exc_info=True)
# 
#             if autoreconnect:
#                 sleep(1)
#                 self.connect()
#             else:
#                 return
# 
#     def disconnect(self):
#         self.logger.info('Disconnect')
#         self.sock.shutdown(0)
#         self.sock.close()


# Path: tests/client/test_TelnetClient.py
import unittest
import unittest.mock as mock
from ogn.client.client import TelnetClient




class TelnetClientTest(unittest.TestCase):
    @mock.patch('ogn.client.client.socket')
    def test_connect_disconnect(self, socket_mock):
        client = TelnetClient()
        client.connect()
        client.sock.connect.assert_called_once_with(('localhost', 50001))

        client.disconnect()
        client.sock.shutdown.assert_called_once_with(0)
        client.sock.close.assert_called_once_with()

    @mock.patch('ogn.client.client.socket')
    def test_run(self, socket_mock):
        def callback(raw_message):
            raise ConnectionRefusedError


        client = TelnetClient()