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/24 02:35:17 UTC

[GitHub] duhenglucky closed pull request #31: Introduce Google test and Google mock infra

duhenglucky closed pull request #31: Introduce Google test and Google mock infra
URL: https://github.com/apache/rocketmq-client-cpp/pull/31
 
 
   

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..547132d6 100755
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -218,3 +218,11 @@ set(HAVE_SOCKLEN_T 1)
 add_subdirectory(libs)
 add_subdirectory(project)
 add_subdirectory(example)
+
+
+option(RUN_UNIT_TEST "RUN_UNIT_TEST" OFF)
+
+if(RUN_UNIT_TEST)
+  message(status "** RUN_UNIT_TEST: ${RUN_UNIT_TEST} Do execution testing")
+  add_subdirectory(test)
+endif()
\ No newline at end of file
diff --git a/build.sh b/build.sh
index b9a8ca65..2261238e 100644
--- a/build.sh
+++ b/build.sh
@@ -27,62 +27,65 @@ 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"
 
-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
-    fi
-    if [ "$2" != "0" -a "$2" != "1" ];then
-        echo "unsupport para value $2, please see the help"
-        Help
-        exit 1
-    fi
-    if [ "$3" != "0" -a "$3" != "1" ];then
-        echo "unsupport para value $3, please see the help"
-        Help
-        exit 1
-    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
+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 "###########################################################################"
-    echo "need_build_libevent: ${need_build_libevent}, need_build_jsoncpp:${need_build_jsoncpp}, need_build_boost:${need_build_boost}"
+    if [ $need_build_libevent -eq 0 ]
+    then
+        echo "no need build libevent lib"
+    else
+        echo "need build libevent lib"
+    fi
+
+    if [ $need_build_jsoncpp -eq 0 ]
+    then
+        echo "no need build jsoncpp lib"
+    else
+        echo "need build jsoncpp lib"
+    fi
+
+    if [ $need_build_boost -eq 0 ]
+    then
+        echo "no need build boost lib"
+    else
+        echo "need build boost lib"
+    fi
+
     echo "###########################################################################"
     echo ""
 }
@@ -97,7 +100,7 @@ Prepare()
     else
         mkdir -p ${down_dir}
     fi
-    
+
     cd ${basepath}
     if [ -e ${fname_libevent} ]
     then
@@ -113,7 +116,7 @@ Prepare()
     then
         mv -f ${basepath}/${fname_boost} ${down_dir}
     fi
-    
+
     if [ -e ${build_dir} ]
     then
         echo "${build_dir} is exist"
@@ -121,7 +124,7 @@ Prepare()
     else
         mkdir -p ${build_dir}
     fi
-	
+
     if [ -e ${packet_dir} ]
     then
         echo "${packet_dir} is exist"
@@ -140,7 +143,8 @@ Prepare()
 
 BuildLibevent()
 {
-    if [ "${need_build_libevent}" == "0" ];then
+    if [ $need_build_libevent -eq 0 ]
+    then
         echo "no need build libevent lib"
         return 0
     fi
@@ -178,9 +182,10 @@ BuildLibevent()
     make install
 }
 
+
 BuildJsonCPP()
 {
-    if [ "${need_build_jsoncpp}" == "0" ];then
+    if [ $need_build_jsoncpp -eq 0 ];then
         echo "no need build jsoncpp lib"
         return 0
     fi
@@ -203,7 +208,7 @@ 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
@@ -213,11 +218,19 @@ 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
+
+
 }
 
 BuildBoost()
 {
-    if [ "${need_build_boost}" == "0" ];then
+    if [ $need_build_boost -eq 0 ];then
         echo "no need build boost lib"
         return 0
     fi
@@ -249,7 +262,11 @@ BuildBoost()
 BuildRocketMQClient()
 {
     cd ${build_dir}
-    cmake ..
+    if [ $test -eq 0 ];then
+        cmake ..
+    else
+        cmake .. -DRUN_UNIT_TEST=ON
+    fi
     make
     if [ $? -ne 0 ];then
         exit 1
@@ -258,6 +275,85 @@ BuildRocketMQClient()
     PackageRocketMQStatic
 }
 
+BuildGoogleTest()
+{
+    if [ $test -eq 0 ];then
+        echo "no need build google test lib"
+        return 0
+    fi
+
+    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
+        echo "Do not execution test"
+        return 0
+    fi
+    echo "##################  test  start  ###########"
+    cd ${basepath}/test/bin
+    if [ ! -d ../log ]; then
+       mkdir ../log
+    fi
+    for files in `ls -F`
+    do
+        ./$files > "../log/$files.txt" 2>&1
+
+        if [ $? -ne 0 ]; then
+            echo "$files erren"
+            cat ../log/$files.txt
+            return 0
+        fi
+        erren=`grep "FAILED TEST" ../log/$files.txt`
+        
+        if [ -n "$erren" ]; then
+            echo "##################  find erren ###########"
+            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
@@ -266,9 +362,12 @@ PackageRocketMQStatic()
     cp -f librocketmq.a ${install_lib_dir}
 }
 
+
 PrintParams
 Prepare
 BuildLibevent
 BuildJsonCPP
 BuildBoost
+BuildGoogleTest
 BuildRocketMQClient
+ExecutionTesting
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
new file mode 100755
index 00000000..f00f66c0
--- /dev/null
+++ b/test/CMakeLists.txt
@@ -0,0 +1,83 @@
+# 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()
+            target_link_libraries (${basename}  rocketmq_shared ${deplibs})
+            target_link_libraries (${basename}  rocketmq_shared ${Gtest_LIBRARIES})
+            target_link_libraries (${basename}  rocketmq_shared ${Gmock_LIBRARIES})
+    endif()
+    
+endforeach()
diff --git a/test/src/UrlTest.cpp b/test/src/UrlTest.cpp
new file mode 100644
index 00000000..c7dead00
--- /dev/null
+++ b/test/src/UrlTest.cpp
@@ -0,0 +1,61 @@
+/*
+ * 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.
+ */
+
+#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