From 6571e55631af10e6d4f4364201e84fa7221b42ca Mon Sep 17 00:00:00 2001 From: An Tao Date: Sat, 7 Dec 2019 17:43:00 +0800 Subject: [PATCH] Adding unit tests to Travis CI (#310) --- CMakeLists.txt | 2 +- build.sh | 2 +- test.sh | 34 ++++++++++++++++++------------ unittest/CMakeLists.txt | 2 -- unittest/TimerUnittest.cpp | 42 -------------------------------------- 5 files changed, 23 insertions(+), 59 deletions(-) delete mode 100644 unittest/TimerUnittest.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 35e49987..1209ccac 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -332,7 +332,7 @@ set(INCLUDING_DIRS ${INS_STRING}) configure_file(${PROJECT_SOURCE_DIR}/cmake/templates/config.h.in ${PROJECT_BINARY_DIR}/drogon/config.h @ONLY) -if(MAKE_TESTING) +if(BUILD_TESTING) add_subdirectory(lib/tests) if(PostgreSQL_FOUND) add_subdirectory(${PROJECT_SOURCE_DIR}/orm_lib/src/postgresql_impl/test) diff --git a/build.sh b/build.sh index a5bebb9d..4166596e 100755 --- a/build.sh +++ b/build.sh @@ -28,7 +28,7 @@ function build_drogon() { echo "Start building drogon ..." if [ $1 -eq 1 ]; then - cmake .. -DMAKE_TESTING=YES + cmake .. -DBUILD_TESTING=YES else cmake .. fi diff --git a/test.sh b/test.sh index 349c6de6..b384ce45 100755 --- a/test.sh +++ b/test.sh @@ -1,4 +1,5 @@ #!/bin/bash + cd build/examples/ #Make webapp run as a daemon @@ -23,7 +24,7 @@ sleep 4 echo "Test http requests and responses." ./webapp_test -if [ $? -ne 0 ];then +if [ $? -ne 0 ]; then echo "Error in testing" exit -1 fi @@ -31,7 +32,7 @@ fi #Test WebSocket echo "Test the WebSocket" ./websocket_test -t -if [ $? -ne 0 ];then +if [ $? -ne 0 ]; then echo "Error in testing" exit -1 fi @@ -39,7 +40,7 @@ fi #Test pipelining echo "Test the pipelining" ./pipelining_test -if [ $? -ne 0 ];then +if [ $? -ne 0 ]; then echo "Error in testing" exit -1 fi @@ -61,12 +62,12 @@ drogon_ctl create controller SimpleCtrl drogon_ctl create controller -h HttpCtrl drogon_ctl create controller -w WebsockCtrl -if [ ! -f "Test_SimpleCtrl.h" -o ! -f "Test_SimpleCtrl.cc" -o ! -f "Test_HttpCtrl.h" -o ! -f "Test_HttpCtrl.cc" -o ! -f "Test_WebsockCtrl.h" -o ! -f "Test_WebsockCtrl.cc" ];then +if [ ! -f "Test_SimpleCtrl.h" -o ! -f "Test_SimpleCtrl.cc" -o ! -f "Test_HttpCtrl.h" -o ! -f "Test_HttpCtrl.cc" -o ! -f "Test_WebsockCtrl.h" -o ! -f "Test_WebsockCtrl.cc" ]; then echo "Failed to create controllers" exit -1 fi -if [ ! -f "SimpleCtrl.h" -o ! -f "SimpleCtrl.cc" -o ! -f "HttpCtrl.h" -o ! -f "HttpCtrl.cc" -o ! -f "WebsockCtrl.h" -o ! -f "WebsockCtrl.cc" ];then +if [ ! -f "SimpleCtrl.h" -o ! -f "SimpleCtrl.cc" -o ! -f "HttpCtrl.h" -o ! -f "HttpCtrl.cc" -o ! -f "WebsockCtrl.h" -o ! -f "WebsockCtrl.cc" ]; then echo "Failed to create controllers" exit -1 fi @@ -75,7 +76,7 @@ cd ../filters drogon_ctl create filter Test::TestFilter -if [ ! -f "Test_TestFilter.h" -o ! -f "Test_TestFilter.cc" ];then +if [ ! -f "Test_TestFilter.h" -o ! -f "Test_TestFilter.cc" ]; then echo "Failed to create filters" exit -1 fi @@ -84,7 +85,7 @@ cd ../plugins drogon_ctl create plugin Test::TestPlugin -if [ ! -f "Test_TestPlugin.h" -o ! -f "Test_TestPlugin.cc" ];then +if [ ! -f "Test_TestPlugin.h" -o ! -f "Test_TestPlugin.cc" ]; then echo "Failed to create plugins" exit -1 fi @@ -92,19 +93,19 @@ fi cd ../build cmake .. -if [ $? -ne 0 ];then +if [ $? -ne 0 ]; then echo "Error in testing" exit -1 fi make -if [ $? -ne 0 ];then +if [ $? -ne 0 ]; then echo "Error in testing" exit -1 fi -if [ ! -f "drogon_test" ];then +if [ ! -f "drogon_test" ]; then echo "Failed to build drogon_test" exit -1 fi @@ -112,11 +113,18 @@ fi cd ../../ rm -rf drogon_test -if [ "$1" = "-t" ];then - echo "Test database" +if [ "$1" = "-t" ]; then + #unit testing cd ../ + echo "Unit testing" + make test + if [ $? -ne 0 ]; then + echo "Error in unit testing" + exit -1 + fi + echo "Test database" ./orm_lib/tests/db_test - if [ $? -ne 0 ];then + if [ $? -ne 0 ]; then echo "Error in testing" exit -1 fi diff --git a/unittest/CMakeLists.txt b/unittest/CMakeLists.txt index 9ab93f3d..815b9b4d 100644 --- a/unittest/CMakeLists.txt +++ b/unittest/CMakeLists.txt @@ -1,11 +1,9 @@ add_executable(msgbuffer_unittest MsgBufferUnittest.cpp) -add_executable(timer_unittest TimerUnittest.cpp) add_executable(drobject_unittest DrObjectUnittest.cpp) add_executable(gzip_unittest GzipUnittest.cpp) set(UNITTEST_TARGETS msgbuffer_unittest - timer_unittest drobject_unittest gzip_unittest) set_property(TARGET ${UNITTEST_TARGETS} diff --git a/unittest/TimerUnittest.cpp b/unittest/TimerUnittest.cpp deleted file mode 100644 index ec7f8325..00000000 --- a/unittest/TimerUnittest.cpp +++ /dev/null @@ -1,42 +0,0 @@ -#include -#include -#include -#include -using namespace trantor; -using namespace std::literals; -EventLoop loop; - -TEST(TimerTest, RunEvery) -{ - size_t count{0}; - std::chrono::steady_clock::time_point tp{std::chrono::steady_clock::now()}; - loop.runEvery(0.1s, [&count, tp] { - ++count; - if (count == 10) - { - auto d = std::chrono::steady_clock::now() - tp; - auto err = d > 1s ? d - 1s : 1s - d; - auto msErr = - std::chrono::duration_cast(err) - .count(); - EXPECT_LT(msErr, 100LL); - loop.quit(); - return; - } - std::this_thread::sleep_for(0.02s); - }); - loop.runAfter(0.35s, [tp] { - auto d = std::chrono::steady_clock::now() - tp; - auto err = d > 0.35s ? d - 0.35s : 0.35s - d; - auto msErr = - std::chrono::duration_cast(err).count(); - EXPECT_LT(msErr, 30LL); - }); - loop.loop(); -} - -int main(int argc, char **argv) -{ - testing::InitGoogleTest(&argc, argv); - return RUN_ALL_TESTS(); -} \ No newline at end of file