def run_data_server(data_server_websocket, data_server_back_seconds, data_server_cleanup_interval, data_server_interval): """Run a CachedDataServer (to be called as a separate process), accepting websocket connections to receive data to be cached and served. """ # First get the port that we're going to run the data server on. Because # we're running it locally, it should only have a port, not a hostname. # We should try to handle it if they prefix with a ':', though. data_server_websocket = data_server_websocket or DEFAULT_DATA_SERVER_WEBSOCKET websocket_port = int(data_server_websocket.split(':')[-1]) server = CachedDataServer(port=websocket_port, interval=data_server_interval) # The server will start serving in its own thread after # initialization, but we need to manually fire up the cleanup loop # if we want it. Maybe we should have this also run automatically in # its own thread after initialization? server.cleanup_loop()
def test_basic(self): WEBSOCKET_PORT = 8766 cds = CachedDataServer(port=WEBSOCKET_PORT) cds.cache_record({ 'fields': { 'field_1': 'value_11', 'field_2': 'value_21', 'field_3': 'value_31' } }) # We call this in ensure_future, below async def run_test(): async with websockets.connect('ws://localhost:%d' % WEBSOCKET_PORT) as ws: now = time.time() ##### to_send = {'type': 'fields'} await ws.send(json.dumps(to_send)) result = await ws.recv() logging.info('got fields result: %s', result) response = json.loads(result) self.assertEqual(response.get('data', None), ['field_1', 'field_2', 'field_3']) ##### to_send = { 'type': 'publish', 'data': { 'timestamp': time.time(), 'fields': { 'field_1': 'value_12', 'field_2': 'value_22' } } } await ws.send(json.dumps(to_send)) result = await ws.recv() logging.info('got publish result: %s', result) ##### to_send = { 'type': 'subscribe', 'interval': 0.2, 'fields': { 'field_1': { 'seconds': 1555507118 }, 'field_2': { 'seconds': 0 }, 'field_3': { 'seconds': -1 } } } await ws.send(json.dumps(to_send)) result = await ws.recv() logging.info('got subscribe result: %s', result) ##### to_send = {'type': 'ready'} await ws.send(json.dumps(to_send)) await asyncio.sleep(0.1) result = await ws.recv() logging.info('got ready 1 result: %s', result) response = json.loads(result) self.assertEqual(len(response['data']), 2) self.assertEqual(len(response['data']['field_1']), 2) self.assertEqual(response['data']['field_1'][0][1], 'value_11') self.assertEqual(response['data']['field_1'][1][1], 'value_12') self.assertEqual(len(response['data']['field_3']), 1) self.assertEqual(response['data']['field_3'][0][1], 'value_31') ##### to_send = { 'type': 'publish', 'data': { 'timestamp': time.time(), 'fields': { 'field_1': 'value_13', 'field_2': 'value_23' } } } await ws.send(json.dumps(to_send)) await asyncio.sleep(0.1) result = await ws.recv() logging.info('got publish result: %s', result) ##### to_send = {'type': 'ready'} await ws.send(json.dumps(to_send)) await asyncio.sleep(0.1) result = await ws.recv() logging.info('got ready 2 result: %s', result) response = json.loads(result) self.assertEqual(len(response['data']), 2) self.assertEqual(len(response['data']['field_1']), 1) self.assertEqual(response['data']['field_1'][0][1], 'value_13') self.assertEqual(len(response['data']['field_2']), 1) self.assertEqual(response['data']['field_2'][0][1], 'value_23') ##### to_send = { 'type': 'publish', 'data': { 'timestamp': time.time(), 'fields': { 'field_3': 'value_33' } } } await ws.send(json.dumps(to_send)) await asyncio.sleep(0.1) result = await ws.recv() logging.info('got publish result: %s', result) ##### to_send = {'type': 'ready'} await ws.send(json.dumps(to_send)) await asyncio.sleep(0.1) result = await ws.recv() logging.info('got ready 3 result: %s', result) response = json.loads(result) self.assertEqual(len(response['data']), 1) self.assertEqual(len(response['data']['field_3']), 1) self.assertEqual(response['data']['field_3'][0][1], 'value_33') asyncio.new_event_loop().run_until_complete(run_test()) time.sleep(1)
def test_basic(self): """Basic test""" # Create the CachedDataServer we're going to try to connect to # and seed it with some initial values. cds = CachedDataServer(port=WEBSOCKET_PORT) cds.cache_record({ 'fields': { 'field_1': 'value_11', 'field_2': 'value_21', 'field_3': 'value_31' } }) cds.cache_record({ 'fields': { 'field_1': 'value_12', 'field_2': 'value_22', 'field_3': 'value_32' } }) # Initialize our CachedDataReader - it won't try to connect to the # server until we actually call read(). We'll ask for 10 seconds # of back data so that we'll get both of the above records on a # single read and will have to parse them out. subscription = { 'fields': { 'field_1': { 'seconds': 10 }, 'field_2': { 'seconds': 10 }, 'field_3': { 'seconds': 10 } } } cdr = CachedDataReader(subscription=subscription, data_server='localhost:%d' % WEBSOCKET_PORT) response = cdr.read() self.assertDictEqual(response.get('fields', None), { 'field_1': 'value_11', 'field_2': 'value_21', 'field_3': 'value_31' }) response = cdr.read() self.assertDictEqual(response.get('fields', None), { 'field_1': 'value_12', 'field_2': 'value_22', 'field_3': 'value_32' }) cds.cache_record( {'fields': { 'field_1': 'value_13', 'field_2': 'value_23' }}) response = cdr.read() self.assertDictEqual(response.get('fields', None), { 'field_1': 'value_13', 'field_2': 'value_23' }) # Fire off a thread that will signal 'quit' in 0.5 seconds threading.Thread(name='quit_thread', target=cdr.quit, args=(0.5, )).start() # Read without anything coming down the pike - should hang until # we get 'quit' response = cdr.read()