You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@rocketmq.apache.org by GitBox <gi...@apache.org> on 2018/12/19 13:01:59 UTC

[GitHub] githublaohu closed pull request #23: Introduce Googletest infra

githublaohu closed pull request #23: Introduce Googletest infra
URL: https://github.com/apache/rocketmq-client-cpp/pull/23
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 74ecb075..c53b184c 100755
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -218,3 +218,6 @@ set(HAVE_SOCKLEN_T 1)
 add_subdirectory(libs)
 add_subdirectory(project)
 add_subdirectory(example)
+add_subdirectory(test)
+
+
diff --git a/build.sh b/build.sh
index 0aa28027..a78b4f6a 100644
--- a/build.sh
+++ b/build.sh
@@ -11,67 +11,70 @@ fname_libevent_down="release-2.0.22-stable.zip"
 fname_jsoncpp_down="0.10.6.zip"
 fname_boost_down="1.58.0/boost_1_58_0.tar.gz"
 
-function Help()
+PrintParams()
 {
     echo "=========================================one key build help============================================"
-    echo "sh build.sh [build libevent:0/1 default:1] [build json:0/1 default:1] [build boost:0/1 default:1]"
-    echo "usage: sh build.sh 1 1 1"
-    echo "[[build libevent]: 1: need build libevent lib, 0: no need build libevent lib; default:1]"
-    echo "[[build json]: 1: need build json lib, 0: no need build json lib; default:1]"
-    echo "[[build boost]: 1: need build boost lib, 0: no need build boost lib; default:1]"
+    echo "sh build.sh [no build libevent:noEvent] [no build json:noJson] [no build boost:noBoost] [ execution test:test]"
+    echo "usage: sh build.sh noJson noEvent noBoost test"
     echo "=========================================one key build help============================================"
     echo ""
 }
 
-if [ $# -eq 3 ];then
-    if [ "$1" != "0" -a "$1" != "1" ];then
-        echo "unsupport para value $1, please see the help"
-        Help
-        exit 1
+need_build_jsoncpp=1
+need_build_libevent=1
+need_build_boost=1
+test=0
+
+pasres_arguments(){
+    for var in "$@"
+    do
+        case "$var" in
+                noJson)
+                        need_build_jsoncpp=0
+                        ;;
+                noEvent)
+                        need_build_libevent=0
+                        ;;
+                noBoost)
+                        need_build_boost=0
+                        ;;
+                test)
+                       test=1
+        esac
+    done
+
+}
+pasres_arguments $@
+
+PrintParams()
+{
+    echo "###########################################################################"
+    if [ $need_build_libevent -eq 0 ]
+    then
+        echo "no need build libevent lib"
+    else
+        echo "need build libevent lib"
     fi
-    if [ "$2" != "0" -a "$2" != "1" ];then
-        echo "unsupport para value $2, please see the help"
-        Help
-        exit 1
+
+    if [ $need_build_jsoncpp -eq 0 ]
+    then
+        echo "no need build jsoncpp lib"
+    else
+        echo "need build jsoncpp lib"
     fi
-    if [ "$3" != "0" -a "$3" != "1" ];then
-        echo "unsupport para value $3, please see the help"
-        Help
-        exit 1
+
+    if [ $need_build_boost -eq 0 ]
+    then
+        echo "no need build boost lib"
+    else
+        echo "need build boost lib"
     fi
-elif [ $# -gt 0 ];then
-    echo "the number of parameter must 0 or 3, please see the help"
-    Help
-    exit 1
-fi
-
-if [ $# -ge 1 ];then
-    need_build_libevent=$1
-else
-    need_build_libevent=1
-fi
-
-if [ $# -ge 2 ];then
-    need_build_jsoncpp=$2
-else
-    need_build_jsoncpp=1
-fi
-
-if [ $# -ge 3 ];then
-    need_build_boost=$3
-else
-    need_build_boost=1
-fi
-
-function PrintParams()
-{
-    echo "###########################################################################"
-    echo "need_build_libevent: ${need_build_libevent}, need_build_jsoncpp:${need_build_jsoncpp}, need_build_boost:${need_build_boost}"
+
     echo "###########################################################################"
     echo ""
 }
 
-function Prepare()
+Prepare()
 {
     if [ -e ${down_dir} ]
     then
@@ -122,9 +125,10 @@ function Prepare()
     fi
 }
 
-function BuildLibevent()
+BuildLibevent()
 {
-    if [ "${need_build_libevent}" == "0" ];then
+    if [ $need_build_libevent -eq 0 ]
+    then
         echo "no need build libevent lib"
         return 0
     fi
@@ -163,9 +167,9 @@ function BuildLibevent()
 }
 
 
-function BuildJsonCPP()
+BuildJsonCPP()
 {
-    if [ "${need_build_jsoncpp}" == "0" ];then
+    if [ $need_build_jsoncpp -eq 0 ];then
         echo "no need build jsoncpp lib"
         return 0
     fi
@@ -188,7 +192,7 @@ function BuildJsonCPP()
         exit 1
     fi
     mkdir build; cd build
-    echo "build jsoncpp static #####################"
+    echo "build jsoncpp static ######################"
     cmake .. -DCMAKE_CXX_FLAGS=-fPIC -DBUILD_STATIC_LIBS=ON -DBUILD_SHARED_LIBS=OFF -DCMAKE_INSTALL_PREFIX=${install_lib_dir}
     if [ $? -ne 0 ];then
         exit 1
@@ -198,11 +202,19 @@ function BuildJsonCPP()
         exit 1
     fi
     make install
+
+    if [ ! -f ${install_lib_dir}/lib/libjsoncpp.a ]
+    then
+        echo " ./bin/lib directory is not libjsoncpp.a"
+        cp ${install_lib_dir}/lib/x86_64-linux-gnu/libjsoncpp.a ${install_lib_dir}/lib/
+    fi
+
+
 }
 
-function BuildBoost()
+BuildBoost()
 {
-    if [ "${need_build_boost}" == "0" ];then
+    if [ $need_build_boost -eq 0 ];then
         echo "no need build boost lib"
         return 0
     fi
@@ -231,7 +243,7 @@ function BuildBoost()
     fi
 }
 
-function BuildRocketMQClient()
+BuildRocketMQClient()
 {
     cd ${build_dir}
     cmake ..
@@ -243,7 +255,83 @@ function BuildRocketMQClient()
     PackageRocketMQStatic
 }
 
-function PackageRocketMQStatic()
+BuildGoogleTest()
+{
+
+    if [ -f ./bin/lib/libgtest.a ]
+    then
+        echo "libgteest already exist no need build test"
+        return 0
+    fi
+
+    cd ${down_dir}
+    if [ -e release-1.8.1.tar.gz ]
+    then
+        echo "${fname_boost} is exist"
+    else
+        wget https://github.com/abseil/googletest/archive/release-1.8.1.tar.gz
+    fi
+    if [ ! -d "googletest-release-1.8.1" ];then
+        tar -zxvf release-1.8.1.tar.gz
+    fi
+    cd googletest-release-1.8.1
+    mkdir build; cd build
+    echo "build googletest static #####################"
+    cmake .. -DCMAKE_CXX_FLAGS=-fPIC -DBUILD_STATIC_LIBS=ON -DBUILD_SHARED_LIBS=OFF -DCMAKE_INSTALL_PREFIX=${install_lib_dir}
+    if [ $? -ne 0 ];then
+        exit 1
+    fi    
+    make
+    if [ $? -ne 0 ];then
+        exit 1
+    fi
+    make install
+
+    if [ ! -f ${install_lib_dir}/lib/libgtest.a ]
+    then
+	    echo " ./bin/lib directory is not libgtest.a"
+        cp ${install_lib_dir}/lib64/lib* ${install_lib_dir}/lib
+    fi
+
+    
+}
+
+ExecutionTesting()
+{
+    if [ $test -eq 0 ];then
+        return 0
+    fi
+    echo "##################  test  start  ###########"
+    cd ${basepath}/test/bin
+    if [ ! -d ../log ]; then
+       mkdir ../log
+    fi
+    for files in `ls -F`
+    do
+        ./$files 2>&1 > "../log/$files.txt"
+
+	if [ $? -ne 0 ]; then
+	    echo "$files erren"
+	    cat ../log/$files.txt
+	    return 0
+        fi
+        erron=`grep "FAILED TEST" ../log/$files.txt`
+	
+        if [ -n "$erron" ]; then
+            echo "##################  find erron ###########"
+            cat ../log/$files.txt
+
+            echo "##################  end ExecutionTesting ###########"
+            return
+        else
+	    echo "$files success"
+	fi
+    done
+    echo "##################  test  end  ###########"
+}
+
+
+PackageRocketMQStatic()
 {
     #packet libevent,jsoncpp,boost,rocketmq,Signature to one librocketmq.a
     cp -f ${basepath}/libs/signature/lib/libSignature.a ${install_lib_dir}/lib
@@ -251,9 +339,12 @@ function PackageRocketMQStatic()
     cp -f librocketmq.a ${install_lib_dir}
 }
 
+
 PrintParams
 Prepare
 BuildLibevent
 BuildJsonCPP
 BuildBoost
-BuildRocketMQClient
\ No newline at end of file
+BuildGoogleTest
+BuildRocketMQClient
+ExecutionTesting
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
new file mode 100755
index 00000000..e79ae6a8
--- /dev/null
+++ b/test/CMakeLists.txt
@@ -0,0 +1,87 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+project(test)
+
+
+SET(SUB_DIRS)
+file(GLOB children ${CMAKE_SOURCE_DIR}/src/*)
+FOREACH(child ${children})
+    IF(IS_DIRECTORY ${child})
+        LIST(APPEND SUB_DIRS ${child})
+    ENDIF()
+ENDFOREACH()
+LIST(APPEND SUB_DIRS ${CMAKE_SOURCE_DIR}/src)
+include_directories(${CMAKE_SOURCE_DIR}/include)
+include_directories(${SUB_DIRS})
+
+set(EXECUTABLE_OUTPUT_PATH ${CMAKE_SOURCE_DIR}/test/bin)
+
+include_directories(${CMAKE_SOURCE_DIR}/include)
+include_directories(${Boost_INCLUDE_DIRS})
+
+
+set(Gtest_INCLUDE_DIR    ${CMAKE_SOURCE_DIR}/bin/include/gtest)
+set(Gmock_INCLUDE_DIR    ${CMAKE_SOURCE_DIR}/bin/include/gmock)
+
+include_directories(${Gtest_INCLUDE_DIR})
+include_directories(${Gtest_INCLUDE_DIR}/internal)
+include_directories(${Gmock_INCLUDE_DIR})
+include_directories(${Gmock_INCLUDE_DIR}/internal)
+
+set(Gtest_LIBRARY_DIRS   ${CMAKE_SOURCE_DIR}/bin/lib)
+set(Gtest_LIBRARIES      ${Gtest_LIBRARY_DIRS}/libgtest_main.a;${Gtest_LIBRARY_DIRS}/libgtest.a)
+set(Gmock_LIBRARIES      ${Gtest_LIBRARY_DIRS}/libgmock_main.a;${Gtest_LIBRARY_DIRS}/libgmock.a)
+message(status "** Gmock_LIBRARIES: ${Gmock_LIBRARIES}")
+
+link_directories(${Boost_LIBRARY_DIRS})
+link_directories(${LIBEVENT_LIBRARY})
+link_directories(${JSONCPP_LIBRARY})
+
+set(ROCKETMQ_LIBRARIES ${CMAKE_SOURCE_DIR}/bin/librocketmq.a)
+message(status "ROCKETMQ_LIBRARIES ${ROCKETMQ_LIBRARIES}")
+
+set(CMAKE_BUILD_TYPE "Debug")
+file(GLOB files "src/*.c*")
+foreach(file ${files})
+    get_filename_component(basename ${file} NAME_WE)
+    add_executable(${basename} ${file})
+    if(MSVC)
+        if(CMAKE_CONFIGURATION_TYPES STREQUAL "Release")
+            set_target_properties( ${basename} PROPERTIES LINK_FLAGS "/NODEFAULTLIB:LIBCMT" )
+        else()
+            set_target_properties( ${basename} PROPERTIES LINK_FLAGS "/NODEFAULTLIB:LIBCMTD" )
+        endif()
+    endif()
+
+    if (MSVC) 
+    	if (BUILD_ROCKETMQ_SHARED)
+        	target_link_libraries (${basename}  rocketmq_shared ${deplibs}
+        	${Boost_LIBRARIES} ${LIBEVENT_LIBRARIES} ${JSONCPP_LIBRARIES} ${x`})
+    	else()
+        	target_link_libraries (${basename}  rocketmq_static ${deplibs}
+        	${Boost_LIBRARIES} ${LIBEVENT_LIBRARIES} ${JSONCPP_LIBRARIES} ${Gtest_LIBRARIES})
+        endif()
+    else()
+        #if (BUILD_ROCKETMQ_SHARED)
+            target_link_libraries (${basename}  rocketmq_shared ${deplibs})
+            target_link_libraries (${basename}  rocketmq_shared ${Gtest_LIBRARIES})
+            target_link_libraries (${basename}  rocketmq_shared ${Gmock_LIBRARIES})
+        #else()
+    	#    target_link_libraries (${basename} rocketmq_static ${deplibs})
+        #endif()
+    endif()
+    
+endforeach()
diff --git a/test/src/UrlTest.cpp b/test/src/UrlTest.cpp
new file mode 100644
index 00000000..aeb0f534
--- /dev/null
+++ b/test/src/UrlTest.cpp
@@ -0,0 +1,51 @@
+/*
+ * UrlTest.cpp
+ *
+ *  Created on: 2018年12月15日
+ *      Author: laohu
+ */
+
+#include "url.h"
+#include "TopicConfig.h"
+#include "gtest/gtest.h"
+#include "gmock/gmock.h"
+
+using namespace std;
+using ::testing::InitGoogleTest;
+using ::testing::InitGoogleMock;
+using testing::Return;
+using rocketmq::Url;
+using rocketmq::TopicConfig;
+
+class MockTopicConfig : public  TopicConfig{
+public:
+	MOCK_METHOD0(getReadQueueNums , int());
+};
+
+
+TEST(Url, Url) {
+	Url url_s("172.17.0.2:9876");
+	EXPECT_EQ(url_s.protocol_ , "172.17.0.2:9876");
+
+	Url url_z("https://www.aliyun.com/RocketMQ?5.0");
+	EXPECT_EQ(url_z.protocol_ , "https");
+	EXPECT_EQ(url_z.host_ , "www.aliyun.com");
+	EXPECT_EQ(url_z.port_ , "80");
+	EXPECT_EQ(url_z.path_ , "/RocketMQ");
+	EXPECT_EQ(url_z.query_ , "5.0");
+
+	Url url_path("https://www.aliyun.com:9876/RocketMQ?5.0");
+	EXPECT_EQ(url_path.port_ , "9876");
+	MockTopicConfig topicConfig;
+	EXPECT_CALL(topicConfig , getReadQueueNums()).WillRepeatedly(Return(-1));
+	int nums = topicConfig.getReadQueueNums();
+	cout << nums << endl;
+
+}
+
+int main(int argc, char* argv[]) {
+	InitGoogleMock(&argc, argv);
+
+	testing::GTEST_FLAG(filter) = "Url.Url";
+	return RUN_ALL_TESTS();
+}


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services