Adding unit tests to Travis CI (#310)

This commit is contained in:
An Tao 2019-12-07 17:43:00 +08:00 committed by GitHub
parent 5bb54cd4cf
commit 6571e55631
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 23 additions and 59 deletions

View File

@ -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)

View File

@ -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

34
test.sh
View File

@ -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

View File

@ -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}

View File

@ -1,42 +0,0 @@
#include <trantor/net/EventLoop.h>
#include <gtest/gtest.h>
#include <string>
#include <iostream>
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<std::chrono::milliseconds>(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<std::chrono::milliseconds>(err).count();
EXPECT_LT(msErr, 30LL);
});
loop.loop();
}
int main(int argc, char **argv)
{
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}