├── framework ├── LogServer │ ├── CMakeLists.txt │ ├── main.cpp │ ├── LogServer.h │ └── LogServer.cpp ├── StatServer │ ├── CMakeLists.txt │ ├── main.cpp │ ├── StatServer.h │ ├── StatImp.h │ └── StatHashMap.h └── CMakeLists.txt ├── protocol └── TarsTest │ ├── UnitTest │ ├── CMakeLists.txt │ ├── DyeingTest.tars │ ├── TarsPingTest.tars │ ├── BServant.tars │ ├── UserInfo.tars │ ├── TupDemo.tars │ ├── TypeDemo.tars │ ├── Proxy.tars │ ├── AServant.tars │ ├── AServantImp.h │ ├── UnitTest.h │ ├── TarsPingTestImp.cpp │ ├── TarsPingTestImp.h │ ├── AServantImp.cpp │ ├── BServantImp.h │ ├── TupDemoImp.h │ ├── BServantImp.cpp │ ├── TupDemoImp.cpp │ ├── DyeingTestImp.h │ ├── ProxyImp.h │ ├── DyeingTestImp.cpp │ ├── TypeDemoImp.h │ ├── ProxyImp.cpp │ ├── TupProxyImp.h │ ├── TupCallback.h │ └── TypeDemoImp.cpp │ └── TestcaseServer │ ├── OneWayRpcTest.tars │ ├── RPCTest.tars │ ├── CMakeLists.txt │ ├── EpsTestImp.cpp │ ├── RPCTestImp.cpp │ ├── OneWayRpcTestImp.cpp │ ├── HttpDemoImp.h │ ├── RPCTestImp.h │ ├── EpsTestImp.h │ ├── OneWayRpcTestImp.h │ ├── HttpDemoImp.cpp │ └── TestcaseServer.h ├── testcode ├── source │ ├── stub │ │ ├── RegisteryStub │ │ │ └── DbHandle.cpp │ │ ├── MockProxy │ │ │ ├── MockServerObjProxy.cpp │ │ │ └── MockProxyObjCallback.cpp │ │ └── PushServerDemo │ │ │ ├── TestPushServantImp.h │ │ │ └── TestPushServantImp.cpp │ ├── testcase │ │ ├── util │ │ │ ├── test_tc_cgi.cpp │ │ │ ├── test_tc_singleton.cpp │ │ │ ├── template.config.conf │ │ │ ├── test_tc_thread.cpp │ │ │ ├── test_tc_gzip.cpp │ │ │ ├── log.config.conf │ │ │ ├── test_tc_option.cpp │ │ │ ├── test_tc_pack.cpp │ │ │ ├── test_tc_mmap.cpp │ │ │ ├── test_tc_mem_chunk.cpp │ │ │ ├── test_tc_mem_queue.cpp │ │ │ ├── test_tc_autoptr.cpp │ │ │ ├── test_tc_config.cpp │ │ │ ├── test_tc_thread_queue.cpp │ │ │ └── test_tc_log.cpp │ │ ├── UdpRpcTest.cpp │ │ ├── TarsPingTest.cpp │ │ ├── OneWayRpcTest.cpp │ │ ├── PropertyReportTest.cpp │ │ ├── MockProxyTest.cpp │ │ ├── SimpleRPCTest.cpp │ │ ├── GlobalUTTest.cpp │ │ ├── framework │ │ │ └── testAdminRegistry │ │ │ │ └── test_tc_AdminRegistry.cpp │ │ ├── LoggerTest.cpp │ │ ├── StressTest.cpp │ │ ├── TupTest.cpp │ │ ├── ProxyTest.cpp │ │ ├── CoroutineRpcTest.cpp │ │ ├── ActiveDyeTest.cpp │ │ ├── PropertyTest.cpp │ │ ├── BaseRpcTest.cpp │ │ ├── DyeingCaseTest.cpp │ │ ├── AdminTest.cpp │ │ ├── CommGetEpTest.cpp │ │ ├── TupProxyTest.cpp │ │ └── TupRpc.cpp │ ├── utils │ │ └── ServerAdmin.cpp │ └── main.cpp ├── CMakeLists.txt └── include │ ├── TarsTest.h │ ├── utils │ └── ServerAdmin.h │ ├── stub │ ├── TestPushServantImp.h │ ├── MockServerObjProxy.h │ ├── MockProxyObjCallback.h │ ├── MockProxyObjImp.h │ └── QueryImp.h │ ├── RunTestThread.h │ └── TarsServantName.h ├── .gitignore ├── script ├── run_test.sh ├── lcov.sh └── CMakeLists.txt ├── README.zh.md ├── LICENSE ├── conf ├── TarsTest.LogServer.config.conf └── TarsTest.StatServer.config.conf ├── README.md └── CMakeLists.txt /framework/LogServer/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | build_unitest(logServer) 3 | -------------------------------------------------------------------------------- /framework/StatServer/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | build_unitest(statServer) 3 | -------------------------------------------------------------------------------- /protocol/TarsTest/UnitTest/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | build_unitest("unitTestServer") 3 | 4 | -------------------------------------------------------------------------------- /framework/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_subdirectory(LogServer) 2 | add_subdirectory(StatServer) 3 | -------------------------------------------------------------------------------- /protocol/TarsTest/TestcaseServer/OneWayRpcTest.tars: -------------------------------------------------------------------------------- 1 | module TarsTest 2 | { 3 | interface OneWayRpcTest 4 | { 5 | int test(); 6 | }; 7 | }; 8 | -------------------------------------------------------------------------------- /testcode/source/stub/RegisteryStub/DbHandle.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TarsCloud/Tars-Unittest/HEAD/testcode/source/stub/RegisteryStub/DbHandle.cpp -------------------------------------------------------------------------------- /testcode/source/testcase/util/test_tc_cgi.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/TarsCloud/Tars-Unittest/HEAD/testcode/source/testcase/util/test_tc_cgi.cpp -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /project/build/* 2 | /result/* 3 | /covResult/* 4 | /tools/* 5 | /protocol/*.o 6 | /protocol/*.d 7 | /taf_result.xml 8 | 9 | *.o 10 | *.d 11 | -------------------------------------------------------------------------------- /protocol/TarsTest/TestcaseServer/RPCTest.tars: -------------------------------------------------------------------------------- 1 | 2 | module TarsTest 3 | { 4 | 5 | interface RPCTest 6 | { 7 | int test(); 8 | int testStr(out string sOut); 9 | }; 10 | 11 | }; 12 | -------------------------------------------------------------------------------- /protocol/TarsTest/UnitTest/DyeingTest.tars: -------------------------------------------------------------------------------- 1 | 2 | module TarsTest 3 | { 4 | 5 | interface DyeingTest 6 | { 7 | int test(); 8 | int testDyeing(routekey string strIn,out string strOut); 9 | }; 10 | 11 | }; 12 | -------------------------------------------------------------------------------- /protocol/TarsTest/UnitTest/TarsPingTest.tars: -------------------------------------------------------------------------------- 1 | module TarsTest 2 | { 3 | 4 | interface TarsPingTest 5 | { 6 | int test(); 7 | //int testDyeing(routekey string strIn,out string strOut); 8 | }; 9 | 10 | }; 11 | 12 | -------------------------------------------------------------------------------- /protocol/TarsTest/TestcaseServer/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | include_directories(../../../testcode/include) 3 | include_directories(../../../../servant/protocol/servant) 4 | include_directories(../../../../servant/protocol/framework) 5 | build_unitest_lib("TestcaseServer") 6 | 7 | -------------------------------------------------------------------------------- /protocol/TarsTest/UnitTest/BServant.tars: -------------------------------------------------------------------------------- 1 | 2 | module TarsTest 3 | { 4 | interface BServant 5 | { 6 | int test(); 7 | //int testInt(int iIn,out int iOut); 8 | int testHello(string sReq, out string sRsp); 9 | //int saySomething(string something,out string strOut); 10 | //int queryTest(ReqInfo req, out vector vRsp); 11 | }; 12 | 13 | }; 14 | -------------------------------------------------------------------------------- /protocol/TarsTest/UnitTest/UserInfo.tars: -------------------------------------------------------------------------------- 1 | module Test 2 | { 3 | struct FriendInfo 4 | { 5 | 1 require int uin; 6 | 2 require string nick; 7 | 3 optional vector birthday; 8 | }; 9 | struct UserInfo 10 | { 11 | 1 require int qq; 12 | 2 require string nick; 13 | 3 require vector birthday; 14 | 5 require map friends; 15 | 4 optional string city = "gd"; 16 | }; 17 | }; 18 | -------------------------------------------------------------------------------- /protocol/TarsTest/UnitTest/TupDemo.tars: -------------------------------------------------------------------------------- 1 | module TarsTest 2 | { 3 | struct AddInt 4 | { 5 | 0 require int adda; 6 | 1 require int addb; 7 | 2 optional string addStr="this is a add b"; 8 | }; 9 | 10 | struct Result 11 | { 12 | 0 require int addResult; 13 | 1 require string strResult; 14 | }; 15 | 16 | interface TupDemo 17 | { 18 | //int test(); 19 | int aAddb(AddInt addData, out Result aAddbResult); 20 | int helloWord(string strIn, out string strOut); 21 | }; 22 | }; -------------------------------------------------------------------------------- /testcode/source/utils/ServerAdmin.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * ServerAdmin.cpp 3 | * 4 | * Created on: 2018年10月16日 5 | * Author: abelguo 6 | */ 7 | #include "utils/ServerAdmin.h" 8 | #include "servant/AdminF.h" 9 | #include "servant/Application.h" 10 | 11 | TARSTEST_NS_START 12 | 13 | ServerAdmin::ServerAdmin() 14 | { 15 | _comm = new Communicator(); 16 | } 17 | ServerAdmin& ServerAdmin::shutdown(const string& serverName) 18 | { 19 | AdminFPrx adminPrx = _comm->stringToProxy (serverName); 20 | 21 | adminPrx->shutdown(); 22 | 23 | return *this; 24 | } 25 | 26 | TARSTEST_NS_END 27 | 28 | -------------------------------------------------------------------------------- /protocol/TarsTest/UnitTest/TypeDemo.tars: -------------------------------------------------------------------------------- 1 | 2 | module TarsTest 3 | { 4 | 5 | interface TypeDemo 6 | { 7 | int test(); 8 | int echoInt(int iInput,out int iEcho); 9 | int echoBool(bool bInput,out bool bEcho); 10 | int echoShort(short sInput,out short sEcho); 11 | int echoLong(long lInput,out long lEcho); 12 | int echoByte(byte bInput,out byte bEcho); 13 | int echoString(string strInput,out string strEcho); 14 | int echoVector(vector vInput,out vector vEcho); 15 | int echoMap(map mInput,out map mEcho); 16 | int echoMultiType(vector> mtInput, out vector> mtEcho); 17 | }; 18 | 19 | }; 20 | -------------------------------------------------------------------------------- /testcode/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | # include_directories(${util_SOURCE_DIR}/include) 3 | include_directories(${CMAKE_BINARY_DIR}/src/gtest/include) 4 | link_directories(${CMAKE_BINARY_DIR}/src/gtest/lib) 5 | link_directories(${CMAKE_BINARY_DIR}/src/gtest/lib64) 6 | 7 | include_directories(include) 8 | include_directories(../protocol) 9 | 10 | include_directories(../../servant/protocol/servant) 11 | include_directories(../../servant/protocol/framework) 12 | build_unitest("testcase") 13 | 14 | IF(WIN32) 15 | target_link_libraries(testcase TestcaseServer ${LIB_GTEST}) 16 | ELSE() 17 | target_link_libraries(testcase TestcaseServer ${LIB_GTEST} z) 18 | ENDIF() 19 | add_dependencies(testcase TestcaseServer) -------------------------------------------------------------------------------- /protocol/TarsTest/UnitTest/Proxy.tars: -------------------------------------------------------------------------------- 1 | /** 2 | * Tencent is pleased to support the open source community by making Tars available. 3 | * 4 | * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. 5 | * 6 | * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except 7 | * in compliance with the License. You may obtain a copy of the License at 8 | * 9 | * https://opensource.org/licenses/BSD-3-Clause 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | module TarsTest 18 | { 19 | 20 | interface Proxy 21 | { 22 | int test(); 23 | int testProxy(string sReq, out string sRsp); 24 | }; 25 | 26 | }; 27 | -------------------------------------------------------------------------------- /protocol/TarsTest/UnitTest/AServant.tars: -------------------------------------------------------------------------------- 1 | 2 | module TarsTest 3 | { 4 | struct ServiceState{ 5 | 0 require int time_interval_s = -1; 6 | 1 require int succ_count = -1; 7 | 2 require int total_time_ms = -1; 8 | 3 require int timeout_count = -1; 9 | 4 require int except_count = -1; 10 | }; 11 | struct ReqInfo 12 | { 13 | 0 require string sServerName; 14 | 1 require string sDate; 15 | 2 require string sStartTime; 16 | 3 require string sEndTime; 17 | 4 require long uInterval= 0; 18 | 5 require vector vIpList; 19 | }; 20 | 21 | struct RspInfo 22 | { 23 | 0 require string sSlaveName; 24 | 1 require string sMasterIp; 25 | 2 require string sStartTime; 26 | 3 require string sEndTime; 27 | }; 28 | interface AServant 29 | { 30 | int test(); 31 | int testInt(int iIn,out int iOut); 32 | int testHello(string sReq, out string sRsp); 33 | int saySomething(string something,out string strOut); 34 | int queryTest(ReqInfo req, out vector vRsp); 35 | }; 36 | 37 | }; 38 | -------------------------------------------------------------------------------- /script/run_test.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | killall -9 logServer 4 | killall -9 statServer 5 | killall -9 unitTestServer 6 | 7 | bin/logServer --config=../unittest/conf/TarsTest.LogServer.config.conf& 8 | 9 | bin/statServer --config=../unittest/conf/TarsTest.StatServer.config.conf& 10 | 11 | bin/unitTestServer --config=../unittest/conf/TarsTest.UnitTest.config.conf& 12 | 13 | # cd ../testcase 14 | 15 | if [ "$debug_mode"x == "gdb"x ] ;then 16 | gdb --args bin/testcase --config=../unittest/conf/TarsTest.TestcaseServer.config.conf 17 | else 18 | bin/testcase --config=../unittest/conf/TarsTest.TestcaseServer.config.conf 19 | fi 20 | 21 | killall -9 logServer 22 | killall -9 statServer 23 | killall -9 unitTestServer 24 | 25 | # kill $(pidof logServer) 26 | # kill $(pidof statServer) 27 | 28 | # cd ${TARS_TEST_ROOT} 29 | # if [ ! -d "./result" ];then 30 | # mkdir ./result 31 | # fi 32 | # mv ./taf_result.xml ./result 33 | 34 | # if [ "$run_lcov"x == "YES"x ] ;then 35 | # cd ${TARS_TEST_ROOT}/script 36 | # sh ./lcov.sh ${TARS_ROOT} 37 | # fi 38 | 39 | -------------------------------------------------------------------------------- /protocol/TarsTest/TestcaseServer/EpsTestImp.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Tencent is pleased to support the open source community by making Tars available. 3 | * 4 | * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. 5 | * 6 | * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except 7 | * in compliance with the License. You may obtain a copy of the License at 8 | * 9 | * https://opensource.org/licenses/BSD-3-Clause 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | #include "EpsTestImp.h" 17 | #include "servant/Application.h" 18 | 19 | using namespace std; 20 | 21 | void EpsTestImp::initialize() 22 | { 23 | //initialize servant here: 24 | // 25 | } 26 | void EpsTestImp::destroy() 27 | { 28 | //destroy servant here: 29 | //... 30 | } 31 | -------------------------------------------------------------------------------- /protocol/TarsTest/UnitTest/AServantImp.h: -------------------------------------------------------------------------------- 1 | #ifndef _AServantImp_H_ 2 | #define _AServantImp_H_ 3 | 4 | #include "servant/Application.h" 5 | #include "AServant.h" 6 | 7 | /** 8 | * 9 | * 10 | */ 11 | class AServantImp : public TarsTest::AServant 12 | { 13 | public: 14 | /** 15 | * 16 | */ 17 | virtual ~AServantImp() {} 18 | 19 | /** 20 | * 21 | */ 22 | virtual void initialize(); 23 | 24 | /** 25 | * 26 | */ 27 | virtual void destroy(); 28 | virtual tars::Int32 test(tars::TarsCurrentPtr current); 29 | virtual tars::Int32 testInt(tars::Int32 iIn,tars::Int32 &iOut,tars::TarsCurrentPtr current); 30 | virtual tars::Int32 testHello(const std::string & sReq,std::string &sRsp,tars::TarsCurrentPtr current); 31 | virtual tars::Int32 saySomething(const std::string & something,std::string &strOut,tars::TarsCurrentPtr current); 32 | virtual tars::Int32 queryTest(const TarsTest::ReqInfo & req,vector &vRsp,tars::TarsCurrentPtr current); 33 | /** 34 | * 35 | */ 36 | }; 37 | ///////////////////////////////////////////////////// 38 | #endif 39 | -------------------------------------------------------------------------------- /README.zh.md: -------------------------------------------------------------------------------- 1 | Tars-Test单元测试说明 2 | ---- 3 | Tars-Test单元测试的开发主要基于GoogleTest测试框架,关于GoogleTest的具体细节请见[官方说明文档](https://github.com/abseil/googletest/blob/master/googletest/docs/primer.md)。Tars-Test中目前提供的用例已经覆盖了RPC服务的基本场景。 4 | 5 | 1、环境搭建 6 | ---- 7 | 由于Tars-Test以GoogleTest为基础,因此需要安装GoogleTest及生成覆盖率的相关文件,如下命令: 8 | ```c 9 | [sudo] yum install gtest-devel lcov 10 | ``` 11 | 2、使用说明 12 | ---- 13 | 14 | 在tarscpp/build下编译: 15 | ``` 16 | cmake .. -DONLY_LIB=OFF 17 | make -j8 18 | make run-unittest 19 | ``` 20 | 33 | 34 | 3、测试用例添加 35 | --- 36 | 当前工程中的测试用例代码主要在testcode文件夹中,后续可根据需要在其中添加文件和测试用例。新的测试用例可以复用现有的OBJ、服务和应用,如果需要部署新的服务或OBJ,可在protocol文件夹中添加服务端的相关代码,新增服务端的配置文件可加入到conf文件夹中。 37 | -------------------------------------------------------------------------------- /script/lcov.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | ENABLE_BRANCH="--rc lcov_branch_coverage=1" 4 | 5 | cd ../.. 6 | TARS_ROOT=$(cd `dirname $0`; pwd) 7 | echo ${TARS_ROOT} 8 | 9 | TARS_TEST_ROOT=${TARS_ROOT}/unittest 10 | 11 | cd ${TARS_TEST_ROOT} 12 | 13 | if [ -d "./result" ];then 14 | mv ./result/taf_result.xml ./ 15 | rm -rf ./result/* 16 | fi 17 | 18 | #compute the coverage of servant only 19 | lcov -d ${TARS_ROOT}/ -c -o out.info 20 | lcov --remove out.info '/usr/include/*' -o out.info 21 | lcov --remove out.info '/usr/local/include/*' -o out.info 22 | lcov --remove out.info "${TARS_TEST_ROOT}/*" -o out.info 23 | lcov --remove out.info "${TARS_ROOT}/servant/servant/*F.h" -o out.info 24 | lcov --remove out.info "${TARS_ROOT}/servant/servant/Auth.h" -o out.info 25 | lcov --remove out.info "${TARS_ROOT}/tools/*" -o out.info 26 | lcov --remove out.info "${TARS_ROOT}/util/*" -o out.info 27 | lcov --remove out.info "${TARS_ROOT}/examples" -o out.info 28 | lcov --remove out.info "${TARS_ROOT}/test_deprecated" -o out.info 29 | genhtml -o result out.info 30 | 31 | rm out.info 32 | mv ./taf_result.xml ./result 33 | cd ./result 34 | pwd 35 | ls 36 | 37 | -------------------------------------------------------------------------------- /framework/LogServer/main.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Tencent is pleased to support the open source community by making Tars available. 3 | * 4 | * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. 5 | * 6 | * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except 7 | * in compliance with the License. You may obtain a copy of the License at 8 | * 9 | * https://opensource.org/licenses/BSD-3-Clause 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | #include "LogServer.h" 18 | #include 19 | 20 | using namespace tars; 21 | 22 | LogServer g_app; 23 | 24 | int main(int argc, char *argv[]) 25 | { 26 | try 27 | { 28 | g_app.main(argc, argv); 29 | g_app.waitForShutdown(); 30 | } 31 | catch(exception &ex) 32 | { 33 | cout << ex.what() << endl; 34 | } 35 | 36 | return 0; 37 | } 38 | 39 | 40 | -------------------------------------------------------------------------------- /protocol/TarsTest/TestcaseServer/RPCTestImp.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Tencent is pleased to support the open source community by making Tars available. 3 | * 4 | * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. 5 | * 6 | * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except 7 | * in compliance with the License. You may obtain a copy of the License at 8 | * 9 | * https://opensource.org/licenses/BSD-3-Clause 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | #include "RPCTestImp.h" 18 | #include "servant/Application.h" 19 | 20 | using namespace std; 21 | 22 | ////////////////////////////////////////////////////// 23 | void RPCTestImp::initialize() 24 | { 25 | //initialize servant here: 26 | //... 27 | } 28 | 29 | ////////////////////////////////////////////////////// 30 | void RPCTestImp::destroy() 31 | { 32 | //destroy servant here: 33 | //... 34 | } 35 | 36 | -------------------------------------------------------------------------------- /testcode/include/TarsTest.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Tencent is pleased to support the open source community by making Tars available. 3 | * 4 | * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. 5 | * 6 | * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except 7 | * in compliance with the License. You may obtain a copy of the License at 8 | * 9 | * https://opensource.org/licenses/BSD-3-Clause 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | 18 | #ifndef TARS_TAF_TEST_TESTCODE_INCLUDE_TARSTEST_H_ 19 | #define TARS_TAF_TEST_TESTCODE_INCLUDE_TARSTEST_H_ 20 | 21 | #define TARSTEST tars_test 22 | #define TARSTEST_NS_START namespace TARSTEST{ 23 | #define TARSTEST_NS_END } 24 | #define USING_NS_TARSTEST using namespace TARSTEST; 25 | #define USING_NS_TARS using namespace tars; 26 | #define USING_NS_STD using namespace std; 27 | 28 | #endif /* TARS_TAF_TEST_TESTCODE_INCLUDE_TARSTEST_H_ */ 29 | -------------------------------------------------------------------------------- /framework/StatServer/main.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Tencent is pleased to support the open source community by making Tars available. 3 | * 4 | * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. 5 | * 6 | * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except 7 | * in compliance with the License. You may obtain a copy of the License at 8 | * 9 | * https://opensource.org/licenses/BSD-3-Clause 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | #include "StatServer.h" 18 | #include 19 | 20 | using namespace std; 21 | 22 | TC_Config* g_pconf; 23 | StatServer g_app; 24 | 25 | int main( int argc, char* argv[] ) 26 | { 27 | try 28 | { 29 | g_pconf = &g_app.getConfig(); 30 | g_app.main( argc, argv ); 31 | g_app.waitForShutdown(); 32 | } 33 | catch ( exception& ex ) 34 | { 35 | cout << ex.what() << endl; 36 | } 37 | 38 | return 0; 39 | } 40 | 41 | 42 | -------------------------------------------------------------------------------- /protocol/TarsTest/TestcaseServer/OneWayRpcTestImp.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Tencent is pleased to support the open source community by making Tars available. 3 | * 4 | * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. 5 | * 6 | * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except 7 | * in compliance with the License. You may obtain a copy of the License at 8 | * 9 | * https://opensource.org/licenses/BSD-3-Clause 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | #include "OneWayRpcTestImp.h" 18 | #include "servant/Application.h" 19 | 20 | using namespace std; 21 | 22 | ////////////////////////////////////////////////////// 23 | void OneWayRpcTestImp::initialize() 24 | { 25 | //initialize servant here: 26 | //... 27 | } 28 | 29 | ////////////////////////////////////////////////////// 30 | void OneWayRpcTestImp::destroy() 31 | { 32 | //destroy servant here: 33 | //... 34 | } 35 | 36 | 37 | -------------------------------------------------------------------------------- /testcode/source/testcase/util/test_tc_singleton.cpp: -------------------------------------------------------------------------------- 1 | #include "gtest/gtest.h" 2 | #include "servant/Application.h" 3 | #include "TarsTest/TestcaseServer/RPCTest.h" 4 | #include "servant/AdminF.h" 5 | #include "TarsServantName.h" 6 | 7 | 8 | #include "util/tc_singleton.h" 9 | #include "util/tc_timeprovider.h" 10 | #include 11 | 12 | using namespace std; 13 | using namespace tars; 14 | using namespace TarsTest; 15 | 16 | class B : public TC_Singleton 17 | { 18 | public: 19 | B(){cout << "B" << endl;} 20 | ~B(){cout << "~B" << endl;} 21 | 22 | void test(){cout << "test B" << endl;} 23 | }; 24 | 25 | class A : public TC_Singleton 26 | { 27 | public: 28 | A(){cout << "A" << endl;} 29 | ~A() 30 | { 31 | cout << "~A" << endl; 32 | B::getInstance()->test(); 33 | } 34 | 35 | void test(){cout << "test A" << endl;} 36 | }; 37 | 38 | 39 | 40 | TEST(TarsUtilTestcase, UT_TC_Singleton) 41 | { 42 | 43 | // A::getInstance()->test(); 44 | // B::getInstance()->test(); 45 | 46 | cout << TC_TimeProvider::getInstance()->getNow() << endl; 47 | TC_Common::sleep(1); 48 | 49 | } 50 | 51 | 52 | -------------------------------------------------------------------------------- /framework/LogServer/LogServer.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Tencent is pleased to support the open source community by making Tars available. 3 | * 4 | * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. 5 | * 6 | * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except 7 | * in compliance with the License. You may obtain a copy of the License at 8 | * 9 | * https://opensource.org/licenses/BSD-3-Clause 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | #ifndef __LOG_SERVER_H_ 18 | #define __LOG_SERVER_H_ 19 | 20 | #include "servant/Application.h" 21 | 22 | using namespace tars; 23 | 24 | class LogServer : public Application 25 | { 26 | protected: 27 | /** 28 | * 初始化, 进程会调用一次 29 | */ 30 | virtual void initialize(); 31 | 32 | /** 33 | * 析够, 进程退出时会调用一次 34 | */ 35 | virtual void destroyApp(); 36 | 37 | private: 38 | 39 | bool loadLogFormat(const string& command, const string& params, string& result); 40 | }; 41 | 42 | #endif 43 | 44 | -------------------------------------------------------------------------------- /protocol/TarsTest/UnitTest/UnitTest.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Tencent is pleased to support the open source community by making Tars available. 3 | * 4 | * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. 5 | * 6 | * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except 7 | * in compliance with the License. You may obtain a copy of the License at 8 | * 9 | * https://opensource.org/licenses/BSD-3-Clause 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | #ifndef _UnitTest_H_ 18 | #define _UnitTest_H_ 19 | 20 | #include 21 | #include "servant/Application.h" 22 | 23 | using namespace tars; 24 | 25 | /** 26 | * 27 | **/ 28 | class UnitTest : public Application 29 | { 30 | public: 31 | /** 32 | * 33 | **/ 34 | virtual ~UnitTest() {}; 35 | 36 | /** 37 | * 38 | **/ 39 | virtual void initialize(); 40 | 41 | /** 42 | * 43 | **/ 44 | virtual void destroyApp(); 45 | }; 46 | 47 | extern UnitTest g_app; 48 | 49 | //////////////////////////////////////////// 50 | #endif 51 | -------------------------------------------------------------------------------- /script/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | 3 | project(tars_cpp) 4 | 5 | set(CMAKE_VERBOSE_MAKEFILE off) 6 | 7 | #set(MYSQL_DIR_INC "/usr/local/mysql/include") 8 | #set(MYSQL_DIR_LIB "/usr/local/mysql/lib") 9 | set(MYSQL_DIR_INC "/usr/local/mysql/include/mysql") 10 | set(MYSQL_DIR_LIB "/usr/local/mysql/lib/mysql") 11 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -g -O2 -Wall -Wno-deprecated") 12 | set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -O2 -Wall -Wno-deprecated") 13 | 14 | #set(CMAKE_BUILD_TYPE "Debug") 15 | 16 | if(ENABLE_TARS_GCOV AND NOT WIN32 AND NOT APPLE) 17 | SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage") 18 | SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage") 19 | SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fprofile-arcs -ftest-coverage -lgcov") 20 | endif() 21 | 22 | 23 | set(TARS_VERSION "1.1.0") 24 | add_definitions(-DTARS_VERSION="${TARS_VERSION}") 25 | set(TARS_SSL 0) 26 | add_definitions(-DTARS_SSL=${TARS_SSL}) 27 | set(TARS_HTTP2 0) 28 | add_definitions(-DTARS_HTTP2=${TARS_HTTP2}) 29 | 30 | set(INSTALL_PREFIX "/usr/local/tars/cpp") 31 | 32 | set(CMAKE_INSTALL_PREFIX ${INSTALL_PREFIX}) 33 | 34 | add_subdirectory(util) 35 | add_subdirectory(tools) 36 | 37 | set(TARS2CPP "${tools_BINARY_DIR}/tars2cpp/tars2cpp") 38 | 39 | add_subdirectory(servant) 40 | 41 | -------------------------------------------------------------------------------- /protocol/TarsTest/UnitTest/TarsPingTestImp.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Tencent is pleased to support the open source community by making Tars available. 3 | * 4 | * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. 5 | * 6 | * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except 7 | * in compliance with the License. You may obtain a copy of the License at 8 | * 9 | * https://opensource.org/licenses/BSD-3-Clause 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | #include "TarsPingTestImp.h" 18 | #include "servant/Application.h" 19 | 20 | using namespace std; 21 | 22 | ////////////////////////////////////////////////////// 23 | void TarsPingTestImp::initialize() 24 | { 25 | //initialize servant here: 26 | //... 27 | } 28 | 29 | ////////////////////////////////////////////////////// 30 | void TarsPingTestImp::destroy() 31 | { 32 | //destroy servant here: 33 | //... 34 | } 35 | 36 | tars::Int32 TarsPingTestImp::test(tars::TarsCurrentPtr current) 37 | { 38 | 39 | return 0; 40 | } 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /testcode/source/testcase/util/template.config.conf: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #proxy需要的配置 5 | 6 | #地址 7 | locator = QueryObj@tcp -h 127.0.0.1 -p 20000:tcp -h 127.0.0.1 -p 30000 8 | #最大超时时间 9 | maxinvoketimeout = 3 10 | #模块间调用[可选] 11 | stat = tars.tarsstat.StatObj 12 | #网络发送线程个数 13 | sendthread = 3 14 | #网络接收线程个数 15 | recvthread = 3 16 | #网络异步回调线程个数 17 | asyncthread = 3 18 | #模块名称 19 | modulename = ${modulename} 20 | 21 | 22 | #定义所有绑定的IP 23 | 24 | #应用名称 25 | app = ${app} 26 | #服务名称 27 | server = ${server} 28 | #服务的数据目录,可执行文件,配置文件等 29 | basepath = ${basepath} 30 | #日志路径 31 | logpath = ${logpath} 32 | #网络线程个数 33 | netthread = 2 34 | #本地管理套接字[可选] 35 | local = ${local} 36 | #本地node的ip:port:timeout[可选] 37 | node = ${node} 38 | #配置中心的地址[可选] 39 | config = tars.tarsconfig.ConfigObj 40 | #远程LogServer[可选] 41 | log = tars.tarslog.LogObj 42 | 43 | 44 | 45 | 46 | A = B 47 | 127.0.0.1 48 | 192.168.1.1 49 | 4.5.6.6 50 | 51 | 52 | 53 | 54 | 55 | 56 | -------------------------------------------------------------------------------- /testcode/include/utils/ServerAdmin.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Tencent is pleased to support the open source community by making Tars available. 3 | * 4 | * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. 5 | * 6 | * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except 7 | * in compliance with the License. You may obtain a copy of the License at 8 | * 9 | * https://opensource.org/licenses/BSD-3-Clause 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | /* 17 | * ServerAdmin.h 18 | * 19 | * Created on: 2018年10月16日 20 | * Author: abelguo 21 | */ 22 | 23 | #ifndef TARS_TAF_TEST_TESTCODE_INCLUDE_UTILS_SERVERADMIN_H_ 24 | #define TARS_TAF_TEST_TESTCODE_INCLUDE_UTILS_SERVERADMIN_H_ 25 | #include 26 | #include "TarsTest.h" 27 | #include "servant/Communicator.h" 28 | 29 | TARSTEST_NS_START 30 | USING_NS_STD 31 | 32 | class ServerAdmin 33 | { 34 | public: 35 | ServerAdmin(); 36 | ServerAdmin& shutdown(const string& serverName); 37 | 38 | private: 39 | CommunicatorPtr _comm; 40 | }; 41 | 42 | TARSTEST_NS_END 43 | 44 | #endif /* TARS_TAF_TEST_TESTCODE_INCLUDE_UTILS_SERVERADMIN_H_ */ 45 | -------------------------------------------------------------------------------- /protocol/TarsTest/UnitTest/TarsPingTestImp.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Tencent is pleased to support the open source community by making Tars available. 3 | * 4 | * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. 5 | * 6 | * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except 7 | * in compliance with the License. You may obtain a copy of the License at 8 | * 9 | * https://opensource.org/licenses/BSD-3-Clause 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | #ifndef _TarsPingTestImp_H_ 18 | #define _TarsPingTestImp_H_ 19 | 20 | #include "servant/Application.h" 21 | #include "TarsPingTest.h" 22 | 23 | /** 24 | * 25 | * 26 | */ 27 | class TarsPingTestImp : public TarsTest::TarsPingTest 28 | { 29 | public: 30 | /** 31 | * 32 | */ 33 | virtual ~TarsPingTestImp() {} 34 | 35 | /** 36 | * 37 | */ 38 | virtual void initialize(); 39 | 40 | /** 41 | * 42 | */ 43 | virtual void destroy(); 44 | virtual tars::Int32 test(tars::TarsCurrentPtr current); 45 | /** 46 | * 47 | */ 48 | }; 49 | ///////////////////////////////////////////////////// 50 | #endif 51 | -------------------------------------------------------------------------------- /protocol/TarsTest/UnitTest/AServantImp.cpp: -------------------------------------------------------------------------------- 1 | #include "AServantImp.h" 2 | #include "servant/Application.h" 3 | 4 | using namespace std; 5 | 6 | ////////////////////////////////////////////////////// 7 | void AServantImp::initialize() 8 | { 9 | //initialize servant here: 10 | //... 11 | } 12 | 13 | ////////////////////////////////////////////////////// 14 | void AServantImp::destroy() 15 | { 16 | //destroy servant here: 17 | //... 18 | } 19 | 20 | tars::Int32 AServantImp::test(tars::TarsCurrentPtr current) 21 | { 22 | 23 | return 0; 24 | } 25 | tars::Int32 AServantImp::testInt(tars::Int32 iIn,tars::Int32 &iOut,tars::TarsCurrentPtr current) 26 | { 27 | iOut=iIn; 28 | return 0; 29 | } 30 | tars::Int32 AServantImp::testHello(const std::string & sReq,std::string &sRsp,tars::TarsCurrentPtr current) 31 | { 32 | TLOGDEBUG("HelloImp::testHellosReq:"< &vRsp,tars::TarsCurrentPtr current) 44 | { 45 | TarsTest::RspInfo stRsp; 46 | stRsp.sSlaveName="tars"; 47 | stRsp.sMasterIp="127.0.0.1"; 48 | stRsp.sStartTime="0000"; 49 | stRsp.sEndTime="2360"; 50 | vRsp.push_back(stRsp); 51 | return 0; 52 | } 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | -------------------------------------------------------------------------------- /protocol/TarsTest/UnitTest/BServantImp.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Tencent is pleased to support the open source community by making Tars available. 3 | * 4 | * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. 5 | * 6 | * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except 7 | * in compliance with the License. You may obtain a copy of the License at 8 | * 9 | * https://opensource.org/licenses/BSD-3-Clause 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | #ifndef _BServantImp_H_ 18 | #define _BServantImp_H_ 19 | 20 | #include "servant/Application.h" 21 | #include "BServant.h" 22 | 23 | /** 24 | * 25 | * 26 | */ 27 | class BServantImp : public TarsTest::BServant 28 | { 29 | public: 30 | /** 31 | * 32 | */ 33 | virtual ~BServantImp() {} 34 | 35 | /** 36 | * 37 | */ 38 | virtual void initialize(); 39 | 40 | /** 41 | * 42 | */ 43 | virtual void destroy(); 44 | virtual tars::Int32 test(tars::TarsCurrentPtr current); 45 | virtual tars::Int32 testHello(const std::string & sReq,std::string &sRsp,tars::TarsCurrentPtr current); 46 | /** 47 | * 48 | */ 49 | }; 50 | ///////////////////////////////////////////////////// 51 | #endif 52 | -------------------------------------------------------------------------------- /protocol/TarsTest/TestcaseServer/HttpDemoImp.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Tencent is pleased to support the open source community by making Tars available. 3 | * 4 | * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. 5 | * 6 | * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except 7 | * in compliance with the License. You may obtain a copy of the License at 8 | * 9 | * https://opensource.org/licenses/BSD-3-Clause 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | #ifndef _HttpDemoImp_H_ 17 | #define _HttpDemoImp_H_ 18 | 19 | #include "servant/Application.h" 20 | 21 | /** 22 | * 23 | * 24 | */ 25 | class HttpDemoImp : public Servant 26 | { 27 | public: 28 | /** 29 | * 30 | */ 31 | virtual ~HttpDemoImp() {} 32 | 33 | /** 34 | * 35 | */ 36 | virtual void initialize(); 37 | 38 | /** 39 | * 40 | */ 41 | virtual void destroy(); 42 | 43 | /** 44 | * 45 | */ 46 | int test() 47 | { 48 | return 0; 49 | } 50 | int doRequest(TarsCurrentPtr current, vector &buffer); 51 | 52 | private: 53 | int doRequest(const TC_HttpRequest &req, TC_HttpResponse &rsp); 54 | }; 55 | ///////////////////////////////////////////////////// 56 | #endif 57 | -------------------------------------------------------------------------------- /protocol/TarsTest/TestcaseServer/RPCTestImp.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Tencent is pleased to support the open source community by making Tars available. 3 | * 4 | * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. 5 | * 6 | * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except 7 | * in compliance with the License. You may obtain a copy of the License at 8 | * 9 | * https://opensource.org/licenses/BSD-3-Clause 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | #ifndef _RPCTestImp_H_ 18 | #define _RPCTestImp_H_ 19 | #include 20 | #include "servant/Application.h" 21 | #include "RPCTest.h" 22 | 23 | /** 24 | * 25 | * 26 | */ 27 | class RPCTestImp : public TarsTest::RPCTest 28 | { 29 | public: 30 | /** 31 | * 32 | */ 33 | virtual ~RPCTestImp() {} 34 | 35 | /** 36 | * 37 | */ 38 | virtual void initialize(); 39 | 40 | /** 41 | * 42 | */ 43 | virtual void destroy(); 44 | 45 | /** 46 | * 47 | */ 48 | virtual int test(tars::TarsCurrentPtr current) { return 2;}; 49 | virtual int testStr(std::string& sOut, tars::TarsCurrentPtr current) { sOut = "out"; return 2;}; 50 | }; 51 | ///////////////////////////////////////////////////// 52 | #endif 53 | -------------------------------------------------------------------------------- /protocol/TarsTest/UnitTest/TupDemoImp.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Tencent is pleased to support the open source community by making Tars available. 3 | * 4 | * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. 5 | * 6 | * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except 7 | * in compliance with the License. You may obtain a copy of the License at 8 | * 9 | * https://opensource.org/licenses/BSD-3-Clause 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | #ifndef _TupDemoImp_H_ 18 | #define _TupDemoImp_H_ 19 | 20 | #include "servant/Application.h" 21 | #include "TupDemo.h" 22 | 23 | /** 24 | * 25 | * 26 | */ 27 | class TupDemoImp : public TarsTest::TupDemo 28 | { 29 | public: 30 | /** 31 | * 32 | */ 33 | virtual ~TupDemoImp() {} 34 | 35 | /** 36 | * 37 | */ 38 | virtual void initialize(); 39 | 40 | /** 41 | * 42 | */ 43 | virtual void destroy(); 44 | virtual tars::Int32 aAddb(const TarsTest::AddInt & addData,TarsTest::Result &aAddbResult,tars::TarsCurrentPtr current); 45 | virtual tars::Int32 helloWord(const std::string & strIn,std::string &strOut,tars::TarsCurrentPtr current); 46 | /** 47 | * 48 | */ 49 | }; 50 | ///////////////////////////////////////////////////// 51 | #endif 52 | -------------------------------------------------------------------------------- /testcode/source/testcase/util/test_tc_thread.cpp: -------------------------------------------------------------------------------- 1 | #include "gtest/gtest.h" 2 | #include "servant/Application.h" 3 | #include "TarsTest/TestcaseServer/RPCTest.h" 4 | #include "servant/AdminF.h" 5 | #include "TarsServantName.h" 6 | 7 | 8 | #include "util/tc_thread.h" 9 | 10 | #include 11 | #include 12 | 13 | using namespace std; 14 | using namespace tars; 15 | using namespace TarsTest; 16 | 17 | class MyThread : public TC_Thread, public TC_ThreadLock 18 | { 19 | public: 20 | MyThread() 21 | { 22 | bTerminate = false; 23 | } 24 | /** 25 | * �����߳� 26 | */ 27 | void terminate() 28 | { 29 | bTerminate = true; 30 | 31 | { 32 | TC_ThreadLock::Lock sync(*this); 33 | notifyAll(); 34 | } 35 | } 36 | 37 | void doSomething() 38 | { 39 | cout << "doSomething" << endl; 40 | } 41 | /** 42 | * ���� 43 | */ 44 | protected: 45 | virtual void run() 46 | { 47 | while(!bTerminate) 48 | { 49 | //TODO: your business 50 | doSomething(); 51 | 52 | { 53 | TC_ThreadLock::Lock sync(*this); 54 | timedWait(1000); 55 | } 56 | } 57 | } 58 | 59 | protected: 60 | bool bTerminate; 61 | }; 62 | 63 | TEST(TarsUtilTestcase, UT_TC_Thread) 64 | { 65 | MyThread mt; 66 | mt.start(); 67 | 68 | TC_Common::sleep(5); 69 | 70 | mt.terminate(); 71 | mt.getThreadControl().join(); 72 | 73 | } 74 | 75 | 76 | -------------------------------------------------------------------------------- /protocol/TarsTest/UnitTest/BServantImp.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Tencent is pleased to support the open source community by making Tars available. 3 | * 4 | * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. 5 | * 6 | * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except 7 | * in compliance with the License. You may obtain a copy of the License at 8 | * 9 | * https://opensource.org/licenses/BSD-3-Clause 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | #include "BServantImp.h" 18 | #include "servant/Application.h" 19 | 20 | using namespace std; 21 | 22 | ////////////////////////////////////////////////////// 23 | void BServantImp::initialize() 24 | { 25 | //initialize servant here: 26 | //... 27 | } 28 | 29 | ////////////////////////////////////////////////////// 30 | void BServantImp::destroy() 31 | { 32 | //destroy servant here: 33 | //... 34 | } 35 | 36 | tars::Int32 BServantImp::test(tars::TarsCurrentPtr current) 37 | { 38 | 39 | return 0; 40 | } 41 | tars::Int32 BServantImp::testHello(const std::string & sReq,std::string &sRsp,tars::TarsCurrentPtr current) 42 | { 43 | TLOGDEBUG("HelloImp::testHellosReq:"< 20 | #include "servant/Application.h" 21 | #include "RPCTest.h" 22 | 23 | /** 24 | * * 25 | * * 26 | * */ 27 | class EpsTestImp : public TarsTest::RPCTest 28 | { 29 | public: 30 | /** 31 | * * 32 | * */ 33 | virtual ~EpsTestImp() {} 34 | 35 | /** 36 | * * * 37 | * */ 38 | virtual void initialize(); 39 | 40 | /** 41 | * * 42 | * */ 43 | virtual void destroy(); 44 | 45 | /** 46 | * * 47 | * */ 48 | virtual int test(tars::TarsCurrentPtr current) { return 2;}; 49 | virtual int testStr(std::string& sOut, tars::TarsCurrentPtr current) { sOut = "out"; return 2;}; 50 | }; 51 | ///////////////////////////////////////////////////// 52 | #endif 53 | // 54 | -------------------------------------------------------------------------------- /testcode/source/testcase/UdpRpcTest.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Tencent is pleased to support the open source community by making Tars available. 3 | * 4 | * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. 5 | * 6 | * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except 7 | * in compliance with the License. You may obtain a copy of the License at 8 | * 9 | * https://opensource.org/licenses/BSD-3-Clause 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | /* 18 | * UdpRpcTest.cpp 19 | * 20 | * Created on: 2018年10月19日 21 | * Author: abelguo 22 | */ 23 | #include "gtest/gtest.h" 24 | #include "servant/Application.h" 25 | #include "TarsTest/TestcaseServer/RPCTest.h" 26 | #include "servant/AdminF.h" 27 | #include "TarsServantName.h" 28 | 29 | USING_NS_STD 30 | USING_NS_TARS 31 | using namespace TarsTest; 32 | USING_NS_TARSTEST 33 | 34 | struct UdpRpcTest : public ::testing::Test 35 | { 36 | UdpRpcTest() 37 | { 38 | _comm = Application::getCommunicator(); 39 | } 40 | protected: 41 | CommunicatorPtr _comm; 42 | }; 43 | 44 | TEST_F(UdpRpcTest, should_response_udp_rpc_when_client_sync_call_server_by_ip) 45 | { 46 | RPCTestPrx prx = _comm->stringToProxy (UDP_RPC_SERVANT_ENDPOINT); 47 | 48 | int res = prx->test(); 49 | 50 | EXPECT_EQ(res, 2); 51 | } 52 | 53 | -------------------------------------------------------------------------------- /testcode/source/stub/MockProxy/MockServerObjProxy.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Tencent is pleased to support the open source community by making Tars available. 3 | * 4 | * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. 5 | * 6 | * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except 7 | * in compliance with the License. You may obtain a copy of the License at 8 | * 9 | * https://opensource.org/licenses/BSD-3-Clause 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | /* 18 | * MockServerObjProxy.cpp 19 | * 20 | * Created on: 2018年8月22日 21 | * Author: abelguo 22 | */ 23 | #include "servant/BaseF.h" 24 | #include "stub/MockServerObjProxy.h" 25 | 26 | using namespace tars; 27 | using namespace std; 28 | 29 | namespace mockProxy 30 | { 31 | 32 | MockServerObjProxy* MockServerObjProxy::tars_hash(int64_t key) 33 | { 34 | return (MockServerObjProxy*)ServantProxy::tars_hash(key); 35 | } 36 | 37 | MockServerObjProxy* MockServerObjProxy::tars_consistent_hash(int64_t key) 38 | { 39 | return (MockServerObjProxy*)ServantProxy::tars_consistent_hash(key); 40 | } 41 | 42 | MockServerObjProxy* MockServerObjProxy::tars_set_timeout(int msecond) 43 | { 44 | return (MockServerObjProxy*)ServantProxy::tars_set_timeout(msecond); 45 | } 46 | } 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /testcode/include/stub/TestPushServantImp.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Tencent is pleased to support the open source community by making Tars available. 3 | * 4 | * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. 5 | * 6 | * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except 7 | * in compliance with the License. You may obtain a copy of the License at 8 | * 9 | * https://opensource.org/licenses/BSD-3-Clause 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | #ifndef _TestPushServantImp_H_ 17 | #define _TestPushServantImp_H_ 18 | 19 | #include "servant/Application.h" 20 | 21 | /** 22 | * 23 | * 24 | */ 25 | class TestPushServantImp : public tars::Servant 26 | { 27 | public: 28 | /** 29 | * 30 | */ 31 | virtual ~TestPushServantImp() {} 32 | 33 | /** 34 | * 35 | */ 36 | virtual void initialize(); 37 | 38 | /** 39 | * 40 | */ 41 | virtual void destroy(); 42 | 43 | //重载Servant的doRequest方法 44 | int doRequest(tars::TarsCurrentPtr current, vector& response); 45 | 46 | //重载Servant的doClose方法 47 | int doClose(tars::TarsCurrentPtr current); 48 | 49 | private: 50 | void setPushInfo(const string &sInfo, string& pushInfo); 51 | 52 | void push(const string& sInfo); 53 | }; 54 | ///////////////////////////////////////////////////// 55 | #endif 56 | -------------------------------------------------------------------------------- /protocol/TarsTest/UnitTest/TupDemoImp.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Tencent is pleased to support the open source community by making Tars available. 3 | * 4 | * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. 5 | * 6 | * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except 7 | * in compliance with the License. You may obtain a copy of the License at 8 | * 9 | * https://opensource.org/licenses/BSD-3-Clause 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | #include "TupDemoImp.h" 18 | #include "servant/Application.h" 19 | 20 | using namespace std; 21 | 22 | ////////////////////////////////////////////////////// 23 | void TupDemoImp::initialize() 24 | { 25 | //initialize servant here: 26 | //... 27 | } 28 | 29 | ////////////////////////////////////////////////////// 30 | void TupDemoImp::destroy() 31 | { 32 | //destroy servant here: 33 | //... 34 | } 35 | tars::Int32 TupDemoImp::helloWord(const std::string & strIn,std::string &strOut,tars::TarsCurrentPtr current) 36 | { 37 | strOut=strIn; 38 | return 2; 39 | } 40 | tars::Int32 TupDemoImp::aAddb(const TarsTest::AddInt & addData,TarsTest::Result &aAddbResult,tars::TarsCurrentPtr current) 41 | { 42 | int res=addData.adda+addData.addb; 43 | aAddbResult.addResult=res; 44 | aAddbResult.strResult=addData.addStr; 45 | return 0; 46 | } 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | -------------------------------------------------------------------------------- /testcode/source/stub/PushServerDemo/TestPushServantImp.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Tencent is pleased to support the open source community by making Tars available. 3 | * 4 | * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. 5 | * 6 | * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except 7 | * in compliance with the License. You may obtain a copy of the License at 8 | * 9 | * https://opensource.org/licenses/BSD-3-Clause 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | #ifndef _TestPushServantImp_H_ 18 | #define _TestPushServantImp_H_ 19 | 20 | #include "servant/Application.h" 21 | //#include "TestPushServant.h" 22 | 23 | /** 24 | * 25 | * 26 | */ 27 | class TestPushServantImp : public tars::Servant 28 | { 29 | public: 30 | /** 31 | * 32 | */ 33 | virtual ~TestPushServantImp() {} 34 | 35 | /** 36 | * 37 | */ 38 | virtual void initialize(); 39 | 40 | /** 41 | * 42 | */ 43 | virtual void destroy(); 44 | 45 | /** 46 | * 47 | */ 48 | // virtual int test(tars::TarsCurrentPtr current) { return 0;}; 49 | 50 | 51 | //重载Servant的doRequest方法 52 | int doRequest(tars::TarsCurrentPtr current, vector& response); 53 | 54 | //重载Servant的doClose方法 55 | int doClose(tars::TarsCurrentPtr current); 56 | 57 | }; 58 | ///////////////////////////////////////////////////// 59 | #endif 60 | -------------------------------------------------------------------------------- /protocol/TarsTest/TestcaseServer/OneWayRpcTestImp.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Tencent is pleased to support the open source community by making Tars available. 3 | * 4 | * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. 5 | * 6 | * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except 7 | * in compliance with the License. You may obtain a copy of the License at 8 | * 9 | * https://opensource.org/licenses/BSD-3-Clause 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | #ifndef _OneWayRpcTest_H_ 17 | #define _OneWayRpcTest_H_ 18 | #include 19 | #include "servant/Application.h" 20 | #include "OneWayRpcTest.h" 21 | 22 | /** 23 | * 24 | * 25 | */ 26 | extern bool g_OneWayRpcTestSuccess; 27 | 28 | class OneWayRpcTestImp : public TarsTest::OneWayRpcTest 29 | { 30 | public: 31 | /** 32 | * 33 | */ 34 | virtual ~OneWayRpcTestImp() {} 35 | 36 | /** 37 | * 38 | */ 39 | virtual void initialize(); 40 | 41 | /** 42 | * 43 | */ 44 | virtual void destroy(); 45 | 46 | /** 47 | * 48 | */ 49 | virtual tars::Int32 test(tars::TarsCurrentPtr current) 50 | { 51 | //cerr << "std::log:test g_OneWayRpcTestSuccess" << std::endl; 52 | g_OneWayRpcTestSuccess = true; 53 | 54 | return 2; 55 | }; 56 | }; 57 | ///////////////////////////////////////////////////// 58 | #endif 59 | 60 | -------------------------------------------------------------------------------- /protocol/TarsTest/UnitTest/DyeingTestImp.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Tencent is pleased to support the open source community by making Tars available. 3 | * 4 | * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. 5 | * 6 | * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except 7 | * in compliance with the License. You may obtain a copy of the License at 8 | * 9 | * https://opensource.org/licenses/BSD-3-Clause 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | #ifndef _DyeingTestImp_H_ 18 | #define _DyeingTestImp_H_ 19 | 20 | #include "servant/Application.h" 21 | #include "DyeingTest.h" 22 | 23 | /** 24 | * 25 | * 26 | */ 27 | class DyeingTestImp : public TarsTest::DyeingTest 28 | { 29 | public: 30 | /** 31 | * 32 | */ 33 | virtual ~DyeingTestImp() {} 34 | 35 | /** 36 | * 37 | */ 38 | virtual void initialize(); 39 | 40 | /** 41 | * 42 | */ 43 | virtual void destroy(); 44 | 45 | /** 46 | * 47 | */ 48 | virtual int test(tars::TarsCurrentPtr current) { return 0;}; 49 | virtual tars::Int32 testDyeing(const std::string & strIn,std::string &strOut,tars::TarsCurrentPtr current); 50 | int doRequest(TarsCurrentPtr current, vector &buffer); 51 | 52 | private: 53 | int doRequest(const TC_HttpRequest &req, TC_HttpResponse &rsp); 54 | }; 55 | ///////////////////////////////////////////////////// 56 | #endif 57 | -------------------------------------------------------------------------------- /protocol/TarsTest/UnitTest/ProxyImp.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Tencent is pleased to support the open source community by making Tars available. 3 | * 4 | * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. 5 | * 6 | * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except 7 | * in compliance with the License. You may obtain a copy of the License at 8 | * 9 | * https://opensource.org/licenses/BSD-3-Clause 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | #ifndef __PROXY_IMP_H_ 18 | #define __PROXY_IMP_H_ 19 | 20 | #include "BServant.h" 21 | 22 | #include "servant/Application.h" 23 | #include "Proxy.h" 24 | 25 | using namespace TarsTest; 26 | 27 | /** 28 | * 29 | * 30 | */ 31 | class ProxyImp : public Proxy 32 | { 33 | public: 34 | /** 35 | * 36 | */ 37 | virtual ~ProxyImp() 38 | { 39 | } 40 | 41 | /** 42 | * 43 | */ 44 | virtual void initialize(); 45 | 46 | /** 47 | * 48 | */ 49 | virtual void destroy(); 50 | 51 | /** 52 | * 53 | */ 54 | virtual tars::Int32 test(tars::TarsCurrentPtr current); 55 | 56 | /** 57 | * 58 | */ 59 | virtual tars::Int32 testProxy(const std::string& sReq, std::string &sRsp, tars::TarsCurrentPtr current); 60 | 61 | protected: 62 | CommunicatorPtr _comm; 63 | BServantPrx _prx; 64 | 65 | }; 66 | ///////////////////////////////////////////////////// 67 | #endif 68 | -------------------------------------------------------------------------------- /testcode/include/stub/MockServerObjProxy.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Tencent is pleased to support the open source community by making Tars available. 3 | * 4 | * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. 5 | * 6 | * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except 7 | * in compliance with the License. You may obtain a copy of the License at 8 | * 9 | * https://opensource.org/licenses/BSD-3-Clause 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | /* 18 | * MockServerObjProxy.h 19 | * 20 | * Created on: 2018年8月22日 21 | * Author: abelguo 22 | */ 23 | 24 | #ifndef MOCK_PROXY_MOCKSERVEROBJPROXY_H_ 25 | #define MOCK_PROXY_MOCKSERVEROBJPROXY_H_ 26 | #include "servant/Servant.h" 27 | #include "servant/ServantProxy.h" 28 | 29 | using namespace tars; 30 | using namespace std; 31 | 32 | namespace mockProxy 33 | { 34 | /* proxy for client */ 35 | class MockServerObjProxy : public ServantProxy 36 | { 37 | public: 38 | typedef map TARS_CONTEXT; 39 | MockServerObjProxy* tars_hash(int64_t key); 40 | MockServerObjProxy* tars_consistent_hash(int64_t key); 41 | MockServerObjProxy* tars_set_timeout(int msecond); 42 | const char* tars_prxname() const { return "MockServerObjProxy"; } 43 | }; 44 | typedef TC_AutoPtr MockServerObjPrx; 45 | } 46 | 47 | #endif /* MOCK_PROXY_MOCKSERVEROBJPROXY_H_ */ 48 | -------------------------------------------------------------------------------- /testcode/source/testcase/util/test_tc_gzip.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Tencent is pleased to support the open source community by making Tars available. 3 | * 4 | * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved. 5 | * 6 | * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except 7 | * in compliance with the License. You may obtain a copy of the License at 8 | * 9 | * https://opensource.org/licenses/BSD-3-Clause 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | #include "gtest/gtest.h" 18 | #include "servant/Application.h" 19 | #include "TarsTest/TestcaseServer/RPCTest.h" 20 | #include "servant/AdminF.h" 21 | #include "TarsServantName.h" 22 | 23 | #include "util/tc_file.h" 24 | #include "util/tc_gzip.h" 25 | #include "util/tc_common.h" 26 | 27 | 28 | using namespace std; 29 | using namespace tars; 30 | using namespace TarsTest; 31 | 32 | namespace TarsTest { 33 | 34 | #if !TARGET_PLATFORM_WINDOWS 35 | 36 | TEST(TarsUtilTestcase, UT_TC_GZip) 37 | { 38 | 39 | vector file; 40 | TC_File::load2str("./example_tc_gzip.cpp", file); 41 | 42 | { 43 | vector v; 44 | tars::TC_GZip::compress(&file[0], file.size(), v); 45 | 46 | // cout << "compress ok, size:" << v.size() << endl; 47 | 48 | vector s1; 49 | tars::TC_GZip::uncompress(&v[0], v.size(), s1); 50 | 51 | assert(file == s1); 52 | } 53 | } 54 | 55 | #endif 56 | 57 | 58 | } -------------------------------------------------------------------------------- /testcode/source/testcase/util/log.config.conf: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | #proxy需要的配置 5 | 6 | #地址 7 | locator = 8 | #最大超时时间 9 | maxinvoketimeout = 10 | #模块间调用[可选] 11 | stat = one.onestat.StatObj 12 | #网络发送线程个数 13 | sendthread = 1 14 | #网络接收线程个数 15 | recvthread = 1 16 | #网络异步回调线程个数 17 | asyncthread = 3 18 | #模块名称 19 | modulename = one.onelog 20 | 21 | 22 | #定义所有绑定的IP 23 | 24 | #应用名称 25 | app = one 26 | #服务名称 27 | server = onelog 28 | #服务的数据目录,可执行文件,配置文件等 29 | basepath = /usr/local/app/one/onelog/data 30 | #日志路径 31 | logpath = /usr/local/app/one/app_log/ 32 | #网络线程个数 33 | netthread = 2 34 | #本地管理套接字[可选] 35 | local = tcp -h /tmp/one.onelog.sock -p 0 -t 10000 36 | #本地node的ip:port:timeout[可选] 37 | node = ServerObj@tcp -h 127.0.0.1 -p 2345 -t 10000 38 | #配置中心的地址[可选] 39 | config = one.oneconfig.ConfigObj 40 | 41 | #配置绑定端口 42 | 43 | #ip:port:timeout 44 | endpoint = tcp -h 10.1.36.39 -p 20500 -t 10000 45 | #允许的IP地址 46 | allow = 47 | #最大连接数 48 | maxconns = 4096 49 | #当前线程个数 50 | threads = 10 51 | #处理对象, 支持多个, 以,分隔 52 | servants = LogObj 53 | 54 | 55 | 56 | 57 | 58 | #log的数据目录 59 | logpath = /usr/local/app/one/app_log 60 | #写线程个数 61 | logthread = 10 62 | 63 | 64 | 65 | -------------------------------------------------------------------------------- /conf/TarsTest.LogServer.config.conf: -------------------------------------------------------------------------------- 1 | 2 | 3 | container=taf.tarstest.unittest.TSZ2 4 | isdocker=N 5 | enableset=N 6 | setdivision=.. 7 | 8 | node=Docker.DCNode.ServerObj@tcp -h 100.94.21.161 -p 9931 -t 180000 9 | app=TarsTest 10 | server=LogServer 11 | localip=127.0.0.1 12 | netthread=2 13 | basepath=/usr/local/app/tars/app_log/ 14 | datapath=/usr/local/app/tars/app_log/ 15 | logpath=/usr/local/app/tars/app_log/ 16 | logsize=15M 17 | config=taf.tafconfig.ConfigObj 18 | notify=taf.tafnotify.NotifyObj 19 | deactivating-timeout=59000 20 | closecout=0 21 | logLevel=DEBUG 22 | local=tcp -h 127.0.0.1 -p 10300 -t 60000 23 | 24 | allow 25 | endpoint=tcp -h 127.0.0.1 -p 10301 -t 60000 26 | handlegroup=TarsTest.LogServer.LogObjAdapter 27 | maxconns=200000 28 | protocol=tars 29 | queuecap=10000 30 | queuetimeout=60000 31 | servant=TarsTest.LogServer.LogObj 32 | shmcap=0 33 | shmkey=0 34 | threads=6 35 | 36 | 37 | 38 | isgray=N 39 | sync-invoke-timeout=20000 40 | async-invoke-timeout=20000 41 | refresh-endpoint-interval=60000 42 | stat=taf.tafstat.StatObj 43 | property=taf.tafproperty.PropertyObj 44 | report-interval=60 45 | sendthread=1 46 | recvthread=1 47 | asyncthread=3 48 | modulename=TarsTest.LogServer 49 | sample-rate=1000000 50 | max-sample-count=10 51 | locator=Docker.DockerRegistry.QueryObj@tcp -h 10.137.145.142 -p 9903 -t 5000 52 | 53 | HttpProxyObj=10375 54 | 55 | collector_host=127.0.0.1 56 | collector_port=9411 57 | 58 | 59 | 60 | logpath=/usr/local/app/tars/remote_app_log 61 | logthread=2 62 | 63 | 64 | -------------------------------------------------------------------------------- /conf/TarsTest.StatServer.config.conf: -------------------------------------------------------------------------------- 1 | 2 | 3 | container=taf.tarstest.unittest.TSZ2 4 | isdocker=N 5 | enableset=N 6 | setdivision=.. 7 | 8 | node=Docker.DCNode.ServerObj@tcp -h 100.94.21.161 -p 9931 -t 180000 9 | app=TarsTest 10 | server=StatServer 11 | localip=127.0.0.1 12 | netthread=2 13 | basepath=/usr/local/app/tars/app_log/ 14 | datapath=/usr/local/app/tars/app_log/ 15 | logpath=/usr/local/app/tars/app_log/ 16 | logsize=15M 17 | config=taf.tafconfig.ConfigObj 18 | notify=taf.tafnotify.NotifyObj 19 | deactivating-timeout=59000 20 | closecout=0 21 | logLevel=DEBUG 22 | local=tcp -h 127.0.0.1 -p 10400 -t 60000 23 | 24 | allow 25 | endpoint=tcp -h 127.0.0.1 -p 10401 -t 60000 26 | handlegroup=TarsTest.StatServer.StatObjAdapter 27 | maxconns=200000 28 | protocol=tars 29 | queuecap=10000 30 | queuetimeout=60000 31 | servant=TarsTest.StatServer.StatObj 32 | shmcap=0 33 | shmkey=0 34 | threads=6 35 | 36 | 37 | 38 | isgray=N 39 | sync-invoke-timeout=20000 40 | async-invoke-timeout=20000 41 | refresh-endpoint-interval=60000 42 | stat=taf.tafstat.StatObj 43 | property=taf.tafproperty.PropertyObj 44 | report-interval=60 45 | sendthread=1 46 | recvthread=1 47 | asyncthread=3 48 | modulename=TarsTest.StatServer 49 | sample-rate=1000000 50 | max-sample-count=10 51 | locator=Docker.DockerRegistry.QueryObj@tcp -h 10.137.145.142 -p 9903 -t 5000 52 | 53 | HttpProxyObj=10375 54 | 55 | collector_host=127.0.0.1 56 | collector_port=9411 57 | 58 | 59 | 60 | logpath=/usr/local/app/tars/remote_app_log 61 | logthread=2 62 | 63 | 64 | -------------------------------------------------------------------------------- /testcode/source/testcase/util/test_tc_option.cpp: -------------------------------------------------------------------------------- 1 | #include "gtest/gtest.h" 2 | #include "servant/Application.h" 3 | #include "TarsTest/TestcaseServer/RPCTest.h" 4 | #include "servant/AdminF.h" 5 | #include "TarsServantName.h" 6 | 7 | 8 | #include "util/tc_option.h" 9 | #include "util/tc_common.h" 10 | #include 11 | #include 12 | // #include 13 | 14 | using namespace std; 15 | using namespace tars; 16 | using namespace TarsTest; 17 | 18 | 19 | 20 | TEST(TarsUtilTestcase, UT_TC_Option) 21 | { 22 | char * g_array_argv[6][3] = { 23 | { (char*)"test", (char*)"--get=0", (char*)"--bit=1", }, 24 | { (char*)"test", (char*)"--set=98", (char*)"--bit=1", }, 25 | { (char*)"test", (char*)"--clear=99", (char*)"--bit=1", }, 26 | { (char*)"test", (char*)"--clear4all", (char*)"--bit=1", }, 27 | { (char*)"test", (char*)"--dump=./bitmap.dump", (char*)" ", }, 28 | { (char*)"test", (char*)"--load=./bitmap.dump", (char*)" ", }, 29 | }; 30 | 31 | TC_Option *op; 32 | 33 | for(unsigned int i=0; i<(sizeof(g_array_argv)/sizeof(char*[3])); i++) 34 | { 35 | char **argv = g_array_argv[i]; 36 | int argc = (sizeof(g_array_argv[i]) / sizeof(char *)); 37 | 38 | //cout << (sizeof(g_array_argv[i]) / sizeof(char *)) << endl; 39 | //cout << i << " " << argv[1] << endl; 40 | op = new TC_Option(); 41 | op->decode(argc, argv); 42 | 43 | map mp = op->getMulti(); 44 | 45 | cout << "map:" << endl; 46 | cout << TC_Common::tostr(mp) << endl; 47 | 48 | vector d = op->getSingle(); 49 | cout << "vector:" << endl; 50 | cout << TC_Common::tostr(d) << endl; 51 | 52 | cout << op->getValue("abc") << endl; 53 | } 54 | 55 | } 56 | 57 | 58 | -------------------------------------------------------------------------------- /testcode/source/main.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Tencent is pleased to support the open source community by making Tars available. 3 | * 4 | * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. 5 | * 6 | * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except 7 | * in compliance with the License. You may obtain a copy of the License at 8 | * 9 | * https://opensource.org/licenses/BSD-3-Clause 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | #include "gtest/gtest.h" 18 | #include "TarsTest.h" 19 | #include "util/tc_thread.h" 20 | #include "utils/ServerAdmin.h" 21 | #include "servant/Application.h" 22 | 23 | #include "TarsServantName.h" 24 | #include "servant/AdminF.h" 25 | 26 | USING_NS_TARS 27 | USING_NS_TARSTEST 28 | 29 | extern "C" void RunTestCaseServer(int argc, char* argv[]); 30 | 31 | ///////////////////////////////////////////////////////////////// 32 | int main(int argc, char* argv[]) 33 | { 34 | RunTestCaseServer(argc, argv); 35 | TC_Common::sleep(2); 36 | testing::GTEST_FLAG(output)="xml:./../../../taf_result.xml"; 37 | testing::GTEST_FLAG(filter)="**"; 38 | 39 | testing::InitGoogleTest(&argc, argv); 40 | RUN_ALL_TESTS(); 41 | TC_Common::sleep(2); 42 | ServerAdmin().shutdown(STAT_SERVER_ADMIN_NAME_ENDPOINT); 43 | ServerAdmin().shutdown(LOG_SERVER_ADMIN_NAME_ENDPOINT); 44 | ServerAdmin().shutdown(UNIT_TEST_ADMIN_NAME_ENDPOINT) 45 | .shutdown(TEST_CASE_ADMIN_NAME_ENDPOINT); 46 | 47 | TC_Common::sleep(2); 48 | 49 | return 0; 50 | } 51 | 52 | 53 | 54 | -------------------------------------------------------------------------------- /testcode/source/stub/MockProxy/MockProxyObjCallback.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Tencent is pleased to support the open source community by making Tars available. 3 | * 4 | * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. 5 | * 6 | * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except 7 | * in compliance with the License. You may obtain a copy of the License at 8 | * 9 | * https://opensource.org/licenses/BSD-3-Clause 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | /* 18 | * MockProxyObjCallBack.cpp 19 | * 20 | * Created on: 2018年8月22日 21 | * Author: abelguo 22 | */ 23 | #include "servant/TarsLogger.h" 24 | #include "servant/BaseF.h" 25 | #include "stub/MockProxyObjCallback.h" 26 | 27 | using namespace tars; 28 | 29 | namespace mockProxy 30 | { 31 | void MockProxyObjCallback::doResponse(shared_ptr responsePacket) 32 | { 33 | responsePacket->iRequestId = iRequestId; 34 | 35 | TarsOutputStream os; 36 | 37 | responsePacket->writeTo(os); 38 | 39 | string response; 40 | 41 | Int32 iHeaderLen = htonl(sizeof(Int32) + os.getByteBuffer().size()); 42 | 43 | response.reserve(sizeof(Int32) + os.getByteBuffer().size()); 44 | 45 | response.append((const char*)&iHeaderLen, sizeof(Int32)); 46 | 47 | response.append((const char*)&os.getByteBuffer()[0], os.getByteBuffer().size()); 48 | TLOGDEBUG("[MockProxy]MockProxyObjCallback response[" << response.size() << "]: " << response <sendResponse(response.c_str(), response.size()); 51 | 52 | return; 53 | } 54 | } 55 | 56 | 57 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [点我查看中文版](README.zh.md) 2 | 3 | ## Description of Tars-Test 4 | The development of Tars-Test(unit testcase of Tarscpp) is mainly based on the GoogleTest test framework. For details on GoogleTest, please see [Official Documentation] (https://github.com/abseil/googletest/blob/master/googletest/docs/primer.md) . 5 | The current testcases have covered the basic scenario of the RPC service. 6 | 7 | ### Dependent environment 8 | Since Tars-Test is based on GoogleTest, you need to install GoogleTest and generate related files for coverage, as follows: 9 | 10 | ```c 11 | [sudo] yum install gtest-devel lcov 12 | ``` 13 | ### instructions for use 14 | 15 | 16 | 在tarscpp/build下编译: 17 | ``` 18 | cmake .. -DONLY_LIB=OFF 19 | make -j8 20 | make run-unittest 21 | ``` 22 | 37 | 38 | ### How to add one testcase 39 | The test case code in the current project is mainly in the testcode folder, and subsequent files and test cases can be added as needed. New test cases can reuse existing OBJs, services, and applications. If you need to deploy new services or OBJs, you can add services to the protocol folder. Related code, the new server configuration file can be added to the conf folder. 40 | -------------------------------------------------------------------------------- /testcode/source/testcase/TarsPingTest.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Tencent is pleased to support the open source community by making Tars available. 3 | * 4 | * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. 5 | * 6 | * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except 7 | * in compliance with the License. You may obtain a copy of the License at 8 | * 9 | * https://opensource.org/licenses/BSD-3-Clause 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | #include "gtest/gtest.h" 17 | #include "servant/Application.h" 18 | #include "servant/AdminF.h" 19 | #include "TarsServantName.h" 20 | #include "TarsTest/UnitTest/TarsPingTest.h" 21 | using namespace std; 22 | using namespace tars; 23 | using namespace TarsTest; 24 | 25 | /**************************************************** 26 | Ping测试,测试主调和被调之间连接是否通 27 | ****************************************************/ 28 | 29 | struct PingTest : public ::testing::Test 30 | { 31 | PingTest() 32 | { 33 | _comm = Application::getCommunicator(); 34 | } 35 | protected: 36 | CommunicatorPtr _comm; 37 | }; 38 | 39 | TEST_F(PingTest, should_response_when_syncall_ping_cmd) 40 | { 41 | int64_t tBegin = TC_TimeProvider::getInstance()->getNowMs(); 42 | 43 | TarsPingTestPrx pPrx; 44 | int iRet=-1; 45 | try 46 | { 47 | 48 | pPrx=_comm->stringToProxy(PING_SERVANT_ENDPOINT); 49 | pPrx->tars_ping(); 50 | iRet = 0; 51 | } 52 | catch(exception &e) 53 | { 54 | cout<getNowMs() - tBegin << "(ms)" << endl); 61 | } 62 | 63 | 64 | 65 | -------------------------------------------------------------------------------- /testcode/source/testcase/OneWayRpcTest.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Tencent is pleased to support the open source community by making Tars available. 3 | * 4 | * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. 5 | * 6 | * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except 7 | * in compliance with the License. You may obtain a copy of the License at 8 | * 9 | * https://opensource.org/licenses/BSD-3-Clause 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | #include "gtest/gtest.h" 17 | // #include "util/tc_bind.h" 18 | #include "servant/Application.h" 19 | #include "TarsTest/TestcaseServer/OneWayRpcTest.h" 20 | #include "servant/AdminF.h" 21 | #include "TarsServantName.h" 22 | 23 | using namespace std; 24 | using namespace tars; 25 | using namespace TarsTest; 26 | 27 | bool g_OneWayRpcTestSuccess = false; 28 | 29 | struct OneWayRpcTestS : public::testing::Test 30 | { 31 | public: 32 | OneWayRpcTestS() 33 | { 34 | _comm = new Communicator(); 35 | } 36 | protected: 37 | CommunicatorPtr _comm; 38 | }; 39 | 40 | class OneWayRpcTestObjCallback: public OneWayRpcTestPrxCallback 41 | { 42 | public: 43 | virtual void callback_test(Int32 ret) 44 | { 45 | cerr << "std::exception: err callback" << std::endl; 46 | } 47 | }; 48 | typedef tars::TC_AutoPtr OneWayRpcTestObjCallbackPtr; 49 | 50 | 51 | TEST_F(OneWayRpcTestS, oneway_should_response_rpc_when_client_async_call_server_by_ip) 52 | { 53 | g_OneWayRpcTestSuccess = false; 54 | 55 | OneWayRpcTestPrx prx = _comm->stringToProxy (ONEWAY_RPC_SERVANT_ENDPOIONT); 56 | 57 | prx -> async_test(NULL); 58 | 59 | TC_Common::sleep(1); 60 | 61 | EXPECT_TRUE(g_OneWayRpcTestSuccess); 62 | } 63 | 64 | 65 | 66 | -------------------------------------------------------------------------------- /protocol/TarsTest/TestcaseServer/HttpDemoImp.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Tencent is pleased to support the open source community by making Tars available. 3 | * 4 | * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. 5 | * 6 | * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except 7 | * in compliance with the License. You may obtain a copy of the License at 8 | * 9 | * https://opensource.org/licenses/BSD-3-Clause 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | #include "HttpDemoImp.h" 17 | #include "servant/Application.h" 18 | 19 | using namespace std; 20 | 21 | ////////////////////////////////////////////////////// 22 | void HttpDemoImp::initialize() 23 | { 24 | //initialize servant here: 25 | //... 26 | } 27 | 28 | ////////////////////////////////////////////////////// 29 | void HttpDemoImp::destroy() 30 | { 31 | //destroy servant here: 32 | //... 33 | } 34 | 35 | int HttpDemoImp::doRequest(TarsCurrentPtr current, vector &buffer) 36 | { 37 | TC_HttpRequest req; 38 | TC_HttpResponse rsp; 39 | 40 | // parse request header 41 | vector v = current->getRequestBuffer(); 42 | string sBuf; 43 | sBuf.assign(&v[0], v.size()); 44 | TLOGDEBUG("[HttpDemo]Receive buffer size: "<< sBuf.length() << ", request: " << sBuf.c_str() < &buffer) 36 | { 37 | TC_HttpRequest req; 38 | TC_HttpResponse rsp; 39 | 40 | // parse request header 41 | vector v = current->getRequestBuffer(); 42 | string sBuf; 43 | sBuf.assign(&v[0], v.size()); 44 | req.decode(sBuf); 45 | 46 | int ret = doRequest(req, rsp); 47 | 48 | rsp.encode(buffer); 49 | 50 | return ret; 51 | } 52 | 53 | int DyeingTestImp::doRequest(const TC_HttpRequest &req, TC_HttpResponse &rsp) 54 | { 55 | string msg = "Hello Tars!"; 56 | rsp.setContentType("html/text"); 57 | rsp.setResponse(msg.c_str(), msg.size()); 58 | return 0; 59 | } 60 | 61 | tars::Int32 DyeingTestImp::testDyeing(const std::string & strIn,std::string &strOut,tars::TarsCurrentPtr current) 62 | { 63 | strOut="this is a dyeing message"; 64 | return 0; 65 | } 66 | 67 | -------------------------------------------------------------------------------- /testcode/include/RunTestThread.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Tencent is pleased to support the open source community by making Tars available. 3 | * 4 | * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. 5 | * 6 | * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except 7 | * in compliance with the License. You may obtain a copy of the License at 8 | * 9 | * https://opensource.org/licenses/BSD-3-Clause 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | #ifndef TARS_TAF_TEST_TESTCODE_INCLUDE_STUB_RUNTESTTHREAD_H_ 18 | #define TARS_TAF_TEST_TESTCODE_INCLUDE_STUB_RUNTESTTHREAD_H_ 19 | #include "gtest/gtest.h" 20 | #include 21 | #include "TarsTest.h" 22 | #include "util/tc_autoptr.h" 23 | #include "util/tc_thread.h" 24 | #include "utils/ServerAdmin.h" 25 | #include "TarsServantName.h" 26 | #include "" 27 | TARSTEST_NS_START 28 | USING_NS_TARS 29 | 30 | extern TestcaseServer g_app; 31 | class RunTestThread : public TC_Thread, public TC_HandleBase 32 | { 33 | public: 34 | virtual void run() 35 | { 36 | try 37 | { 38 | g_app.main(argc, argv); 39 | g_app.waitForShutdown(); 40 | } 41 | catch (std::exception& e) 42 | { 43 | cerr << "std::exception:" << e.what() << std::endl; 44 | } 45 | catch (...) 46 | { 47 | cerr << "unknown exception." << std::endl; 48 | } 49 | } 50 | void init(int argc, char* argv[]) 51 | { 52 | this->argc = argc; 53 | this->argv = argv; 54 | 55 | } 56 | private: 57 | int argc; 58 | char* argv[]; 59 | 60 | 61 | }; 62 | typedef TC_AutoPtr RunTestThreadPtr; 63 | 64 | 65 | TARSTEST_NS_END 66 | 67 | #endif /* TARS_TAF_TEST_TESTCODE_INCLUDE_STUB_RUNTESTTHREAD_H_ */ 68 | -------------------------------------------------------------------------------- /protocol/TarsTest/TestcaseServer/TestcaseServer.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Tencent is pleased to support the open source community by making Tars available. 3 | * 4 | * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. 5 | * 6 | * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except 7 | * in compliance with the License. You may obtain a copy of the License at 8 | * 9 | * https://opensource.org/licenses/BSD-3-Clause 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | #ifndef _TestcaseServer_H_ 18 | #define _TestcaseServer_H_ 19 | 20 | #include 21 | #include "servant/Application.h" 22 | #include "util/tc_autoptr.h" 23 | #include "util/tc_thread.h" 24 | 25 | using namespace tars; 26 | 27 | /** 28 | * 29 | **/ 30 | class TestcaseServer : public Application 31 | { 32 | public: 33 | /** 34 | * 35 | **/ 36 | virtual ~TestcaseServer() {}; 37 | 38 | /** 39 | * 40 | **/ 41 | virtual void initialize(); 42 | 43 | /** 44 | * 45 | **/ 46 | virtual void destroyApp(); 47 | 48 | 49 | bool cmdAdd(const string& command, const string& params, string& result); 50 | 51 | 52 | bool delTarsViewVersion(const string& command, const string& params, string& result); 53 | }; 54 | 55 | extern TestcaseServer g_app; 56 | 57 | class RunTestThread : public TC_Thread, public TC_HandleBase 58 | { 59 | public: 60 | virtual void run(); 61 | void init(int argc, char* argv[]); 62 | private: 63 | int argc; 64 | char** argv; 65 | }; 66 | typedef TC_AutoPtr RunTestThreadPtr; 67 | 68 | extern "C" void RunTestCaseServer(int argc, char* argv[]) 69 | { 70 | RunTestThreadPtr thread = new RunTestThread(); 71 | thread->init(argc, argv); 72 | thread->start(); 73 | } 74 | 75 | //////////////////////////////////////////// 76 | #endif 77 | -------------------------------------------------------------------------------- /testcode/source/testcase/PropertyReportTest.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Tencent is pleased to support the open source community by making Tars available. 3 | * 4 | * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. 5 | * 6 | * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except 7 | * in compliance with the License. You may obtain a copy of the License at 8 | * 9 | * https://opensource.org/licenses/BSD-3-Clause 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | #include "gtest/gtest.h" 18 | // #include "util/tc_bind.h" 19 | #include "servant/Application.h" 20 | #include "TarsTest/TestcaseServer/RPCTest.h" 21 | #include "TarsServantName.h" 22 | 23 | 24 | USING_NS_STD 25 | USING_NS_TARS 26 | using namespace TarsTest; 27 | 28 | struct PropertyReportTest : public ::testing::Test 29 | { 30 | PropertyReportTest() 31 | { 32 | _comm = Application::getCommunicator(); 33 | } 34 | protected: 35 | CommunicatorPtr _comm; 36 | }; 37 | 38 | TEST_F(PropertyReportTest, should_report_property) 39 | { 40 | _comm->setProperty("property", "TarsTest.TestcaseServer.PropertyObj@ tcp -h 127.0.0.1 -p 4444"); 41 | //初始化分布数据范围 42 | vector v; 43 | v.push_back(10); 44 | v.push_back(30); 45 | v.push_back(50); 46 | v.push_back(80); 47 | v.push_back(100); 48 | 49 | PropertyReportPtr srp = _comm->getStatReport()->createPropertyReport("PropertyReportTest", 50 | PropertyReport::sum(), //求和 51 | PropertyReport::avg(), 52 | PropertyReport::count(), 53 | PropertyReport::max(), 54 | PropertyReport::min(), 55 | PropertyReport::distr(v)); 56 | 57 | for ( int is = 0; is < 10; is++ ) 58 | { 59 | srp->report(rand() % 10); 60 | } 61 | 62 | vector > vs = srp->get(); 63 | 64 | EXPECT_TRUE(vs.size() != 0); 65 | 66 | } 67 | 68 | -------------------------------------------------------------------------------- /testcode/include/stub/MockProxyObjCallback.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Tencent is pleased to support the open source community by making Tars available. 3 | * 4 | * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. 5 | * 6 | * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except 7 | * in compliance with the License. You may obtain a copy of the License at 8 | * 9 | * https://opensource.org/licenses/BSD-3-Clause 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | /* 17 | * MockProxyObjCallback.h 18 | * 19 | * Created on: 2018年8月22日 20 | * Author: abelguo 21 | */ 22 | 23 | #ifndef MOCK_PROXY_MOCKPROXYOBJCALLBACK_H_ 24 | #define MOCK_PROXY_MOCKPROXYOBJCALLBACK_H_ 25 | #include 26 | #include 27 | #include "servant/Servant.h" 28 | 29 | using namespace tars; 30 | using namespace std; 31 | 32 | namespace mockProxy 33 | { 34 | const static string MOCK_PROXY_CALLBACK_TYPE = "MockProxyCallback"; 35 | 36 | /* callback of async proxy for client */ 37 | class MockProxyObjCallback: public ServantCallback 38 | { 39 | public: 40 | MockProxyObjCallback(const ServantPtr& servant, const TarsCurrentPtr& current) 41 | :ServantCallback(MOCK_PROXY_CALLBACK_TYPE, servant, current),iRequestId(0) 42 | { 43 | } 44 | 45 | virtual ~MockProxyObjCallback(){} 46 | 47 | /** 48 | * 异步回调处理函数 49 | * @param responsePacket 50 | */ 51 | virtual void doResponse(shared_ptr responsePacket); 52 | 53 | /** 54 | * 设置RequestId,在回应消息时,需要使用 55 | * @param id 56 | */ 57 | void setRequestId(Int32 id) { iRequestId = id; } 58 | 59 | protected: 60 | 61 | Int32 iRequestId; 62 | }; 63 | typedef TC_AutoPtr MockProxyObjCallbackPtr; 64 | 65 | } 66 | 67 | 68 | #endif /* MOCK_PROXY_MOCKPROXYOBJCALLBACK_H_ */ 69 | -------------------------------------------------------------------------------- /testcode/source/testcase/util/test_tc_pack.cpp: -------------------------------------------------------------------------------- 1 | #include "gtest/gtest.h" 2 | #include "servant/Application.h" 3 | #include "TarsTest/TestcaseServer/RPCTest.h" 4 | #include "servant/AdminF.h" 5 | #include "TarsServantName.h" 6 | 7 | 8 | #include "util/tc_pack.h" 9 | #include 10 | #include 11 | 12 | using namespace std; 13 | using namespace tars; 14 | using namespace TarsTest; 15 | 16 | TEST(TarsUtilTestcase, UT_TC_Pack) 17 | { 18 | bool b = true; 19 | char c = 'a'; 20 | short si = 3; 21 | int ii = 4; 22 | char cn[] = "abc"; 23 | string sn = "def"; 24 | 25 | TC_PackIn pi; 26 | pi << b << c << si << ii << cn << sn; 27 | 28 | string s = pi.topacket(); 29 | 30 | TC_PackOut po(s.c_str(), s.length()); 31 | po >> b; 32 | assert(b == true); 33 | cout << "bool OK" << endl; 34 | 35 | po >> c; 36 | assert(c == 'a'); 37 | cout << "char OK" << endl; 38 | 39 | po >> si; 40 | assert(si == 3); 41 | cout << "short OK" << endl; 42 | 43 | po >> ii; 44 | assert(ii == 4); 45 | cout << "int OK" << endl; 46 | 47 | po >> cn; 48 | assert(cn == string("abc")); 49 | cout << "char[] OK" << endl; 50 | 51 | po >> sn; 52 | assert(sn == "def"); 53 | cout << "string OK" << endl; 54 | 55 | { 56 | pi.clear(); 57 | 58 | pi << b << c; 59 | pi.insert(1) << cn; 60 | 61 | s = pi.topacket(); 62 | po.init(s.c_str(), s.length()); 63 | po >> b; 64 | assert(b == true); 65 | cout << "bool OK" << endl; 66 | 67 | po >> cn; 68 | assert(cn == string("abc")); 69 | cout << "char[] OK" << endl; 70 | 71 | po >> c; 72 | assert(c == 'a'); 73 | cout << "char OK" << endl; 74 | } 75 | 76 | { 77 | pi.clear(); 78 | pi << b << c; 79 | pi.replace(1) << 'b'; 80 | 81 | s = pi.topacket(); 82 | po.init(s.c_str(), s.length()); 83 | po >> b; 84 | assert(b == true); 85 | cout << "bool OK" << endl; 86 | 87 | po >> c; 88 | assert(c == 'b'); 89 | cout << "char OK" << endl; 90 | } 91 | 92 | } 93 | 94 | 95 | -------------------------------------------------------------------------------- /testcode/source/testcase/MockProxyTest.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Tencent is pleased to support the open source community by making Tars available. 3 | * 4 | * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. 5 | * 6 | * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except 7 | * in compliance with the License. You may obtain a copy of the License at 8 | * 9 | * https://opensource.org/licenses/BSD-3-Clause 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | #include "gtest/gtest.h" 18 | #include "servant/Application.h" 19 | #include "TarsTest/TestcaseServer/RPCTest.h" 20 | #include "servant/AdminF.h" 21 | #include "TarsServantName.h" 22 | 23 | USING_NS_STD 24 | USING_NS_TARS 25 | using namespace TarsTest; 26 | 27 | struct MockProxyTest : public ::testing::Test 28 | { 29 | MockProxyTest() 30 | { 31 | _comm = Application::getCommunicator(); 32 | } 33 | void SetUp() 34 | { 35 | isCallbackCalled = false; 36 | } 37 | static bool isCallbackCalled; 38 | protected: 39 | CommunicatorPtr _comm; 40 | }; 41 | bool MockProxyTest::isCallbackCalled = false; 42 | 43 | class MockProxyTestObjCallback: public RPCTestPrxCallback 44 | { 45 | public: 46 | virtual void callback_test(Int32 ret) 47 | { 48 | EXPECT_EQ(ret, 2); 49 | MockProxyTest::isCallbackCalled = true; 50 | } 51 | }; 52 | typedef tars::TC_AutoPtr MockProxyTestObjCallbackPtr; 53 | 54 | TEST_F(MockProxyTest, should_response_rpc_when_client_sync_call_server_by_ip) 55 | { 56 | RPCTestPrx prx = _comm->stringToProxy (MOCK_PROXY_SERVANT_ENDPOINT); 57 | 58 | int res = prx->test(); 59 | 60 | EXPECT_EQ(res, 2); 61 | } 62 | 63 | TEST_F(MockProxyTest, should_response_rpc_when_client_async_call_server_by_ip) 64 | { 65 | RPCTestPrx prx = _comm->stringToProxy (MOCK_PROXY_SERVANT_ENDPOINT); 66 | 67 | MockProxyTestObjCallbackPtr cbp = new MockProxyTestObjCallback(); 68 | 69 | prx -> async_test(cbp); 70 | 71 | TC_Common::sleep(1); 72 | 73 | EXPECT_TRUE(isCallbackCalled); 74 | } 75 | -------------------------------------------------------------------------------- /testcode/source/testcase/util/test_tc_mmap.cpp: -------------------------------------------------------------------------------- 1 | #include "gtest/gtest.h" 2 | #include "servant/Application.h" 3 | #include "TarsTest/TestcaseServer/RPCTest.h" 4 | #include "servant/AdminF.h" 5 | #include "TarsServantName.h" 6 | 7 | 8 | #include "util/tc_mmap.h" 9 | #include "util/tc_option.h" 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | using namespace std; 17 | using namespace tars; 18 | using namespace TarsTest; 19 | 20 | 21 | 22 | void testCreate(size_t n) 23 | { 24 | TC_Mmap mmap; 25 | cout << "create mmap" << endl; 26 | mmap.mmap("mmap.dat", n); 27 | mmap.munmap(); 28 | cout << "create mmap OK" << endl; 29 | } 30 | 31 | void testWrite(const string &s) 32 | { 33 | TC_Mmap mmap; 34 | cout << "write mmap" << endl; 35 | mmap.mmap("mmap.dat", 1000); 36 | memcpy(mmap.getPointer(), s.c_str(), s.length()); 37 | 38 | TC_Common::sleep(10); 39 | 40 | mmap.munmap(); 41 | } 42 | 43 | void testRead() 44 | { 45 | TC_Mmap mmap; 46 | cout << "read mmap" << endl; 47 | mmap.mmap("mmap.dat", 1000); 48 | 49 | string s; 50 | s.assign((char*)mmap.getPointer(), mmap.getSize()); 51 | mmap.munmap(); 52 | 53 | cout << s << endl; 54 | } 55 | 56 | void main_test(int argc, char *argv[]) 57 | { 58 | 59 | TC_Option option; 60 | option.decode(argc, argv); 61 | 62 | // int pagesize = sysconf(_SC_PAGESIZE); 63 | 64 | // cout << "pagesize:" << pagesize << endl; 65 | 66 | if(option.getValue("test") == "create") 67 | { 68 | size_t n = 50; 69 | testCreate(n); 70 | } 71 | else if(option.getValue("test") == "write") 72 | { 73 | testWrite(option.getValue("c")); 74 | } 75 | else if(option.getValue("test") == "read") 76 | { 77 | testRead(); 78 | } 79 | 80 | } 81 | 82 | 83 | 84 | TEST(TarsUtilTestcase, UT_TC_Mmap) 85 | { 86 | testCreate(50); 87 | testWrite("just a test."); 88 | testRead(); 89 | /* 90 | char * g_array_argv[6][3] = { 91 | 92 | }; 93 | 94 | for(unsigned int i=0; i<(sizeof(g_array_argv)/sizeof(char*[3])); i++) 95 | { 96 | char **argv = g_array_argv[i]; 97 | int argc = (sizeof(g_array_argv[i]) / sizeof(char *)); 98 | main_test(argc, argv); 99 | } 100 | */ 101 | } 102 | 103 | -------------------------------------------------------------------------------- /testcode/include/stub/MockProxyObjImp.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Tencent is pleased to support the open source community by making Tars available. 3 | * 4 | * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. 5 | * 6 | * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except 7 | * in compliance with the License. You may obtain a copy of the License at 8 | * 9 | * https://opensource.org/licenses/BSD-3-Clause 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | #ifndef _MockProxyObjImp_H_ 18 | #define _MockProxyObjImp_H_ 19 | // #include "util/tc_atomic.h" 20 | #include "servant/Application.h" 21 | #include "servant/Servant.h" 22 | #include "MockServerObjProxy.h" 23 | 24 | using namespace tars; 25 | using namespace std; 26 | 27 | namespace mockProxy 28 | { 29 | /** 30 | * 31 | * 32 | */ 33 | class MockProxyObjImp : public Servant 34 | { 35 | public: 36 | /** 37 | * 38 | */ 39 | virtual ~MockProxyObjImp() {} 40 | 41 | /** 42 | * 43 | */ 44 | virtual void initialize(); 45 | 46 | /** 47 | * 48 | */ 49 | virtual void destroy(); 50 | 51 | /** 52 | * 应用接收消息处理入口 53 | * @param current 54 | * @param response 55 | * 56 | * @return int 57 | */ 58 | virtual int doRequest(tars::TarsCurrentPtr current, vector& response); 59 | 60 | /** 61 | * 应用回应消息处理入口 62 | * @param resp 63 | * @return int 64 | */ 65 | virtual int doResponse(ReqMessagePtr resp); 66 | 67 | private: 68 | void setPushInfo(const string &sInfo); 69 | 70 | void push(const string& pushInfo); 71 | 72 | void parseOriginalRequest(const TarsCurrentPtr current, RequestPacket &requestPacket); 73 | 74 | void replaceRequestId(const RequestPacket &oldRequest, RequestPacket &newRequest); 75 | 76 | void callMockServer(const RequestPacket &requestPacket, const ServantProxyCallbackPtr& callback); 77 | 78 | static std::atomic requestId; 79 | 80 | string _sMockServerObjName ; 81 | }; 82 | 83 | } 84 | ///////////////////////////////////////////////////// 85 | #endif 86 | -------------------------------------------------------------------------------- /testcode/source/testcase/util/test_tc_mem_chunk.cpp: -------------------------------------------------------------------------------- 1 | #include "gtest/gtest.h" 2 | #include "servant/Application.h" 3 | #include "TarsTest/TestcaseServer/RPCTest.h" 4 | #include "servant/AdminF.h" 5 | #include "TarsServantName.h" 6 | 7 | 8 | #include "util/tc_mem_chunk.h" 9 | #include "util/tc_sem_mutex.h" 10 | #include "util/tc_shm.h" 11 | #include "util/tc_mmap.h" 12 | #include "util/tc_file.h" 13 | #include "util/tc_option.h" 14 | #include "util/tc_common.h" 15 | #include 16 | #include 17 | using namespace std; 18 | using namespace tars; 19 | using namespace TarsTest; 20 | 21 | TC_Mmap m; 22 | TC_MemMultiChunkAllocator alloc; 23 | 24 | 25 | void testAllocate() 26 | { 27 | vector b = alloc.getBlockSize(); 28 | cout << TC_Common::tostr(b) << endl; 29 | cout << alloc.allBlockChunkCount() << endl; 30 | 31 | vector v; 32 | size_t n = 77; 33 | do 34 | { 35 | size_t iAllocSize; 36 | void *p = alloc.allocate(n, iAllocSize); 37 | cout << p << endl; 38 | if(!p) 39 | { 40 | break; 41 | } 42 | v.push_back(p); 43 | n += 2; 44 | } while(0); 45 | 46 | cout << v.size() << endl; 47 | 48 | for(size_t i = 0; i < v.size(); i++) 49 | { 50 | alloc.deallocate(v[i]); 51 | } 52 | } 53 | 54 | 55 | TEST(TarsUtilTestcase, UT_TC_MemMultiChunkAllocator) 56 | { 57 | return; 58 | 59 | if(TC_File::isFileExist("mc.hmap")) 60 | { 61 | m.mmap("mc.hmap", TC_File::getFileSize("mc.hmap")); 62 | } 63 | else 64 | { 65 | m.mmap("mc.hmap", 1204); 66 | } 67 | 68 | if(m.iscreate()) 69 | { 70 | alloc.create(m.getPointer(), m.getSize(), 30, 100, 1.2); 71 | } 72 | else 73 | { 74 | alloc.connect(m.getPointer()); 75 | } 76 | 77 | //if(option.hasParam("append")) 78 | { 79 | size_t i = m.getSize() * 3; 80 | 81 | m.munmap(); 82 | m.mmap("mc.hmap", i); 83 | alloc.append(m.getPointer(), i); 84 | 85 | m.munmap(); 86 | m.mmap("mc.hmap", i*3); 87 | alloc.append(m.getPointer(), i*3); 88 | 89 | m.munmap(); 90 | m.mmap("mc.hmap", i*5); 91 | alloc.append(m.getPointer(), i*5); 92 | } 93 | //else if(option.hasParam("alloc")) 94 | { 95 | testAllocate(); 96 | } 97 | 98 | } 99 | 100 | 101 | -------------------------------------------------------------------------------- /protocol/TarsTest/UnitTest/TypeDemoImp.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Tencent is pleased to support the open source community by making Tars available. 3 | * 4 | * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. 5 | * 6 | * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except 7 | * in compliance with the License. You may obtain a copy of the License at 8 | * 9 | * https://opensource.org/licenses/BSD-3-Clause 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | #ifndef _TypeDemoImp_H_ 18 | #define _TypeDemoImp_H_ 19 | 20 | #include "servant/Application.h" 21 | #include "TypeDemo.h" 22 | 23 | /** 24 | * 25 | * 26 | */ 27 | class TypeDemoImp : public TarsTest::TypeDemo 28 | { 29 | public: 30 | /** 31 | * 32 | */ 33 | virtual ~TypeDemoImp() {} 34 | 35 | /** 36 | * 37 | */ 38 | virtual void initialize(); 39 | 40 | /** 41 | * 42 | */ 43 | virtual void destroy(); 44 | 45 | /** 46 | * 47 | */ 48 | virtual int test(tars::TarsCurrentPtr current) { return 0;}; 49 | virtual tars::Int32 echoInt(tars::Int32 iInput,tars::Int32 &iEcho,tars::TarsCurrentPtr current); 50 | virtual tars::Int32 echoBool(tars::Bool bInput,tars::Bool &bEcho,tars::TarsCurrentPtr current); 51 | virtual tars::Int32 echoByte(tars::Char bInput,tars::Char &bEcho,tars::TarsCurrentPtr current); 52 | virtual tars::Int32 echoShort(tars::Short sInput,tars::Short &sEcho,tars::TarsCurrentPtr current); 53 | virtual tars::Int32 echoLong(tars::Int64 lInput,tars::Int64 &lEcho,tars::TarsCurrentPtr current) ; 54 | virtual tars::Int32 echoString(const std::string & strInput,std::string & strEcho,tars::TarsCurrentPtr current); 55 | virtual tars::Int32 echoVector(const vector & vInput,vector & vEcho,tars::TarsCurrentPtr current); 56 | virtual tars::Int32 echoMap(const map & mInput,map &mEcho,tars::TarsCurrentPtr current); 57 | virtual tars::Int32 echoMultiType(const vector > & mtInput,vector > &mtEcho,tars::TarsCurrentPtr current); 58 | //virtual tars::Int32 queryForTafPlus(const TarsTest::ReqInfo & req,vector &vRsp,tars::TarsCurrentPtr current); 59 | }; 60 | ///////////////////////////////////////////////////// 61 | #endif 62 | -------------------------------------------------------------------------------- /protocol/TarsTest/UnitTest/ProxyImp.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Tencent is pleased to support the open source community by making Tars available. 3 | * 4 | * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. 5 | * 6 | * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except 7 | * in compliance with the License. You may obtain a copy of the License at 8 | * 9 | * https://opensource.org/licenses/BSD-3-Clause 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | #include "ProxyImp.h" 18 | //#include "ProxyServer.h" 19 | #include "UnitTest.h" 20 | using namespace std; 21 | using namespace tars; 22 | using namespace TarsTest; 23 | class BServantCallback : public BServantPrxCallback 24 | { 25 | 26 | public: 27 | BServantCallback(TarsCurrentPtr ¤t) 28 | : _current(current) 29 | {} 30 | 31 | virtual void callback_testHello(tars::Int32 ret, const std::string& sOut) 32 | { 33 | Proxy::async_response_testProxy(_current, ret, sOut); 34 | } 35 | virtual void callback_testHello_exception(tars::Int32 ret) 36 | { 37 | TLOGERROR("HelloCallback callback_testHello_exception ret:" << ret << endl); 38 | 39 | Proxy::async_response_testProxy(_current, ret, ""); 40 | } 41 | 42 | TarsCurrentPtr _current; 43 | }; 44 | 45 | 46 | 47 | ////////////////////////////////////////////////////// 48 | void ProxyImp::initialize() 49 | { 50 | //initialize servant here: 51 | //... 52 | ; 53 | _prx=Application::getCommunicator()->stringToProxy("TarsTest.UnitTest.BServantObj@tcp -h 127.0.0.1 -p 10007"); 54 | } 55 | 56 | ////////////////////////////////////////////////////// 57 | void ProxyImp::destroy() 58 | { 59 | } 60 | 61 | ////////////////////////////////////////////////////// 62 | tars::Int32 ProxyImp::test(tars::TarsCurrentPtr current) { return 0;} 63 | 64 | ////////////////////////////////////////////////////// 65 | tars::Int32 ProxyImp::testProxy(const std::string& sIn, std::string &sOut, tars::TarsCurrentPtr current) 66 | { 67 | try 68 | { 69 | current->setResponse(false); 70 | 71 | BServantPrxCallbackPtr cb = new BServantCallback(current); 72 | 73 | _prx->async_testHello(cb,sIn); 74 | } 75 | catch(std::exception &ex) 76 | { 77 | current->setResponse(true); 78 | 79 | TLOGERROR("ProxyImp::testProxy ex:" << ex.what() << endl); 80 | } 81 | 82 | return 0; 83 | } 84 | -------------------------------------------------------------------------------- /protocol/TarsTest/UnitTest/TupProxyImp.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Tencent is pleased to support the open source community by making Tars available. 3 | * 4 | * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. 5 | * 6 | * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except 7 | * in compliance with the License. You may obtain a copy of the License at 8 | * 9 | * https://opensource.org/licenses/BSD-3-Clause 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | #ifndef _TupProxyImp_H_ 18 | #define _TupProxyImp_H_ 19 | 20 | #include "servant/Application.h" 21 | #include "util/tc_http.h" 22 | #include "TupCallback.h" 23 | #include "AServant.h" 24 | 25 | using namespace std; 26 | using namespace tars; 27 | using namespace TarsTest; 28 | 29 | /** 30 | * 31 | * 32 | */ 33 | class TupProxyImp : public Servant 34 | { 35 | public: 36 | TupProxyImp(); 37 | /** 38 | * 39 | */ 40 | virtual ~TupProxyImp(); 41 | 42 | /** 43 | * 44 | */ 45 | virtual void initialize(); 46 | 47 | /** 48 | * 49 | */ 50 | virtual void destroy(); 51 | 52 | /** 53 | * 处理客户端的主动请求 54 | * @param current 55 | * @param response 56 | * @return int 57 | */ 58 | virtual int doRequest(TarsCurrentPtr current, vector& response); 59 | 60 | /** 61 | * @param resp 62 | * @return int 63 | */ 64 | virtual int doResponse(ReqMessagePtr resp); 65 | 66 | protected: 67 | /** 68 | * 处理请求 69 | * 70 | * @param current 71 | */ 72 | int doRequest(const tars::TarsCurrentPtr ¤t, const char *buffer, size_t length); 73 | 74 | /** 75 | * 解析出多个TUP包 76 | * 77 | * @param buffer 78 | * @param length 79 | * @param map > 80 | * @return int 81 | */ 82 | int parseTupRequest(const char *buffer, size_t length, map &mTupRequest); 83 | 84 | /** 85 | * 异步发送 86 | * 87 | * @param wup 88 | * @param proxy 89 | */ 90 | void tupAsyncCall(RequestPacket *wup, const AServantPrx &proxy, const TupCallbackPtr &cb); 91 | 92 | protected: 93 | int _iNum; 94 | int64_t _iTime; 95 | AServantPrx _pPrx; 96 | ProxyProtocol _prot_tup; 97 | TC_HttpRequest httpRequest; 98 | }; 99 | 100 | 101 | ///////////////////////////////////////////////////// 102 | #endif 103 | -------------------------------------------------------------------------------- /framework/StatServer/StatServer.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Tencent is pleased to support the open source community by making Tars available. 3 | * 4 | * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. 5 | * 6 | * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except 7 | * in compliance with the License. You may obtain a copy of the License at 8 | * 9 | * https://opensource.org/licenses/BSD-3-Clause 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | #ifndef __STAT_SERVER_H_ 18 | #define __STAT_SERVER_H_ 19 | 20 | #include "servant/Application.h" 21 | #include "servant/StatF.h" 22 | #include "StatHashMap.h" 23 | //#include "ReapSSDThread.h" 24 | 25 | using namespace tars; 26 | 27 | class StatServer : public Application 28 | { 29 | protected: 30 | /** 31 | * 初始化, 只会进程调用一次 32 | */ 33 | virtual void initialize(); 34 | 35 | /** 36 | * 析够, 每个进程都会调用一次 37 | */ 38 | virtual void destroyApp(); 39 | 40 | public: 41 | 42 | void getTimeInfo(time_t &tTime,string &sDate,string &sFlag); 43 | 44 | bool cmdSetRandOrder(const string& command, const string& params, string& result); 45 | 46 | //获取主调虚拟ip映射 47 | map& getVirtualMasterIp(void); 48 | 49 | string getRandOrder(void); 50 | 51 | string getClonePath(void); 52 | 53 | int getInserInterv(void); 54 | 55 | map >& getBuffer(){ return _vBuffer; } 56 | 57 | bool getSelectBuffer(int iIndex, int64_t iInterval); 58 | 59 | int getSelectBufferFromFlag(const string& sFlag); 60 | 61 | int getSelectBufferIndex() { return _iSelectBuffer; } 62 | 63 | void setSelectBufferIndex(int iIndex) { _iSelectBuffer = iIndex; } 64 | 65 | StatHashMap * getHashMapBuff(int iIndex, int iBuffer) { return &(_hashmap[iIndex][iBuffer]); } 66 | 67 | int getBuffNum() { return _iBuffNum; } 68 | 69 | private: 70 | void initHashMap(); 71 | 72 | private: 73 | 74 | //ReapSSDThread* _pReapSSDThread; 75 | 76 | //主调虚拟ip配置 77 | map _mVirtualMasterIp; 78 | 79 | // 随机入库开关 80 | string _sRandOrder; 81 | 82 | //数据换存目录 83 | string _sClonePath; 84 | 85 | //数据库插入间隔,单位分钟 86 | int _iInsertInterval; 87 | 88 | //双buffer机制 89 | map > _vBuffer; 90 | 91 | int _iSelectBuffer; 92 | 93 | StatHashMap **_hashmap; 94 | 95 | int _iBuffNum; 96 | }; 97 | 98 | extern TC_Config* g_pconf; 99 | extern StatServer g_app; 100 | 101 | #endif 102 | -------------------------------------------------------------------------------- /testcode/source/testcase/SimpleRPCTest.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Tencent is pleased to support the open source community by making Tars available. 3 | * 4 | * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. 5 | * 6 | * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except 7 | * in compliance with the License. You may obtain a copy of the License at 8 | * 9 | * https://opensource.org/licenses/BSD-3-Clause 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | #include "gtest/gtest.h" 18 | #include 19 | #include "servant/Application.h" 20 | #include "TarsServantName.h" 21 | #include "TarsTest/UnitTest/AServant.h" 22 | USING_NS_STD 23 | USING_NS_TARS 24 | using namespace TarsTest; 25 | 26 | /**************************************************** 27 | Tars典型测试 28 | 校验返回值 29 | ****************************************************/ 30 | 31 | struct SimpleRpcTest : public ::testing::Test 32 | { 33 | SimpleRpcTest() 34 | { 35 | _comm = Application::getCommunicator(); 36 | } 37 | protected: 38 | CommunicatorPtr _comm; 39 | }; 40 | 41 | class SimpleRpcObjCallback: public AServantPrxCallback 42 | { 43 | public: 44 | virtual void callback_queryTest(tars::Int32 ret, const vector& vRsp) 45 | { 46 | int iSize=vRsp.size(); 47 | EXPECT_EQ(iSize, 1); 48 | } 49 | }; 50 | typedef tars::TC_AutoPtr SimpleRpcObjCallbackPtr; 51 | 52 | TEST_F(SimpleRpcTest, should_response_rpc_when_client_sync_call_server_by_ip) 53 | { 54 | int64_t tBegin = TC_TimeProvider::getInstance()->getNowMs(); 55 | 56 | AServantPrx prx = _comm->stringToProxy (ASERVANT_ENDPOINT); 57 | ReqInfo stReq; 58 | stReq.sServerName="AServer"; 59 | stReq.sDate="20181106"; 60 | stReq.sStartTime="0000"; 61 | stReq.sEndTime="2360"; 62 | stReq.uInterval=1; 63 | vector vRsp; 64 | int iRet = prx->queryTest(stReq,vRsp); 65 | 66 | EXPECT_EQ(iRet, 0); 67 | TLOGDEBUG("Simple RPC test time cost: "<< " | " << TC_TimeProvider::getInstance()->getNowMs() - tBegin << "(ms)" << endl); 68 | } 69 | TEST_F(SimpleRpcTest, should_response_rpc_when_client_async_call_server_by_ip) 70 | { 71 | AServantPrx prx = _comm->stringToProxy (ASERVANT_ENDPOINT); 72 | ReqInfo stReq; 73 | stReq.sServerName="AServer"; 74 | stReq.sDate="20181106"; 75 | stReq.sStartTime="0000"; 76 | stReq.sEndTime="2360"; 77 | stReq.uInterval=1; 78 | SimpleRpcObjCallbackPtr cbp = new SimpleRpcObjCallback(); 79 | prx -> async_queryTest(cbp,stReq); 80 | 81 | TC_Common::sleep(1); 82 | 83 | } 84 | 85 | -------------------------------------------------------------------------------- /protocol/TarsTest/UnitTest/TupCallback.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Tencent is pleased to support the open source community by making Tars available. 3 | * 4 | * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. 5 | * 6 | * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except 7 | * in compliance with the License. You may obtain a copy of the License at 8 | * 9 | * https://opensource.org/licenses/BSD-3-Clause 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | #ifndef _TupCallback_H_ 18 | #define _TupCallback_H_ 19 | 20 | #include "servant/Application.h" 21 | #include "util/tc_http.h" 22 | 23 | using namespace std; 24 | using namespace tars; 25 | 26 | struct StatInfo 27 | { 28 | string sIp; 29 | int64_t iTime; 30 | string sHttpHeaderValue; 31 | }; 32 | 33 | /** 34 | * 异步回调对象 35 | */ 36 | class TupCallback : public ServantCallback 37 | { 38 | public: 39 | TupCallback(const string& type, 40 | const ServantPtr& servant, 41 | const TarsCurrentPtr& current) 42 | :ServantCallback(type, servant, current), _iResponse(0), _isHttpProt(true) 43 | { 44 | } 45 | 46 | /** 47 | * 析构 48 | */ 49 | virtual ~TupCallback(); 50 | 51 | /** 52 | * 响应 53 | * 54 | * @param buffer 55 | */ 56 | void doResponse_jce(const vector &buffer); 57 | void doResponse_tup(const vector &buffer); 58 | 59 | /** 60 | * 添加一个原始的tup 61 | * 62 | * @param tup 63 | */ 64 | void addtup(int32_t iRequestId, RequestPacket *tup); 65 | 66 | /** 67 | * 删除 68 | * @param iRequestId 69 | */ 70 | void deltup(int32_t iRequestId); 71 | 72 | //virtual int onDispatch(ReqMessagePtr msg); 73 | 74 | 75 | protected: 76 | void doResponse(); 77 | 78 | protected: 79 | 80 | /** 81 | * 请求的tup包 82 | */ 83 | map _tupRequest; 84 | 85 | /** 86 | * 相应包个数 87 | */ 88 | size_t _iResponse; 89 | 90 | /** 91 | * 所有的回应包 92 | */ 93 | //vector _allBuffer; 94 | string _allBuffer; 95 | 96 | string _nowServantName; //现在正在回包的servantname 97 | 98 | bool _isHttpProt; 99 | }; 100 | 101 | typedef TC_AutoPtr TupCallbackPtr; 102 | 103 | 104 | ///////////////////////////////////////////////////// 105 | #endif 106 | -------------------------------------------------------------------------------- /testcode/source/testcase/GlobalUTTest.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Tencent is pleased to support the open source community by making Tars available. 3 | * 4 | * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. 5 | * 6 | * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except 7 | * in compliance with the License. You may obtain a copy of the License at 8 | * 9 | * https://opensource.org/licenses/BSD-3-Clause 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | /* 18 | * GlobalUTTest.cpp 19 | * 20 | * Created on: 2018年10月29日 21 | * Author: abelguo 22 | */ 23 | 24 | #include "gtest/gtest.h" 25 | #include "TarsTest.h" 26 | #include "servant/BaseF.h" 27 | #include "servant/Global.h" 28 | 29 | USING_NS_STD 30 | USING_NS_TARS 31 | 32 | TEST(GlobalUTTest, UT_should_not_throw_exception_when_throwException_input_TARSSERVERSUCCESS) 33 | { 34 | EXPECT_NO_THROW(TarsException::throwException(TARSSERVERSUCCESS, "")); 35 | } 36 | 37 | TEST(GlobalUTTest, UT_should_throw_TarsServerDecodeException_when_throwException_input_TARSSERVERDECODEERR) 38 | { 39 | EXPECT_THROW(TarsException::throwException(TARSSERVERDECODEERR, ""), TarsServerDecodeException); 40 | } 41 | 42 | TEST(GlobalUTTest, UT_should_throw_TarsServerEncodeException_when_throwException_input_TARSSERVERENCODEERR) 43 | { 44 | EXPECT_THROW(TarsException::throwException(TARSSERVERENCODEERR, ""), TarsServerEncodeException); 45 | } 46 | 47 | TEST(GlobalUTTest, UT_should_throw_TarsServerNoFuncException_when_throwException_input_TARSSERVERNOFUNCERR) 48 | { 49 | EXPECT_THROW(TarsException::throwException(TARSSERVERNOFUNCERR, ""), TarsServerNoFuncException); 50 | } 51 | 52 | TEST(GlobalUTTest, UT_should_throw_TarsServerNoServantException_when_throwException_input_TARSSERVERNOSERVANTERR) 53 | { 54 | EXPECT_THROW(TarsException::throwException(TARSSERVERNOSERVANTERR, ""), TarsServerNoServantException); 55 | } 56 | 57 | TEST(GlobalUTTest, UT_should_throw_TarsServerQueueTimeoutException_when_throwException_input_TARSSERVERQUEUETIMEOUT) 58 | { 59 | EXPECT_THROW(TarsException::throwException(TARSSERVERQUEUETIMEOUT, ""), TarsServerQueueTimeoutException); 60 | } 61 | 62 | TEST(GlobalUTTest, UT_should_throw_TarsServerQueueTimeoutException_when_throwException_input_TARSPROXYCONNECTERR) 63 | { 64 | EXPECT_THROW(TarsException::throwException(TARSPROXYCONNECTERR, ""), TarsServerQueueTimeoutException); 65 | } 66 | 67 | TEST(GlobalUTTest, UT_should_throw_TarsServerUnknownException_when_throwException_input_unknow_ret) 68 | { 69 | int unknowRet = 30; 70 | EXPECT_THROW(TarsException::throwException(unknowRet, ""), TarsServerUnknownException); 71 | } 72 | -------------------------------------------------------------------------------- /testcode/source/testcase/framework/testAdminRegistry/test_tc_AdminRegistry.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Tencent is pleased to support the open source community by making Tars available. 3 | * 4 | * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved. 5 | * 6 | * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except 7 | * in compliance with the License. You may obtain a copy of the License at 8 | * 9 | * https://opensource.org/licenses/BSD-3-Clause 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | #include "gtest/gtest.h" 17 | #include "AdminReg.h" 18 | #include "servant/Application.h" 19 | #include "servant/Communicator.h" 20 | #include 21 | 22 | using namespace std; 23 | using namespace tars; 24 | 25 | 26 | class Test1 27 | { 28 | public: 29 | Test1(const string &sStr); 30 | ~Test1(); 31 | void th_dohandle(int excut_num); 32 | private: 33 | CommunicatorPtr _comm; 34 | AdminRegPrx _prx; 35 | }; 36 | 37 | Test1::Test1(const string &sStr) 38 | { 39 | _comm = Application::getCommunicator(); 40 | _prx = _comm->stringToProxy("tars.tarsAdminRegistry.AdminRegObj@tcp -h 10.208.139.242 -p 12000 -t 60000"); 41 | } 42 | 43 | Test1::~Test1() 44 | { 45 | 46 | } 47 | 48 | void Test1::th_dohandle(int excut_num) 49 | { 50 | for(int i=0; itars_set_timeout(15000)->getServerState(application, serverName, nodeName, state, result); 62 | EXPECT_TRUE(iRet == 0) << "tars get tarsconfig ServerState fail" << endl; 63 | } 64 | catch(TC_Exception &e) 65 | { 66 | EXPECT_TRUE(false) << "pthread id: " << std::this_thread::get_id() << "id: " << i << "exception: " << e.what() << endl; 67 | } 68 | catch(...) 69 | { 70 | EXPECT_TRUE(false) << "pthread id: " << std::this_thread::get_id() << "id: " << i << "unknown exception." << endl; 71 | } 72 | } 73 | } 74 | 75 | TEST(TarsFramework, AdminRegistry) 76 | { 77 | string s = "sObj"; 78 | Test1 test1(s); 79 | try 80 | { 81 | 82 | tars::Int32 times = TC_Common::strto(s); 83 | 84 | test1.th_dohandle(times); 85 | 86 | } 87 | catch(exception &e) 88 | { 89 | EXPECT_TRUE(false)< & vInput,vector & vEcho,tars::TarsCurrentPtr current) 70 | { 71 | vEcho.assign(vInput.begin(),vInput.end()); 72 | return 0; 73 | } 74 | tars::Int32 TypeDemoImp::echoMap(const map & mInput,map &mEcho,tars::TarsCurrentPtr current) 75 | { 76 | mEcho=mInput; 77 | return 0; 78 | } 79 | tars::Int32 TypeDemoImp::echoMultiType(const vector > & mtInput,vector > &mtEcho,tars::TarsCurrentPtr current) 80 | { 81 | mtEcho=mtInput; 82 | return 0; 83 | } 84 | 85 | //tars::Int32 TypeDemoImp::queryForTafPlus(const TarsTest::ReqInfo & req,vector &vRsp,tars::TarsCurrentPtr current) 86 | //{ 87 | // return 0; 88 | //} 89 | 90 | 91 | 92 | 93 | 94 | -------------------------------------------------------------------------------- /testcode/source/testcase/LoggerTest.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Tencent is pleased to support the open source community by making Tars available. 3 | * 4 | * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. 5 | * 6 | * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except 7 | * in compliance with the License. You may obtain a copy of the License at 8 | * 9 | * https://opensource.org/licenses/BSD-3-Clause 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | #include "gtest/gtest.h" 18 | #include 19 | #include "servant/Application.h" 20 | #include "servant/TarsLogger.h" 21 | #include 22 | using namespace tars; 23 | #define FILEPATH "/usr/local/app/tars/app_log/TarsTest/TestcaseServer/TarsTest.TestcaseServer" 24 | 25 | 26 | /**************************************************** 27 | Logger测试,包括测试日志等级和独立日志打印 28 | 1.改变本地日志等级,打印不同日志等级的日志 29 | 2.打印日志到不同的文件 30 | 3.校验日志文件是否存在 31 | ****************************************************/ 32 | 33 | /** 34 | * @brief 获取当前日期 35 | * @return int(返回码,0是成功,-3为超时) 36 | */ 37 | 38 | string getTime() 39 | { 40 | time_t timep; 41 | time (&timep); 42 | char tmp[64]; 43 | strftime(tmp, sizeof(tmp), "%Y%m%d",localtime(&timep) ); 44 | return tmp; 45 | } 46 | 47 | /** 48 | * @brief 打印日志 49 | */ 50 | 51 | int logger() 52 | { 53 | int i = 10; 54 | int64_t t = TC_Common::now2ms(); 55 | while(i) 56 | { 57 | 58 | LOG->info() << i << ":" << t << endl; 59 | LOG->debug() << i << ":" << t << endl; 60 | LOG->error() << i << ":" << t << endl; 61 | 62 | FDLOG("abc1") << i << ":" << t << endl; 63 | FDLOG("abc2") << i << ":" << t << endl; 64 | FDLOG("abc3") << i << ":" << t << endl; 65 | i--; 66 | } 67 | string strTime=getTime(); 68 | return i; 69 | 70 | } 71 | 72 | //日志打印测试 73 | TEST(LoggerTest, should_response_when_call_logger) 74 | { 75 | int64_t tBegin = TC_TimeProvider::getInstance()->getNowMs(); 76 | 77 | try 78 | { 79 | string strTime=getTime(); 80 | string logFile=FILEPATH; 81 | logFile.append("_"); 82 | logFile.append("abc1"); 83 | logFile.append("_"); 84 | logFile.append(strTime); 85 | logFile.append(".log"); 86 | int iRet=logger(); 87 | EXPECT_EQ(iRet, 0); 88 | 89 | TC_Common::sleep(1); 90 | EXPECT_EQ(TC_File::isFileExist(logFile), true); 91 | // int iRes=access(logFile.c_str(), F_OK); 92 | 93 | // EXPECT_EQ(iRes, 0); 94 | 95 | } 96 | catch(exception &ex) 97 | { 98 | TLOGDEBUG(ex.what() << endl); 99 | } 100 | 101 | TLOGDEBUG("LoggerTest time cost: "<< " | " << TC_TimeProvider::getInstance()->getNowMs() - tBegin << "(ms)" << endl); 102 | } 103 | 104 | 105 | -------------------------------------------------------------------------------- /testcode/source/testcase/util/test_tc_mem_queue.cpp: -------------------------------------------------------------------------------- 1 | #include "gtest/gtest.h" 2 | #include "servant/Application.h" 3 | #include "TarsTest/TestcaseServer/RPCTest.h" 4 | #include "servant/AdminF.h" 5 | #include "TarsServantName.h" 6 | 7 | 8 | 9 | #include "util/tc_mem_queue.h" 10 | #include "util/tc_sem_mutex.h" 11 | #include "util/tc_shm.h" 12 | #include "util/tc_thread_pool.h" 13 | #include "util/tc_common.h" 14 | // #include "util/tc_functor.h" 15 | #include 16 | #include 17 | 18 | using namespace std; 19 | using namespace tars; 20 | using namespace TarsTest; 21 | 22 | TC_Shm shm; 23 | TC_SemMutex semLock; 24 | TC_MemQueue memQueue; 25 | 26 | /** 27 | * @param s 28 | * @param i 29 | */ 30 | void writeQueue() 31 | { 32 | int i = 10000; 33 | while(i) 34 | { 35 | TC_LockT l(semLock); 36 | if(memQueue.push_back(TC_Common::tostr(i))) 37 | { 38 | // cout << pthread_self() << " | writeQueue OK " << i << ":" << memQueue.elementCount() << endl; 39 | i--; 40 | } 41 | else 42 | { 43 | // cout << pthread_self() << " | writeQueue FULL " << i << endl; 44 | return; 45 | } 46 | } 47 | } 48 | 49 | void readQueue() 50 | { 51 | while(true) 52 | { 53 | string s; 54 | TC_LockT l(semLock); 55 | if(memQueue.pop_front(s)) 56 | { 57 | // cout << pthread_self() << " | readQueue OK " << s << endl; 58 | } 59 | else 60 | { 61 | // cout << pthread_self() << " | readQueue EMPTY" << endl; 62 | // sleep(1); 63 | return; 64 | } 65 | } 66 | } 67 | 68 | TEST(TarsUtilTestcase, UT_TC_MemQueue) 69 | { 70 | 71 | size_t l = 1024000; 72 | shm.init(l, 8000); 73 | semLock.init(8000); 74 | 75 | if(shm.iscreate()) 76 | { 77 | memQueue.create(shm.getPointer(), l); 78 | } 79 | else 80 | { 81 | memQueue.connect(shm.getPointer(), l); 82 | } 83 | 84 | 85 | { 86 | TC_ThreadPool twpool; 87 | twpool.init(4); 88 | twpool.start(); 89 | 90 | // TC_Functor w(writeQueue); 91 | // TC_Functor::wrapper_type iwt(w); 92 | for(size_t i = 0; i < twpool.getThreadNum(); i++) 93 | { 94 | twpool.exec(writeQueue); 95 | } 96 | 97 | twpool.waitForAllDone(); 98 | } 99 | 100 | { 101 | TC_ThreadPool trpool; 102 | trpool.init(4); 103 | trpool.start(); 104 | 105 | // TC_Functor r(readQueue); 106 | // TC_Functor::wrapper_type irt(r); 107 | 108 | for(size_t i = 0; i < trpool.getThreadNum(); i++) 109 | { 110 | trpool.exec(readQueue); 111 | } 112 | 113 | trpool.waitForAllDone(); 114 | } 115 | 116 | } 117 | 118 | 119 | -------------------------------------------------------------------------------- /testcode/source/testcase/util/test_tc_autoptr.cpp: -------------------------------------------------------------------------------- 1 | #include "gtest/gtest.h" 2 | #include "servant/Application.h" 3 | #include "TarsTest/TestcaseServer/RPCTest.h" 4 | #include "servant/AdminF.h" 5 | #include "TarsServantName.h" 6 | 7 | 8 | #include "util/tc_autoptr.h" 9 | // #include "util/tc_functor.h" 10 | #include "util/tc_thread_pool.h" 11 | #include 12 | #include 13 | #include 14 | 15 | 16 | using namespace std; 17 | using namespace tars; 18 | using namespace TarsTest; 19 | 20 | 21 | /// begin test data 22 | // TC_Atomic a; 23 | 24 | class TestPointer : public TC_HandleBase 25 | { 26 | public: 27 | void func(int &i) 28 | { 29 | int n = 10000; 30 | while(n) 31 | { 32 | i++; 33 | n--; 34 | } 35 | } 36 | }; 37 | 38 | // void testAdd() 39 | // { 40 | // int i = 1000000; 41 | // while(i--) 42 | // { 43 | // a.inc(); 44 | // } 45 | // } 46 | 47 | // void testDel() 48 | // { 49 | // int i = 1000000; 50 | // while(i--) 51 | // { 52 | // a.dec(); 53 | // } 54 | // } 55 | 56 | // /// end test data 57 | 58 | // // 测试多线程并行读写 TC_Atomic 时的原子性与正确性 59 | // TEST(TarsUtilTestcase, UT_TC_Atomic) 60 | // { 61 | // EXPECT_EQ(0, a.get()); 62 | 63 | // TC_ThreadPool tpoolA; 64 | // tpoolA.init(10); 65 | // tpoolA.start(); 66 | 67 | // TC_ThreadPool tpoolB; 68 | // tpoolB.init(10); 69 | // tpoolB.start(); 70 | 71 | // { 72 | // // TC_Functor functor(testAdd); 73 | // // TC_Functor::wrapper_type wt(functor); 74 | 75 | // for(size_t i = 0; i < tpoolA.getThreadNum(); i++) 76 | // { 77 | // tpoolA.exec(testAdd); 78 | // } 79 | // } 80 | 81 | // { 82 | // // TC_Functor functor(testDel); 83 | // // TC_Functor::wrapper_type wt(functor); 84 | 85 | // for(size_t i = 0; i < tpoolB.getThreadNum(); i++) 86 | // { 87 | // tpoolB.exec(testDel); 88 | // } 89 | // } 90 | 91 | // tpoolA.waitForAllDone(); 92 | // tpoolB.waitForAllDone(); 93 | 94 | // EXPECT_EQ(0, a.get()); 95 | 96 | 97 | // } 98 | 99 | 100 | // 测试 TC_AutoPtr 的构造函数 TC_AutoPtr(T* p = 0) ? 101 | TEST(TarsUtilTestcase, UT_TC_AutoPtr) 102 | { 103 | int i = 0; 104 | 105 | typedef TC_AutoPtr TestPointerPtr; 106 | vector vtp; 107 | for(size_t j = 0; j < 10; j++) 108 | { 109 | vtp.push_back(new TestPointer()); 110 | } 111 | 112 | EXPECT_EQ(0, i); 113 | 114 | for(size_t j = 0; j < 10; j++) 115 | { 116 | vtp[j]->func(i); 117 | } 118 | EXPECT_EQ(10*10000, i); 119 | 120 | vector vtp1 = vtp; 121 | for(size_t j = 0; j < 10; j++) 122 | { 123 | vtp[j]->func(i); 124 | } 125 | 126 | EXPECT_EQ(20*10000, i); 127 | } 128 | 129 | -------------------------------------------------------------------------------- /testcode/source/testcase/StressTest.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Tencent is pleased to support the open source community by making Tars available. 3 | * 4 | * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. 5 | * 6 | * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except 7 | * in compliance with the License. You may obtain a copy of the License at 8 | * 9 | * https://opensource.org/licenses/BSD-3-Clause 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | #include "gtest/gtest.h" 17 | #include "TarsTest/UnitTest/AServant.h" 18 | #include "TarsServantName.h" 19 | #include "servant/Application.h" 20 | #include "servant/Communicator.h" 21 | #include "util/tc_thread_pool.h" 22 | #include 23 | 24 | using namespace std; 25 | using namespace TarsTest; 26 | using namespace tars; 27 | 28 | /**************************************************** 29 | 压力测试用例 30 | 利用TC_ThreadPool启用多个线程进行压力测试 31 | ****************************************************/ 32 | 33 | /** 34 | * @brief 请求执行方法 35 | * @param pprx 36 | * @param excut_num 37 | * @param size 38 | */ 39 | void dohandle(AServantPrx pprx,int excut_num,int size) 40 | { 41 | string s(size,'a'); 42 | for(int i=0; itars_set_timeout(15000)->saySomething(s,ret); 49 | } 50 | catch(TC_Exception &e) 51 | { 52 | cout << "pthread id: " << std::this_thread::get_id() << "id: " << i << "exception: " << e.what() << endl; 53 | } 54 | catch(...) 55 | { 56 | cout << "pthread id: " << std::this_thread::get_id()<< "id: " << i << "unknown exception." << endl; 57 | } 58 | } 59 | } 60 | 61 | TEST(StressTest,stress_test_when_call_multi_times) 62 | { 63 | int64_t tBegin = TC_TimeProvider::getInstance()->getNowMs(); 64 | 65 | CommunicatorPtr _comm=Application::getCommunicator(); 66 | AServantPrx prx=_comm->stringToProxy (ASERVANT_ENDPOINT); 67 | try 68 | { 69 | tars::Int32 threads = 10; 70 | TC_ThreadPool tp; 71 | tp.init(threads); 72 | tp.start(); 73 | 74 | TLOGDEBUG("init tp succ" << endl); 75 | int times=10; 76 | int size=100; 77 | for(int i = 0; igetNowMs() - tBegin << "(ms)" << endl); 96 | } 97 | 98 | -------------------------------------------------------------------------------- /testcode/source/testcase/TupTest.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Tencent is pleased to support the open source community by making Tars available. 3 | * 4 | * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. 5 | * 6 | * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except 7 | * in compliance with the License. You may obtain a copy of the License at 8 | * 9 | * https://opensource.org/licenses/BSD-3-Clause 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | #include 17 | #include 18 | // #include 19 | #include "gtest/gtest.h" 20 | #include "util/tc_common.h" 21 | #include "util/tc_clientsocket.h" 22 | #include "servant/Application.h" 23 | #include "tup/tup.h" 24 | #include "TarsTest/UnitTest/TupDemo.h" 25 | using namespace std; 26 | using namespace tars; 27 | using namespace TarsTest; 28 | 29 | /**************************************************** 30 | TCP请求方式测试TUP协议 31 | TUP协议是构建在Tars编解基础之上的传输协议 32 | TUP协议通过put和get方法打包和解包 33 | ****************************************************/ 34 | 35 | TEST(TupHttpTest, test_tup_http_when_client_sync_call_server_by_ip) 36 | { 37 | 38 | int64_t tBegin = TC_TimeProvider::getInstance()->getNowMs(); 39 | 40 | TC_TCPClient tcpClient; 41 | tcpClient.init("127.0.0.1", 10001, 3000); 42 | 43 | TarsUniPacket<> req; 44 | TarsUniPacket<>rsp; 45 | int iRequestId=1; 46 | req.setRequestId(iRequestId); 47 | req.setServantName("TarsTest.UnitTest.TupDemoObj"); 48 | req.setFuncName("aAddb"); 49 | 50 | struct AddInt addData; 51 | addData.adda = 2; 52 | addData.addb = 1; 53 | addData.addStr = "2 + 1 = 3"; 54 | req.put("addData",addData); 55 | 56 | string sendBuff; 57 | req.encode(sendBuff); 58 | 59 | int iRet = -1; 60 | try 61 | { 62 | 63 | 64 | int iSuc = tcpClient.send(sendBuff.c_str(),sendBuff.size()); 65 | char recvBuff[1024]={0}; 66 | size_t recvLen = sizeof(recvBuff); 67 | iSuc = tcpClient.recv(recvBuff,recvLen); 68 | rsp.decode(recvBuff,recvLen); 69 | 70 | 71 | TLOGDEBUG("[requestId]:" << rsp.getRequestId() << endl); 72 | TLOGDEBUG("[servantName]:" << rsp.getServantName() << endl); 73 | TLOGDEBUG("[funcName]:" << rsp.getFuncName() << endl); 74 | TLOGDEBUG("getTarsVersion|"<(""); 77 | 78 | 79 | } 80 | catch(TC_Exception &e) 81 | { 82 | cout << " exception: " << e.what() << endl; 83 | } 84 | catch(...) 85 | { 86 | cout << " unknown exception." << endl; 87 | } 88 | 89 | 90 | struct Result aAddbResult; 91 | rsp.get("aAddbResult",aAddbResult); 92 | EXPECT_EQ(iRet,0); 93 | EXPECT_EQ(aAddbResult.addResult,3); 94 | 95 | TLOGDEBUG("tup http test time cost: "<< " | " << TC_TimeProvider::getInstance()->getNowMs() - tBegin << "(ms)" << endl); 96 | } 97 | 98 | -------------------------------------------------------------------------------- /testcode/source/testcase/ProxyTest.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Tencent is pleased to support the open source community by making Tars available. 3 | * 4 | * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. 5 | * 6 | * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except 7 | * in compliance with the License. You may obtain a copy of the License at 8 | * 9 | * https://opensource.org/licenses/BSD-3-Clause 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | #include "gtest/gtest.h" 18 | #include "servant/Application.h" 19 | #include "TarsTest/UnitTest/Proxy.h" 20 | #include "TarsServantName.h" 21 | using namespace std; 22 | using namespace TarsTest; 23 | using namespace tars; 24 | /**************************************************** 25 | Porxy测试,主调调用Proxy的接口,Proxy调用被调 26 | 主调与Proxy、Proxy与被调之间都是tars调用 27 | 校验返回值 28 | ****************************************************/ 29 | 30 | /** 31 | * @brief 代理回调类 32 | */ 33 | 34 | class ProxyTestCallback: public ProxyPrxCallback 35 | { 36 | virtual void callback_testProxy(tars::Int32 ret, const std::string& sRsp) 37 | { 38 | EXPECT_EQ(ret,0); 39 | EXPECT_STREQ(sRsp.c_str(),"tars go"); 40 | } 41 | }; 42 | 43 | struct ProxyTest : public ::testing::Test 44 | { 45 | ProxyTest() 46 | { 47 | _comm = Application::getCommunicator(); 48 | } 49 | protected: 50 | CommunicatorPtr _comm; 51 | }; 52 | typedef tars::TC_AutoPtr ProxyTestCallbackPtr; 53 | 54 | //同步调用 55 | TEST_F(ProxyTest, should_response_when_syn_call_proxy_call) 56 | { 57 | int64_t tBegin = TC_TimeProvider::getInstance()->getNowMs(); 58 | 59 | ProxyPrx prx; 60 | prx=_comm->stringToProxy(PROXY_ENDPOINT); 61 | 62 | try 63 | { 64 | string sReq("tars go"); 65 | string sRsp(""); 66 | int iRet = prx->testProxy(sReq, sRsp); 67 | EXPECT_EQ(iRet,0); 68 | EXPECT_STREQ(sReq.c_str(),sRsp.c_str()); 69 | 70 | } 71 | catch(exception &ex) 72 | { 73 | 74 | TLOGERROR("ex:" << ex.what() << endl); 75 | } 76 | catch(...) 77 | { 78 | TLOGERROR("unknown exception." << endl); 79 | } 80 | 81 | TLOGDEBUG("Proxy test time cost: "<< " | " << TC_TimeProvider::getInstance()->getNowMs() - tBegin << "(ms)" << endl); 82 | } 83 | 84 | //异步调用 85 | TEST_F(ProxyTest, should_response_when_asyn_call_proxy_call) 86 | { 87 | ProxyPrx prx; 88 | prx=_comm->stringToProxy(PROXY_ENDPOINT); 89 | 90 | try 91 | { 92 | string sReq("tars go"); 93 | ProxyTestCallbackPtr cbp= new ProxyTestCallback(); 94 | prx->async_testProxy(cbp,sReq); 95 | TC_Common::sleep(1); 96 | } 97 | catch(exception &ex) 98 | { 99 | 100 | TLOGERROR("ex:" << ex.what() << endl); 101 | } 102 | catch(...) 103 | { 104 | TLOGERROR("unknown exception." << endl); 105 | } 106 | 107 | } 108 | 109 | -------------------------------------------------------------------------------- /testcode/source/testcase/util/test_tc_config.cpp: -------------------------------------------------------------------------------- 1 | #include "gtest/gtest.h" 2 | #include "servant/Application.h" 3 | #include "TarsTest/TestcaseServer/RPCTest.h" 4 | #include "servant/AdminF.h" 5 | #include "TarsServantName.h" 6 | 7 | 8 | #include "util/tc_config.h" 9 | #include "util/tc_file.h" 10 | #include 11 | // #include 12 | using namespace std; 13 | using namespace tars; 14 | using namespace TarsTest; 15 | 16 | 17 | TEST(TarsUtilTestcase, UT_TC_Config) 18 | { 19 | try { 20 | TC_Config conf; 21 | conf.parseFile("./template.config.conf"); 22 | 23 | cout << "parseFile************************************" << endl; 24 | cout << conf.tostr() << endl; 25 | 26 | vector n = conf.getDomainVector("/"); 27 | cout << TC_Common::tostr(n) << endl; 28 | 29 | // return 0; 30 | cout << "parseFile************************************" << endl; 31 | TC_Config conft = conf; 32 | cout << conft.tostr() << endl; 33 | 34 | cout << conf["/tars/application/client"] << endl; 35 | map m1 = conf.getDomainMap("/tars/allow"); 36 | cout << TC_Common::tostr(m1) << endl; 37 | cout << conf["/tars/allow"] << endl; 38 | 39 | cout << "parseString************************************" << endl; 40 | TC_File tf; 41 | conf.parseString(tf.load2str("./template.config.conf")); 42 | cout << conf.tostr() << endl; 43 | cout << conf["/tars/application/client"] << endl; 44 | 45 | cout << "insertDomain create false************************************" << endl; 46 | 47 | conf.insertDomain("/tars/insert", "insert", false); 48 | cout << conf.tostr() << endl; 49 | conf.parseString(conf.tostr()); 50 | cout << conf.tostr() << endl; 51 | 52 | cout << "insertDomain create true************************************" << endl; 53 | 54 | conf.insertDomain("/tars/insert", "insert", true); 55 | cout << conf.tostr() << endl; 56 | conf.parseString(conf.tostr()); 57 | cout << conf.tostr() << endl; 58 | 59 | cout << "insertDomainParam************************************" << endl; 60 | 61 | map m; 62 | m["abc"] = "def"; 63 | m["ttt"] = "yyy"; 64 | conf.insertDomainParam("/tars/insert/insert1", m, false); 65 | cout << conf.tostr() << endl; 66 | conf.insertDomainParam("/tars/insert/insert1", m, true); 67 | cout << conf.tostr() << endl; 68 | 69 | cout << "joinConfig true************************************" << endl; 70 | TC_Config conf1; 71 | conf1.parseFile("./log.config.conf"); 72 | 73 | conf1.joinConfig(conf, true); 74 | cout << conf1.tostr() << endl; 75 | 76 | cout << "joinConfig false************************************" << endl; 77 | TC_Config conf2; 78 | conf2.parseFile("./log.config.conf"); 79 | conf2.joinConfig(conf, false); 80 | cout << conf2.tostr() << endl; 81 | 82 | TC_Config conf3 = conf2; 83 | cout << conf3.tostr() << endl; 84 | } 85 | catch(exception &ex) 86 | { 87 | EXPECT_FALSE(false) << ex.what() << endl; 88 | } 89 | 90 | } 91 | 92 | 93 | -------------------------------------------------------------------------------- /testcode/source/testcase/util/test_tc_thread_queue.cpp: -------------------------------------------------------------------------------- 1 | #include "gtest/gtest.h" 2 | #include "servant/Application.h" 3 | #include "TarsTest/TestcaseServer/RPCTest.h" 4 | #include "servant/AdminF.h" 5 | #include "TarsServantName.h" 6 | 7 | 8 | #include "util/tc_monitor.h" 9 | #include "util/tc_common.h" 10 | #include "util/tc_thread_queue.h" 11 | #include "util/tc_autoptr.h" 12 | #include "util/tc_thread.h" 13 | #include "util/tc_logger.h" 14 | #include 15 | using namespace std; 16 | using namespace tars; 17 | using namespace TarsTest; 18 | 19 | TC_ThreadQueue _queue; 20 | TC_RollLogger _logger; 21 | 22 | struct A : public TC_HandleBase 23 | { 24 | }; 25 | 26 | typedef TC_AutoPtr APtr; 27 | 28 | class WriteThread : public TC_Thread, public TC_HandleBase 29 | { 30 | /** 31 | * ���� 32 | */ 33 | protected: 34 | virtual void run() 35 | { 36 | int i = 300; 37 | while(i-- > 0) 38 | { 39 | timeval t1; 40 | TC_Common::gettimeofday(t1); 41 | 42 | _queue.push_back("abc"); 43 | TC_Common::msleep(1); 44 | 45 | timeval t2; 46 | TC_Common::gettimeofday(t2); 47 | 48 | _logger.debug() << "push_back:" << t2.tv_usec - t1.tv_usec << endl; 49 | TC_Common::msleep(1); 50 | cout << "write" << endl; 51 | } 52 | } 53 | }; 54 | 55 | typedef TC_AutoPtr WriteThreadPtr; 56 | 57 | class ReadThread : public TC_Thread, public TC_HandleBase 58 | { 59 | protected: 60 | virtual void run() 61 | { 62 | int i = 100; 63 | while(i-- > 0) 64 | { 65 | string t; 66 | timeval t1; 67 | TC_Common::gettimeofday(t1); 68 | if(_queue.pop_front(t, 10)) 69 | { 70 | timeval t2; 71 | TC_Common::gettimeofday(t2); 72 | _logger.debug() << "pop_front:" << t2.tv_usec - t1.tv_usec << endl; 73 | // cout << pthread_self() << ":" << t << endl; 74 | TC_Common::msleep(20); 75 | // usleep(20 * 1000); 76 | } 77 | 78 | } 79 | } 80 | }; 81 | 82 | typedef TC_AutoPtr ReadThreadPtr; 83 | 84 | TEST(TarsUtilTestcase, UT_TC_ThreadQueue) 85 | { 86 | return; 87 | 88 | WriteThreadPtr wt = new WriteThread(); 89 | wt->start(); 90 | wt->getThreadControl().join(); 91 | 92 | vector rts; 93 | for(size_t i = 0; i < 3;i++) 94 | { 95 | rts.push_back(new ReadThread()); 96 | rts.back()->start(); 97 | rts.back()->getThreadControl().join(); 98 | } 99 | 100 | int done = true; 101 | while(true) { 102 | done = true; 103 | TC_Common::msleep(1000); 104 | // usleep(1000*1000); 105 | for(size_t i = 0; i < 3;i++) 106 | { 107 | if(rts[i]->isAlive()) { 108 | done = false; 109 | } 110 | } 111 | 112 | if(wt->isAlive()) { 113 | done =false; 114 | } 115 | 116 | if(done) { break; } 117 | } 118 | 119 | } 120 | 121 | 122 | -------------------------------------------------------------------------------- /testcode/source/testcase/CoroutineRpcTest.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Tencent is pleased to support the open source community by making Tars available. 3 | * 4 | * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. 5 | * 6 | * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except 7 | * in compliance with the License. You may obtain a copy of the License at 8 | * 9 | * https://opensource.org/licenses/BSD-3-Clause 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | 18 | #include "gtest/gtest.h" 19 | // #include "util/tc_bind.h" 20 | #include "servant/Application.h" 21 | #include "TarsTest/TestcaseServer/RPCTest.h" 22 | #include "TarsServantName.h" 23 | 24 | USING_NS_STD 25 | USING_NS_TARS 26 | using namespace TarsTest; 27 | 28 | struct CoroutineRpcTest : public ::testing::Test 29 | { 30 | CoroutineRpcTest() 31 | { 32 | _comm = Application::getCommunicator(); 33 | } 34 | void SetUp() 35 | { 36 | isCallbackCalled = false; 37 | } 38 | static bool isCallbackCalled; 39 | protected: 40 | CommunicatorPtr _comm; 41 | }; 42 | bool CoroutineRpcTest::isCallbackCalled = false; 43 | 44 | class RPCTestObjCoroPrxCallback: public RPCTestCoroPrxCallback 45 | { 46 | public: 47 | virtual void callback_test(Int32 ret) 48 | { 49 | EXPECT_EQ(ret, 2); 50 | CoroutineRpcTest::isCallbackCalled = true; 51 | } 52 | virtual void callback_testStr(Int32 ret, const std::string& sOut) 53 | { 54 | EXPECT_STREQ(sOut.c_str(), "out"); 55 | EXPECT_EQ(ret, 2); 56 | CoroutineRpcTest::isCallbackCalled = true; 57 | } 58 | }; 59 | typedef tars::TC_AutoPtr RPCTestObjCoroPrxCallbackPtr; 60 | 61 | //继承框架的协程类 62 | class TestCoroutine : public Coroutine 63 | { 64 | public: 65 | TestCoroutine(const string &sObj):_sObj(sObj) 66 | { 67 | _comm.stringToProxy(_sObj, prx); 68 | } 69 | ~TestCoroutine() {} 70 | 71 | void handle() 72 | { 73 | CoroParallelBasePtr sharedPtr = new CoroParallelBase(2); 74 | 75 | RPCTestObjCoroPrxCallbackPtr cb1 = new RPCTestObjCoroPrxCallback(); 76 | cb1->setCoroParallelBasePtr(sharedPtr); 77 | prx->coro_test(cb1); 78 | 79 | RPCTestObjCoroPrxCallbackPtr cb2 = new RPCTestObjCoroPrxCallback(); 80 | cb2->setCoroParallelBasePtr(sharedPtr); 81 | prx->coro_testStr(cb2); 82 | 83 | coroWhenAll(sharedPtr); 84 | } 85 | private: 86 | string _sObj; 87 | Communicator _comm; 88 | RPCTestPrx prx; 89 | }; 90 | 91 | 92 | TEST_F(CoroutineRpcTest, should_response_rpc_when_client_Coroutine_call_server_by_ip) 93 | { 94 | ServerConfig::OpenCoroutine = true; 95 | 96 | TestCoroutine testCoro(BASE_RPC_SERVANT_ENDPOINT); 97 | 98 | testCoro.setCoroInfo(1, 1, 128*1024); 99 | 100 | testCoro.start(); 101 | 102 | testCoro.getThreadControl().join(); 103 | 104 | TC_Common::sleep(1); 105 | 106 | EXPECT_TRUE(isCallbackCalled); 107 | } 108 | 109 | -------------------------------------------------------------------------------- /testcode/source/testcase/util/test_tc_log.cpp: -------------------------------------------------------------------------------- 1 | #include "gtest/gtest.h" 2 | #include "servant/Application.h" 3 | #include "TarsTest/TestcaseServer/RPCTest.h" 4 | #include "servant/AdminF.h" 5 | #include "TarsServantName.h" 6 | 7 | 8 | #include "util/tc_logger.h" 9 | // #include "util/tc_functor.h" 10 | 11 | using namespace std; 12 | using namespace tars; 13 | using namespace TarsTest; 14 | 15 | ostream& display(ostream& os) 16 | { 17 | // cout << &os << endl; 18 | // os << "display" << endl; 19 | return os; 20 | } 21 | 22 | TC_LoggerThreadGroup group; 23 | TC_RollLogger logger; 24 | TC_DayLogger dlogger; 25 | 26 | #define DT \ 27 | n1 = TC_Common::now2ms(); \ 28 | t = n1 - n; \ 29 | n = n1; 30 | 31 | #define OT if(t > 2) cout << i << ":" << t << endl; 32 | 33 | void test(){ofstream os; os << "abc";}; 34 | 35 | void RollTest( ) 36 | { 37 | int i = 1000000; 38 | 39 | while(i) 40 | { 41 | /* 42 | int64_t n = TC_Common::now2ms(); 43 | int64_t n1=TC_Common::now2ms(); 44 | int64_t t =TC_Common::now2ms(); 45 | */ 46 | // t = TC_Common::now2ms() - t; 47 | // logger.info() << "|" << i << "|" << t << endl; 48 | 49 | // DT; 50 | // ostringstream os; 51 | // test(); 52 | 53 | // display(logger.debug()); 54 | // logger.info(); 55 | logger.debug() << "|" << i << "|" << endl; 56 | // OT; 57 | /* 58 | DT; 59 | logger.warn() << "|" << i << "|" << t << endl; 60 | OT; 61 | 62 | DT; 63 | logger.error() << "|" << i << "|" << t << endl; 64 | OT; 65 | */ 66 | --i; 67 | } 68 | 69 | 70 | } 71 | 72 | void RollTestThread() 73 | { 74 | logger.init("./test", 1024000, 10); 75 | logger.modFlag(TC_RollLogger::HAS_LEVEL | TC_RollLogger::HAS_PID, true); 76 | logger.setupThread(&group); 77 | 78 | // cout << TC_Common::now2str() << endl; 79 | TC_ThreadPool tpoolA; 80 | tpoolA.init(10); 81 | tpoolA.start(); 82 | 83 | { 84 | // TC_Functor functor(RollTest); 85 | // TC_Functor::wrapper_type wtA(functor); 86 | 87 | for(size_t i = 0; i < tpoolA.getThreadNum(); i++) 88 | { 89 | tpoolA.exec(RollTest); 90 | } 91 | } 92 | 93 | tpoolA.waitForAllDone(); 94 | // cout << TC_Common::now2str() << endl; 95 | } 96 | 97 | void DayTest( ) 98 | { 99 | int i = 10000000; 100 | while(i) 101 | { 102 | dlogger.any() << i << endl; 103 | --i; 104 | } 105 | } 106 | 107 | void DayTestThread() 108 | { 109 | dlogger.init("./test", "%Y%m%d%H%M"); 110 | 111 | dlogger.setupThread(&group); 112 | 113 | // cout << TC_Common::now2str() << endl; 114 | TC_ThreadPool tpoolA; 115 | tpoolA.init(5); 116 | tpoolA.start(); 117 | 118 | { 119 | // TC_Functor functor(DayTest); 120 | // TC_Functor::wrapper_type wtA(functor); 121 | 122 | for(size_t i = 0; i < tpoolA.getThreadNum(); i++) 123 | { 124 | tpoolA.exec(DayTest); 125 | } 126 | } 127 | 128 | tpoolA.waitForAllDone(); 129 | // cout << TC_Common::now2str() << endl; 130 | } 131 | 132 | TEST(TarsUtilTestcase, UT_TC_Log) 133 | { 134 | 135 | group.start(3); 136 | 137 | RollTestThread(); 138 | // DayTestThread(); 139 | 140 | } 141 | 142 | -------------------------------------------------------------------------------- /framework/StatServer/StatImp.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Tencent is pleased to support the open source community by making Tars available. 3 | * 4 | * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. 5 | * 6 | * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except 7 | * in compliance with the License. You may obtain a copy of the License at 8 | * 9 | * https://opensource.org/licenses/BSD-3-Clause 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | #ifndef __STAT_IMP_H_ 18 | #define __STAT_IMP_H_ 19 | 20 | #include 21 | #include "util/tc_common.h" 22 | #include "util/tc_thread.h" 23 | #include "util/tc_option.h" 24 | #include "util/tc_file.h" 25 | //#include "util/tc_mysql.h" 26 | #include "util/tc_config.h" 27 | #include "util/tc_hash_fun.h" 28 | #include "servant/TarsLogger.h" 29 | #include "jmem/jmem_hashmap.h" 30 | #include "servant/StatF.h" 31 | #include "StatHashMap.h" 32 | 33 | using namespace tars; 34 | 35 | class StatImpThreadData// : public TC_ThreadPool::ThreadData 36 | { 37 | public: 38 | // static TC_ThreadMutex _mutex; //全局互斥锁 39 | // static pthread_key_t _key; //线程私有数据key 40 | static size_t _iNo; 41 | 42 | /** 43 | * 构造函数 44 | */ 45 | StatImpThreadData(); 46 | 47 | /** 48 | * 数据资源释放 49 | * @param p 50 | */ 51 | // static void destructor(void* p); 52 | 53 | /** 54 | * 获取线程数据,没有的话会自动创建 55 | * @return ServantProxyThreadData* 56 | */ 57 | static StatImpThreadData * getData(); 58 | 59 | public: 60 | static thread_local shared_ptr _data; 61 | size_t _iThreadIndex; 62 | }; 63 | 64 | class StatImp : public StatF,public TC_ThreadLock 65 | { 66 | public: 67 | /** 68 | * 69 | */ 70 | StatImp() 71 | : _hashf(tars::hash()) 72 | , _threadIndex(0) 73 | { 74 | }; 75 | 76 | ~StatImp() 77 | { 78 | }; 79 | 80 | /** 81 | * 初始化 82 | * 83 | * @return int 84 | */ 85 | virtual void initialize(); 86 | 87 | /** 88 | * 退出 89 | */ 90 | virtual void destroy() 91 | { 92 | }; 93 | 94 | /** 95 | * 上报模块间调用信息 96 | * @param statmsg, 上报信息 97 | * @return int, 返回0表示成功 98 | */ 99 | virtual int reportMicMsg( const map& statmsg, bool bFromClient, tars::TarsCurrentPtr current ); 100 | 101 | /** 102 | * 上报模块间调用采样信息 103 | * @param sample, 上报信息 104 | * @return int, 返回0表示成功 105 | */ 106 | virtual int reportSampleMsg(const vector &msg,tars::TarsCurrentPtr current ); 107 | 108 | using hash_functor = std::function; 109 | 110 | protected: 111 | 112 | int addHashMap(const StatMicMsgHead &head, const StatMicMsgBody &body); 113 | 114 | private: 115 | void dump2file(); 116 | 117 | string getSlaveName(const string& sSlaveName); 118 | 119 | private: 120 | hash_functor _hashf; 121 | size_t _threadIndex; 122 | }; 123 | 124 | #endif 125 | 126 | 127 | -------------------------------------------------------------------------------- /testcode/include/TarsServantName.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Tencent is pleased to support the open source community by making Tars available. 3 | * 4 | * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. 5 | * 6 | * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except 7 | * in compliance with the License. You may obtain a copy of the License at 8 | * 9 | * https://opensource.org/licenses/BSD-3-Clause 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | 18 | #ifndef TARS_TAF_TEST_TESTCODE_INCLUDE_TARSSERVANTNAME_H_ 19 | #define TARS_TAF_TEST_TESTCODE_INCLUDE_TARSSERVANTNAME_H_ 20 | #include "TarsTest.h" 21 | 22 | TARSTEST_NS_START 23 | 24 | #define EXCEPT_BASE_RPC_SERVANT_ENDPOINT "TarsTest.UnitTest.RPCTestObj@tcp -h 127.0.0.1 -p 9000" 25 | 26 | #define TEST_CASE_ADMIN_NAME_ENDPOINT "AdminObj@tcp -h 127.0.0.1 -p 10100" 27 | #define TEST_CASE_DYEING_ADMIN_NAME_ENDPOINT "AdminObj@tcp -h 127.0.0.1 -p 10200" 28 | 29 | #define HTTP_RPC_SERVANT_ENDPOINT "TarsTest.TestcaseServer.HttpDemoObj@tcp -h 127.0.0.1 -p 10101" 30 | #define MOCK_PROXY_SERVANT_ENDPOINT "TarsTest.TestcaseServer.RPCTestObj@tcp -h 127.0.0.1 -p 10102" 31 | #define BASE_RPC_SERVANT_ENDPOINT "TarsTest.TestcaseServer.RPCTestObj@tcp -h 127.0.0.1 -p 10103" 32 | #define UDP_RPC_SERVANT_ENDPOINT "TarsTest.TestcaseServer.UdpRPCObj@udp -h 127.0.0.1 -p 10104" 33 | #define PUSH_SERVANT_ENDPOINT "TarsTest.TestcaseServer.PushObj@tcp -h 127.0.0.1 -p 10105" 34 | #define ONEWAY_RPC_SERVANT_ENDPOIONT "TarsTest.TestcaseServer.OneWayRpcObj@tcp -h 127.0.0.1 -p 10110" 35 | 36 | #define UNIT_TEST_ADMIN_NAME_ENDPOINT "AdminObj@tcp -h 127.0.0.1 -p 10200" 37 | #define UNIT_TEST_CONFIG_NAME_ENDPOINT "ConfigObj@tcp -h 127.0.0.1 -p 10200" 38 | 39 | 40 | #define LOG_SERVER_ADMIN_NAME_ENDPOINT "AdminObj@tcp -h 127.0.0.1 -p 10300" 41 | #define STAT_SERVER_ADMIN_NAME_ENDPOINT "AdminObj@tcp -h 127.0.0.1 -p 10400" 42 | 43 | 44 | #define TYPT_TEST_ENDPOINT "TarsTest.UnitTest.TypeDemoObj@tcp -h 127.0.0.1 -p 10000 -t 60000" 45 | #define TUP_SERVANT_ENDPOINT "TarsTest.UnitTest.TupDemoObj@tcp -h 127.0.0.1 -p 10001 -t 60000" 46 | #define DYEING_SERVANT_ENDPOINT "TarsTest.UnitTest.DyeingTestObj@tcp -h 127.0.0.1 -p 10002 -t 60000" 47 | #define PING_SERVANT_ENDPOINT "TarsTest.UnitTest.TarsPingTestObj@tcp -h 127.0.0.1 -p 10003 -t 60000" 48 | #define ASERVANT_ENDPOINT "TarsTest.UnitTest.AServantObj@tcp -h 127.0.0.1 -p 10004 -t 60000" 49 | #define TUP_PROXY_ENDPOINT "TarsTest.UnitTest.TupProxyObj@tcp -h 127.0.0.1 -p 10005 -t 60000" 50 | #define AASERVANT_ENDPOINT "TarsTest.UnitTest.AAServantObj@tcp -h 127.0.0.1 -p 10006 -t 60000" 51 | #define PROXY_ENDPOINT "TarsTest.UnitTest.ProxyObj@tcp -h 127.0.0.1 -p 10008" 52 | 53 | 54 | #define BASE_RPC_SERVANT_NAME "TarsTest.TestcaseServer.RPCTestObj" 55 | #define BASE_RPC_SERVANT_NAME_INACTIVE "TarsTest.TestcaseServer.RPCInactiveTestObj" 56 | 57 | #define EPS_TEST_SERVANT_NAME "TarsTest.TestcaseServer.EpsTestObj" 58 | 59 | #define ONEWAY_RPC_SERVANT_NAME "TarsTest.TestcaseServer.OneWayRpcObj" 60 | #define UDP_RPC_SERVANT_NAME "TarsTest.TestcaseServer.UdpRPCObj" 61 | 62 | TARSTEST_NS_END 63 | 64 | #endif /* TARS_TAF_TEST_TESTCODE_INCLUDE_TARSSERVANTNAME_H_ */ 65 | -------------------------------------------------------------------------------- /testcode/source/testcase/ActiveDyeTest.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Tencent is pleased to support the open source community by making Tars available. 3 | * 4 | * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. 5 | * 6 | * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except 7 | * in compliance with the License. You may obtain a copy of the License at 8 | * 9 | * https://opensource.org/licenses/BSD-3-Clause 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | #include "util/tc_http.h" 17 | #include "util/tc_file.h" 18 | #include "util/tc_option.h" 19 | #include "servant/TarsLogger.h" 20 | #include "servant/Application.h" 21 | #include "gtest/gtest.h" 22 | #include "TarsServantName.h" 23 | #include "TarsTest/UnitTest/DyeingTest.h" 24 | using namespace std; 25 | using namespace tars; 26 | using namespace TarsTest; 27 | #define DYEFILEPATH "/usr/local/app/tars/app_log/tars_dyeing/" 28 | extern string getTime(); 29 | /**************************************************** 30 | 主动染色测试,染色开关作用域内日志额外打印到染色日志 31 | 校验染色文件是否存在 32 | ****************************************************/ 33 | 34 | TEST(ActiveDyeTest, should_response_when_call_active_dyeing) 35 | { 36 | int64_t tBegin = TC_TimeProvider::getInstance()->getNowMs(); 37 | try 38 | { 39 | CommunicatorPtr comm = new Communicator(); 40 | DyeingTestPrx dyeingPrx= comm->stringToProxy(DYEING_SERVANT_ENDPOINT); 41 | LOG->debug() << __FILE__ << "|" << __LINE__ << "|CLIENT:noDyeing" << endl; 42 | DLOG << __FILE__ << "|" << __LINE__ << "|CLIENT:noDyeing" << endl; 43 | FDLOG("T_D") << __FILE__ << "|" << __LINE__ << "|CLIENT:noDyeing" << endl; 44 | { 45 | 46 | TarsDyeingSwitch dye; 47 | dye.enableDyeing(); 48 | LOG->debug() << __FILE__ << "|" << __LINE__ << "|CLIENT:Dyeing" << endl; 49 | DLOG << __FILE__ << "|" << __LINE__ << "|CLIENT:Dyeing" << endl; 50 | FDLOG("T_D") << __FILE__ << "|" << __LINE__ << "|CLIENT:Dyeing" << endl; 51 | std::string s("123456"); 52 | std::string ret ; 53 | int iRet = dyeingPrx->testDyeing(s, ret); 54 | EXPECT_EQ(iRet,0); 55 | LOG->debug() << __FILE__ << "|" << __LINE__ << "|CLIENT:Dyed" << endl; 56 | DLOG << __FILE__ << "|" << __LINE__ << "|CLIENT:Dyed" << endl; 57 | FDLOG("T_D") << __FILE__ << "|" << __LINE__ << "|CLIENT:Dyed" << endl; 58 | } 59 | LOG->debug() << __FILE__ << "|" << __LINE__ << "|CLIENT:afterDye" << endl; 60 | DLOG << __FILE__ << "|" << __LINE__ << "|CLIENT:afterDye" << endl; 61 | FDLOG("T_D") << __FILE__ << "|" << __LINE__ << "|CLIENT:afterDye" << endl; 62 | string dyeFile=DYEFILEPATH; 63 | dyeFile.append("dyeing"); 64 | dyeFile.append("_"); 65 | dyeFile.append(getTime()); 66 | dyeFile.append(".log"); 67 | TC_Common::sleep(1); 68 | EXPECT_EQ(TC_File::isFileExist(dyeFile), true); 69 | // int iRet=access(dyeFile.c_str(), F_OK); 70 | // EXPECT_EQ(iRet,0); 71 | 72 | } 73 | catch (std::exception & e) 74 | { 75 | TLOGDEBUG("exception:" << e.what() << endl); 76 | 77 | 78 | } 79 | catch (...) 80 | { 81 | TLOGDEBUG("exception:..." << endl); 82 | } 83 | 84 | TC_Common::sleep(1); //等待异步写日志线程同步日志数据到tarlog 85 | 86 | TLOGDEBUG("Active dyeing time cost: "<< " | " << TC_TimeProvider::getInstance()->getNowMs() - tBegin << "(ms)" << endl); 87 | 88 | } 89 | -------------------------------------------------------------------------------- /testcode/include/stub/QueryImp.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Tencent is pleased to support the open source community by making Tars available. 3 | * 4 | * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. 5 | * 6 | * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except 7 | * in compliance with the License. You may obtain a copy of the License at 8 | * 9 | * https://opensource.org/licenses/BSD-3-Clause 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | #ifndef __QUERY_IMP_H__ 18 | #define __QUERY_IMP_H__ 19 | 20 | #include "servant/QueryF.h" 21 | #include "stub/DbHandle.h" 22 | 23 | using namespace tars; 24 | 25 | ////////////////////////////////////////////////////// 26 | 27 | enum FUNID 28 | { 29 | FUNID_findObjectById = 0, 30 | FUNID_findObjectById4Any = 1, 31 | FUNID_findObjectById4All = 2, 32 | FUNID_findObjectByIdInSameGroup = 3, 33 | FUNID_findObjectByIdInSameStation = 4, 34 | FUNID_findObjectByIdInSameSet = 5 35 | }; 36 | 37 | ////////////////////////////////////////////////////// 38 | /** 39 | * 对象查询接口类 40 | */ 41 | class QueryImp: public QueryF 42 | { 43 | public: 44 | /** 45 | * 构造函数 46 | */ 47 | QueryImp(){}; 48 | 49 | /** 50 | * 初始化 51 | */ 52 | virtual void initialize(); 53 | 54 | /** 55 | ** 退出 56 | */ 57 | virtual void destroy() {}; 58 | 59 | /** 60 | * 根据id获取所有该对象的活动endpoint列表 61 | */ 62 | virtual vector findObjectById(const string & id, tars::TarsCurrentPtr current); 63 | 64 | /** 65 | * 根据id获取所有对象,包括活动和非活动对象 66 | */ 67 | virtual tars::Int32 findObjectById4Any(const std::string & id, vector &activeEp, vector &inactiveEp, tars::TarsCurrentPtr current); 68 | 69 | /** 70 | * 根据id获取对象所有endpoint列表 71 | */ 72 | Int32 findObjectById4All(const std::string & id, vector &activeEp, vector &inactiveEp, tars::TarsCurrentPtr current); 73 | 74 | /** 75 | * 根据id获取对象同组endpoint列表 76 | */ 77 | Int32 findObjectByIdInSameGroup(const std::string & id, vector &activeEp, vector &inactiveEp, tars::TarsCurrentPtr current); 78 | 79 | /** 80 | * 根据id获取对象指定归属地的endpoint列表 81 | */ 82 | Int32 findObjectByIdInSameStation(const std::string & id, const std::string & sStation, vector &activeEp, vector &inactiveEp, tars::TarsCurrentPtr current); 83 | 84 | /** 85 | * 根据id获取对象同set endpoint列表 86 | */ 87 | Int32 findObjectByIdInSameSet(const std::string & id,const std::string & setId,vector &activeEp,vector &inactiveEp, tars::TarsCurrentPtr current); 88 | private: 89 | /** 90 | * 打印按天日志 91 | */ 92 | void doDaylog(const FUNID eFnId,const string& id,const vector &activeEp, const vector &inactiveEp, const tars::TarsCurrentPtr& current,const std::ostringstream& os,const string& sSetid=""); 93 | 94 | /** 95 | * 转化成字符串 96 | */ 97 | string eFunTostr(const FUNID eFnId); 98 | protected: 99 | //数据库操作 100 | CDbHandle _db; 101 | }; 102 | 103 | #endif 104 | -------------------------------------------------------------------------------- /testcode/source/testcase/PropertyTest.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Tencent is pleased to support the open source community by making Tars available. 3 | * 4 | * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. 5 | * 6 | * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except 7 | * in compliance with the License. You may obtain a copy of the License at 8 | * 9 | * https://opensource.org/licenses/BSD-3-Clause 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | #include "gtest/gtest.h" 17 | // #include "util/tc_bind.h" 18 | #include "servant/Application.h" 19 | #include "TarsTest/TestcaseServer/RPCTest.h" 20 | #include "TarsServantName.h" 21 | 22 | USING_NS_STD 23 | USING_NS_TARS 24 | using namespace TarsTest; 25 | 26 | struct PropertyTest : public ::testing::Test 27 | { 28 | PropertyTest() 29 | { 30 | _comm = Application::getCommunicator(); 31 | } 32 | protected: 33 | CommunicatorPtr _comm; 34 | }; 35 | 36 | TEST_F(PropertyTest, should_response_rpc_when_client_sync_call_server_by_ip) 37 | { 38 | 39 | map mapConfig; 40 | 41 | mapConfig["sync-invoke-timeout"] = "3000"; 42 | mapConfig["async-invoke-timeout"] = "3000"; 43 | mapConfig["refresh-endpoint-interval"] = "5000"; 44 | mapConfig["report-interval"] = "30"; 45 | mapConfig["sendthread"] = "2"; 46 | mapConfig["recvthread"] = "2"; 47 | mapConfig["asyncthread"] = "4"; 48 | mapConfig["sample-rate"] = "1000001"; 49 | mapConfig["max-sample-count"] = "15"; 50 | mapConfig["enableset"] = "y"; 51 | 52 | _comm->setProperty(mapConfig); 53 | 54 | RPCTestPrx prx = _comm->stringToProxy (BASE_RPC_SERVANT_ENDPOINT); 55 | 56 | 57 | int syncTimeOut = TC_Common::strto(_comm->getProperty("sync-invoke-timeout")); 58 | int AyncTimeOut = TC_Common::strto(_comm->getProperty("async-invoke-timeout")); 59 | int RefreshEpInterval = TC_Common::strto(_comm->getProperty("refresh-endpoint-interval")); 60 | int reportInterval = TC_Common::strto(_comm->getProperty("report-interval")); 61 | int sendthread = TC_Common::strto(_comm->getProperty("sendthread")); 62 | int recvthread = TC_Common::strto(_comm->getProperty("recvthread")); 63 | int asyncthread = TC_Common::strto(_comm->getProperty("asyncthread")); 64 | int samplerate = TC_Common::strto(_comm->getProperty("sample-rate")); 65 | int maxsamplecount = TC_Common::strto(_comm->getProperty("max-sample-count")); 66 | 67 | EXPECT_EQ(syncTimeOut, 3000); 68 | EXPECT_EQ(AyncTimeOut, 3000); 69 | EXPECT_EQ(RefreshEpInterval, 5000); 70 | EXPECT_EQ(reportInterval, 30); 71 | EXPECT_EQ(sendthread, 2); 72 | EXPECT_EQ(recvthread, 2); 73 | EXPECT_EQ(asyncthread, 4); 74 | EXPECT_EQ(samplerate, 1000001); 75 | EXPECT_EQ(maxsamplecount, 15); 76 | 77 | mapConfig["sync-invoke-timeout"] = "2000"; 78 | mapConfig["async-invoke-timeout"] = "2000"; 79 | mapConfig["refresh-endpoint-interval"] = "6000"; 80 | mapConfig["report-interval"] = "60"; 81 | mapConfig["sendthread"] = "1"; 82 | mapConfig["recvthread"] = "1"; 83 | mapConfig["asyncthread"] = "2"; 84 | mapConfig["sample-rate"] = "1000000"; 85 | mapConfig["max-sample-count"] = "10"; 86 | 87 | _comm->setProperty(mapConfig); 88 | 89 | prx = _comm->stringToProxy (BASE_RPC_SERVANT_ENDPOINT); 90 | } 91 | 92 | 93 | -------------------------------------------------------------------------------- /testcode/source/testcase/BaseRpcTest.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Tencent is pleased to support the open source community by making Tars available. 3 | * 4 | * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. 5 | * 6 | * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except 7 | * in compliance with the License. You may obtain a copy of the License at 8 | * 9 | * https://opensource.org/licenses/BSD-3-Clause 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | 18 | 19 | 20 | #include "gtest/gtest.h" 21 | // #include "util/tc_bind.h" 22 | #include "servant/Application.h" 23 | #include "TarsTest/TestcaseServer/RPCTest.h" 24 | #include "TarsServantName.h" 25 | 26 | USING_NS_STD 27 | USING_NS_TARS 28 | using namespace TarsTest; 29 | 30 | struct BaseRpcTest : public ::testing::Test 31 | { 32 | BaseRpcTest() 33 | { 34 | _comm = Application::getCommunicator(); 35 | } 36 | void SetUp() 37 | { 38 | isCallbackCalled = false; 39 | } 40 | static bool isCallbackCalled; 41 | protected: 42 | CommunicatorPtr _comm; 43 | }; 44 | bool BaseRpcTest::isCallbackCalled = false; 45 | 46 | class RPCTestObjCallback: public RPCTestPrxCallback 47 | { 48 | public: 49 | virtual void callback_test(Int32 ret) 50 | { 51 | EXPECT_EQ(ret, 2); 52 | BaseRpcTest::isCallbackCalled = true; 53 | } 54 | }; 55 | typedef tars::TC_AutoPtr RPCTestObjCallbackPtr; 56 | 57 | TEST_F(BaseRpcTest, should_response_rpc_when_client_sync_call_server_by_ip) 58 | { 59 | RPCTestPrx prx = _comm->stringToProxy (BASE_RPC_SERVANT_ENDPOINT); 60 | 61 | int res = prx->test(); 62 | 63 | EXPECT_EQ(res, 2); 64 | 65 | } 66 | TEST_F(BaseRpcTest, should_rpc_exception_when_client_sync_call_not_exist_server) 67 | { 68 | try 69 | { 70 | RPCTestPrx prx = _comm->stringToProxy (EXCEPT_BASE_RPC_SERVANT_ENDPOINT); 71 | 72 | prx->test(); 73 | 74 | ASSERT_TRUE(false); 75 | } 76 | catch (std::exception& e) 77 | { 78 | EXPECT_TRUE(string(e.what()).find("server unknown exception: ret:-7") != string::npos); 79 | } 80 | } 81 | 82 | 83 | TEST_F(BaseRpcTest, should_response_rpc_when_client_async_call_server_by_ip) 84 | { 85 | RPCTestPrx prx = _comm->stringToProxy (BASE_RPC_SERVANT_ENDPOINT); 86 | 87 | RPCTestObjCallbackPtr cbp = new RPCTestObjCallback(); 88 | 89 | prx -> async_test(cbp); 90 | 91 | TC_Common::sleep(1); 92 | 93 | EXPECT_TRUE(isCallbackCalled); 94 | } 95 | 96 | // extern "C" void handleResult(const promise::Future< RPCTestPrxCallbackPromise::PromisetestPtr >& f) 97 | // { 98 | // do 99 | // { 100 | // RPCTestPrxCallbackPromise::PromisetestPtr ptr = f.get(); 101 | // EXPECT_EQ(ptr->_ret, 2); 102 | // BaseRpcTest::isCallbackCalled = true; 103 | // } while (0); 104 | // } 105 | // TEST_F(BaseRpcTest, should_response_rpc_when_client_promise_async_call_server_by_ip) 106 | // { 107 | // RPCTestPrx prx; 108 | // map context; 109 | // prx = _comm->stringToProxy (BASE_RPC_SERVANT_ENDPOINT); 110 | 111 | // promise::Future< RPCTestPrxCallbackPromise::PromisetestPtr > f = prx -> promise_async_test(context); 112 | 113 | // f.then(tars::TC_Bind(&handleResult)); 114 | 115 | // sleep(1); 116 | 117 | // EXPECT_TRUE(isCallbackCalled); 118 | // } 119 | 120 | -------------------------------------------------------------------------------- /testcode/source/stub/PushServerDemo/TestPushServantImp.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Tencent is pleased to support the open source community by making Tars available. 3 | * 4 | * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. 5 | * 6 | * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except 7 | * in compliance with the License. You may obtain a copy of the License at 8 | * 9 | * https://opensource.org/licenses/BSD-3-Clause 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | #include "stub/TestPushServantImp.h" 18 | #include "servant/Application.h" 19 | 20 | using namespace std; 21 | 22 | class PushUser 23 | { 24 | public: 25 | static map pushUser; 26 | static TC_ThreadMutex mapMutex; 27 | }; 28 | map PushUser::pushUser; 29 | TC_ThreadMutex PushUser::mapMutex; 30 | 31 | ////////////////////////////////////////////////////// 32 | void TestPushServantImp::initialize() 33 | { 34 | //initialize servant here: 35 | //... 36 | } 37 | 38 | ////////////////////////////////////////////////////// 39 | void TestPushServantImp::destroy() 40 | { 41 | //destroy servant here: 42 | //... 43 | } 44 | 45 | void TestPushServantImp::setPushInfo(const string &sInfo, string& pushInfo) 46 | { 47 | unsigned int iBuffLength = htonl(sInfo.size()+8); 48 | unsigned char * pBuff = (unsigned char*)(&iBuffLength); 49 | 50 | pushInfo = ""; 51 | for (int i = 0; i<4; ++i) 52 | { 53 | pushInfo += *pBuff++; 54 | } 55 | 56 | unsigned int iRequestId = htonl(0); 57 | unsigned char * pRequestId = (unsigned char*)(&iRequestId); 58 | 59 | for (int i = 0; i<4; ++i) 60 | { 61 | pushInfo += *pRequestId++; 62 | } 63 | 64 | pushInfo += sInfo; 65 | } 66 | 67 | void TestPushServantImp::push(const string& sInfo) 68 | { 69 | string pushInfo; 70 | setPushInfo(sInfo, pushInfo); 71 | for(map::iterator it = (PushUser::pushUser).begin(); it != (PushUser::pushUser).end(); ++it) 72 | { 73 | (it->second)->sendResponse(pushInfo.c_str(), pushInfo.size()); 74 | LOG->debug() << "sendResponse: " << pushInfo.size() <& response) 79 | { 80 | //保存客户端的信息,以便对客户端进行push消息 81 | 82 | /*map::iterator it = PushUser::pushUser.find(current->getIp()); 83 | if(it == PushUser::pushUser.end()) 84 | { 85 | (PushUser::mapMutex).lock(); 86 | PushUser::pushUser.insert(map::value_type(current->getIp(), current)); 87 | (PushUser::mapMutex).unlock(); 88 | LOG->debug() << "connect ip: " << current->getIp() << endl; 89 | } 90 | push("Push Hello!");*/ 91 | string pushInfo; 92 | setPushInfo("Push Hello!", pushInfo); 93 | current->sendResponse(pushInfo.c_str(), pushInfo.size()); 94 | //返回给客户端它自己请求的数据包,即原包返回 95 | const vector& request = current->getRequestBuffer(); 96 | response = request; 97 | 98 | return 0; 99 | } 100 | //客户端关闭到服务端的连接,或者服务端发现客户端长时间未发送包过来,然后超过60s就关闭连接 101 | //调用的方法 102 | int TestPushServantImp::doClose(TarsCurrentPtr current) 103 | { 104 | /*(PushUser::mapMutex).lock(); 105 | map::iterator it = PushUser::pushUser.find(current->getIp()); 106 | if(it != PushUser::pushUser.end()) 107 | { 108 | PushUser::pushUser.erase(it); 109 | LOG->debug() << "close ip: " << current->getIp() << endl; 110 | } 111 | (PushUser::mapMutex).unlock();*/ 112 | 113 | return 0; 114 | } 115 | 116 | 117 | -------------------------------------------------------------------------------- /testcode/source/testcase/DyeingCaseTest.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Tencent is pleased to support the open source community by making Tars available. 3 | * 4 | * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. 5 | * 6 | * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except 7 | * in compliance with the License. You may obtain a copy of the License at 8 | * 9 | * https://opensource.org/licenses/BSD-3-Clause 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | #include "gtest/gtest.h" 18 | #include "servant/Application.h" 19 | #include "servant/AdminF.h" 20 | #include "TarsServantName.h" 21 | #include "TarsTest/UnitTest/DyeingTest.h" 22 | using namespace std; 23 | using namespace tars; 24 | using namespace TarsTest; 25 | #define DYEFILEPATH "/usr/local/app/tars/app_log/tars_dyeing/" 26 | extern string getTime(); 27 | /**************************************************** 28 | 被动染色测试,对接口调用进行染色。被动染色分为两步: 29 | 1.通过管理接口设置染色字段的key值 30 | 2.通过key值调用接口 31 | 校验染色文件是否存在 32 | ****************************************************/ 33 | 34 | struct DyeingTestCase : public ::testing::Test 35 | { 36 | DyeingTestCase() 37 | { 38 | _comm = Application::getCommunicator(); 39 | } 40 | protected: 41 | CommunicatorPtr _comm; 42 | }; 43 | 44 | 45 | //未打开染色开关的场景 46 | TEST_F(DyeingTestCase, should_response_when_call_no_dyeing_cmd) 47 | { 48 | int64_t tBegin = TC_TimeProvider::getInstance()->getNowMs(); 49 | 50 | DyeingTestPrx dyeingPrx= _comm->stringToProxy(DYEING_SERVANT_ENDPOINT); 51 | string strIn="123456"; 52 | string strOut; 53 | int ret=dyeingPrx->testDyeing(strIn,strOut); 54 | EXPECT_EQ(ret,0); 55 | 56 | TLOGDEBUG("no dyeing request time cost: "<< " | " << TC_TimeProvider::getInstance()->getNowMs() - tBegin << "(ms)" << endl); 57 | } 58 | //打开染色开关,但未使用染色key调用的场景 59 | TEST_F(DyeingTestCase, should_response_when_call_nodykey_cmd) 60 | { 61 | int64_t tBegin = TC_TimeProvider::getInstance()->getNowMs(); 62 | 63 | AdminFPrx adminFPrx = _comm->stringToProxy(UNIT_TEST_ADMIN_NAME_ENDPOINT); 64 | string setdyeing = adminFPrx->notify("tars.setdyeing 123456 TarsTest.UnitTest.DyeingTestObj testDyeing"); 65 | EXPECT_TRUE(setdyeing.find("DyeingKey=123456") != string::npos); 66 | DyeingTestPrx dyeingPrx= _comm->stringToProxy(DYEING_SERVANT_ENDPOINT); 67 | string strIn="abc"; 68 | string strOut; 69 | int ret=dyeingPrx->testDyeing(strIn,strOut); 70 | TC_Common::sleep(1); 71 | EXPECT_EQ(ret,0); 72 | 73 | TLOGDEBUG("dyeing without key request time cost: "<< " | " << TC_TimeProvider::getInstance()->getNowMs() - tBegin << "(ms)" << endl); 74 | 75 | } 76 | //打开染色开关,使用染色key调用的场景 77 | TEST_F(DyeingTestCase, should_response_when_call_dyeing_cmd) 78 | { 79 | int64_t tBegin = TC_TimeProvider::getInstance()->getNowMs(); 80 | 81 | AdminFPrx adminFPrx = _comm->stringToProxy(UNIT_TEST_ADMIN_NAME_ENDPOINT); 82 | string setdyeing = adminFPrx->notify("tars.setdyeing 123456 TarsTest.UnitTest.DyeingTestObj"); 83 | EXPECT_TRUE(setdyeing.find("DyeingKey=123456") != string::npos); 84 | DyeingTestPrx dyeingPrx= _comm->stringToProxy(DYEING_SERVANT_ENDPOINT); 85 | string strIn="123456"; 86 | string strOut; 87 | int ret=dyeingPrx->testDyeing(strIn,strOut); 88 | TC_Common::sleep(1); 89 | EXPECT_EQ(ret,0); 90 | string dyeFile=DYEFILEPATH; 91 | dyeFile.append("dyeing"); 92 | dyeFile.append("_"); 93 | dyeFile.append(getTime()); 94 | dyeFile.append(".log"); 95 | EXPECT_EQ(TC_File::isFileExist(dyeFile), true); 96 | // int iRet=access(dyeFile.c_str(), F_OK); 97 | // EXPECT_EQ(iRet,0); 98 | 99 | TLOGDEBUG("dyeing with key request time cost: "<< " | " << TC_TimeProvider::getInstance()->getNowMs() - tBegin << "(ms)" << endl); 100 | 101 | } 102 | 103 | 104 | 105 | -------------------------------------------------------------------------------- /testcode/source/testcase/AdminTest.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Tencent is pleased to support the open source community by making Tars available. 3 | * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. 4 | * 5 | * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except 6 | * in compliance with the License. You may obtain a copy of the License at 7 | * 8 | * https://opensource.org/licenses/BSD-3-Clause 9 | * 10 | * Unless required by applicable law or agreed to in writing, software distributed 11 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 12 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 13 | * specific language governing permissions and limitations under the License. 14 | */ 15 | 16 | #include "gtest/gtest.h" 17 | #include "servant/Application.h" 18 | #include "servant/AdminF.h" 19 | #include "TarsServantName.h" 20 | 21 | USING_NS_STD 22 | USING_NS_TARS 23 | /**************************************************** 24 | 管理接口测试,校验返回字符串 25 | ****************************************************/ 26 | TEST(AdminTest, should_response_when_call_admin_cmd) 27 | { 28 | CommunicatorPtr c = Application::getCommunicator(); 29 | 30 | AdminFPrx adminFPrx = c->stringToProxy(TEST_CASE_ADMIN_NAME_ENDPOINT); 31 | 32 | string loadconfig = adminFPrx->notify("tars.loadconfig TarsTest.TestcaseServer.config.conf"); 33 | EXPECT_TRUE(loadconfig.find("[succ] get remote config:") != string::npos); 34 | loadconfig = adminFPrx->notify("tars.loadconfig TarsTest.TestcaseServer.notExist.conf"); 35 | EXPECT_TRUE(loadconfig.find("[fail] get remote config ") != string::npos); 36 | 37 | string setloglevel = adminFPrx->notify("tars.setloglevel DEBUG"); 38 | EXPECT_TRUE(setloglevel.find("set log level [DEBUG] ok") != string::npos); 39 | 40 | string viewstatus = adminFPrx->notify("tars.viewstatus"); 41 | EXPECT_TRUE(viewstatus.find("notify prefix object num:1") != string::npos); 42 | 43 | // string viewversion = adminFPrx->notify("tars.viewversion"); 44 | // EXPECT_TRUE(viewversion.find("$1.1.0$") != string::npos); 45 | 46 | string connection = adminFPrx->notify("tars.connection"); 47 | EXPECT_TRUE(connection.find("[adater:AdminAdapter] [connections:1]") != string::npos); 48 | 49 | string loadproperty = adminFPrx->notify("tars.loadproperty"); 50 | EXPECT_TRUE(loadproperty.find("loaded config items:") != string::npos); 51 | 52 | string help = adminFPrx->notify("tars.help"); 53 | EXPECT_TRUE(help.find("tars.closecore") != string::npos); 54 | 55 | string closecout = adminFPrx->notify("tars.closecout NO"); 56 | cout << closecout << endl; 57 | 58 | string enabledaylog = adminFPrx->notify("tars.enabledaylog local|daylog|true"); 59 | EXPECT_TRUE(enabledaylog.find("set local daylog true ok") != string::npos); 60 | enabledaylog = adminFPrx->notify("tars.enabledaylog remote|false"); 61 | EXPECT_TRUE(enabledaylog.find("set remote false ok") != string::npos); 62 | 63 | string reloadlocator = adminFPrx->notify("tars.reloadlocator reload"); 64 | EXPECT_TRUE(reloadlocator.find("[notify prefix object num:1]") != string::npos); 65 | 66 | // string closecore = adminFPrx->notify("tars.closecore no"); 67 | // EXPECT_TRUE(closecore.find("after set cur:18446744073709551615;max: 18446744073709551615") != string::npos); 68 | // closecore = adminFPrx->notify("tars.closecore yes"); 69 | // EXPECT_TRUE(closecore.find("after set cur:0;max: 18446744073709551615") != string::npos); 70 | 71 | string errorcmd = adminFPrx->notify("tars.errorcmd"); 72 | EXPECT_STREQ(errorcmd.c_str(), ""); 73 | 74 | string normalcmd = adminFPrx->notify("AdminCmdNormalTest returnMark"); 75 | EXPECT_STREQ(normalcmd.c_str(), "[notify servant object num:1]\n[1]:returnMark AdminCmdNormalTest success!\n"); 76 | 77 | string normaldeletecmd = adminFPrx->notify("DeletePrefixCmd"); 78 | EXPECT_STREQ(normaldeletecmd.c_str(), "[notify servant object num:1]\n[1]:Delete success!\n"); 79 | 80 | // viewversion = adminFPrx->notify("tars.viewversion"); 81 | // EXPECT_STREQ(viewversion.c_str(), ""); 82 | } 83 | -------------------------------------------------------------------------------- /framework/LogServer/LogServer.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Tencent is pleased to support the open source community by making Tars available. 3 | * 4 | * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. 5 | * 6 | * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except 7 | * in compliance with the License. You may obtain a copy of the License at 8 | * 9 | * https://opensource.org/licenses/BSD-3-Clause 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | #include "LogServer.h" 18 | #include "util/tc_logger.h" 19 | #include "LogImp.h" 20 | 21 | void LogServer::initialize() 22 | { 23 | string s; 24 | loadLogFormat("","",s); 25 | 26 | //日志路径 27 | g_globe._log_path = _conf["/tars/log"]; 28 | 29 | //启动写线程 30 | g_globe._group.start(TC_Common::strto(_conf["/tars/log"])); 31 | 32 | string prefix = TC_Common::lower(_conf.get("/tars/log","true")); 33 | g_globe._bIpPrefix = (prefix == "true") ? true : false; 34 | 35 | //增加对象 36 | addServant(ServerConfig::Application + "." + ServerConfig::ServerName +".LogObj"); 37 | 38 | TARS_ADD_ADMIN_CMD_NORMAL("reloadLogFormat", LogServer::loadLogFormat); 39 | } 40 | 41 | bool LogServer::loadLogFormat(const string& command, const string& params, string& result) 42 | { 43 | TLOGDEBUG("LogServer::loadLogFormat command:" << command << "|params:" << params << endl); 44 | 45 | try 46 | { 47 | TC_Config conf; 48 | 49 | conf.parseFile(ServerConfig::ConfigFile); 50 | 51 | vector vHourlist; 52 | 53 | map mLogType; 54 | 55 | try 56 | { 57 | string sHour = conf["/tars/log/format"]; 58 | 59 | vHourlist = TC_Common::sepstr(sHour,"|;,"); 60 | 61 | sort(vHourlist.begin(),vHourlist.end()); 62 | 63 | unique(vHourlist.begin(),vHourlist.end()); 64 | 65 | result = "loadLogFormat succ:" + sHour; 66 | 67 | TLOGDEBUG("LogServer::loadLogFormat result:" << result << endl); 68 | 69 | DLOG<< "LogServer::loadLogFormat result:" << result << endl; 70 | 71 | //hour=app.server.file|app2.server2.file2 72 | map mType; 73 | if(conf.getDomainMap("/tars/log/logtype", mType)) 74 | { 75 | map::iterator it = mType.begin(); 76 | while(it != mType.end()) 77 | { 78 | vector vList = TC_Common::sepstr(it->second,"|;,"); 79 | for(size_t i = 0;i < vList.size();i++) 80 | { 81 | //app.server.file = hour 82 | mLogType[vList[i]] = it->first; 83 | 84 | TLOGDEBUG("LogServer::loadLogFormat " << vList[i] << "|" << it->first << endl); 85 | 86 | DLOG<<"LogServer::loadLogFormat " << vList[i] << "|" << it->first << endl; 87 | } 88 | it++; 89 | } 90 | } 91 | 92 | g_globe.update(vHourlist, mLogType); 93 | 94 | } 95 | catch(exception& e) 96 | { 97 | result += e.what(); 98 | TLOGERROR("LogServer::loadLogFormat command:" << command << "|params:" << params << "|result:" << result << endl); 99 | } 100 | 101 | return true; 102 | } 103 | catch(exception &e) 104 | { 105 | result += e.what(); 106 | TLOGERROR("LogServer::loadLogFormat command:" << command << "|params:" << params << "|result:" << result << endl); 107 | } 108 | 109 | return false; 110 | } 111 | 112 | void LogServer::destroyApp() 113 | { 114 | TLOGDEBUG("LogServer::destroyApp ok" << endl); 115 | } 116 | 117 | -------------------------------------------------------------------------------- /testcode/source/testcase/CommGetEpTest.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Tencent is pleased to support the open source community by making Tars available. 3 | * 4 | * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. 5 | * 6 | * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except 7 | * in compliance with the License. You may obtain a copy of the License at 8 | * 9 | * https://opensource.org/licenses/BSD-3-Clause 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | #include "gtest/gtest.h" 17 | // #include "util/tc_bind.h" 18 | #include "servant/Application.h" 19 | #include "TarsTest/TestcaseServer/RPCTest.h" 20 | #include "TarsServantName.h" 21 | #include "stub/DbHandle.h" 22 | 23 | 24 | USING_NS_STD 25 | USING_NS_TARS 26 | using namespace TarsTest; 27 | 28 | struct CommGetEpTest : public ::testing::Test 29 | { 30 | CommGetEpTest() 31 | { 32 | _comm = Application::getCommunicator(); 33 | } 34 | protected: 35 | CommunicatorPtr _comm; 36 | }; 37 | 38 | 39 | TEST_F(CommGetEpTest, should_return_endpoint) 40 | { 41 | string setName = "ab.cd.ef"; 42 | 43 | CDbHandle::addInactiveEndPoint(BASE_RPC_SERVANT_NAME, 10121, TC_Endpoint::TCP); 44 | 45 | CDbHandle::addActiveEndPoint(BASE_RPC_SERVANT_NAME, 10103, TC_Endpoint::TCP); 46 | 47 | RPCTestPrx prx = _comm->stringToProxy (BASE_RPC_SERVANT_ENDPOINT); 48 | 49 | vector activeEndPoint, inactiveEndPoint; 50 | prx->tars_endpoints(activeEndPoint, inactiveEndPoint); 51 | 52 | EXPECT_EQ(activeEndPoint.size(), 1); 53 | EXPECT_EQ(inactiveEndPoint.size(), 1); 54 | } 55 | 56 | 57 | TEST_F(CommGetEpTest, should_return_endpoint_all) 58 | { 59 | CDbHandle::addInactiveEndPoint(BASE_RPC_SERVANT_NAME, 10121, TC_Endpoint::TCP); 60 | 61 | RPCTestPrx prx = _comm->stringToProxy (BASE_RPC_SERVANT_ENDPOINT); 62 | 63 | vector activeEndPoint, inactiveEndPoint; 64 | prx->tars_endpointsAll(activeEndPoint, inactiveEndPoint); 65 | 66 | EXPECT_EQ(activeEndPoint.size(), 1); 67 | EXPECT_EQ(inactiveEndPoint.size(), 1); 68 | } 69 | 70 | TEST_F(CommGetEpTest, should_return_endpoint_by_set) 71 | { 72 | string setName = "ab.cd.ef"; 73 | 74 | CDbHandle::addEndPointbySet(BASE_RPC_SERVANT_NAME, 10103, TC_Endpoint::TCP, "ab", "cd", "ef"); 75 | 76 | RPCTestPrx prx = _comm->stringToProxy (BASE_RPC_SERVANT_ENDPOINT); 77 | 78 | vector activeEndPoint, inactiveEndPoint; 79 | prx->tars_endpointsBySet(setName, activeEndPoint, inactiveEndPoint); 80 | 81 | EXPECT_EQ(activeEndPoint.size(), 1); 82 | EXPECT_EQ(inactiveEndPoint.size(), 1); 83 | } 84 | 85 | TEST_F(CommGetEpTest, should_response_all_endpoints) 86 | { 87 | 88 | CDbHandle::addActiveEndPoint(BASE_RPC_SERVANT_NAME, 10103, TC_Endpoint::TCP); 89 | 90 | vector vEndpoint = _comm->getEndpoint("TarsTest.TestcaseServer.RPCTestObj"); 91 | 92 | RPCTestPrx prx; 93 | string sEndpoint(""); 94 | sEndpoint += "TarsTest.TestcaseServer.RPCTestObj"; 95 | sEndpoint += "@"; 96 | sEndpoint += vEndpoint[0].toString(); 97 | 98 | prx = _comm->stringToProxy (sEndpoint); 99 | 100 | int res = prx->test(); 101 | EXPECT_EQ(res, 2); 102 | 103 | vector vEndpointAll = _comm->getEndpoint4All(BASE_RPC_SERVANT_NAME); 104 | 105 | RPCTestPrx prxall; 106 | for(size_t i = 0; i < vEndpointAll.size(); ++i) 107 | { 108 | string sEndpointall(""); 109 | sEndpointall += BASE_RPC_SERVANT_NAME; 110 | sEndpointall += "@"; 111 | sEndpointall += vEndpointAll[i].toString(); 112 | 113 | cout << sEndpointall << endl; 114 | 115 | prxall = _comm->stringToProxy (sEndpointall); 116 | } 117 | 118 | int resall = prxall->test(); 119 | EXPECT_EQ(resall, 2); 120 | } 121 | 122 | 123 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | macro(build_unitest_lib MODULE ) 3 | 4 | project(${MODULE}) 5 | 6 | include_directories(./) 7 | 8 | FILE(GLOB_RECURSE DIR_SRCS "*.cpp") 9 | 10 | FILE(GLOB TARS_LIST "${CMAKE_CURRENT_SOURCE_DIR}/*.tars") 11 | 12 | set(TARS_LIST_DEPENDS) 13 | 14 | if (TARS_LIST) 15 | set(CLEAN_LIST) 16 | 17 | foreach (TARS_SRC ${TARS_LIST}) 18 | get_filename_component(NAME_WE ${TARS_SRC} NAME_WE) 19 | 20 | set(TARS_H ${NAME_WE}.h) 21 | 22 | set(CUR_TARS_GEN ${CMAKE_CURRENT_SOURCE_DIR}/${TARS_H}) 23 | LIST(APPEND TARS_LIST_DEPENDS ${CUR_TARS_GEN}) 24 | 25 | add_custom_command(OUTPUT ${CUR_TARS_GEN} 26 | WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} 27 | DEPENDS ${TARS2CPP} 28 | COMMAND ${TARS2CPP} ${TARS_SRC} 29 | COMMENT "${TARS2CPP} ${TARS_SRC}") 30 | 31 | list(APPEND CLEAN_LIST ${CMAKE_CURRENT_SOURCE_DIR}/${TARS_H}) 32 | 33 | endforeach () 34 | 35 | set_directory_properties(PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES "${CLEAN_LIST}") 36 | 37 | set(TARS_TARGET "TARS_${MODULE}") 38 | add_custom_target(${TARS_TARGET} ALL DEPENDS ${TARS_LIST_DEPENDS} tars2cpp) 39 | 40 | add_library(${MODULE} ${DIR_SRCS}) 41 | 42 | add_dependencies(${MODULE} ${TARS_TARGET}) 43 | 44 | else(TARS_LIST) 45 | add_library(${MODULE} ${DIR_SRCS}) 46 | endif(TARS_LIST) 47 | 48 | endmacro() 49 | 50 | macro(build_unitest MODULE ) 51 | 52 | project(${MODULE}) 53 | 54 | include_directories(./) 55 | 56 | FILE(GLOB_RECURSE DIR_SRCS "*.cpp") 57 | 58 | FILE(GLOB TARS_LIST "${CMAKE_CURRENT_SOURCE_DIR}/*.tars") 59 | 60 | set(TARS_LIST_DEPENDS) 61 | 62 | if (TARS_LIST) 63 | set(CLEAN_LIST) 64 | 65 | foreach (TARS_SRC ${TARS_LIST}) 66 | get_filename_component(NAME_WE ${TARS_SRC} NAME_WE) 67 | 68 | set(TARS_H ${NAME_WE}.h) 69 | 70 | set(CUR_TARS_GEN ${CMAKE_CURRENT_SOURCE_DIR}/${TARS_H}) 71 | LIST(APPEND TARS_LIST_DEPENDS ${CUR_TARS_GEN}) 72 | 73 | add_custom_command(OUTPUT ${CUR_TARS_GEN} 74 | WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} 75 | DEPENDS ${TARS2CPP} 76 | COMMAND ${TARS2CPP} ${TARS_SRC} 77 | COMMENT "${TARS2CPP} ${TARS_SRC}") 78 | 79 | list(APPEND CLEAN_LIST ${CMAKE_CURRENT_SOURCE_DIR}/${TARS_H}) 80 | 81 | endforeach () 82 | 83 | set_directory_properties(PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES "${CLEAN_LIST}") 84 | 85 | set(TARS_TARGET "TARS_${MODULE}") 86 | add_custom_target(${TARS_TARGET} ALL DEPENDS ${TARS_LIST_DEPENDS} tars2cpp) 87 | 88 | add_executable(${MODULE} ${DIR_SRCS}) 89 | 90 | add_dependencies(${MODULE} ${TARS_TARGET}) 91 | 92 | else(TARS_LIST) 93 | add_executable(${MODULE} ${DIR_SRCS}) 94 | endif(TARS_LIST) 95 | 96 | target_link_libraries(${MODULE} tarsservant tarsutil) 97 | 98 | if(TARS_SSL) 99 | target_link_libraries(${MODULE} tarsservant tarsutil ${LIB_SSL} ${LIB_CRYPTO}) 100 | 101 | if(WIN32) 102 | target_link_libraries(${MODULE} Crypt32) 103 | endif() 104 | endif() 105 | 106 | if(TARS_HTTP2) 107 | target_link_libraries(${MODULE} ${LIB_HTTP2}) 108 | endif() 109 | 110 | if(TARS_GPERF) 111 | target_link_libraries(${MODULE} ${LIB_GPERF}) 112 | endif(TARS_GPERF) 113 | 114 | endmacro() 115 | 116 | #----------------------------------------------------------------------- 117 | 118 | add_subdirectory(protocol/TarsTest/TestcaseServer) 119 | add_subdirectory(protocol/TarsTest/UnitTest) 120 | add_subdirectory(testcode) 121 | add_subdirectory(framework) 122 | 123 | #----------------------------------------------------------------------- 124 | set(WORKING_DIRECTORY ${CMAKE_BINARY_DIR}) 125 | add_custom_target(run-unittest 126 | WORKING_DIRECTORY ${WORKING_DIRECTORY} 127 | COMMAND ${WORKING_DIRECTORY}/../unittest/script/run_test.sh 128 | COMMENT "call run unittest") 129 | -------------------------------------------------------------------------------- /framework/StatServer/StatHashMap.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Tencent is pleased to support the open source community by making Tars available. 3 | * 4 | * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. 5 | * 6 | * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except 7 | * in compliance with the License. You may obtain a copy of the License at 8 | * 9 | * https://opensource.org/licenses/BSD-3-Clause 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | #ifndef __STAT_HASH_MAP_H_ 18 | #define __STAT_HASH_MAP_H_ 19 | 20 | #include "servant/StatF.h" 21 | #include "util/tc_common.h" 22 | #include "util/tc_thread.h" 23 | #include "util/tc_option.h" 24 | #include "jmem/jmem_hashmap.h" 25 | #include "util/tc_file.h" 26 | #include "util/tc_config.h" 27 | #include "servant/TarsLogger.h" 28 | 29 | using namespace tars; 30 | 31 | #if TARGET_PLATFORM_IOS 32 | typedef TarsHashMap HashMap;//FileStorePolicy 33 | #elif TARGET_PLATFORM_WINDOWS 34 | typedef TarsHashMap HashMap;//FileStorePolicy 35 | #else 36 | typedef TarsHashMap HashMap;//FileStorePolicy 37 | #endif 38 | 39 | // typedef TarsHashMap HashMap;//FileStorePolicy 40 | 41 | typedef std::map> StatMsg; 42 | 43 | class StatHashMap : public HashMap 44 | { 45 | public: 46 | /** 47 | * 增加数据 48 | * @param Key 49 | * @param Value 50 | * 51 | * @return int 52 | */ 53 | int add(const tars::StatMicMsgHead &head, const tars::StatMicMsgBody &body) 54 | { 55 | StatMicMsgBody stBody; 56 | int ret = TC_HashMap::RT_OK; 57 | tars::TarsOutputStream osk; 58 | head.writeTo(osk); 59 | string sk(osk.getBuffer(), osk.getLength()); 60 | string sv; 61 | time_t t = 0; 62 | 63 | { 64 | 65 | TC_LockT lock(ThreadLockPolicy::mutex()); 66 | ret = this->_t.get(sk, sv,t); 67 | 68 | if (ret < 0) 69 | { 70 | return -1; 71 | } 72 | 73 | //读取到数据了, 解包 74 | if (ret == TC_HashMap::RT_OK) 75 | { 76 | tars::TarsInputStream is; 77 | is.setBuffer(sv.c_str(), sv.length()); 78 | stBody.readFrom(is); 79 | } 80 | 81 | stBody.count += body.count; 82 | stBody.execCount += body.execCount; 83 | stBody.timeoutCount += body.timeoutCount; 84 | 85 | for(map::const_iterator it = body.intervalCount.begin();it!= body.intervalCount.end();it++) 86 | { 87 | stBody.intervalCount[it->first] += it->second; 88 | } 89 | 90 | stBody.totalRspTime += body.totalRspTime; 91 | if(stBody.maxRspTime < body.maxRspTime) 92 | { 93 | stBody.maxRspTime = body.maxRspTime; 94 | } 95 | //非0最小值 96 | if( stBody.minRspTime == 0 || (stBody.minRspTime > body.minRspTime && body.minRspTime != 0)) 97 | { 98 | stBody.minRspTime = body.minRspTime; 99 | } 100 | 101 | tars::TarsOutputStream osv; 102 | stBody.writeTo(osv); 103 | string stemp(osv.getBuffer(), osv.getLength()); 104 | vector vtData; 105 | 106 | TLOGINFO("StatHashMap::add sk.length:" << sk.length() << " v.length:" << stemp.length() << endl); 107 | 108 | ret = this->_t.set(sk, stemp, true, vtData); 109 | } 110 | 111 | return ret; 112 | } 113 | }; 114 | 115 | #endif 116 | 117 | 118 | -------------------------------------------------------------------------------- /testcode/source/testcase/TupProxyTest.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Tencent is pleased to support the open source community by making Tars available. 3 | * 4 | * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. 5 | * 6 | * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except 7 | * in compliance with the License. You may obtain a copy of the License at 8 | * 9 | * https://opensource.org/licenses/BSD-3-Clause 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | #include 18 | #include "servant/Application.h" 19 | #include "gtest/gtest.h" 20 | 21 | using namespace std; 22 | using namespace tars; 23 | 24 | /** 25 | * @brief 构造Tup请求包 26 | * @param id 27 | * @return string 28 | */ 29 | 30 | string parsebuffer(int id) 31 | { 32 | string buff; 33 | UniPacket<> req; 34 | 35 | req.setRequestId(id); 36 | req.setServantName("AServer"); 37 | req.setFuncName("testInt"); 38 | req.put("iIn", 2); 39 | 40 | req.encode(buff); 41 | 42 | return buff; 43 | } 44 | 45 | // /** 46 | // * @brief 请求方法 47 | // * @param stHttp 48 | // * @param tcpClient 49 | // * @param stHttpRsp 50 | // * @param iTimeout 51 | // * @return int(返回码,0是成功,-3为超时) 52 | // */ 53 | // int doTupRequest(TC_HttpRequest& stHttp,TC_TCPClient&tcpClient, TC_HttpResponse &stHttpRsp, int iTimeout) 54 | // { 55 | // string sSendBuffer = stHttp.encode(); 56 | // int iRet = tcpClient.send(sSendBuffer.c_str(), sSendBuffer.length()); 57 | // if(iRet != TC_ClientSocket::EM_SUCCESS) 58 | // { 59 | // return iRet; 60 | // } 61 | 62 | // stHttpRsp.reset(); 63 | 64 | // string sBuffer; 65 | 66 | // char *sTmpBuffer = new char[10240]; 67 | 68 | // size_t iRecvLen = 10240; 69 | 70 | // while(true) 71 | // { 72 | // iRecvLen = 10240; 73 | 74 | // iRet = tcpClient.recv(sTmpBuffer, iRecvLen); 75 | 76 | 77 | // if(iRet == TC_ClientSocket::EM_SUCCESS) 78 | // sBuffer.append(sTmpBuffer, iRecvLen); 79 | 80 | // switch(iRet) 81 | // { 82 | // case TC_ClientSocket::EM_SUCCESS: 83 | // if(stHttpRsp.incrementDecode(sBuffer)) 84 | // { 85 | 86 | // delete []sTmpBuffer; 87 | // return TC_ClientSocket::EM_SUCCESS; 88 | // } 89 | // else 90 | // { 91 | 92 | // } 93 | // continue; 94 | // case TC_ClientSocket::EM_CLOSE: 95 | // delete []sTmpBuffer; 96 | // stHttpRsp.incrementDecode(sBuffer); 97 | // return TC_ClientSocket::EM_SUCCESS; 98 | 99 | // default: 100 | // delete []sTmpBuffer; 101 | // return iRet; 102 | // } 103 | // } 104 | 105 | // return 0; 106 | // } 107 | /** 108 | * @brief 执行请求方法 109 | * @return int(返回码,0是成功,-3为超时) 110 | */ 111 | 112 | int tup_dohandle() 113 | { 114 | 115 | //int64_t _iTime=TC_TimeProvider::getInstance()->getNowMs(); 116 | 117 | int id = 0; 118 | TC_TCPClient tcpClient1; 119 | tcpClient1.init("127.0.0.1", 10005,3000); 120 | TC_HttpRequest stHttpReq; 121 | stHttpReq.setCacheControl("no-cache"); 122 | string sServer1("http://127.0.0.1:10005"); 123 | 124 | int iRet = -1; 125 | try 126 | { 127 | TC_HttpResponse stHttpRep; 128 | 129 | stHttpReq.setPostRequest(sServer1, parsebuffer(id++)); 130 | 131 | iRet = stHttpReq.doRequest(tcpClient1,stHttpRep); 132 | 133 | 134 | } 135 | catch(TC_Exception &e) 136 | { 137 | cout << "exception: " << e.what() << endl; 138 | } 139 | catch(...) 140 | { 141 | cout << " unknown exception." << endl; 142 | } 143 | return iRet; 144 | } 145 | TEST(TupProxyTest, should_response_tup_proxy_test_when_call_by_http) 146 | { 147 | int64_t tBegin = TC_TimeProvider::getInstance()->getNowMs(); 148 | 149 | int iRet=tup_dohandle(); 150 | 151 | EXPECT_EQ(iRet, 0); 152 | 153 | TLOGDEBUG("tupproxy test time cost: "<< " | " << TC_TimeProvider::getInstance()->getNowMs() - tBegin << "(ms)" << endl); 154 | } 155 | 156 | -------------------------------------------------------------------------------- /testcode/source/testcase/TupRpc.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Tencent is pleased to support the open source community by making Tars available. 3 | * 4 | * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. 5 | * 6 | * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except 7 | * in compliance with the License. You may obtain a copy of the License at 8 | * 9 | * https://opensource.org/licenses/BSD-3-Clause 10 | * 11 | * Unless required by applicable law or agreed to in writing, software distributed 12 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 13 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the 14 | * specific language governing permissions and limitations under the License. 15 | */ 16 | 17 | #include 18 | #include 19 | // #include 20 | #include "gtest/gtest.h" 21 | #include "util/tc_common.h" 22 | #include "util/tc_clientsocket.h" 23 | #include "servant/Application.h" 24 | #include "tup/tup.h" 25 | #include "TarsTest/UnitTest/TupDemo.h" 26 | #include "TarsServantName.h" 27 | 28 | using namespace std; 29 | using namespace tars; 30 | using namespace TarsTest; 31 | 32 | /**************************************************** 33 | PRC请求方式测试TUP协议 34 | ****************************************************/ 35 | 36 | class TupTestCallback:public TupDemoPrxCallback 37 | { 38 | virtual void callback_aAddb(tars::Int32 ret, const TarsTest::Result& aAddbResult) 39 | { 40 | EXPECT_EQ(ret, 0); 41 | EXPECT_EQ(aAddbResult.addResult,3); 42 | } 43 | }; 44 | 45 | 46 | struct TupTest : public ::testing::Test 47 | { 48 | TupTest() 49 | { 50 | _comm = Application::getCommunicator(); 51 | } 52 | protected: 53 | CommunicatorPtr _comm; 54 | }; 55 | typedef tars::TC_AutoPtr TupTestCallbackPtr; 56 | 57 | /** 58 | * @brief 同步调用,校验返回值 59 | */ 60 | 61 | TEST_F(TupTest, test_tup_rpc_when_client_sync_call_server_by_ip) 62 | { 63 | 64 | 65 | int64_t tBegin = TC_TimeProvider::getInstance()->getNowMs(); 66 | 67 | TarsUniPacket<> req; 68 | TarsUniPacket<>rsp; 69 | 70 | int iRequestId=1; 71 | req.setRequestId(iRequestId); 72 | req.setServantName("TarsTest.UnitTest.TupDemoObj"); 73 | req.setFuncName("aAddb"); 74 | 75 | struct AddInt addData; 76 | addData.adda = 1; 77 | addData.addb = 2; 78 | addData.addStr = "1 + 2 = 3"; 79 | req.put("addData",addData); 80 | 81 | string sendBuff; 82 | req.encode(sendBuff); 83 | 84 | int iRet = -1; 85 | 86 | TupDemoPrx prx; 87 | prx = _comm->stringToProxy (TUP_SERVANT_ENDPOINT); 88 | tars::ProxyProtocol proxy_codec; 89 | proxy_codec.responseFunc = tars::ProxyProtocol::tupResponse; 90 | prx->tars_set_protocol(proxy_codec); 91 | tars::ResponsePacket response; 92 | prx->rpc_call(iRequestId, "aAddb", sendBuff.c_str(), sendBuff.size(), response); 93 | iRet=response.iRet; 94 | EXPECT_EQ(iRet, 0); 95 | rsp.decode(&response.sBuffer[0],response.sBuffer.size()); 96 | struct Result aAddbResult; 97 | rsp.get("aAddbResult",aAddbResult); 98 | EXPECT_EQ(aAddbResult.addResult,3); 99 | 100 | TLOGDEBUG("tup test time cost: "<< " | " << TC_TimeProvider::getInstance()->getNowMs() - tBegin << "(ms)" << endl); 101 | } 102 | 103 | /** 104 | * @brief 异步调用,校验返回值 105 | */ 106 | 107 | TEST_F(TupTest, test_tup_rpc_when_client_async_call_server_by_ip) 108 | { 109 | 110 | 111 | 112 | TarsUniPacket<> req; 113 | TarsUniPacket<>rsp; 114 | 115 | int iRequestId=1; 116 | req.setRequestId(iRequestId); 117 | req.setServantName("TarsTest.UnitTest.TupDemoObj"); 118 | req.setFuncName("aAddb"); 119 | 120 | struct AddInt addData; 121 | addData.adda = 1; 122 | addData.addb = 2; 123 | addData.addStr = "1 + 2 = 3"; 124 | req.put("addData",addData); 125 | 126 | string sendBuff; 127 | req.encode(sendBuff); 128 | 129 | 130 | TupDemoPrx prx; 131 | prx = _comm->stringToProxy (TUP_SERVANT_ENDPOINT); 132 | tars::ProxyProtocol proxy_codec; 133 | proxy_codec.responseFunc = tars::ProxyProtocol::tupResponse; 134 | prx->tars_set_protocol(proxy_codec); 135 | 136 | TupTestCallbackPtr cbp=new TupTestCallback(); 137 | prx->rpc_call_async(iRequestId, "aAddb", sendBuff.c_str(), sendBuff.size(),cbp); 138 | TC_Common::sleep(1); 139 | } 140 | 141 | 142 | 143 | --------------------------------------------------------------------------------