def test_error(self): """ 测试错误信息 """ if not TEST_FLAG['test_error']: return print("测试错误信息") print("测试错误信息 - 连接失败") _connect_para = SimpleGRpcTools.generate_connect_para(ip='127.0.0.1', port=60051) _cresult = SimpleGRpcTools.grpc_call( _connect_para, SimpleGRpcTools.generate_request_obj('test')) self.assertTrue( _cresult.code == '20408' and grpc.StatusCode.UNAVAILABLE.name == _cresult.i18n_msg_paras[0], '测试错误信息失败 - 连接失败 ') print("测试错误信息 - 服务名不存在") _connect_para = SimpleGRpcTools.generate_connect_para(ip='127.0.0.1', port=50051) _cresult = SimpleGRpcTools.grpc_call( _connect_para, SimpleGRpcTools.generate_request_obj('test', '')) self.assertTrue(_cresult.code == '11403', '测试错误信息失败 - 服务名不存在 ') print("测试错误信息 - 超时") _connect_para = SimpleGRpcTools.generate_connect_para(ip='127.0.0.1', port=50051) _cresult = SimpleGRpcTools.grpc_call( _connect_para, SimpleGRpcTools.generate_request_obj('service_simple_overtime'), timeout=0.1) self.assertTrue(_cresult.code == '30403', '测试错误信息失败 - 超时')
def server_side_stream_call(a, b, *args, **kwargs): """ 服务端流模式,真正调用服务端的函数 """ # 转换参数 _para_values = RunTool.get_current_function_parameter_values(is_simple_mode=True) _para_obj = SimpleGRpcTools.parameters_to_json(_para_values) _req_obj = SimpleGRpcTools.generate_request_obj( service_name='service_server_side_stream', para_json=_para_obj.para_json, has_para_bytes=_para_obj.has_para_bytes, para_bytes=_para_obj.para_bytes ) # 发送请求, 默认使用全局的日志对象 _connect_para = SimpleGRpcConnection.generate_connect_para( ip='127.0.0.1', port=50051 ) _connection = SimpleGRpcConnection(_connect_para) _cresult_iterator = _connection.call(_req_obj, call_mode=EnumCallMode.ServerSideStream) _ret = True for _cresult in _cresult_iterator: if not _cresult.is_success(): _ret = False _connection.close() return _ret
def client_simple_call_para_server_tsl(a, b, *args, c=10, d={'d1': 'd1value'}, **kwargs): """ 测试简单调用,进行单向认证(客户端验证服务端证书) """ # 转换参数 _para_values = RunTool.get_current_function_parameter_values( is_simple_mode=True) _para_obj = SimpleGRpcTools.parameters_to_json(_para_values) _req_obj = SimpleGRpcTools.generate_request_obj( service_name='service_simple_call_para', para_json=_para_obj.para_json, has_para_bytes=_para_obj.has_para_bytes, para_bytes=_para_obj.para_bytes) # 发送请求 with open(_TEMP_DIR + '/../../simple_grpc/server.crt', 'rb') as f: _root_certificates = f.read() _connect_para = SimpleGRpcTools.generate_connect_para( ip='localhost', port=50053, is_use_ssl=True, root_certificates=_root_certificates) _cresult = SimpleGRpcTools.grpc_call(_connect_para, _req_obj) _cresult.return_obj = SimpleGRpcTools.json_to_object_by_para_mapping( _cresult.return_json, 'client_simple_call_para') return _cresult
def client_simple_call_para_double_tsl(a, b, *args, c=10, d={'d1': 'd1value'}, **kwargs): """ 测试简单调用,进行双向认证(服务端验证客户端证书,客户端验证服务端证书) """ # 转换参数 _para_values = RunTool.get_current_function_parameter_values(is_simple_mode=True) _para_obj = SimpleGRpcTools.parameters_to_json(_para_values) _req_obj = SimpleGRpcTools.generate_request_obj( service_name='service_simple_call_para', para_json=_para_obj.para_json, has_para_bytes=_para_obj.has_para_bytes, para_bytes=_para_obj.para_bytes ) # 发送请求 with open(_TEMP_DIR + '/../../simple_grpc/client.pem', 'rb') as f: _client_private_key = f.read() with open(_TEMP_DIR + '/../../simple_grpc/client.crt', 'rb') as f: _client_certificate_chain = f.read() with open(_TEMP_DIR + '/../../simple_grpc/server.crt', 'rb') as f: _root_certificates = f.read() # 发送请求, 默认使用全局的日志对象 _connect_para = SimpleGRpcConnection.generate_connect_para( ip='localhost', port=50052, is_use_ssl=True, root_certificates=_root_certificates, private_key=_client_private_key, certificate_chain=_client_certificate_chain ) _connection = SimpleGRpcConnection(_connect_para) _cresult = _connection.call(_req_obj) _connection.close() _cresult.return_obj = SimpleGRpcTools.json_to_object_by_para_mapping( _cresult.return_json, 'client_simple_call_para' ) return _cresult
def _mutiple_thread_fun(self, i, connect, result: list): """ 多线程处理 """ _i = i + 1 # 参数处理 _para_obj = SimpleGRpcTools.parameters_to_json([['', _i]], is_support_bytes=True) _req_obj = SimpleGRpcTools.generate_request_obj( service_name='service_mutiple_thread', para_json=_para_obj.para_json, has_para_bytes=_para_obj.has_para_bytes, para_bytes=_para_obj.para_bytes ) # 通知外面执行是否成功 result[i] = False # 访问服务 _cresult: CResult = connect.call(_req_obj) self.assertTrue( _cresult.is_success(), msg='_mutiple_thread_fun [%d] error: %s' % (i, str(_cresult)) ) self.assertTrue( _cresult.return_json == str(_i), msg='_mutiple_thread_fun [%d] return error: %s' % (i, str(_cresult.return_json)) ) # 通知外面执行是否成功 result[i] = True
def bidirectional_stream_single_request(a, b, *args, **kwargs): """ 双向流模式,产生请求对象的函数 """ # 转换参数 _para_values = RunTool.get_current_function_parameter_values(is_simple_mode=True) _para_obj = SimpleGRpcTools.parameters_to_json(_para_values) _req_obj = SimpleGRpcTools.generate_request_obj( service_name='service_bidirectional_stream', para_json=_para_obj.para_json, has_para_bytes=_para_obj.has_para_bytes, para_bytes=_para_obj.para_bytes ) return _req_obj
def get_client_simple_call_para(a, b, *args, c=10, d={'d1': 'd1value'}, **kwargs): """ 测试简单调用,直接返回参数 """ # 转换参数 _para_values = RunTool.get_current_function_parameter_values( is_simple_mode=True) _para_obj = SimpleGRpcTools.parameters_to_json(_para_values) _req_obj = SimpleGRpcTools.generate_request_obj( service_name='service_simple_call_para', para_json=_para_obj.para_json, has_para_bytes=_para_obj.has_para_bytes, para_bytes=_para_obj.para_bytes) return _req_obj
def client_simple_call_para(a, b, *args, c=10, d={'d1': 'd1value'}, **kwargs): """ 测试简单调用,直接返回参数 """ # 转换参数 _para_values = RunTool.get_current_function_parameter_values( is_simple_mode=True) _para_obj = SimpleGRpcTools.parameters_to_json(_para_values) _req_obj = SimpleGRpcTools.generate_request_obj( service_name='service_simple_call_para', para_json=_para_obj.para_json, has_para_bytes=_para_obj.has_para_bytes, para_bytes=_para_obj.para_bytes) # 发送请求 _connect_para = SimpleGRpcTools.generate_connect_para( conn_str='127.0.0.1:50051') _cresult = SimpleGRpcTools.grpc_call(_connect_para, _req_obj) _cresult.return_obj = SimpleGRpcTools.json_to_object_by_para_mapping( _cresult.return_json, 'client_simple_call_para') return _cresult
def client_simple_throw_excepiton(): """ 测试简单调用,抛出异常 """ # 转换参数 _para_values = RunTool.get_current_function_parameter_values( is_simple_mode=True) _para_obj = SimpleGRpcTools.parameters_to_json(_para_values) _req_obj = SimpleGRpcTools.generate_request_obj( service_name='service_simple_throw_excepiton', para_json=_para_obj.para_json, has_para_bytes=_para_obj.has_para_bytes, para_bytes=_para_obj.para_bytes) # 发送请求 _connect_para = SimpleGRpcTools.generate_connect_para(ip='127.0.0.1', port=50051) _cresult = SimpleGRpcTools.grpc_call(_connect_para, _req_obj) _cresult.return_obj = SimpleGRpcTools.json_to_object_by_para_mapping( _cresult.return_json, 'client_simple_throw_excepiton') return _cresult
def server_side_stream_call(a, b, *args, **kwargs): """ 服务端流模式,真正调用服务端的函数 """ # 转换参数 _para_values = RunTool.get_current_function_parameter_values( is_simple_mode=True) _para_obj = SimpleGRpcTools.parameters_to_json(_para_values) _req_obj = SimpleGRpcTools.generate_request_obj( service_name='service_server_side_stream', para_json=_para_obj.para_json, has_para_bytes=_para_obj.has_para_bytes, para_bytes=_para_obj.para_bytes) _connect_para = SimpleGRpcTools.generate_connect_para(ip='127.0.0.1', port=50051) with SimpleGRpcTools.generate_channel(_connect_para) as channel: _cresult_iterator = SimpleGRpcTools.grpc_call_by_channel( channel, _req_obj, call_mode=EnumCallMode.ServerSideStream) for _cresult in _cresult_iterator: if not _cresult.is_success(): return False return True
def client_simple_throw_excepiton(): """ 测试简单调用,抛出异常 """ # 转换参数 _para_values = RunTool.get_current_function_parameter_values( is_simple_mode=True) _para_obj = SimpleGRpcTools.parameters_to_json(_para_values) _req_obj = SimpleGRpcTools.generate_request_obj( service_name='service_simple_throw_excepiton', para_json=_para_obj.para_json, has_para_bytes=_para_obj.has_para_bytes, para_bytes=_para_obj.para_bytes) # 发送请求, 默认使用全局的日志对象 _connect_para = SimpleGRpcConnection.generate_connect_para( conn_str='127.0.0.1:50051') _connection = SimpleGRpcConnection(_connect_para) _cresult = _connection.call(_req_obj) _connection.close() _cresult.return_obj = SimpleGRpcTools.json_to_object_by_para_mapping( _cresult.return_json, 'client_simple_throw_excepiton') return _cresult
def client_simple_call_para(a, b, *args, c=10, d={'d1': 'd1value'}, **kwargs): """ 测试简单调用,直接返回参数 """ # 转换参数 _para_values = RunTool.get_current_function_parameter_values(is_simple_mode=True) _para_obj = SimpleGRpcTools.parameters_to_json(_para_values) _req_obj = SimpleGRpcTools.generate_request_obj( service_name='service_simple_call_para', para_json=_para_obj.para_json, has_para_bytes=_para_obj.has_para_bytes, para_bytes=_para_obj.para_bytes ) # 发送请求, 默认使用全局的日志对象 _connect_para = SimpleGRpcConnection.generate_connect_para( conn_str='127.0.0.1:50051', servicer_name='servicer_simple_call', test_on_connect=True, test_use_health_check=False ) _connection = SimpleGRpcConnection(_connect_para) _cresult = _connection.call(_req_obj) _connection.close() _cresult.return_obj = SimpleGRpcTools.json_to_object_by_para_mapping( _cresult.return_json, 'client_simple_call_para' ) return _cresult