summaryrefslogtreecommitdiff
path: root/src/components/utils
diff options
context:
space:
mode:
authorJackLivio <jack@livio.io>2018-08-29 14:08:41 -0400
committerJackLivio <jack@livio.io>2018-08-29 14:08:41 -0400
commitcee3ba5cec980dcee4cc37af71bd09dc306d3867 (patch)
tree3f22a7132d77dde142019231bf10ab2e3be1f5dd /src/components/utils
parent90db9ca480eabeaf85ec77626b1bf10fa491270d (diff)
parent09e4e684708ad5ab2544c72745516e9fe481ba0b (diff)
downloadsdl_core-cee3ba5cec980dcee4cc37af71bd09dc306d3867.tar.gz
Merge remote-tracking branch 'origin/develop' into feature/boost_filesystem_implementation
# Conflicts: # src/3rd_party/CMakeLists.txt # src/components/utils/CMakeLists.txt
Diffstat (limited to 'src/components/utils')
-rw-r--r--src/components/utils/CMakeLists.txt8
-rw-r--r--src/components/utils/include/utils/log_message_loop_thread.h10
-rw-r--r--src/components/utils/include/utils/signals.h54
-rw-r--r--src/components/utils/src/date_time.cc123
-rw-r--r--src/components/utils/src/log_message_loop_thread.cc7
-rw-r--r--src/components/utils/src/logger.cc3
-rw-r--r--src/components/utils/src/semantic_version.cc38
-rw-r--r--src/components/utils/src/signals_posix.cc44
-rw-r--r--src/components/utils/test/date_time_test.cc331
-rw-r--r--src/components/utils/test/include/utils/mock_signals_posix.h59
-rw-r--r--src/components/utils/test/log_message_loop_thread_test.cc2
-rw-r--r--src/components/utils/test/messagemeter_test.cc42
-rw-r--r--src/components/utils/test/mock_signals_posix.cc76
13 files changed, 460 insertions, 337 deletions
diff --git a/src/components/utils/CMakeLists.txt b/src/components/utils/CMakeLists.txt
index e2d7546dc8..98da4cf2ab 100644
--- a/src/components/utils/CMakeLists.txt
+++ b/src/components/utils/CMakeLists.txt
@@ -40,6 +40,7 @@ include_directories (
${COMPONENTS_DIR}/protocol_handler/include
${JSONCPP_INCLUDE_DIRECTORY}
${LOG4CXX_INCLUDE_DIRECTORY}
+ ${CMAKE_SOURCE_DIR}/src
)
# dbms
@@ -114,8 +115,11 @@ if(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
endif()
add_library("Utils" ${SOURCES})
-
-list(APPEND LIBRARIES -lboost_system -lboost_thread -lboost_filesystem)
+GET_PROPERTY(BOOST_LIBS_DIRECTORY GLOBAL PROPERTY GLOBAL_BOOST_LIBS)
+list(APPEND LIBRARIES boost_system -L${BOOST_LIBS_DIRECTORY})
+list(APPEND LIBRARIES boost_thread -L${BOOST_LIBS_DIRECTORY})
+list(APPEND LIBRARIES boost_date_time -L${BOOST_LIBS_DIRECTORY})
+list(APPEND LIBRARIES boost_filesystem -L${BOOST_LIBS_DIRECTORY})
target_link_libraries("Utils" ${LIBRARIES})
add_dependencies("Utils" Boost)
diff --git a/src/components/utils/include/utils/log_message_loop_thread.h b/src/components/utils/include/utils/log_message_loop_thread.h
index d6a9edd072..b20b43c251 100644
--- a/src/components/utils/include/utils/log_message_loop_thread.h
+++ b/src/components/utils/include/utils/log_message_loop_thread.h
@@ -56,18 +56,14 @@ typedef std::queue<LogMessage> LogMessageQueue;
typedef threads::MessageLoopThread<LogMessageQueue>
LogMessageLoopThreadTemplate;
-class LogMessageHandler : public LogMessageLoopThreadTemplate::Handler {
- public:
- virtual void Handle(const LogMessage message) OVERRIDE;
-};
-
-class LogMessageLoopThread : public LogMessageLoopThreadTemplate {
+class LogMessageLoopThread : public LogMessageLoopThreadTemplate,
+ public LogMessageLoopThreadTemplate::Handler {
public:
LogMessageLoopThread();
~LogMessageLoopThread();
+ void Handle(const LogMessage message) OVERRIDE;
private:
- LogMessageHandler* handler_;
DISALLOW_COPY_AND_ASSIGN(LogMessageLoopThread);
};
diff --git a/src/components/utils/include/utils/signals.h b/src/components/utils/include/utils/signals.h
index 72d29a9e28..bda83f315f 100644
--- a/src/components/utils/include/utils/signals.h
+++ b/src/components/utils/include/utils/signals.h
@@ -38,11 +38,61 @@ typedef void (*sighandler_t)(int);
#else
#include <signal.h>
#endif
+#include "appMain/low_voltage_signals_handler.h"
namespace utils {
-bool UnsibscribeFromTermination();
-bool WaitTerminationSignals(sighandler_t sig_handler);
+class Signals {
+ public:
+ /**
+ * @brief Unsubscribe thread from termination signals SIGINT and SIGTERM
+ * @return True if thread unsubscribed successfuly, otherwise false
+ */
+ static bool UnsubscribeFromTermination();
+
+ /**
+ * @brief Triggers thread to wait for termination signals SIGINT and SIGTERM
+ * @param sig_handler - handler to work with signals specidied above
+ * @return True if handler handles signals successfuly, otherwise false
+ */
+ static bool WaitTerminationSignals(sighandler_t sig_handler);
+
+ /**
+ * @brief Unsubscribe thread from low voltage signals
+ * SIGLOWVOLTAGE, SIGWAKEUP and SIGIGNOFF
+ * @return True if thread unsubscribed successfuly, otherwise false
+ */
+ static bool UnsubscribeFromLowVoltageSignals(
+ const main_namespace::LowVoltageSignalsOffset& offset_data);
+
+ /**
+ * @brief Sends signal to specified process
+ * @param signal to send
+ * @param destination process signal to be sent to
+ */
+ static void SendSignal(const int signo, const pid_t pid);
+
+ /**
+ * @brief Creates child process
+ * @return created process id or -1 in case of error
+ */
+ static pid_t Fork();
+
+ /**
+ * @brief Wait for child process to be terminated
+ * @param cpid - process to wait for termination
+ * @param status store status information in the int to which it points
+ * @param options - options for exit form function
+ * detailed options can be found here: https://linux.die.net/man/2/waitpid
+ */
+ static void WaitPid(pid_t cpid, int* status, int options);
+
+ /**
+ * @brief Exits for process
+ * @param status - exit status code
+ */
+ static void ExitProcess(const int status);
+};
} // namespace utils
diff --git a/src/components/utils/src/date_time.cc b/src/components/utils/src/date_time.cc
index fdf0926eb2..3ca6050b3f 100644
--- a/src/components/utils/src/date_time.cc
+++ b/src/components/utils/src/date_time.cc
@@ -30,94 +30,71 @@
* POSSIBILITY OF SUCH DAMAGE.
*/
-#include <sys/time.h>
-#include <stdint.h>
#include "utils/date_time.h"
-namespace date_time {
+#include <stdint.h>
+#include <sys/time.h>
+#include "boost/date_time/posix_time/posix_time.hpp"
-TimevalStruct DateTime::getCurrentTime() {
- TimevalStruct currentTime;
- timezone timeZone;
+namespace bpt = boost::posix_time;
+using namespace boost::date_time;
+namespace date_time {
- gettimeofday(&currentTime, &timeZone);
+/* Set of helper functions for the TimeDuration struct
+ */
- return currentTime;
+TimeDuration getCurrentTime() {
+ return bpt::microsec_clock::local_time() - bpt::from_time_t(0);
+}
+TimeDuration TimeDurationZero() {
+ return TimeDuration(0, 0, 0, 0);
+}
+int64_t getSecs(const TimeDuration& t) {
+ return t.total_seconds();
}
-int64_t date_time::DateTime::getSecs(const TimevalStruct& time) {
- const TimevalStruct times = ConvertionUsecs(time);
- return static_cast<int64_t>(times.tv_sec);
+int64_t getmSecs(const TimeDuration& t) {
+ return t.total_milliseconds();
}
-int64_t DateTime::getmSecs(const TimevalStruct& time) {
- const TimevalStruct times = ConvertionUsecs(time);
- return static_cast<int64_t>(times.tv_sec) * MILLISECONDS_IN_SECOND +
- times.tv_usec / MICROSECONDS_IN_MILLISECOND;
+int64_t getuSecs(const TimeDuration& t) {
+ return t.total_microseconds();
}
-int64_t DateTime::getuSecs(const TimevalStruct& time) {
- const TimevalStruct times = ConvertionUsecs(time);
- return static_cast<int64_t>(times.tv_sec) * MILLISECONDS_IN_SECOND *
- MICROSECONDS_IN_MILLISECOND +
- times.tv_usec;
+int64_t get_just_mSecs(const TimeDuration& t) {
+ return t.total_milliseconds() % MILLISECONDS_IN_SECOND;
}
-int64_t DateTime::calculateTimeSpan(const TimevalStruct& sinceTime) {
- return calculateTimeDiff(getCurrentTime(), sinceTime);
+int64_t get_just_uSecs(const TimeDuration& t) {
+ return t.total_microseconds() % MICROSECONDS_IN_SECOND;
}
-int64_t DateTime::calculateTimeDiff(const TimevalStruct& time1,
- const TimevalStruct& time2) {
- const TimevalStruct times1 = ConvertionUsecs(time1);
- const TimevalStruct times2 = ConvertionUsecs(time2);
- TimevalStruct ret;
- if (Greater(times1, times2)) {
- ret = Sub(times1, times2);
- } else {
- ret = Sub(times2, times1);
- }
- return getmSecs(ret);
+int64_t calculateTimeSpan(const TimeDuration& sinceTime) {
+ return calculateTimeDiff(getCurrentTime(), sinceTime);
}
-void DateTime::AddMilliseconds(TimevalStruct& time, uint32_t milliseconds) {
- const uint32_t sec = milliseconds / MILLISECONDS_IN_SECOND;
- const uint32_t usec =
- (milliseconds % MILLISECONDS_IN_SECOND) * MICROSECONDS_IN_MILLISECOND;
- time.tv_sec += sec;
- time.tv_usec += usec;
- time = ConvertionUsecs(time);
+int64_t calculateTimeDiff(const TimeDuration& time1,
+ const TimeDuration& time2) {
+ return std::abs((time1 + -time2).total_milliseconds());
}
-TimevalStruct DateTime::Sub(const TimevalStruct& time1,
- const TimevalStruct& time2) {
- const TimevalStruct times1 = ConvertionUsecs(time1);
- const TimevalStruct times2 = ConvertionUsecs(time2);
- TimevalStruct ret;
- timersub(&times1, &times2, &ret);
- return ret;
+void AddMilliseconds(TimeDuration& t, uint32_t milliseconds) {
+ t += bpt::milliseconds(milliseconds);
}
-bool DateTime::Greater(const TimevalStruct& time1, const TimevalStruct& time2) {
- const TimevalStruct times1 = ConvertionUsecs(time1);
- const TimevalStruct times2 = ConvertionUsecs(time2);
- return timercmp(&times1, &times2, > );
+bool Greater(const TimeDuration& time1, const TimeDuration& time2) {
+ return time1 > time2;
}
-bool DateTime::Less(const TimevalStruct& time1, const TimevalStruct& time2) {
- const TimevalStruct times1 = ConvertionUsecs(time1);
- const TimevalStruct times2 = ConvertionUsecs(time2);
- return timercmp(&times1, &times2, < );
+bool Less(const TimeDuration& time1, const TimeDuration& time2) {
+ return time1 < time2;
}
-bool DateTime::Equal(const TimevalStruct& time1, const TimevalStruct& time2) {
- const TimevalStruct times1 = ConvertionUsecs(time1);
- const TimevalStruct times2 = ConvertionUsecs(time2);
- return !timercmp(&times1, &times2, != );
+bool Equal(const TimeDuration& time1, const TimeDuration& time2) {
+ return time1 == time2;
}
-TimeCompare date_time::DateTime::compareTime(const TimevalStruct& time1,
- const TimevalStruct& time2) {
+TimeCompare compareTime(const TimeDuration& time1, const TimeDuration& time2) {
if (Greater(time1, time2))
return GREATER;
if (Less(time1, time2))
@@ -125,28 +102,4 @@ TimeCompare date_time::DateTime::compareTime(const TimevalStruct& time1,
return EQUAL;
}
-TimevalStruct date_time::DateTime::ConvertionUsecs(const TimevalStruct& time) {
- if (time.tv_usec >= MICROSECONDS_IN_SECOND) {
- TimevalStruct time1;
- time1.tv_sec = static_cast<int64_t>(time.tv_sec) +
- (time.tv_usec / MICROSECONDS_IN_SECOND);
- time1.tv_usec = static_cast<int64_t>(time.tv_usec) % MICROSECONDS_IN_SECOND;
- return time1;
- }
- return time;
-}
-
} // namespace date_time
-
-bool operator<(const TimevalStruct& time1, const TimevalStruct& time2) {
- return date_time::DateTime::Less(time1, time2);
-}
-
-bool operator==(const TimevalStruct& time1, const TimevalStruct& time2) {
- return date_time::DateTime::Equal(time1, time2);
-}
-
-const TimevalStruct operator-(const TimevalStruct& time1,
- const TimevalStruct& time2) {
- return date_time::DateTime::Sub(time1, time2);
-}
diff --git a/src/components/utils/src/log_message_loop_thread.cc b/src/components/utils/src/log_message_loop_thread.cc
index 2b7f28fbfd..182eb64534 100644
--- a/src/components/utils/src/log_message_loop_thread.cc
+++ b/src/components/utils/src/log_message_loop_thread.cc
@@ -35,7 +35,7 @@
namespace logger {
-void LogMessageHandler::Handle(const LogMessage message) {
+void LogMessageLoopThread::Handle(const LogMessage message) {
message.logger->forcedLog(message.level,
message.entry,
message.timeStamp,
@@ -44,14 +44,13 @@ void LogMessageHandler::Handle(const LogMessage message) {
}
LogMessageLoopThread::LogMessageLoopThread()
- : LogMessageLoopThreadTemplate("Logger",
- handler_ = new LogMessageHandler()) {}
+ : LogMessageLoopThreadTemplate("Logger", this) {}
LogMessageLoopThread::~LogMessageLoopThread() {
// we'll have to drop messages
// while deleting logger thread
logger_status = DeletingLoggerThread;
- delete handler_;
+ LogMessageLoopThreadTemplate::Shutdown();
}
} // namespace logger
diff --git a/src/components/utils/src/logger.cc b/src/components/utils/src/logger.cc
index 4e93ca3d14..af21aeac77 100644
--- a/src/components/utils/src/logger.cc
+++ b/src/components/utils/src/logger.cc
@@ -39,6 +39,9 @@ void deinit_logger() {
CREATE_LOGGERPTR_LOCAL(logger_, "Utils")
LOG4CXX_DEBUG(logger_, "Logger deinitialization");
logger::set_logs_enabled(false);
+ if (logger::logger_status == logger::LoggerThreadCreated) {
+ logger::flush_logger();
+ }
logger::delete_log_message_loop_thread();
log4cxx::LoggerPtr rootLogger = log4cxx::Logger::getRootLogger();
log4cxx::spi::LoggerRepositoryPtr repository =
diff --git a/src/components/utils/src/semantic_version.cc b/src/components/utils/src/semantic_version.cc
new file mode 100644
index 0000000000..4b8d37b913
--- /dev/null
+++ b/src/components/utils/src/semantic_version.cc
@@ -0,0 +1,38 @@
+/*
+ * Copyright (c) 2018, Livio
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ *
+ * Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following
+ * disclaimer in the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * Neither the name of the Ford Motor Company nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+#include "utils/semantic_version.h"
+
+namespace utils {
+
+const SemanticVersion version_4_5(4, 5, 0);
+
+} // namespace utils \ No newline at end of file
diff --git a/src/components/utils/src/signals_posix.cc b/src/components/utils/src/signals_posix.cc
index 274c254716..e13dc04f40 100644
--- a/src/components/utils/src/signals_posix.cc
+++ b/src/components/utils/src/signals_posix.cc
@@ -29,13 +29,16 @@
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
+#include <sys/wait.h>
#include <csignal>
#include <cstdlib>
#include <stdint.h>
+#include <iostream>
#include "utils/signals.h"
-bool utils::UnsibscribeFromTermination() {
+namespace utils {
+bool Signals::UnsubscribeFromTermination() {
// Disable some system signals receiving in thread
// by blocking those signals
// (system signals processes only in the main thread)
@@ -51,6 +54,42 @@ bool utils::UnsibscribeFromTermination() {
return !pthread_sigmask(SIG_BLOCK, &signal_set, NULL);
}
+bool Signals::UnsubscribeFromLowVoltageSignals(
+ const main_namespace::LowVoltageSignalsOffset& offset_data) {
+ // Disable Low Voltage signals in main thread
+ // due to all further threads will inherit signals mask
+ sigset_t signal_set;
+ sigemptyset(&signal_set);
+ const int SIGLOWVOLTAGE = SIGRTMIN + offset_data.low_voltage_signal_offset;
+ const int SIGWAKEUP = SIGRTMIN + offset_data.wake_up_signal_offset;
+ const int SIGIGNOFF = SIGRTMIN + offset_data.ignition_off_signal_offset;
+ sigaddset(&signal_set, SIGLOWVOLTAGE);
+ sigaddset(&signal_set, SIGWAKEUP);
+ sigaddset(&signal_set, SIGIGNOFF);
+
+ // Set signals mask to be blocked by thread
+ return !pthread_sigmask(SIG_BLOCK, &signal_set, nullptr);
+}
+
+void Signals::SendSignal(const int signo, const pid_t pid) {
+ if (kill(pid, signo) == -1) {
+ std::cerr << "Error sending signal: " << strsignal(signo) << " to " << pid
+ << " .Error: " << strerror(errno) << std::endl;
+ }
+}
+
+pid_t Signals::Fork() {
+ return fork();
+}
+
+void Signals::ExitProcess(const int status) {
+ exit(status);
+}
+
+void Signals::WaitPid(pid_t cpid, int* status, int options) {
+ waitpid(cpid, status, options);
+}
+
namespace {
bool CatchSIGSEGV(sighandler_t handler) {
struct sigaction act;
@@ -63,7 +102,7 @@ bool CatchSIGSEGV(sighandler_t handler) {
}
} // namespace
-bool utils::WaitTerminationSignals(sighandler_t sig_handler) {
+bool Signals::WaitTerminationSignals(sighandler_t sig_handler) {
sigset_t signal_set;
int sig = -1;
@@ -81,3 +120,4 @@ bool utils::WaitTerminationSignals(sighandler_t sig_handler) {
}
return false;
}
+} // namespace utils
diff --git a/src/components/utils/test/date_time_test.cc b/src/components/utils/test/date_time_test.cc
index a209ab4d43..00085813aa 100644
--- a/src/components/utils/test/date_time_test.cc
+++ b/src/components/utils/test/date_time_test.cc
@@ -30,6 +30,7 @@
* POSSIBILITY OF SUCH DAMAGE.
*/
+#include <boost/thread/thread.hpp>
#include "gtest/gtest.h"
#include "utils/date_time.h"
@@ -38,364 +39,270 @@ namespace components {
namespace utils_test {
using namespace date_time;
+
+namespace bpt = boost::posix_time;
TEST(DateTimeTest, GetCurrentTime) {
// arrange
- const TimevalStruct time1 = date_time::DateTime::getCurrentTime();
+ const date_time::TimeDuration time1 = getCurrentTime();
// assert
- ASSERT_NE(0, time1.tv_sec);
- ASSERT_GE(time1.tv_usec, 0);
+ ASSERT_NE(0, time1.total_seconds());
+ ASSERT_GE(get_just_uSecs(time1), 0);
// act
- const TimevalStruct time2 = date_time::DateTime::getCurrentTime();
+ const date_time::TimeDuration time2 = getCurrentTime();
// assert
- ASSERT_NE(0, time2.tv_sec);
- ASSERT_GE(time2.tv_usec, 0);
- ASSERT_GE(time2.tv_sec, time1.tv_sec);
+ ASSERT_NE(0, time2.total_seconds());
+ ASSERT_GE(get_just_uSecs(time1), 0);
+ ASSERT_GE(time2.total_seconds(), time1.total_seconds());
}
TEST(DateTimeTest, GetSecs) {
// arrange
- TimevalStruct time;
- time.tv_sec = 1;
- time.tv_usec = 2 * date_time::DateTime::MICROSECONDS_IN_MILLISECOND;
+ date_time::TimeDuration time1 = bpt::seconds(1) + bpt::milliseconds(2);
// assert
- ASSERT_EQ(1, date_time::DateTime::getSecs(time));
+ ASSERT_EQ(1, getSecs(time1));
}
TEST(DateTimeTest, GetmSecs) {
// arrange
- TimevalStruct time;
- time.tv_sec = 1;
- time.tv_usec = 2 * date_time::DateTime::MICROSECONDS_IN_MILLISECOND;
+ date_time::TimeDuration time1 = bpt::seconds(1) + bpt::milliseconds(2);
+ int expected = 1 * MILLISECONDS_IN_SECOND + 2;
- int64_t expect_value =
- time.tv_sec * date_time::DateTime::MILLISECONDS_IN_SECOND +
- time.tv_usec / date_time::DateTime::MICROSECONDS_IN_MILLISECOND;
// assert
- ASSERT_EQ(expect_value, date_time::DateTime::getmSecs(time));
+ ASSERT_EQ(expected, getmSecs(time1));
}
TEST(DateTimeTest, GetuSecs) {
// arrange
- TimevalStruct time;
- time.tv_sec = 3;
- time.tv_usec = 4;
-
- int64_t expect_value = time.tv_sec *
- date_time::DateTime::MILLISECONDS_IN_SECOND *
- date_time::DateTime::MICROSECONDS_IN_MILLISECOND +
- time.tv_usec;
+ date_time::TimeDuration time1 = bpt::seconds(3) + bpt::microseconds(4);
+ int expect_value =
+ 3 * MILLISECONDS_IN_SECOND * MICROSECONDS_IN_MILLISECOND + 4;
+
// assert
- ASSERT_EQ(expect_value, date_time::DateTime::getuSecs(time));
+ ASSERT_EQ(expect_value, getuSecs(time1));
}
TEST(DateTimeTest, GetuSecsmSecs) {
// arrange
- TimevalStruct time;
- time.tv_sec = 5;
- time.tv_usec = 6;
+ date_time::TimeDuration time1 = bpt::seconds(5) + bpt::microseconds(6);
- int64_t expect_value = date_time::DateTime::getuSecs(time) /
- date_time::DateTime::MICROSECONDS_IN_MILLISECOND;
+ int64_t expect_value = getuSecs(time1) / MICROSECONDS_IN_MILLISECOND;
// assert
- ASSERT_EQ(expect_value, date_time::DateTime::getmSecs(time));
+ ASSERT_EQ(expect_value, getmSecs(time1));
}
TEST(DateTimeTest, CalculateTimeSpan) {
// arrange
- const TimevalStruct time = date_time::DateTime::getCurrentTime();
+ const date_time::TimeDuration time1 = getCurrentTime();
const uint32_t sleep_time_mSec = 10;
- usleep(sleep_time_mSec * date_time::DateTime::MICROSECONDS_IN_MILLISECOND);
+ // sleep current thread
+ boost::this_thread::sleep(boost::posix_time::milliseconds(sleep_time_mSec));
// assert
- ASSERT_GE(date_time::DateTime::calculateTimeSpan(time), sleep_time_mSec);
+ ASSERT_GE(calculateTimeSpan(time1), sleep_time_mSec);
}
TEST(DateTimeTest, CalculateTimeDiff) {
// arrange
- TimevalStruct time1;
- time1.tv_sec = 1;
- time1.tv_usec = 2 * date_time::DateTime::MICROSECONDS_IN_MILLISECOND;
- TimevalStruct time2;
- time2.tv_sec = 3;
- time2.tv_usec = 4 * date_time::DateTime::MICROSECONDS_IN_MILLISECOND;
+ date_time::TimeDuration time1 = bpt::seconds(2) + bpt::milliseconds(2);
+ date_time::TimeDuration time2 = bpt::seconds(3) + bpt::milliseconds(4);
// time2 to time1
- TimevalStruct diff1;
- diff1.tv_sec = time2.tv_sec - time1.tv_sec;
- diff1.tv_usec = time2.tv_usec - time1.tv_usec;
-
- const int64_t mSecDiff =
- static_cast<int64_t>(diff1.tv_sec) * 1000 + diff1.tv_usec / 1000;
+ date_time::TimeDuration diff1 = bpt::seconds(1) + bpt::milliseconds(2);
// assert
- ASSERT_EQ(mSecDiff, date_time::DateTime::calculateTimeDiff(time2, time1));
+ ASSERT_EQ(diff1.total_milliseconds(), calculateTimeDiff(time2, time1));
- // time1 to time2
- TimevalStruct diff2;
- diff2.tv_sec = time1.tv_sec - time2.tv_sec;
- diff2.tv_usec = time1.tv_usec - time2.tv_usec;
-
- const int64_t mSecDiff2 =
- -(static_cast<int64_t>(diff2.tv_sec) * 1000 + diff2.tv_usec / 1000);
+ // time1 to time2, but calculateTimeDiff returns an absolute value
+ date_time::TimeDuration diff2 = -diff1.invert_sign();
// assert
- ASSERT_EQ(mSecDiff2, date_time::DateTime::calculateTimeDiff(time1, time2));
+ ASSERT_EQ(diff2.total_milliseconds(), calculateTimeDiff(time1, time2));
}
TEST(DateTimeTest, CalculateEqualTimeDiff) {
- TimevalStruct time1;
- time1.tv_sec = 1;
- time1.tv_usec = 2 * date_time::DateTime::MICROSECONDS_IN_MILLISECOND;
+ date_time::TimeDuration time1 = bpt::seconds(1) + bpt::milliseconds(2);
- TimevalStruct time2;
- time2.tv_sec = 1;
- time2.tv_usec = 2 * date_time::DateTime::MICROSECONDS_IN_MILLISECOND;
+ date_time::TimeDuration time2 = bpt::seconds(1) + bpt::milliseconds(2);
- ASSERT_EQ(0, date_time::DateTime::calculateTimeDiff(time2, time1));
- ASSERT_EQ(0, date_time::DateTime::calculateTimeDiff(time1, time2));
+ ASSERT_EQ(0, calculateTimeDiff(time2, time1));
+ ASSERT_EQ(0, calculateTimeDiff(time1, time2));
}
TEST(DateTimeTest, compareTime) {
// arrange
- TimevalStruct time1;
- time1.tv_sec = 1;
- time1.tv_usec = 2 * date_time::DateTime::MICROSECONDS_IN_MILLISECOND;
+ date_time::TimeDuration time1 = bpt::seconds(1) + bpt::milliseconds(2);
- TimevalStruct time2;
- time2.tv_sec = 2;
- time2.tv_usec = 4 * date_time::DateTime::MICROSECONDS_IN_MILLISECOND;
+ date_time::TimeDuration time2 = bpt::seconds(2) + bpt::milliseconds(4);
// assert
- ASSERT_EQ(LESS, date_time::DateTime::compareTime(time1, time2));
- ASSERT_EQ(GREATER, date_time::DateTime::compareTime(time2, time1));
- ASSERT_NE(EQUAL, date_time::DateTime::compareTime(time2, time1));
+ ASSERT_EQ(LESS, compareTime(time1, time2));
+ ASSERT_EQ(GREATER, compareTime(time2, time1));
+ ASSERT_NE(EQUAL, compareTime(time2, time1));
// act
- TimevalStruct time3 = date_time::DateTime::Sub(time2, time1);
+ date_time::TimeDuration time3 = time2 - time1;
// assert
- ASSERT_EQ(EQUAL, date_time::DateTime::compareTime(time1, time3));
+ ASSERT_EQ(EQUAL, compareTime(time1, time3));
}
TEST(DateTimeTest, GetSecs_UsecConvertedInSec) {
// arrange
- TimevalStruct time1;
- time1.tv_sec = 0;
- time1.tv_usec = date_time::DateTime::MICROSECONDS_IN_SECOND;
+ date_time::TimeDuration time1 = bpt::microseconds(MICROSECONDS_IN_SECOND);
// assert
- ASSERT_EQ(1, date_time::DateTime::getSecs(time1));
+ ASSERT_EQ(1, getSecs(time1));
}
TEST(DateTimeTest, compareTime_UsecConvertedInSec) {
// arrange
- TimevalStruct time1;
- time1.tv_sec = 1;
- time1.tv_usec = 0;
+ date_time::TimeDuration time1 = bpt::seconds(1);
- TimevalStruct time2;
- time2.tv_sec = 0;
- time2.tv_usec = date_time::DateTime::MICROSECONDS_IN_SECOND;
+ date_time::TimeDuration time2 = bpt::microseconds(MICROSECONDS_IN_SECOND);
// assert
- ASSERT_EQ(1, date_time::DateTime::getSecs(time1));
- ASSERT_EQ(1, date_time::DateTime::getSecs(time2));
- ASSERT_EQ(EQUAL, date_time::DateTime::compareTime(time1, time2));
-}
-
-TEST(DateTimeTest, compareEqualTime_UsecConvertedInSec) {
- // arrange
- TimevalStruct time1;
- time1.tv_sec = 1;
- time1.tv_usec = 0;
-
- TimevalStruct time2;
- time2.tv_sec = 0;
- time2.tv_usec = date_time::DateTime::MICROSECONDS_IN_SECOND;
-
- // assert
- ASSERT_TRUE(date_time::DateTime::Equal(time1, time2));
+ ASSERT_EQ(1, getSecs(time1));
+ ASSERT_EQ(1, getSecs(time2));
+ ASSERT_EQ(EQUAL, compareTime(time1, time2));
+ ASSERT_TRUE(Equal(time1, time2));
}
TEST(DateTimeTest, compareLessTime_UsecConvertedInSec) {
// arrange
- TimevalStruct time1;
- time1.tv_sec = 1;
- time1.tv_usec = 0;
+ date_time::TimeDuration time1 = bpt::seconds(1);
- TimevalStruct time2;
- time2.tv_sec = 0;
- time2.tv_usec = 2 * date_time::DateTime::MICROSECONDS_IN_SECOND;
+ date_time::TimeDuration time2 = bpt::microseconds(2 * MICROSECONDS_IN_SECOND);
// assert
- ASSERT_TRUE(date_time::DateTime::Less(time1, time2));
+ ASSERT_TRUE(Less(time1, time2));
}
TEST(DateTimeTest, compareGreaterTime_UsecConvertedInSec) {
// arrange
- TimevalStruct time1;
- time1.tv_sec = 1;
- time1.tv_usec = 0;
+ date_time::TimeDuration time1 = bpt::seconds(1);
- TimevalStruct time2;
- time2.tv_sec = 0;
- time2.tv_usec = 2 * date_time::DateTime::MICROSECONDS_IN_SECOND;
+ date_time::TimeDuration time2 = bpt::milliseconds(2 * MICROSECONDS_IN_SECOND);
// assert
- ASSERT_TRUE(date_time::DateTime::Greater(time2, time1));
+ ASSERT_TRUE(Greater(time2, time1));
}
TEST(DateTimeTest, CalculateTimeSub_UsecConvertedInSec) {
// arrange
- TimevalStruct time1;
- time1.tv_sec = 1;
- time1.tv_usec = 0;
+ date_time::TimeDuration time1 = bpt::seconds(1);
- TimevalStruct time2;
- time2.tv_sec = 0;
- time2.tv_usec = 2 * date_time::DateTime::MICROSECONDS_IN_SECOND;
+ date_time::TimeDuration time2 = bpt::microseconds(2 * MICROSECONDS_IN_SECOND);
- TimevalStruct time3 = date_time::DateTime::Sub(time2, time1);
+ date_time::TimeDuration time3 = time2 - time1;
// assert
- ASSERT_EQ(EQUAL, date_time::DateTime::compareTime(time1, time3));
+ ASSERT_EQ(EQUAL, compareTime(time1, time3));
}
TEST(DateTimeTest, CalculateTimeDiff_UsecConvertedInSec) {
// arrange
- TimevalStruct time1;
- time1.tv_sec = 2;
- time1.tv_usec = 5 * date_time::DateTime::MICROSECONDS_IN_SECOND;
- TimevalStruct time2;
- time2.tv_sec = 3;
- time2.tv_usec = 1 * date_time::DateTime::MICROSECONDS_IN_SECOND;
+ date_time::TimeDuration time1 =
+ bpt::seconds(2) + bpt::microseconds(5 * MICROSECONDS_IN_SECOND);
+ date_time::TimeDuration time2 =
+ bpt::seconds(3) + bpt::microseconds(1 * MICROSECONDS_IN_SECOND);
// assert
- ASSERT_EQ(3000, date_time::DateTime::calculateTimeDiff(time2, time1));
- ASSERT_EQ(3000, date_time::DateTime::calculateTimeDiff(time1, time2));
+ ASSERT_EQ(3000, calculateTimeDiff(time2, time1));
+ ASSERT_EQ(3000, calculateTimeDiff(time1, time2));
}
TEST(DateTimeTest, CalculateEqualTimeDiff_UsecConvertedInSec) {
- // arrange
- TimevalStruct time1;
- time1.tv_sec = 2;
- time1.tv_usec = 2 * date_time::DateTime::MICROSECONDS_IN_SECOND;
-
- TimevalStruct time2;
- time2.tv_sec = 3;
- time2.tv_usec = 1 * date_time::DateTime::MICROSECONDS_IN_SECOND;
+ date_time::TimeDuration time1 =
+ bpt::seconds(2) + bpt::microseconds(2 * MICROSECONDS_IN_SECOND);
+ date_time::TimeDuration time2 =
+ bpt::seconds(3) + bpt::microseconds(1 * MICROSECONDS_IN_SECOND);
// assert
- ASSERT_EQ(0, date_time::DateTime::calculateTimeDiff(time2, time1));
- ASSERT_EQ(0, date_time::DateTime::calculateTimeDiff(time1, time2));
-}
+ ASSERT_EQ(0, calculateTimeDiff(time2, time1));
+ ASSERT_EQ(0, calculateTimeDiff(time1, time2));
-TEST(DateTimeTest, CalculateEqualTimeSub_UsecConvertedInSec) {
- // arrange
- TimevalStruct time1;
- time1.tv_sec = 3;
- time1.tv_usec = 0;
+ date_time::TimeDuration time3 = time2 - time1;
+ date_time::TimeDuration time4 = time1 - time2;
- TimevalStruct time2;
- time2.tv_sec = 2;
- time2.tv_usec = 1 * date_time::DateTime::MICROSECONDS_IN_SECOND;
+ date_time::TimeDuration time_expected(0, 0, 0, 0);
- TimevalStruct time3 = date_time::DateTime::Sub(time2, time1);
- TimevalStruct time4 = date_time::DateTime::Sub(time1, time2);
-
- TimevalStruct time_expected;
- time_expected.tv_sec = 0;
- time_expected.tv_usec = 0;
- // assert
- ASSERT_EQ(EQUAL, date_time::DateTime::compareTime(time_expected, time3));
- ASSERT_EQ(EQUAL, date_time::DateTime::compareTime(time_expected, time4));
+ ASSERT_EQ(EQUAL, compareTime(time_expected, time3));
+ ASSERT_EQ(EQUAL, compareTime(time_expected, time4));
}
TEST(DateTimeTest,
AddMilliseconds_SetMillisecondMultiplesSecond_ExpectChangeTime) {
- TimevalStruct time1;
- time1.tv_sec = 3;
- time1.tv_usec = 0;
+ date_time::TimeDuration time1 = bpt::seconds(3);
uint32_t milliseconds = 7000;
- date_time::DateTime::AddMilliseconds(time1, milliseconds);
- ASSERT_EQ(10, time1.tv_sec);
- ASSERT_EQ(0, time1.tv_usec);
+ AddMilliseconds(time1, milliseconds);
+ ASSERT_EQ(10, getSecs(time1));
+ ASSERT_EQ(0, get_just_mSecs(time1));
}
TEST(DateTimeTest,
AddMilliseconds_SetMillisecondNotMultiplesSecond_ExpectChangeTime) {
- TimevalStruct time1;
+ date_time::TimeDuration time1 = bpt::seconds(3);
uint32_t milliseconds = 7500;
- time1.tv_sec = 3;
- time1.tv_usec = 0;
- date_time::DateTime::AddMilliseconds(time1, milliseconds);
- ASSERT_EQ(10, time1.tv_sec);
- ASSERT_EQ(500000, time1.tv_usec);
+
+ AddMilliseconds(time1, milliseconds);
+ ASSERT_EQ(10, time1.total_seconds());
+ ASSERT_EQ(500000, get_just_uSecs(time1));
}
TEST(DateTimeTest,
AddMilliseconds_SetMilliSecondLessThenSeconds_ExpectChangeTime) {
- TimevalStruct time1;
+ date_time::TimeDuration time1 = bpt::seconds(3);
uint32_t milliseconds = 500;
- time1.tv_sec = 3;
- time1.tv_usec = 0;
- date_time::DateTime::AddMilliseconds(time1, milliseconds);
- ASSERT_EQ(3, time1.tv_sec);
- ASSERT_EQ(500000, time1.tv_usec);
+
+ AddMilliseconds(time1, milliseconds);
+ ASSERT_EQ(3, getSecs(time1));
+ ASSERT_EQ(500000, get_just_uSecs(time1));
}
TEST(DateTimeTest,
AddMilliseconds_SetMillisecondEqualNull_ExpectNotChangeTime) {
- TimevalStruct time1;
+ date_time::TimeDuration time1 = bpt::seconds(3);
uint32_t milliseconds = 0;
- time1.tv_sec = 3;
- time1.tv_usec = 0;
- date_time::DateTime::AddMilliseconds(time1, milliseconds);
- ASSERT_EQ(3, time1.tv_sec);
- ASSERT_EQ(0, time1.tv_usec);
+
+ AddMilliseconds(time1, milliseconds);
+ ASSERT_EQ(3, getSecs(time1));
+ ASSERT_EQ(0, get_just_mSecs(time1));
}
TEST(DateTimeTest, AddMilliseconds_SetOverlowMicrosecond_ExpectChangeTime) {
- TimevalStruct time1;
+ date_time::TimeDuration time1 = bpt::seconds(3);
uint32_t milliseconds = 7600;
- time1.tv_sec = 3;
- time1.tv_usec = 500000;
- date_time::DateTime::AddMilliseconds(time1, milliseconds);
- ASSERT_EQ(11, time1.tv_sec);
- ASSERT_EQ(100000, time1.tv_usec);
+
+ AddMilliseconds(time1, milliseconds);
+ ASSERT_EQ(10, getSecs(time1));
+ ASSERT_EQ(600000, get_just_uSecs(time1));
}
-TEST(DateTimeTest, Operator_minus_TimevalStruct_positive) {
- TimevalStruct time1;
- TimevalStruct time2;
- TimevalStruct time3;
- time1.tv_sec = 3;
- time1.tv_usec = 0;
- time2.tv_sec = 3;
- time2.tv_usec = 0;
- time3.tv_sec = 2;
- time3.tv_usec = 9000000;
- ASSERT_EQ(0, date_time::DateTime::getSecs(time1 - time2));
- ASSERT_EQ(8000000, date_time::DateTime::getuSecs(time3 - time1));
+TEST(DateTimeTest, Operator_minus_TimeDuration_positive) {
+ date_time::TimeDuration time1 = bpt::seconds(3);
+ date_time::TimeDuration time2 = bpt::seconds(3);
+ date_time::TimeDuration time3 =
+ bpt::seconds(2) + bpt::microseconds(9 * MICROSECONDS_IN_SECOND);
+ date_time::TimeDuration diff1 = time1 - time2;
+ ASSERT_EQ(0, getSecs(diff1));
+ ASSERT_EQ(8000000, getuSecs(time3 - time1));
}
-TEST(DateTimeTest, Operator_minus_TimevalStruct_negative) {
- TimevalStruct time1;
- TimevalStruct time2;
- time1.tv_sec = 3;
- time1.tv_usec = 0;
- time2.tv_sec = 2;
- time2.tv_usec = 9000000;
- ASSERT_NE(1, date_time::DateTime::getSecs(time1 - time2));
- ASSERT_NE(-8000000, date_time::DateTime::getSecs(time2 - time1));
+TEST(DateTimeTest, Operator_minus_TimeDuration_negative) {
+ date_time::TimeDuration time1 = bpt::seconds(3);
+ date_time::TimeDuration time2 =
+ bpt::seconds(2) + bpt::microseconds(9 * MICROSECONDS_IN_SECOND);
+
+ ASSERT_NE(1, getSecs(time1 - time2));
+ ASSERT_NE(-8000000, getSecs(time2 - time1));
}
} // namespace utils_test
diff --git a/src/components/utils/test/include/utils/mock_signals_posix.h b/src/components/utils/test/include/utils/mock_signals_posix.h
new file mode 100644
index 0000000000..1aad2d5873
--- /dev/null
+++ b/src/components/utils/test/include/utils/mock_signals_posix.h
@@ -0,0 +1,59 @@
+/*
+ * Copyright (c) 2018, Ford Motor Company
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ *
+ * Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following
+ * disclaimer in the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * Neither the name of the Ford Motor Company nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef SRC_COMPONENTS_UTILS_TEST_INCLUDE_UTILS_MOCK_SIGNALS_POSIX_H_
+#define SRC_COMPONENTS_UTILS_TEST_INCLUDE_UTILS_MOCK_SIGNALS_POSIX_H_
+
+#include "gmock/gmock.h"
+#include "utils/signals.h"
+#include <signal.h>
+#include "appMain/low_voltage_signals_handler.h"
+
+namespace utils {
+
+class MockSignalsPosix {
+ public:
+ MOCK_METHOD0(UnsubscribeFromTermination, bool());
+ MOCK_METHOD1(WaitTerminationSignals, bool(sighandler_t sig_handler));
+ MOCK_METHOD1(
+ UnsubscribeFromLowVoltageSignals,
+ bool(const main_namespace::LowVoltageSignalsOffset& offset_data));
+ MOCK_METHOD2(SendSignal, void(const int signo, const pid_t pid));
+ MOCK_METHOD0(Fork, pid_t());
+ MOCK_METHOD1(ExitProcess, void(const int status));
+ MOCK_METHOD3(WaitPid, void(pid_t cpid, int* status, int options));
+
+ static MockSignalsPosix* signals_posix_mock();
+};
+
+} // namespace utils
+#endif // SRC_COMPONENTS_UTILS_TEST_INCLUDE_UTILS_MOCK_SIGNALS_POSIX_H_
diff --git a/src/components/utils/test/log_message_loop_thread_test.cc b/src/components/utils/test/log_message_loop_thread_test.cc
index 032c7ecae1..92287fb45b 100644
--- a/src/components/utils/test/log_message_loop_thread_test.cc
+++ b/src/components/utils/test/log_message_loop_thread_test.cc
@@ -57,7 +57,7 @@ TEST(LogMessageLoopThread, DestroyLogMessage_loggerStatusDeletingLogger) {
logger::logger_status = LoggerThreadNotCreated;
}
-class MockLogMessageTest : public LogMessageHandler {
+class MockLogMessageTest : public LogMessageLoopThread {
public:
MOCK_CONST_METHOD1(Handle, void(const LogMessage message));
};
diff --git a/src/components/utils/test/messagemeter_test.cc b/src/components/utils/test/messagemeter_test.cc
index 4ec14ac1cc..d45e600034 100644
--- a/src/components/utils/test/messagemeter_test.cc
+++ b/src/components/utils/test/messagemeter_test.cc
@@ -57,25 +57,25 @@ const TimePair testing_time_pairs[] = {TimePair(0, 50),
class MessageMeterTest : public ::testing::TestWithParam<TimePair> {
protected:
void SetUp() OVERRIDE {
- usecs = date_time::DateTime::MICROSECONDS_IN_MILLISECOND;
+ usecs = date_time::MICROSECONDS_IN_MILLISECOND;
id1 = 0x0;
id2 = 0xABCDEF;
id3 = 0xFEBCDA;
const TimePair time_pair = GetParam();
EXPECT_GT(usecs, time_pair.second) << "Wrong time (msecs) value";
+ time_range = date_time::seconds(time_pair.first) +
+ date_time::microseconds(time_pair.second * usecs);
- time_range.tv_sec = time_pair.first;
- time_range.tv_usec = time_pair.second * usecs;
- EXPECT_LT(0, date_time::DateTime::getuSecs(time_range))
+ EXPECT_LT(0, date_time::getuSecs(time_range))
<< "Wrong test case with null range value";
meter.set_time_range(time_range);
- time_range_msecs = date_time::DateTime::getmSecs(time_range);
+ time_range_msecs = date_time::getmSecs(time_range);
}
void TearDown() OVERRIDE {}
::utils::MessageMeter<int> meter;
- TimevalStruct time_range = {0, 0};
+ date_time::TimeDuration time_range = date_time::TimeDurationZero();
int64_t time_range_msecs;
int usecs;
int id1, id2, id3;
@@ -83,27 +83,25 @@ class MessageMeterTest : public ::testing::TestWithParam<TimePair> {
TEST(MessageMeterTest, DefaultTimeRange) {
const ::utils::MessageMeter<int> default_meter;
- const TimevalStruct time_second{1, 0};
+ const date_time::TimeDuration time_second = date_time::seconds(1);
EXPECT_EQ(time_second, default_meter.time_range());
}
TEST(MessageMeterTest, TimeRangeSetter) {
::utils::MessageMeter<int> meter;
- TimevalStruct time_range{0, 0};
+ date_time::TimeDuration time_range;
const int test_count_secs = 1000;
- // Skip 1000th msec value as wrong for TimevalStruct
+ // Skip 1000th msec value as wrong for date_time::TimeDuration
const int test_count_msecs = 999;
for (int sec = test_count_secs; sec >= 0; --sec) {
for (int msec = test_count_msecs; msec >= 0; --msec) {
- time_range.tv_sec = sec;
- time_range.tv_usec =
- msec * date_time::DateTime::MICROSECONDS_IN_MILLISECOND;
- // Setter TimevalStruct
+ time_range = date_time::seconds(sec) + date_time::milliseconds(msec);
+
+ // Setter date_time::TimeDuration
meter.set_time_range(time_range);
EXPECT_EQ(time_range, meter.time_range()) << sec << "." << msec << " sec";
// Setter mSecs
- meter.set_time_range(sec * date_time::DateTime::MILLISECONDS_IN_SECOND +
- msec);
+ meter.set_time_range(sec * date_time::MILLISECONDS_IN_SECOND + msec);
EXPECT_EQ(time_range, meter.time_range()) << sec << "." << msec << " sec";
}
}
@@ -113,7 +111,7 @@ TEST(MessageMeterTest, AddingWithNullTimeRange) {
::utils::MessageMeter<int> meter;
const int id1 = 1;
const int id2 = 2;
- const TimevalStruct null_time_range{0, 0};
+ const date_time::TimeDuration null_time_range = date_time::TimeDurationZero();
meter.set_time_range(null_time_range);
for (int i = 0; i < 10000; ++i) {
// 1st Connection
@@ -129,10 +127,10 @@ TEST(MessageMeterTest, AddingWithNullTimeRange) {
TEST_P(MessageMeterTest,
DISABLED_TrackMessage_AddingOverPeriod_CorrectCountOfMessages) {
size_t messages = 0;
- const TimevalStruct start_time = date_time::DateTime::getCurrentTime();
+ const date_time::TimeDuration start_time = date_time::getCurrentTime();
// Add messages for less range period
int64_t time_span;
- while ((time_span = date_time::DateTime::calculateTimeSpan(start_time)) <
+ while ((time_span = date_time::calculateTimeSpan(start_time)) <
time_range_msecs) {
++messages;
@@ -153,10 +151,10 @@ TEST_P(MessageMeterTest,
TEST_P(MessageMeterTest,
DISABLED_TrackMessage_AddingOverPeriodMultiIds_CorrectCountOfMessages) {
size_t messages = 0;
- const TimevalStruct start_time = date_time::DateTime::getCurrentTime();
+ const date_time::TimeDuration start_time = date_time::getCurrentTime();
// Add messages for less range period
int64_t time_span;
- while ((time_span = date_time::DateTime::calculateTimeSpan(start_time)) <
+ while ((time_span = date_time::calculateTimeSpan(start_time)) <
time_range_msecs) {
++messages;
@@ -188,14 +186,14 @@ TEST_P(MessageMeterTest,
TEST_P(MessageMeterTest,
DISABLED_Frequency_CountingOverPeriod_CorrectCountOfMessages) {
const size_t one_message = 1;
- const TimevalStruct start_time = date_time::DateTime::getCurrentTime();
+ const date_time::TimeDuration start_time = date_time::getCurrentTime();
EXPECT_EQ(one_message, meter.TrackMessage(id1));
EXPECT_EQ(one_message, meter.TrackMessage(id2));
EXPECT_EQ(one_message, meter.TrackMessage(id3));
// Check messages count over period
int64_t time_span;
- while ((time_span = date_time::DateTime::calculateTimeSpan(start_time)) <
+ while ((time_span = date_time::calculateTimeSpan(start_time)) <
time_range_msecs) {
usleep(time_range_msecs);
diff --git a/src/components/utils/test/mock_signals_posix.cc b/src/components/utils/test/mock_signals_posix.cc
new file mode 100644
index 0000000000..47531b4a9f
--- /dev/null
+++ b/src/components/utils/test/mock_signals_posix.cc
@@ -0,0 +1,76 @@
+/*
+ * Copyright (c) 2018, Ford Motor Company
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ *
+ * Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following
+ * disclaimer in the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * Neither the name of the Ford Motor Company nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "gtest/gtest.h"
+#include "gmock/gmock.h"
+#include "utils/mock_signals_posix.h"
+#include "utils/signals.h"
+
+namespace utils {
+
+bool Signals::UnsubscribeFromTermination() {
+ return MockSignalsPosix::signals_posix_mock()->UnsubscribeFromTermination();
+}
+
+bool Signals::WaitTerminationSignals(sighandler_t sig_handler) {
+ return MockSignalsPosix::signals_posix_mock()->WaitTerminationSignals(
+ sig_handler);
+}
+
+bool Signals::UnsubscribeFromLowVoltageSignals(
+ const main_namespace::LowVoltageSignalsOffset& offset_data) {
+ return MockSignalsPosix::signals_posix_mock()
+ ->UnsubscribeFromLowVoltageSignals(offset_data);
+}
+
+void Signals::SendSignal(const int signo, const pid_t pid) {
+ MockSignalsPosix::signals_posix_mock()->SendSignal(signo, pid);
+}
+
+void Signals::ExitProcess(const int status) {
+ MockSignalsPosix::signals_posix_mock()->ExitProcess(status);
+}
+
+void Signals::WaitPid(pid_t cpid, int* status, int options) {
+ MockSignalsPosix::signals_posix_mock()->WaitPid(cpid, status, options);
+}
+
+pid_t Signals::Fork() {
+ return MockSignalsPosix::signals_posix_mock()->Fork();
+}
+
+MockSignalsPosix* MockSignalsPosix::signals_posix_mock() {
+ static ::testing::NiceMock<MockSignalsPosix> signals_posix_mock;
+ return &signals_posix_mock;
+}
+
+} // namespace utils