def view_geosquizzy_listening(app, socket_io, timeout): # with app.app_context(): # http://kronosapiens.github.io/blog/2014/08/14/understanding-contexts-in-flask.html with app.test_request_context('geosquizzy'): socket_io.emit('geosquizzy', {'status': 1, 'data': None}) try: client = GsSocketClient(HOST='localhost', PORT=6004, FAMILY=AF_INET, TYPE=SOCK_STREAM) client.connect() # start = time.time() while True: data = client.socket.recv(1024) if data: # TODO some error occurred - > unexpected EOF while parsing (<string>, line 1) res = eval(str(data, 'utf-8')) if res == 0: socket_io.emit('geosquizzy', {'status': 0, 'data': None}) break else: socket_io.emit('geosquizzy', {'status': 2, 'data': res}) except (Exception,) as err: print(err)
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 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)
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 __init__(self, **kwargs): if not (kwargs.get('socket_options', None) is None): self.Socket = GsSocketClient(**kwargs.get('socket_options')) self.ProgressQueue = queue.Queue() else: self.Socket = None self.ProgressQueue = None self.FeTree = FeaturesTree(**kwargs) self.Fsm = GeojsonFiniteStateMachine(progress_queue=self.ProgressQueue, structure=self.FeTree) self.geojson = None self.options = kwargs.get('geojson_options', {}) self.__processes__()
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")
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)