Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fake time #147

Merged
merged 23 commits into from
Nov 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
f0b244a
core: add mocked time for unit tests
julianoes Nov 1, 2017
20c0447
core: use mock time in unit tests
julianoes Nov 1, 2017
765470c
core: mock time at runtime
julianoes Nov 2, 2017
3e7f5ab
core: call it fake and not mock time
julianoes Nov 2, 2017
3c57e82
core: fake time for global_include_test
julianoes Nov 2, 2017
e546c1c
travis: we need to do make default install again
julianoes Nov 2, 2017
c642485
cmake: let's put cmake in charge of thread libs
julianoes Oct 24, 2017
2655bc9
cmake: build shared library instead of static
julianoes Oct 23, 2017
5d8fb76
cmake: remove redundant curl linking step
julianoes Oct 23, 2017
59e311a
cmake: link dronecore to tinyxml2
julianoes Nov 2, 2017
3e7e433
cmake: fix check for Windows build
julianoes Nov 1, 2017
fc49906
cmake: fix linking to gmock on Windows
julianoes Nov 2, 2017
dd55567
cmake: don't specify the build type for Windows
julianoes Nov 2, 2017
2b8d549
cmake: specify the build type MD or MDd
julianoes Nov 2, 2017
1b69d2d
cmake: main function for test was missing somehow
julianoes Nov 3, 2017
6e3db0b
cmake: add linking fix for both tests
julianoes Nov 3, 2017
fcc9010
core: autostarting SITL not available on Windows
julianoes Nov 3, 2017
aa2e066
core: the unit test main had a weird include
julianoes Nov 3, 2017
3c82a50
appveyor: copy the gtest dlls
julianoes Nov 3, 2017
aed9283
appveyor: trying to fix syntax
julianoes Nov 3, 2017
4bb16fd
example: fix linking of examples
julianoes Nov 3, 2017
b238d35
cmake: add EXPORT for appveyor release build
julianoes Nov 3, 2017
fbf1917
appveyor: remove strange define
julianoes Nov 3, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,14 @@ install:
script:
- if [[ "${BUILD_TARGET}" = "gcc_build" ]]; then
make;
make install;
make default install;
make docs;
make run_unit_tests;
(cd example/takeoff_land && mkdir build && cd build && cmake .. && make);
(cd example/fly_mission && mkdir build && cd build && cmake .. && make);
make clean;
make BUILD_TYPE=Release;
make install;
make default install;
make run_unit_tests;
fi
- if [[ "${BUILD_TARGET}" = "coverage_build" ]]; then
Expand All @@ -91,22 +91,22 @@ script:

- if [[ "${BUILD_TARGET}" = "docker_build" ]]; then
docker run -it -v $TRAVIS_BUILD_DIR:/home/docker1000/src/DroneCore:rw dronecore/dronecore make;
docker run -it -v $TRAVIS_BUILD_DIR:/home/docker1000/src/DroneCore:rw dronecore/dronecore make install;
docker run -it -v $TRAVIS_BUILD_DIR:/home/docker1000/src/DroneCore:rw dronecore/dronecore make default install;
docker run -it -v $TRAVIS_BUILD_DIR:/home/docker1000/src/DroneCore:rw dronecore/dronecore make run_unit_tests;
docker run -it -v $TRAVIS_BUILD_DIR:/home/docker1000/src/DroneCore:rw dronecore/dronecore make fix_style;
docker run -it -v $TRAVIS_BUILD_DIR:/home/docker1000/src/DroneCore:rw dronecore/dronecore make docs;
docker run -it -v $TRAVIS_BUILD_DIR:/home/docker1000/src/DroneCore:rw dronecore/dronecore make clean;
docker run -it -v $TRAVIS_BUILD_DIR:/home/docker1000/src/DroneCore:rw dronecore/dronecore make BUILD_TYPE=Release;
docker run -it -v $TRAVIS_BUILD_DIR:/home/docker1000/src/DroneCore:rw dronecore/dronecore make install;
docker run -it -v $TRAVIS_BUILD_DIR:/home/docker1000/src/DroneCore:rw dronecore/dronecore make default install;
docker run -it -v $TRAVIS_BUILD_DIR:/home/docker1000/src/DroneCore:rw dronecore/dronecore make run_unit_tests;
fi
- if [[ "${BUILD_TARGET}" = "osx_build" ]]; then
make;
make install;
make default install;
make run_unit_tests;
make clean;
make BUILD_TYPE=Release;
make install;
make default install;
make run_unit_tests;
fi

Expand Down
48 changes: 36 additions & 12 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
cmake_minimum_required(VERSION 2.8.12)

set(CMAKE_INSTALL_PREFIX "../install" CACHE PATH "default cache path")

project(dronecore)

# Add DEBUG define for Debug target
set(CMAKE_CXX_FLAGS_DEBUG "-DDEBUG")

# This finds thread libs on Linux, Mac, and Windows.
find_package(Threads REQUIRED)

if(NOT MSVC)
# Clang and GCC
Expand Down Expand Up @@ -33,7 +36,6 @@ if(NOT MSVC)
set(warnings "${warnings} -Wuseless-cast -Wlogical-op")
endif()

set(platform_libs "pthread")
set(curl_lib "curl")

else()
Expand All @@ -42,12 +44,12 @@ else()
-std=c++11
-DWINDOWS
-DCURL_STATICLIB
/MD
)
set(CMAKE_CXX_FLAGS_RELEASE "/MD")
set(CMAKE_CXX_FLAGS_DEBUG "/MDd")

set(warnings "-WX -W2")

set(platform_libs "Ws2_32.lib")
# You need to call cmake with -DWIN_CURL_INCLUDE_DIR:STRING="C:\\curl-7.54.1\\include"
if(NOT WIN_CURL_INCLUDE_DIR)
message(FATAL_ERROR "Please provide argument -DWIN_CURL_INCLUDE_DIR:STRING=\"path_to_curl_include\"")
Expand Down Expand Up @@ -108,8 +110,14 @@ if (IOS)
include_directories(curl-android-ios/prebuilt-with-ssl/ios/include)
endif()

if (IOS OR ANDROID OR MSVC)
set(library_type "STATIC")
else()
set(library_type "SHARED")
endif()

# We build one static library.
add_library(dronecore STATIC
add_library(dronecore ${library_type}
core/global_include.cpp
core/connection.cpp
core/device.cpp
Expand All @@ -132,6 +140,12 @@ add_library(dronecore STATIC
${plugin_source_files}
)

target_link_libraries(dronecore
${CMAKE_THREAD_LIBS_INIT}
${curl_lib}
tinyxml2
)

# cmake should check for C++11
set_property(TARGET dronecore PROPERTY CXX_STANDARD 11)
set_property(TARGET dronecore PROPERTY CXX_STANDARD_REQUIRED ON)
Expand All @@ -144,7 +158,7 @@ set_target_properties(dronecore
add_subdirectory(libs/tinyxml2 EXCLUDE_FROM_ALL)
include_directories(SYSTEM libs/tinyxml2)

# We support install in order to use the header and static library files in
# We support install in order to use the header and library files in
# other applications.
if(ANDROID)
set(lib_path "lib/android/${ANDROID_ABI}")
Expand All @@ -155,6 +169,7 @@ else()
endif()

install(TARGETS dronecore
EXPORT dronecore-targets
DESTINATION ${lib_path}
)

Expand Down Expand Up @@ -195,6 +210,14 @@ if(NOT IOS AND NOT ANDROID)
${unit_tests_src}
)

if (MSVC)
# We need this to prevent linking errors from happening in the Windows build.
target_compile_definitions(unit_tests_runner PRIVATE -DGTEST_LINKED_AS_SHARED_LIBRARY)
target_compile_options(unit_tests_runner PUBLIC "/wd4251" "/wd4275")
endif()

target_compile_definitions(unit_tests_runner PRIVATE FAKE_TIME=1)

set_target_properties(unit_tests_runner
PROPERTIES COMPILE_FLAGS ${warnings}
)
Expand All @@ -203,11 +226,8 @@ if(NOT IOS AND NOT ANDROID)
dronecore
gtest
gtest_main
${platform_libs}
${curl_lib}
gmock
${additional_libs}
tinyxml2
)

add_test(unit_tests
Expand All @@ -219,6 +239,7 @@ if(NOT IOS AND NOT ANDROID)

# This includes all GTests that run integration tests
add_executable(integration_tests_runner
core/unittests_main.cpp
${integration_tests_src}
)

Expand All @@ -230,12 +251,16 @@ if(NOT IOS AND NOT ANDROID)
dronecore
gtest
gtest_main
${platform_libs}
${curl_lib}
gmock
${additional_libs}
tinyxml2
)

if (MSVC)
# We need this to prevent linking errors from happening in the Windows build.
target_compile_definitions(integration_tests_runner PRIVATE -DGTEST_LINKED_AS_SHARED_LIBRARY)
target_compile_options(integration_tests_runner PUBLIC "/wd4251" "/wd4275")
endif()

add_test(integration_tests
integration_tests_runner
)
Expand Down Expand Up @@ -291,6 +316,5 @@ if (DROP_DEBUG EQUAL 1)

target_link_libraries(drop_debug
dronecore
${platform_libs}
)
endif()
15 changes: 12 additions & 3 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ build_script:
- if "%configuration%"=="Debug" (
cmake -DWIN_CURL_INCLUDE_DIR:STRING="C:\curl-7.55.1\include" -DWIN_CURL_LIB:STRING="C:\curl-7.55.1\builds\libcurl-vc14-x64-debug-static-ipv6-sspi-winssl\lib\libcurl_a_debug.lib" -G "Visual Studio 14 2015 Win64" ..
) else (
cmake -DWIN_CURL_INCLUDE_DIR:STRING="C:\curl-7.55.1\include" -DWIN_CURL_LIB:STRING="C:\curl-7.55.1\builds\libcurl-vc14-x64-release-static-ipv6-sspi-winssl\lib\libcurl_a.lib" -G "Visual Studio 14 2015 Win64" -DBUILD_SHARED_LIBS=OFF ..
cmake -DWIN_CURL_INCLUDE_DIR:STRING="C:\curl-7.55.1\include" -DWIN_CURL_LIB:STRING="C:\curl-7.55.1\builds\libcurl-vc14-x64-release-static-ipv6-sspi-winssl\lib\libcurl_a.lib" -G "Visual Studio 14 2015 Win64" ..
)
- if "%configuration%"=="Debug" (
cmake --build . --target install --config Debug
Expand All @@ -62,10 +62,19 @@ build_script:

test: on

# We need to manually copy the dlls for now, otherwise they are not found.
test_script:
- cd C:\dronecore
- if "%configuration%"=="Debug" (
build\Debug\unit_tests_runner.exe
cp build\libs\gtest\googlemock\gtest\Debug\gtest.dll build\Debug\ &&
cp build\libs\gtest\googlemock\gtest\Debug\gtest_main.dll build\Debug\ &&
cp build\libs\gtest\googlemock\Debug\gmock.dll build\Debug\ &&
cd build\Debug &&
unit_tests_runner.exe
) else (
build\Release\unit_tests_runner.exe
cp build\libs\gtest\googlemock\gtest\Release\gtest.dll build\Release\ &&
cp build\libs\gtest\googlemock\gtest\Release\gtest_main.dll build\Release\ &&
cp build\libs\gtest\googlemock\Release\gmock.dll build\Release\ &&
cd build\Release &&
unit_tests_runner.exe
)
11 changes: 6 additions & 5 deletions core/call_every_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

namespace dronecore {

CallEveryHandler::CallEveryHandler()
CallEveryHandler::CallEveryHandler(Time &time) :
_time(time)
{
}

Expand All @@ -14,7 +15,7 @@ void CallEveryHandler::add(std::function<void()> callback, float interval_s, voi
{
auto new_entry = std::make_shared<Entry>();
new_entry->callback = callback;
new_entry->last_time = steady_time();
new_entry->last_time = _time.steady_time();
new_entry->interval_s = interval_s;

void *new_cookie = static_cast<void *>(new_entry.get());
Expand Down Expand Up @@ -45,7 +46,7 @@ void CallEveryHandler::reset(const void *cookie)

auto it = _entries.find(const_cast<void *>(cookie));
if (it != _entries.end()) {
it->second->last_time = steady_time();
it->second->last_time = _time.steady_time();
}
}

Expand All @@ -65,9 +66,9 @@ void CallEveryHandler::run_once()

for (auto it = _entries.begin(); it != _entries.end(); ++it) {

if (elapsed_since_s(it->second->last_time) > double(it->second->interval_s)) {
if (_time.elapsed_since_s(it->second->last_time) > double(it->second->interval_s)) {

shift_steady_time_by(it->second->last_time, double(it->second->interval_s));
_time.shift_steady_time_by(it->second->last_time, double(it->second->interval_s));

if (it->second->callback) {

Expand Down
4 changes: 3 additions & 1 deletion core/call_every_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace dronecore {
class CallEveryHandler
{
public:
CallEveryHandler();
CallEveryHandler(Time &time);
~CallEveryHandler();

// delete copy and move constructors and assign operators
Expand All @@ -36,6 +36,8 @@ class CallEveryHandler

std::map<void *, std::shared_ptr<Entry>> _entries {};
std::mutex _entries_mutex {};

Time &_time;
};

} // namespace dronecore
Loading