MRPT  2.0.4
RawlogGrabberApp_unittest.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include <gtest/gtest.h>
12 #include <mrpt/config.h>
13 #include <mrpt/core/lock_helper.h>
14 #include <mrpt/system/filesystem.h>
15 #include <test_mrpt_common.h>
16 #include <cstdlib>
17 #include <thread>
18 
19 #if MRPT_HAS_FFMPEG && MRPT_HAS_OPENCV
20 TEST(RawlogGrabberApp, CGenericCamera_AVI)
21 #else
22 TEST(RawlogGrabberApp, DISABLED_CGenericCamera_AVI)
23 #endif
24 {
25  // This particular unit test is REALLY problematic for some reason on build
26  // farms. It's safer to just disable it in this case:
27  if (::getenv("DEB_BUILD_ARCH") || ::getenv("DEB_BUILD_MAINT_OPTIONS"))
28  {
29  std::cerr << "Warning: Disabling test since we are building a Debian "
30  "package.\n";
31  return;
32  }
33 
34  using namespace std::string_literals;
35 
36  const std::string ini_fil =
37  mrpt::UNITTEST_BASEDIR +
38  "/share/mrpt/config_files/rawlog-grabber/camera_ffmpeg_video_file.ini"s;
40 
41  const std::string video_fil =
42  mrpt::UNITTEST_BASEDIR + "/share/mrpt/datasets/dummy_video.avi"s;
44 
45  try
46  {
49 
50  const char* argv[] = {"rawlog-grabber", ini_fil.c_str()};
51  const int argc = sizeof(argv) / sizeof(argv[0]);
52 
53  app.initialize(argc, argv);
54 
55  // Create output dir:
56  const auto out_dir = mrpt::system::getTempFileName() + "_dir"s;
58  THROW_EXCEPTION_FMT("Could not mkdir: `%s`", out_dir.c_str());
59 
60  app.params.write("global", "rawlog_prefix", out_dir + "/dataset"s);
61  app.params.write("Camera1", "ffmpeg_url", video_fil);
62 
63  // Run slowly, so we have time to capture a few frames from the
64  // (otherwise really short) test video file:
65  app.params.write("Camera1", "process_rate", "10.0");
66 
67  // Max. run time.
68  // Should end much sooner when the video file is entirely processed.
69  app.run_for_seconds = 45.0;
70 
71  // Less verbose output in tests:
73 
74  const std::size_t REQUIRED_GRAB_OBS = 3U;
75  std::atomic_bool runEnded = false;
76 
77  auto tWatchDog = std::thread([&]() {
78  while (!runEnded)
79  {
80  const auto numSavedObjs = [&]() {
81  auto lk = mrpt::lockHelper(app.results_mtx);
82  return app.rawlog_saved_objects;
83  }();
84  if (numSavedObjs >= REQUIRED_GRAB_OBS)
85  {
86  auto lk = mrpt::lockHelper(app.params_mtx);
87  app.run_for_seconds = 1.0; // make it exit
88  break;
89  }
90  std::this_thread::sleep_for(std::chrono::milliseconds(500));
91  }
92  });
93 
94  // Run:
95  app.run();
96 
97  runEnded = true;
98 
99  // Check expected results:
100  std::cout << "Rawlog grabbed objects: " << app.rawlog_saved_objects
101  << std::endl;
102  if (tWatchDog.joinable()) tWatchDog.join();
103 
104  EXPECT_GE(app.rawlog_saved_objects, REQUIRED_GRAB_OBS);
105  }
106  catch (const std::exception& e)
107  {
108  std::cerr << mrpt::exception_to_str(e);
109  GTEST_FAIL();
110  }
111 }
filesystem.h
mrpt::apps::RawlogGrabberApp::params
mrpt::config::CConfigFileMemory params
Populated in initialize().
Definition: RawlogGrabberApp.h:75
mrpt::config::CConfigFileBase::write
void write(const std::string &section, const std::string &name, enum_t value, const int name_padding_width=-1, const int value_padding_width=-1, const std::string &comment=std::string())
Definition: config/CConfigFileBase.h:107
mrpt::apps::RawlogGrabberApp::show_sensor_thread_exceptions
bool show_sensor_thread_exceptions
If enabled (default), exceptions in sensor threads will be reported to std::cerr.
Definition: RawlogGrabberApp.h:70
TEST
TEST(ICP_SLAM_App, MapFromRawlog_PointMap)
Definition: ICP_SLAM_App_unittest.cpp:80
video_fil
const std::string video_fil
Definition: RawlogGrabberApp_unittest.cpp:41
THROW_EXCEPTION_FMT
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
Definition: exceptions.h:69
out_dir
const auto out_dir
Definition: RawlogGrabberApp_unittest.cpp:56
ini_fil
const std::string ini_fil
Definition: RawlogGrabberApp_unittest.cpp:36
mrpt::system::getTempFileName
std::string getTempFileName()
Returns the name of a proposed temporary file name.
Definition: filesystem.cpp:283
mrpt::system::fileExists
bool fileExists(const std::string &fileName)
Test if a given file (or directory) exists.
Definition: filesystem.cpp:128
mrpt::apps::RawlogGrabberApp
RawlogGrabber application wrapper class.
Definition: RawlogGrabberApp.h:29
mrpt::apps::RawlogGrabberApp::initialize
void initialize(int argc, const char **argv)
Initializes the application from CLI parameters.
Definition: RawlogGrabberApp.cpp:42
mrpt::apps::RawlogGrabberApp::run_for_seconds
double run_for_seconds
If >0, run() will return after this period (in seconds)
Definition: RawlogGrabberApp.h:66
mrpt::system::COutputLogger::setMinLoggingLevel
void setMinLoggingLevel(const VerbosityLevel level)
Set the minimum logging level for which the incoming logs are going to be taken into account.
Definition: COutputLogger.cpp:144
argv
const char * argv[]
Definition: RawlogGrabberApp_unittest.cpp:50
mrpt::apps::RawlogGrabberApp::run
void run()
Runs with the current parameter set.
Definition: RawlogGrabberApp.cpp:242
lock_helper.h
EXPECT_TRUE
EXPECT_TRUE(mrpt::system::fileExists(ini_fil))
mrpt::apps::RawlogGrabberApp::params_mtx
std::mutex params_mtx
Acquire this mutex if changing parameters while the app is running.
Definition: RawlogGrabberApp.h:63
RawlogGrabberApp.h
mrpt::apps::RawlogGrabberApp::rawlog_saved_objects
std::size_t rawlog_saved_objects
Counter of saved objects.
Definition: RawlogGrabberApp.h:86
mrpt::lockHelper
LockHelper< T > lockHelper(T &t)
Syntactic sugar to easily create a locker to any kind of std::mutex.
Definition: lock_helper.h:50
argc
const int argc
Definition: RawlogGrabberApp_unittest.cpp:51
mrpt::system::LVL_ERROR
@ LVL_ERROR
Definition: system/COutputLogger.h:33
mrpt::exception_to_str
std::string exception_to_str(const std::exception &e)
Builds a nice textual representation of a nested exception, which if generated using MRPT macros (THR...
Definition: exceptions.cpp:59
mrpt::system::createDirectory
bool createDirectory(const std::string &dirName)
Creates a directory.
Definition: filesystem.cpp:161
mrpt::apps::RawlogGrabberApp::results_mtx
std::mutex results_mtx
Acquire this mutex if reading these output variables while the app is running.
Definition: RawlogGrabberApp.h:83



Page generated by Doxygen 1.8.17 for MRPT 2.0.4 at Sun Jul 19 17:54:30 UTC 2020