def test_compose2(): root = os.path.dirname(os.path.realpath(__file__)) dataDir = os.path.join(root, 'test_data', 'apps_config') path = os.path.join(dataDir, 'apps_channel_builder.yaml') appsConfig = AppsConfig(path) appkey = 'health' rules = appsConfig.getChannelBuilderRules(appkey) msg = { 'action': 'rtm/publish', 'body': { 'message': { 'device': { 'game': 'ody' }, 'id': 'engine_fps_id' }, 'channels': ['sms_republished_v1_neo'], }, } updatedMsg = updateMsg(rules, msg) channels = updatedMsg['body']['channels'] assert len(channels) == 4 assert 'sms_republished_v1_neo' in channels assert 'ody_engine_fps_id' in channels assert 'foo' in channels assert 'bar' in channels
def test_none_error(): root = os.path.dirname(os.path.realpath(__file__)) dataDir = os.path.join(root, 'test_data', 'apps_config') path = os.path.join(dataDir, 'apps_channel_builder_none_error.yaml') appsConfig = AppsConfig(path) appkey = '_pubsub' rules = appsConfig.getChannelBuilderRules(appkey) msg = {'action': 'rtm/publish', 'body': {'channels': ['a_channel']}} updatedMsg = updateMsg(rules, msg) channels = updatedMsg['body']['channels'] assert len(channels) == 1 assert 'a_channel' in channels
def test_remove_channel(): root = os.path.dirname(os.path.realpath(__file__)) dataDir = os.path.join(root, 'test_data', 'apps_config') path = os.path.join(dataDir, 'apps_channel_builder.yaml') appsConfig = AppsConfig(path) appkey = 'health' rules = appsConfig.getChannelBuilderRules(appkey) msg = { 'action': 'rtm/publish', 'body': { 'message': { 'data': { 'rate_control': { 'engine_fps_id': 60, 'engine_memory_used_id': 60, 'engine_message_loop_id': 86400, } }, 'device': None, 'id': 'sms_set_rate_control_id', 'per_id_counter': 0, 'session': '98c3eb2c042e42068fc4d2b39fe720f8', 'timestamp': 1582066343026, 'version': 1, }, 'channels': ['a_channel', 'sms_live_shard_v1.wiso.9'], }, } updatedMsg = updateMsg(rules, msg) channels = updatedMsg['body']['channels'] assert len(channels) == 3 assert 'a_channel' in channels assert 'sms_live_shard_v1.wiso.9' not in channels assert 'bar' in channels
async def handlePublish(state: ConnectionState, ws, app: Dict, pdu: JsonDict, serializedPdu: str): '''Here we don't write back a result to the client for efficiency. Client doesn't really needs it. ''' # Potentially add extra channels with channel builder rules rules = app['apps_config'].getChannelBuilderRules(state.appkey) pdu = updateMsg(rules, pdu) # Missing message message = pdu.get('body', {}).get('message') if message is None: errMsg = 'publish: empty message' logging.warning(errMsg) response = { "action": "rtm/publish/error", "id": pdu.get('id', 1), "body": { "error": errMsg }, } await state.respond(ws, response) return # Missing channels channel = pdu.get('body', {}).get('channel') channels = pdu.get('body', {}).get('channels') if channel is None and channels is None: errMsg = 'publish: no channel or channels field' logging.warning(errMsg) response = { "action": "rtm/publish/error", "id": pdu.get('id', 1), "body": { "error": errMsg }, } await state.respond(ws, response) return if channels is None: channels = [channel] streams = {} appkey = state.appkey redis = app['redis_clients'].getRedisClient(appkey) for chan in channels: # sanity check to skip empty channels if chan is None: continue try: maxLen = app['channel_max_length'] stream = '{}::{}'.format(appkey, chan) streamId = await redis.xadd(stream, 'json', serializedPdu, maxLen) streams[chan] = streamId except Exception as e: # await publishers.erasePublisher(appkey, chan) # FIXME errMsg = f'publish: cannot connect to redis {e}' logging.warning(errMsg) response = { "action": "rtm/publish/error", "id": pdu.get('id', 1), "body": { "error": errMsg }, } await state.respond(ws, response) return app['stats'].updateChannelPublished(chan, len(serializedPdu)) response = { "action": "rtm/publish/ok", "id": pdu.get('id', 1), "body": { 'channels': channels }, } await state.respond(ws, response) # Stats app['stats'].updatePublished(state.role, len(serializedPdu))