def setUp(self):
        self.socket_options = {
            'HOST': 'localhost',
            'PORT': 8030,
            'FAMILY': AF_INET,
            'TYPE': SOCK_STREAM,
            'CONNECTIONS': 2
        }
        self.socket_client = GsSocketClient(**self.socket_options)
        self.socket_server = GsSocketServer(**self.socket_options)

        self.geojson_options = {
            'geojson_options': {
                'mode': 'static',
                'geojson_type': 'FeatureCollection'
            },
            'outcome_options': {},
            'optim': {
                'batch': 1,
                'loss': -5.0
            },
            'socket_options': self.socket_options
        }
        self.data = get_geojson(path="/home/ing/PycharmProjects/geo-squizzy/"
                                "geosquizzy/build_big_data/data/dump1000.json")
Example #2
0
 def setUp(self):
     self.options = {'HOST': 'localhost',
                     'PORT': 8030,
                     'FAMILY': AF_INET,
                     'TYPE': SOCK_STREAM,
                     'CONNECTIONS': 2}
     self.socket_server = GsSocketServer(**self.options)
     self.socket_client = GsSocketClient(**self.options)
Example #3
0
class SocketServerClientConnectionTest(unittest.TestCase):
    """
    Testing server/client write <-> read interactions
    """
    def setUp(self):
        self.options = {'HOST': 'localhost',
                        'PORT': 8030,
                        'FAMILY': AF_INET,
                        'TYPE': SOCK_STREAM,
                        'CONNECTIONS': 2}
        self.socket_server = GsSocketServer(**self.options)
        self.socket_client = GsSocketClient(**self.options)

    def tearDown(self):
        # self.socket_server.disconnect()
        pass
        # kill server_socket
        # tcpkill read ->

    def test_bind_socket(self):
        """
        Create Two sockets server and client, bind and connect them to the same port, triggering
        Write/Read talks and shutdown/close them
        """
        self.socket_server.create_connection()
        server_thread = threading.Thread(target=self.socket_server.run)
        server_thread.daemon = True
        server_thread.start()
        """
        Small timeout is needed here (could be caused by Thread not executed run function when client.connect()
        is called)
        """
        time.sleep(0.5)
        self.socket_client.connect()

        def __messaging__(socket, message):
            """
            :param socket: socket client
            :param message: 'TEST'+int()
            :return: Bool(message==res)
            """
            socket.write(message)
            res = socket.read(1024)
            if res:
                return message == str(res, 'utf-8')

        [self.assertTrue(__messaging__(self.socket_client, 'TEST'+str(x))) for x in range(0, 1000, 1)]
        self.socket_client.disconnect()
    def setUp(self):
        self.socket_options = {'HOST': 'localhost',
                               'PORT': 8030,
                               'FAMILY': AF_INET,
                               'TYPE': SOCK_STREAM,
                               'CONNECTIONS': 2}
        self.socket_client = GsSocketClient(**self.socket_options)
        self.socket_server = GsSocketServer(**self.socket_options)

        self.geojson_options = {'geojson_options': {'mode': 'static', 'geojson_type': 'FeatureCollection'},
                                'outcome_options': {},
                                'optim': {'batch': 1, 'loss': -5.0},
                                'socket_options': self.socket_options
                                }
        self.data = get_geojson(path="/home/ing/PycharmProjects/geo-squizzy/"
                                     "geosquizzy/build_big_data/data/dump1000.json")
Example #5
0
 def __init__(self, *args, **kwargs):
     Demon.__init__(self, *args, **kwargs)
     GsSocketServer.__init__(self, *args, **kwargs)
Example #6
0
 def __init__(self, *args, **kwargs):
     Demon.__init__(self, *args, **kwargs)
     GsSocketServer.__init__(self, *args, **kwargs)
class SocketClientServerConnectionTest(unittest.TestCase):
    """
    Testing interactions between server/clients during geosquizzy algorithm execution
    """
    def setUp(self):
        self.socket_options = {
            'HOST': 'localhost',
            'PORT': 8030,
            'FAMILY': AF_INET,
            'TYPE': SOCK_STREAM,
            'CONNECTIONS': 2
        }
        self.socket_client = GsSocketClient(**self.socket_options)
        self.socket_server = GsSocketServer(**self.socket_options)

        self.geojson_options = {
            'geojson_options': {
                'mode': 'static',
                'geojson_type': 'FeatureCollection'
            },
            'outcome_options': {},
            'optim': {
                'batch': 1,
                'loss': -5.0
            },
            'socket_options': self.socket_options
        }
        self.data = get_geojson(path="/home/ing/PycharmProjects/geo-squizzy/"
                                "geosquizzy/build_big_data/data/dump1000.json")

    def tearDown(self):
        pass

    def test_geosquizzy_sockets(self):
        """
        It testing listening to messages broadcasted by socket server(which receive it from geosquizzy algorithm
        client) by another client
        """

        # Starting server thread
        self.socket_server.create_connection()

        server_thread = threading.Thread(target=self.socket_server.run)
        server_thread.daemon = True
        server_thread.start()

        second_client_data = queue.Queue()

        def second_client_listen(client, q):
            while True:
                data = client.read(1024)
                if data:
                    q.put(str(data, 'utf-8'))
                    q.task_done()
                else:
                    break

        time.sleep(0.5)

        # Starting second client thread
        self.socket_client.connect()
        second_client_thread = threading.Thread(target=second_client_listen,
                                                args=(self.socket_client,
                                                      second_client_data))
        second_client_thread.start()

        # Starting geosquizzy socket client and algorithm execution
        self.geosquizzy = GeoSquizzy(**self.geojson_options)
        self.geosquizzy.start(geojson=self.data)

        # self.socket_client.disconnect()
        time.sleep(0.5)
        self.socket_client.disconnect()

        # Waiting for second_client to receive all data
        second_client_thread.join()

        # Waiting for second_client data
        second_client_data.join()

        # Getting second client
        second_client_res = []
        while True:

            if not second_client_data.empty():
                val = second_client_data.get()

                if bool(val):
                    val = eval(val)
                    second_client_res = second_client_res + val
            else:
                break

        # Getting geosquizzy result
        geosquizzy_res = self.geosquizzy.get_results()

        # Compare test
        org_len = len(geosquizzy_res)
        sock_len = 0
        for x in geosquizzy_res:
            for y in second_client_res:
                if y['keys'] == x['keys']:
                    sock_len += 1
                    break

        # [print(x, '\n') for x in geosquizzy_res]
        # [print(x, '\n') for x in second_client_res]
        # TODO difference is with one key, which is gathered after leaving features array
        self.assertTrue(org_len - 1 == sock_len)
class SocketClientServerConnectionTest(unittest.TestCase):
    """
    Testing interactions between server/clients during geosquizzy algorithm execution
    """
    def setUp(self):
        self.socket_options = {'HOST': 'localhost',
                               'PORT': 8030,
                               'FAMILY': AF_INET,
                               'TYPE': SOCK_STREAM,
                               'CONNECTIONS': 2}
        self.socket_client = GsSocketClient(**self.socket_options)
        self.socket_server = GsSocketServer(**self.socket_options)

        self.geojson_options = {'geojson_options': {'mode': 'static', 'geojson_type': 'FeatureCollection'},
                                'outcome_options': {},
                                'optim': {'batch': 1, 'loss': -5.0},
                                'socket_options': self.socket_options
                                }
        self.data = get_geojson(path="/home/ing/PycharmProjects/geo-squizzy/"
                                     "geosquizzy/build_big_data/data/dump1000.json")

    def tearDown(self):
        pass

    def test_geosquizzy_sockets(self):
        """
        It testing listening to messages broadcasted by socket server(which receive it from geosquizzy algorithm
        client) by another client
        """

        # Starting server thread
        self.socket_server.create_connection()

        server_thread = threading.Thread(target=self.socket_server.run)
        server_thread.daemon = True
        server_thread.start()

        second_client_data = queue.Queue()

        def second_client_listen(client, q):
            while True:
                data = client.read(1024)
                if data:
                    q.put(str(data, 'utf-8'))
                    q.task_done()
                else:
                    break

        time.sleep(0.5)

        # Starting second client thread
        self.socket_client.connect()
        second_client_thread = threading.Thread(target=second_client_listen,
                                                args=(self.socket_client, second_client_data))
        second_client_thread.start()

        # Starting geosquizzy socket client and algorithm execution
        self.geosquizzy = GeoSquizzy(**self.geojson_options)
        self.geosquizzy.start(geojson=self.data)

        # self.socket_client.disconnect()
        time.sleep(0.5)
        self.socket_client.disconnect()

        # Waiting for second_client to receive all data
        second_client_thread.join()

        # Waiting for second_client data
        second_client_data.join()

        # Getting second client
        second_client_res = []
        while True:

            if not second_client_data.empty():
                val = second_client_data.get()

                if bool(val):
                    val = eval(val)
                    second_client_res = second_client_res + val
            else:
                break

        # Getting geosquizzy result
        geosquizzy_res = self.geosquizzy.get_results()

        # Compare test
        org_len = len(geosquizzy_res)
        sock_len = 0
        for x in geosquizzy_res:
            for y in second_client_res:
                if y['keys'] == x['keys']:
                    sock_len += 1
                    break

        # [print(x, '\n') for x in geosquizzy_res]
        # [print(x, '\n') for x in second_client_res]
        # TODO difference is with one key, which is gathered after leaving features array
        self.assertTrue(org_len-1 == sock_len)