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 client_side_stream_call(): """ 客户端流模式,真正调用服务端的函数 """ # 发送请求 _connect_para = SimpleGRpcTools.generate_connect_para(ip='127.0.0.1', port=50051) _cresult = SimpleGRpcTools.grpc_call( _connect_para, client_side_stream_generator(), call_mode=EnumCallMode.ClientSideStream) _cresult.return_obj = SimpleGRpcTools.json_to_object_by_para_mapping( _cresult.return_json, 'service_client_side_stream') 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 = 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 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
def test_connect_pool(self): """ 测试连接池 """ print("开始测试连接池") _connect_para = SimpleGRpcConnection.generate_connect_para( conn_str='127.0.0.1:50051', servicer_name='servicer_simple_call', test_on_connect=False, test_use_health_check=False) # 建立连接池 _pool = SimpleGRpcConnectionPool(_connect_para, name='ConnectionPool', maxsize=3, minsize=0, realse_free_time=5, test_on_get=True, test_on_free=True, test_while_idle=True, test_idle_time=5, validation_query='', get_connection_timeout=1, logger=self._asyn_logger, init_break_if_connect_error=True) print("测试连接池-获取连接并执行") # 尝试获取连接 _connection = _pool.get_connection() _back_server = service_simple_call_para('a1', 'b1') _req_obj = get_client_simple_call_para('a1', 'b1') _back_client = _connection.call(_req_obj) _back_client.return_obj = SimpleGRpcTools.json_to_object_by_para_mapping( _back_client.return_json, 'client_simple_call_para') self.assertTrue( _back_client.is_success(), msg= '测试连接池-获取连接并执行失败,执行RPC失败: code=%s, msg=%s, error=%s, i18n_msg_paras=%s' % (_back_client.code, _back_client.msg, _back_client.error, str(_back_client.i18n_msg_paras))) self.assertTrue(compare_object_by_json(_back_server, _back_client.return_obj), msg='测试连接池-获取连接并执行, 执行参数不一致') print('测试连接池-获取连接超时') _c1 = _pool.get_connection() _c2 = _pool.get_connection() try: _c3 = _pool.get_connection() self.assertTrue(False, msg='测试连接池-获取连接超时失败,应抛出超时') except TimeoutError: pass except Exception as e: self.assertTrue(False, msg='测试连接池-获取连接超时失败,未期望的异常:%s' % str(e)) self.assertTrue(3 == _pool.current_size, msg='测试连接池-获取连接超时-当前连接池大小错误:%d' % _pool.current_size) print('测试连接池-释放连接') _pool.free_connection(_connection) _c3 = _pool.get_connection() # 这样c3可用获取连接并使用 self.assertTrue(3 == _pool.current_size, msg='测试连接池-释放连接-当前连接池大小错误:%d' % _pool.current_size) print('测试连接池-自动释放连接') _pool.free_connection(_c1) _pool.free_connection(_c2) _pool.free_connection(_c3) time.sleep(10) self.assertTrue(0 == _pool.current_size, msg='测试连接池-自动释放连接-当前连接池大小错误:%d' % _pool.current_size)