diff options
author | Artem Nosach <ANosach@luxoft.com> | 2016-06-03 16:51:29 +0300 |
---|---|---|
committer | Artem Nosach <ANosach@luxoft.com> | 2016-06-03 16:51:29 +0300 |
commit | abc288bea816d891cf22cea26db0dcb11610a445 (patch) | |
tree | c25610acb874e31c00e39ea149158d9ed46c24f2 | |
parent | 4e837a60d21ec9c7d211d18306d9651d8def0866 (diff) | |
parent | e7f0905c8df5225cc95ca928c9300a56ba3f4b7a (diff) | |
download | sdl_core-abc288bea816d891cf22cea26db0dcb11610a445.tar.gz |
Merge pull request #37 from anosach-luxoft/fix/enable-utils-tests
Fix utils issues found by unit tests
Related-issues: APPLINK-22713, APPLINK-22716
-rw-r--r-- | src/components/config_profile/include/config_profile/profile.h | 13 | ||||
-rw-r--r-- | src/components/utils/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/components/utils/include/utils/file_system.h | 4 | ||||
-rw-r--r-- | src/components/utils/src/file_system_posix.cc | 8 | ||||
-rw-r--r-- | src/components/utils/src/file_system_qt.cc | 27 | ||||
-rw-r--r-- | src/components/utils/src/file_system_win.cc | 35 | ||||
-rw-r--r-- | src/components/utils/test/CMakeLists.txt | 25 | ||||
-rw-r--r-- | src/components/utils/test/async_runner_test.cc | 8 | ||||
-rw-r--r-- | src/components/utils/test/date_time_test.cc | 2 | ||||
-rw-r--r-- | src/components/utils/test/file_system_test.cc | 27 | ||||
-rw-r--r-- | src/components/utils/test/lock_test.cc (renamed from src/components/utils/test/lock_posix_test.cc) | 14 | ||||
-rw-r--r-- | src/components/utils/test/posix_thread_test.cc | 329 | ||||
-rw-r--r-- | src/components/utils/test/system_test.cc | 3 | ||||
-rw-r--r-- | src/components/utils/test/thread_test.cc | 180 | ||||
-rw-r--r-- | src/components/utils/test/timer_test.cc | 8 |
15 files changed, 257 insertions, 427 deletions
diff --git a/src/components/config_profile/include/config_profile/profile.h b/src/components/config_profile/include/config_profile/profile.h index 416d8266d6..1a69da974f 100644 --- a/src/components/config_profile/include/config_profile/profile.h +++ b/src/components/config_profile/include/config_profile/profile.h @@ -671,19 +671,6 @@ class Profile : public protocol_handler::ProtocolHandlerSettings, const char* const pKey) const; /** - * @brief Checks, if path is relative - * @param path Path - * @return true, if is relative, otherwise - false - */ - bool IsRelativePath(const std::string& path); - - /** - * @brief Makes relative path absolute - * @param path Path - */ - void MakeAbsolutePath(std::string& path); - - /** * @brief Converts input string to number * @param input Input string * @param output Output number diff --git a/src/components/utils/CMakeLists.txt b/src/components/utils/CMakeLists.txt index c07b2ae349..3e6071ace6 100644 --- a/src/components/utils/CMakeLists.txt +++ b/src/components/utils/CMakeLists.txt @@ -64,7 +64,6 @@ if(${CMAKE_SYSTEM_NAME} MATCHES "Windows") ${UTILS_SRC_DIR}/lock_qt.cc ${UTILS_SRC_DIR}/rwlock_qt.cc ${UTILS_SRC_DIR}/date_time_qt.cc - ${UTILS_SRC_DIR}/system.cc ${UTILS_SRC_DIR}/signals_qt.cc ${UTILS_SRC_DIR}/gen_hash.cc ${UTILS_SRC_DIR}/threads/thread_validator.cc diff --git a/src/components/utils/include/utils/file_system.h b/src/components/utils/include/utils/file_system.h index eda04703e0..95db8740c3 100644 --- a/src/components/utils/include/utils/file_system.h +++ b/src/components/utils/include/utils/file_system.h @@ -69,9 +69,9 @@ FileSizeType FileSize(const std::string& utf8_path); /** * @brief Creates directory * @param utf8_path path to directory - * @return path to created directory. + * @return return true if directory was created or already exist */ -std::string CreateDirectory(const std::string& utf8_path); +bool CreateDirectory(const std::string& utf8_path); /** * @brief Creates directory recursively diff --git a/src/components/utils/src/file_system_posix.cc b/src/components/utils/src/file_system_posix.cc index 2f1d737621..a80517548f 100644 --- a/src/components/utils/src/file_system_posix.cc +++ b/src/components/utils/src/file_system_posix.cc @@ -102,12 +102,8 @@ file_system::FileSizeType file_system::DirectorySize( return size; } -std::string file_system::CreateDirectory(const std::string& utf8_path) { - if (!DirectoryExists(utf8_path)) { - mkdir(utf8_path.c_str(), S_IRWXU); - } - - return utf8_path; +bool file_system::CreateDirectory(const std::string& utf8_path) { + return DirectoryExists(utf8_path) || 0 == mkdir(utf8_path.c_str(), S_IRWXU); } bool file_system::CreateDirectoryRecursively(const std::string& utf8_path) { diff --git a/src/components/utils/src/file_system_qt.cc b/src/components/utils/src/file_system_qt.cc index 85da75fa67..8d71fc4a05 100644 --- a/src/components/utils/src/file_system_qt.cc +++ b/src/components/utils/src/file_system_qt.cc @@ -108,12 +108,9 @@ file_system::FileSizeType file_system::DirectorySize( return size; } -std::string file_system::CreateDirectory(const std::string& utf8_path) { +bool file_system::CreateDirectory(const std::string& utf8_path) { QDir dir; - if (dir.mkdir(QString::fromUtf8(utf8_path.c_str()))) { - return utf8_path; - } - return ""; + return dir.mkdir(QString::fromUtf8(utf8_path.c_str())); } bool file_system::CreateDirectoryRecursively(const std::string& utf8_path) { @@ -187,10 +184,17 @@ bool file_system::DeleteFile(const std::string& utf8_path) { void file_system::RemoveDirectoryContent(const std::string& utf8_path) { QDir dir(QString::fromUtf8(utf8_path.c_str())); - dir.setNameFilters(QStringList() << "*.*"); - dir.setFilter(QDir::Files); - foreach (QString dirFile, dir.entryList()) { dir.remove(dirFile); } - dir.rmpath(QString::fromUtf8(utf8_path.c_str())); + dir.setFilter(QDir::Files | QDir::NoDotAndDotDot); + foreach(QString dirItem, dir.entryList()) + { + dir.remove(dirItem); + } + dir.setFilter(QDir::Dirs | QDir::NoDotAndDotDot); + foreach(QString dirItem, dir.entryList()) + { + QDir subDir(dir.absoluteFilePath(dirItem)); + subDir.removeRecursively(); + } } bool file_system::RemoveDirectory(const std::string& utf8_path, @@ -200,7 +204,7 @@ bool file_system::RemoveDirectory(const std::string& utf8_path, if (is_recursively) { return dir.removeRecursively(); } - return dir.rmdir(QString::fromUtf8(utf8_path.c_str())); + return dir.rmdir(dir.absolutePath()); } return false; } @@ -232,7 +236,8 @@ std::vector<std::string> file_system::ListFiles(const std::string& utf8_path) { return listFiles; } QDir dir(QString::fromUtf8(utf8_path.c_str())); - QStringList list_files = dir.entryList(QDir::Files); + QStringList list_files = + dir.entryList(QDir::AllEntries | QDir::NoDotAndDotDot | QDir::NoSymLinks); foreach (QString str, list_files) { listFiles.push_back(str.toStdString()); } return listFiles; } diff --git a/src/components/utils/src/file_system_win.cc b/src/components/utils/src/file_system_win.cc index a776676149..1591ab4ce2 100644 --- a/src/components/utils/src/file_system_win.cc +++ b/src/components/utils/src/file_system_win.cc @@ -173,27 +173,20 @@ file_system::FileSizeType file_system::DirectorySize( return size; } -std::string file_system::CreateDirectory(const std::string& utf8_path) { - if (!DirectoryExists(utf8_path)) { - _wmkdir(ConvertUTF8ToWString(utf8_path).c_str()); - } - return utf8_path; +bool file_system::CreateDirectory(const std::string& utf8_path) { + return DirectoryExists(utf8_path) || + 0 == _wmkdir(ConvertUTF8ToWString(utf8_path).c_str()); } bool file_system::CreateDirectoryRecursively(const std::string& utf8_path) { - const std::string delimiter = GetPathDelimiter(); - std::size_t pos = utf8_path.find(delimiter, 0); + size_t pos = IsRelativePath(utf8_path) ? 0 : utf8_path.find_first_of(":") + 2; while (pos < utf8_path.length()) { - pos = utf8_path.find(delimiter, pos + 1); - if (pos == std::string::npos) { - pos = utf8_path.length(); - } - if (!DirectoryExists(utf8_path.substr(0, pos))) { - if (0 != - _wmkdir(ConvertUTF8ToWString(utf8_path.substr(0, pos)).c_str())) { - return false; - } + pos = std::min(utf8_path.find_first_of("/", pos), + utf8_path.find_first_of("\\", pos)); + if (!CreateDirectory(utf8_path.substr(0, pos))) { + return false; } + pos = std::string::npos == pos ? pos : pos + 1; } return true; } @@ -283,14 +276,14 @@ void file_system::RemoveDirectoryContent(const std::string& utf8_path) { } do { + const std::string utf8_file_name = ConvertWStringToUTF8(ffd.cFileName); if (FILE_ATTRIBUTE_DIRECTORY == ffd.dwFileAttributes) { - const std::string utf8_file_name = ConvertWStringToUTF8(ffd.cFileName); if (utf8_file_name.compare(kCurrentDirectoryEntry) != 0 && utf8_file_name.compare(kParentDirectoryEntry) != 0) { - RemoveDirectory(utf8_file_name, true); + RemoveDirectory(ConcatPath(utf8_path, utf8_file_name), true); } } else { - _wremove(ffd.cFileName); + DeleteFile(ConcatPath(utf8_path, utf8_file_name)); } } while (FindNextFileW(find, &ffd) != 0); @@ -390,7 +383,9 @@ std::vector<std::string> file_system::ListFiles(const std::string& utf8_path) { } do { - if (FILE_ATTRIBUTE_DIRECTORY != ffd.dwFileAttributes) { + const std::string utf8_file_name = ConvertWStringToUTF8(ffd.cFileName); + if (utf8_file_name.compare(kCurrentDirectoryEntry) != 0 && + utf8_file_name.compare(kParentDirectoryEntry) != 0) { list_files.push_back(ConvertWStringToUTF8(ffd.cFileName)); } } while (FindNextFileW(find, &ffd) != 0); diff --git a/src/components/utils/test/CMakeLists.txt b/src/components/utils/test/CMakeLists.txt index 3cf9898e36..efa93e3442 100644 --- a/src/components/utils/test/CMakeLists.txt +++ b/src/components/utils/test/CMakeLists.txt @@ -58,34 +58,35 @@ set(testSources prioritized_queue_test.cc data_accessor_test.cc stl_utils_test.cc - #async_runner_test.cc + async_runner_test.cc shared_ptr_test.cc scope_guard_test.cc atomic_object_test.cc - #message_loop_thread_test.cc + message_loop_thread_test.cc custom_string_test.cc - #timer_test.cc + timer_test.cc + lock_test.cc + thread_test.cc ) +if (ENABLE_LOG) + list(APPEND testSources auto_trace_test.cc) +endif() + +if (BUILD_BACKTRACE_SUPPORT) + list(APPEND testSources back_trace_test.cc) +endif() + if (${CMAKE_SYSTEM_NAME} MATCHES "Linux") list (APPEND testSources system_test.cc conditional_variable_test.cc message_queue_test.cc - lock_posix_test.cc singleton_test.cc - #posix_thread_test.cc rwlock_posix_test.cc resource_usage_test.cc ) - if (ENABLE_LOG) - list(APPEND testSources auto_trace_test.cc) - endif() - - if (BUILD_BACKTRACE_SUPPORT) - list(APPEND testSources back_trace_test.cc) - endif() endif() set(testLibraries diff --git a/src/components/utils/test/async_runner_test.cc b/src/components/utils/test/async_runner_test.cc index 3dd6383588..c38f3ff5e6 100644 --- a/src/components/utils/test/async_runner_test.cc +++ b/src/components/utils/test/async_runner_test.cc @@ -104,8 +104,8 @@ TEST_F(AsyncRunnerTest, ASyncRunManyDelegates_ExpectSuccessfulAllDelegatesRun) { delegates_[i] = new TestThreadDelegate(); asr_pt_->AsyncRun(delegates_[i]); } - // Wait for 2 secs. Give this time to delegates to be run - cond_var_.WaitFor(lock, 2000); + // Wait for 1 secs. Give this time to delegates to be run + cond_var_.WaitFor(lock, 1000); // Expect all delegates run successfully EXPECT_EQ(kDelegatesNum_, check_value); } @@ -120,8 +120,8 @@ TEST_F(AsyncRunnerTest, for (unsigned int i = 0; i < kDelegatesNum_; ++i) { delegates_[i] = new TestThreadDelegate(); } - // Wait for 2 secs - cond_var_.WaitFor(lock, 2000); + // Wait for 1 secs + cond_var_.WaitFor(lock, 1000); // Run created delegates for (unsigned int i = 0; i < kDelegatesNum_; ++i) { if (kDelegatesNum_ > 1) { diff --git a/src/components/utils/test/date_time_test.cc b/src/components/utils/test/date_time_test.cc index a630d7b524..7fd6c38e22 100644 --- a/src/components/utils/test/date_time_test.cc +++ b/src/components/utils/test/date_time_test.cc @@ -104,7 +104,7 @@ TEST(DateTimeTest, GetuSecsmSecs) { ASSERT_EQ(expect_value, date_time::DateTime::getmSecs(time)); } -TEST(DateTimeTest, DISABLED_CalculateTimeSpan) { +TEST(DateTimeTest, CalculateTimeSpan) { // arrange const TimevalStruct time = date_time::DateTime::getCurrentTime(); diff --git a/src/components/utils/test/file_system_test.cc b/src/components/utils/test/file_system_test.cc index d32fdc7a6b..6a6cd6b70e 100644 --- a/src/components/utils/test/file_system_test.cc +++ b/src/components/utils/test/file_system_test.cc @@ -44,7 +44,7 @@ namespace utils_test { using namespace file_system; -TEST(FileSystemTest, DISABLED_CreateDeleteDirectory) { +TEST(FileSystemTest, CreateDeleteDirectory) { ASSERT_FALSE(DirectoryExists("./Test directory")); // Directory creation CreateDirectory("./Test directory"); @@ -56,7 +56,7 @@ TEST(FileSystemTest, DISABLED_CreateDeleteDirectory) { EXPECT_FALSE(DirectoryExists("./Test directory")); } -TEST(FileSystemTest, DISABLED_CreateDirectoryTwice) { +TEST(FileSystemTest, CreateDirectoryTwice) { ASSERT_FALSE(DirectoryExists("./Test directory")); // Directory creation CreateDirectory("./Test directory"); @@ -74,7 +74,7 @@ TEST(FileSystemTest, DISABLED_CreateDirectoryTwice) { EXPECT_FALSE(DirectoryExists("./Test directory")); } -TEST(FileSystemTest, DISABLED_DeleteDirectoryRecursively) { +TEST(FileSystemTest, DeleteDirectoryRecursively) { ASSERT_FALSE(DirectoryExists("./Test directory")); // Create directories CreateDirectory("./Test directory"); @@ -90,7 +90,7 @@ TEST(FileSystemTest, DISABLED_DeleteDirectoryRecursively) { EXPECT_FALSE(DirectoryExists("./Test directory")); } -TEST(FileSystemTest, DISABLED_CreateDirectoryRecursivelyDeleteRecursively) { +TEST(FileSystemTest, CreateDirectoryRecursivelyDeleteRecursively) { ASSERT_FALSE(DirectoryExists("./Test directory")); // Create directories recursively CreateDirectoryRecursively( @@ -111,8 +111,7 @@ TEST(FileSystemTest, DISABLED_CreateDirectoryRecursivelyDeleteRecursively) { DirectoryExists("./Test directory/Test directory 2/Test directory 3")); } -TEST(FileSystemTest, - DISABLED_TwiceCreateDirectoryRecursivelyDeleteRecursivelyOnce) { +TEST(FileSystemTest, TwiceCreateDirectoryRecursivelyDeleteRecursivelyOnce) { ASSERT_FALSE(DirectoryExists("./Test directory")); // Create directories recursively EXPECT_TRUE(CreateDirectoryRecursively( @@ -960,7 +959,7 @@ TEST(FileSystemTest, GetFileModificationTime) { EXPECT_FALSE(FileExists("./test file")); } -TEST(FileSystemTest, DISABLED_ListFiles) { +TEST(FileSystemTest, ListFiles) { ASSERT_FALSE(DirectoryExists("./Test directory")); CreateDirectory("./Test directory"); @@ -985,7 +984,7 @@ TEST(FileSystemTest, DISABLED_ListFiles) { EXPECT_FALSE(FileExists("./Test directory/test file 2")); } -TEST(FileSystemTest, DISABLED_ListFilesIncludeSubdirectory) { +TEST(FileSystemTest, ListFilesIncludeSubdirectory) { ASSERT_FALSE(DirectoryExists("./Test directory")); CreateDirectoryRecursively("./Test directory/Test directory 2/"); @@ -999,7 +998,7 @@ TEST(FileSystemTest, DISABLED_ListFilesIncludeSubdirectory) { EXPECT_FALSE(DirectoryExists("./Test directory")); } -TEST(FileSystemTest, DISABLED_ListFilesDoesNotIncludeFilesInSubdirectory) { +TEST(FileSystemTest, ListFilesDoesNotIncludeFilesInSubdirectory) { ASSERT_FALSE(DirectoryExists("./Test directory")); CreateDirectoryRecursively("./Test directory/Test directory 2/"); @@ -1021,7 +1020,7 @@ TEST(FileSystemTest, DISABLED_ListFilesDoesNotIncludeFilesInSubdirectory) { EXPECT_FALSE(DirectoryExists("./Test directory")); } -TEST(FileSystemTest, DISABLED_GetAvailableDiskSpace) { +TEST(FileSystemTest, GetAvailableDiskSpace) { // Get available disk space before directory with file creaction and after uint64_t available_space = GetAvailableDiskSpace("."); EXPECT_NE(0u, available_space); @@ -1037,16 +1036,14 @@ TEST(FileSystemTest, DISABLED_GetAvailableDiskSpace) { EXPECT_FALSE(DirectoryExists("./Test directory")); } -TEST(FileSystemTest, DISABLED_ConvertPathForURL) { +TEST(FileSystemTest, ConvertPathForURL) { std::string path = "./Test directory"; EXPECT_NE(path, ConvertPathForURL(path)); std::string path_brackets = "./Test_directory_with(brackets)"; EXPECT_NE(path_brackets, ConvertPathForURL(path)); - std::string another_path = "./Test_directory/new_directory_without_spaces"; - EXPECT_EQ(another_path, ConvertPathForURL(another_path)); } -TEST(FileSystemTest, DISABLED_DirectorySize) { +TEST(FileSystemTest, DirectorySize) { ASSERT_FALSE(DirectoryExists("./Test directory")); CreateDirectory("./Test directory"); EXPECT_TRUE(DirectoryExists("./Test directory")); @@ -1070,7 +1067,7 @@ TEST(FileSystemTest, DISABLED_DirectorySize) { EXPECT_FALSE(DirectoryExists("./Test directory")); } -TEST(FileSystemTest, DISABLED_DeleteAllContentInDirectory) { +TEST(FileSystemTest, DeleteAllContentInDirectory) { ASSERT_FALSE(DirectoryExists("./Test directory")); CreateDirectory("./Test directory"); diff --git a/src/components/utils/test/lock_posix_test.cc b/src/components/utils/test/lock_test.cc index a78659ab31..fbcc16c707 100644 --- a/src/components/utils/test/lock_posix_test.cc +++ b/src/components/utils/test/lock_test.cc @@ -39,7 +39,7 @@ namespace utils_test { using sync_primitives::Lock; -TEST(LockPosixTest, DefaultCtorTest_ExpectNonRecursiveMutexCreated) { +TEST(LockTest, DefaultCtorTest_ExpectNonRecursiveMutexCreated) { // Create Lock object Lock test_mutex; // Lock mutex @@ -50,7 +50,7 @@ TEST(LockPosixTest, DefaultCtorTest_ExpectNonRecursiveMutexCreated) { test_mutex.Release(); } -TEST(LockPosixTest, CtorTestWithFalseArgument_ExpectNonRecursiveMutexCreated) { +TEST(LockTest, CtorTestWithFalseArgument_ExpectNonRecursiveMutexCreated) { // Create Lock object Lock test_mutex(false); // Lock mutex @@ -61,7 +61,7 @@ TEST(LockPosixTest, CtorTestWithFalseArgument_ExpectNonRecursiveMutexCreated) { test_mutex.Release(); } -TEST(LockPosixTest, CtorTestWithTrueArgument_ExpectRecursiveMutexCreated) { +TEST(LockTest, CtorTestWithTrueArgument_ExpectRecursiveMutexCreated) { // Create Lock object Lock test_mutex(true); // Lock mutex @@ -73,7 +73,7 @@ TEST(LockPosixTest, CtorTestWithTrueArgument_ExpectRecursiveMutexCreated) { test_mutex.Release(); } -TEST(LockPosixTest, AcquireMutex_ExpectMutexLocked) { +TEST(LockTest, AcquireMutex_ExpectMutexLocked) { // Create Lock object (non-recursive mutex) Lock test_mutex; // Lock mutex @@ -83,7 +83,7 @@ TEST(LockPosixTest, AcquireMutex_ExpectMutexLocked) { test_mutex.Release(); } -TEST(LockPosixTest, ReleaseMutex_ExpectMutexReleased) { +TEST(LockTest, ReleaseMutex_ExpectMutexReleased) { // Create Lock object (non-recursive mutex) Lock test_mutex; // Lock mutex @@ -95,7 +95,7 @@ TEST(LockPosixTest, ReleaseMutex_ExpectMutexReleased) { test_mutex.Release(); } -TEST(LockPosixTest, TryLockNonRecursiveMutex_ExpectMutexNotLockedTwice) { +TEST(LockTest, TryLockNonRecursiveMutex_ExpectMutexNotLockedTwice) { // Create Lock object (non-recursive mutex) Lock test_mutex; // Lock mutex @@ -105,7 +105,7 @@ TEST(LockPosixTest, TryLockNonRecursiveMutex_ExpectMutexNotLockedTwice) { test_mutex.Release(); } -TEST(LockPosixTest, TryLockRecursiveMutex_ExpectMutexLockedTwice) { +TEST(LockTest, TryLockRecursiveMutex_ExpectMutexLockedTwice) { // Create Lock object (recursive mutex) Lock test_mutex(true); // Lock mutex diff --git a/src/components/utils/test/posix_thread_test.cc b/src/components/utils/test/posix_thread_test.cc deleted file mode 100644 index 0401332dab..0000000000 --- a/src/components/utils/test/posix_thread_test.cc +++ /dev/null @@ -1,329 +0,0 @@ -/* - * Copyright (c) 2015, 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 "utils/lock.h" -#include "utils/threads/thread.h" - -namespace test { -namespace components { -namespace utils_test { - -using namespace sync_primitives; -using namespace threads; - -// TODO(AByzhynar): Change this to use Gtest class to create all variables for -// every TEST_F -// TODO(AByzhynar): Add multithreading tests - -namespace { -const uint32_t MAX_SIZE = 20; -const size_t MyStackSize = 32768; -const char* threadName("test thread"); -const std::string test_thread_name("THREAD"); -sync_primitives::ConditionalVariable cond_var_; -sync_primitives::Lock test_mutex_; -}; - -// ThreadDelegate successor -class TestThreadDelegate : public threads::ThreadDelegate { - public: - TestThreadDelegate() : check_value_(false) {} - void threadMain() { - AutoLock test_lock(test_mutex_); - check_value_ = true; - cond_var_.NotifyOne(); - } - - bool check_value() const { - return check_value_; - } - - private: - bool check_value_; -}; - -TEST(PosixThreadTest, CreateThread_ExpectThreadCreated) { - // Arrange - threads::Thread* thread = NULL; - TestThreadDelegate* threadDelegate = new TestThreadDelegate(); - // Create thread - ASSERT_NO_THROW(thread = CreateThread(threadName, threadDelegate)); - EXPECT_TRUE(thread != NULL); - EXPECT_EQ(thread, threadDelegate->thread()); - EXPECT_EQ(thread->delegate(), threadDelegate); - DeleteThread(thread); - delete threadDelegate; - // Check Delegate Dtor worked successfully - EXPECT_EQ(NULL, thread->delegate()); -} - -TEST(PosixThreadTest, CheckCreatedThreadName_ExpectCorrectName) { - // Arrange - threads::Thread* thread = NULL; - threads::ThreadDelegate* threadDelegate = new TestThreadDelegate(); - // Create thread - ASSERT_NO_THROW(thread = CreateThread(threadName, threadDelegate)); - // Check thread was created with correct name - EXPECT_EQ(threadName, thread->name()); - DeleteThread(thread); - delete threadDelegate; - EXPECT_EQ(NULL, thread->delegate()); -} - -TEST(PosixThreadTest, - CheckCreatedThreadNameChangeToLongName_ExpectThreadNameReduced) { - // Arrange - threads::Thread* thread = NULL; - TestThreadDelegate* threadDelegate = new TestThreadDelegate(); - AutoLock test_lock(test_mutex_); - // Create thread - ASSERT_NO_THROW(thread = CreateThread(threadName, threadDelegate)); - thread->start(threads::ThreadOptions(threads::Thread::kMinStackSize)); - // Rename started thread. Name will be cut to 15 symbols + '\0' - // This is the limit in current POSIX thread implementation - thread->SetNameForId(thread->thread_handle(), - std::string("new thread with changed name")); - // Name must be large enough to keep 16 symbols. Read previous comment - char name[MAX_SIZE]; - int result = pthread_getname_np(thread->thread_handle(), name, sizeof(name)); - if (!result) - EXPECT_EQ(std::string("new thread with"), std::string(name)); - cond_var_.WaitFor(test_lock, 10000); - EXPECT_TRUE(threadDelegate->check_value()); - DeleteThread(thread); - delete threadDelegate; - EXPECT_EQ(NULL, thread->delegate()); -} - -TEST( - PosixThreadTest, - StartCreatedThreadWithOptionsJoinableAndMyStackSize_ExpectMyStackSizeStackAndJoinableThreadStarted) { - // Arrange - threads::Thread* thread = NULL; - TestThreadDelegate* threadDelegate = new TestThreadDelegate(); - AutoLock test_lock(test_mutex_); - // Create thread - ASSERT_NO_THROW(thread = CreateThread(threadName, threadDelegate)); - // Start thread with following options (Stack size = 32768 & thread is - // joinable) - thread->start(threads::ThreadOptions(MyStackSize)); - // Check thread is joinable - EXPECT_TRUE(thread->is_joinable()); - // Check thread stack size is 32768 - EXPECT_EQ(MyStackSize, thread->stack_size()); - cond_var_.WaitFor(test_lock, 10000); - EXPECT_TRUE(threadDelegate->check_value()); - DeleteThread(thread); - delete threadDelegate; - EXPECT_EQ(NULL, thread->delegate()); -} - -TEST( - PosixThreadTest, - StartCreatedThreadWithDefaultOptions_ExpectZeroStackAndJoinableThreadStarted) { - // Arrange - threads::Thread* thread = NULL; - TestThreadDelegate* threadDelegate = new TestThreadDelegate(); - AutoLock test_lock(test_mutex_); - // Create thread - ASSERT_NO_THROW(thread = CreateThread(threadName, threadDelegate)); - // Start thread with default options (Stack size = 0 & thread is joinable) - thread->start(threads::ThreadOptions()); - // Check thread is joinable - EXPECT_TRUE(thread->is_joinable()); - // Check thread stack size is minimum value. Stack can not be 0 - EXPECT_EQ(Thread::kMinStackSize, thread->stack_size()); - cond_var_.WaitFor(test_lock, 10000); - EXPECT_TRUE(threadDelegate->check_value()); - DeleteThread(thread); - delete threadDelegate; - EXPECT_EQ(NULL, thread->delegate()); -} - -TEST( - PosixThreadTest, - StartThreadWithZeroStackAndDetached_ExpectMinimumStackAndDetachedThreadStarted) { - // Arrange - threads::Thread* thread = NULL; - TestThreadDelegate* threadDelegate = new TestThreadDelegate(); - AutoLock test_lock(test_mutex_); - // Create thread - ASSERT_NO_THROW(thread = CreateThread(threadName, threadDelegate)); - // Start thread with default options (Stack size = 0 & thread is detached) - thread->start(threads::ThreadOptions(0, false)); - // Check thread is detached - EXPECT_FALSE(thread->is_joinable()); - // Check thread stack size is 0 - EXPECT_EQ(Thread::kMinStackSize, thread->stack_size()); - cond_var_.WaitFor(test_lock, 10000); - EXPECT_TRUE(threadDelegate->check_value()); - DeleteThread(thread); - delete threadDelegate; - EXPECT_EQ(NULL, thread->delegate()); -} - -TEST( - PosixThreadTest, - DISABLED_CheckCreatedThreadNameChangeToEmpty_ExpectThreadNameChangedToEmpty) { - // Arrange - threads::Thread* thread = NULL; - TestThreadDelegate* threadDelegate = new TestThreadDelegate(); - AutoLock test_lock(test_mutex_); - // Create thread - ASSERT_NO_THROW(thread = CreateThread(threadName, threadDelegate)); - thread->start(threads::ThreadOptions(threads::Thread::kMinStackSize)); - // Rename started thread. Name will be cut to 15 symbols + '\0' - // This is the limit in current POSIX thread implementation - thread->SetNameForId(thread->thread_handle(), std::string("")); - // Name must be large enough to keep 16 symbols. Read previous comment - char name[MAX_SIZE]; - int result = pthread_getname_np(thread->thread_handle(), name, sizeof(name)); - if (!result) { - EXPECT_EQ(std::string(""), std::string(name)); - } - cond_var_.WaitFor(test_lock, 10000); - EXPECT_TRUE(threadDelegate->check_value()); - DeleteThread(thread); - delete threadDelegate; - EXPECT_EQ(NULL, thread->delegate()); -} - -TEST(PosixThreadTest, - CheckCreatedThreadNameChangeToShortName_ExpectThreadNameChangedToShort) { - // Arrange - threads::Thread* thread = NULL; - TestThreadDelegate* threadDelegate = new TestThreadDelegate(); - AutoLock test_lock(test_mutex_); - // Create thread - ASSERT_NO_THROW(thread = CreateThread(threadName, threadDelegate)); - // Start created thread - thread->start(threads::ThreadOptions(threads::Thread::kMinStackSize)); - // Rename started thread. Name will be cut to 15 symbols + '\0' - // This is the limit in current POSIX thread implementation - thread->SetNameForId(thread->thread_handle(), test_thread_name); - // Name must be large enough to keep 16 symbols. Read previous comment - char name[MAX_SIZE]; - int result = pthread_getname_np(thread->thread_handle(), name, sizeof(name)); - if (!result) { - EXPECT_EQ(test_thread_name, std::string(name)); - } - cond_var_.WaitFor(test_lock, 10000); - EXPECT_TRUE(threadDelegate->check_value()); - DeleteThread(thread); - delete threadDelegate; - EXPECT_EQ(NULL, thread->delegate()); -} - -TEST(PosixThreadTest, StartThread_ExpectThreadStarted) { - // Arrange - threads::Thread* thread = NULL; - TestThreadDelegate* threadDelegate = new TestThreadDelegate(); - AutoLock test_lock(test_mutex_); - // Create thread - ASSERT_NO_THROW(thread = CreateThread(threadName, threadDelegate)); - // Start created thread - EXPECT_TRUE( - thread->start(threads::ThreadOptions(threads::Thread::kMinStackSize))); - cond_var_.WaitFor(test_lock, 10000); - EXPECT_TRUE(threadDelegate->check_value()); - DeleteThread(thread); - delete threadDelegate; - EXPECT_EQ(NULL, thread->delegate()); -} - -TEST(PosixThreadTest, StartOneThreadTwice_ExpectTheSameThreadStartedTwice) { - // Arrange - uint64_t thread1_id; - uint64_t thread2_id; - threads::Thread* thread = NULL; - TestThreadDelegate* threadDelegate = new TestThreadDelegate(); - AutoLock test_lock(test_mutex_); - // Create thread - ASSERT_NO_THROW(thread = CreateThread(threadName, threadDelegate)); - // Start created thread - EXPECT_TRUE( - thread->start(threads::ThreadOptions(threads::Thread::kMinStackSize))); - thread1_id = thread->CurrentId(); - thread->stop(); - // Try to start thread again - EXPECT_TRUE( - thread->start(threads::ThreadOptions(threads::Thread::kMinStackSize))); - thread2_id = thread->CurrentId(); - EXPECT_EQ(thread1_id, thread2_id); - cond_var_.WaitFor(test_lock, 10000); - EXPECT_TRUE(threadDelegate->check_value()); - DeleteThread(thread); - delete threadDelegate; - EXPECT_EQ(NULL, thread->delegate()); -} - -TEST(PosixThreadTest, - StartOneThreadAgainAfterRename_ExpectRenamedThreadStarted) { - // Arrange - uint64_t thread1_id; - uint64_t thread2_id; - threads::Thread* thread = NULL; - TestThreadDelegate* threadDelegate = new TestThreadDelegate(); - AutoLock test_lock(test_mutex_); - // Create thread - ASSERT_NO_THROW(thread = CreateThread(threadName, threadDelegate)); - // Start created thread - EXPECT_TRUE( - thread->start(threads::ThreadOptions(threads::Thread::kMinStackSize))); - thread1_id = thread->CurrentId(); - // Rename started thread. Name will be cut to 15 symbols + '\0' - // This is the limit in current POSIX thread implementation - thread->SetNameForId(thread->thread_handle(), test_thread_name); - // Name must be large enough to keep 16 symbols. Read previous comment - char name[MAX_SIZE]; - int result = pthread_getname_np(thread->thread_handle(), name, sizeof(name)); - if (!result) - EXPECT_EQ(test_thread_name, std::string(name)); - // Stop thread - thread->stop(); - EXPECT_TRUE( - thread->start(threads::ThreadOptions(threads::Thread::kMinStackSize))); - thread2_id = thread->CurrentId(); - // Expect the same thread started with the the same name - EXPECT_EQ(test_thread_name, std::string(name)); - EXPECT_EQ(thread1_id, thread2_id); - cond_var_.WaitFor(test_lock, 10000); - EXPECT_TRUE(threadDelegate->check_value()); - DeleteThread(thread); - delete threadDelegate; - EXPECT_EQ(NULL, thread->delegate()); -} - -} // namespace utils_test -} // namespace components -} // namespace test diff --git a/src/components/utils/test/system_test.cc b/src/components/utils/test/system_test.cc index 889c8fc09b..b479823aa5 100644 --- a/src/components/utils/test/system_test.cc +++ b/src/components/utils/test/system_test.cc @@ -92,8 +92,7 @@ TEST(SystemTest, AddTwoArgsToCommand_CheckOrder_ExpectOrderCorrect) { EXPECT_STREQ(object.argv()[2].c_str(), args[1]); } -TEST(SystemTest, - DISABLED_SynchronousInvokeWithExistingCommand_ExpectSuccessfull) { +TEST(SystemTest, SynchronousInvokeWithExistingCommand_ExpectSuccessfull) { const std::string test_command("./testscript.sh"); System object(test_command); diff --git a/src/components/utils/test/thread_test.cc b/src/components/utils/test/thread_test.cc new file mode 100644 index 0000000000..9d0877623c --- /dev/null +++ b/src/components/utils/test/thread_test.cc @@ -0,0 +1,180 @@ +/* + * Copyright (c) 2015, 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 "utils/lock.h" +#include "utils/threads/thread.h" + +namespace test { +namespace components { +namespace utils { + +using namespace sync_primitives; +using namespace threads; + +// TODO(AByzhynar): Add multithreading tests + +namespace { + +const size_t kThreadStackSize = 32768u; +const std::string kThreadName("Test thread"); +const uint32_t kWaitTime = 1000u; + +sync_primitives::ConditionalVariable cond_var_; +sync_primitives::Lock test_mutex_; + +} // namespace + +class TestDelegate : public threads::ThreadDelegate { + public: + TestDelegate() : check_value_(false) {} + void threadMain() { + AutoLock test_lock(test_mutex_); + check_value_ = true; + cond_var_.NotifyOne(); + } + void exitThreadMain() {} + + bool check_value() const { + return check_value_; + } + + private: + bool check_value_; +}; + +class ThreadTest : public ::testing::Test { + public: + ThreadTest() : delegate_(NULL), thread_(NULL) {} + + TestDelegate* delegate_; + Thread* thread_; + + protected: + void SetUp() { + delegate_ = new TestDelegate(); + thread_ = CreateThread(kThreadName.c_str(), delegate_); + } + + void TearDown() { + DeleteThread(thread_); + delete delegate_; + } +}; + +TEST_F(ThreadTest, CreateThread_ExpectThreadCreated) { + EXPECT_TRUE(thread_ != NULL); + EXPECT_EQ(thread_, delegate_->thread()); + EXPECT_EQ(thread_->delegate(), delegate_); +} + +TEST_F(ThreadTest, CheckCreatedThreadName_ExpectCorrectName) { + // Check thread was created with correct name + EXPECT_EQ(kThreadName, thread_->name()); +} + +TEST_F( + ThreadTest, + StartCreatedThreadWithOptionsJoinableAndMyStackSize_ExpectMyStackSizeStackAndJoinableThreadStarted) { + AutoLock test_lock(test_mutex_); + // Start thread with following options (Stack size = 32768 & thread is + // joinable) + thread_->start(threads::ThreadOptions(kThreadStackSize)); + // Check thread is joinable + EXPECT_TRUE(thread_->is_joinable()); + // Check thread stack size is 32768 + EXPECT_EQ(kThreadStackSize, thread_->stack_size()); + cond_var_.WaitFor(test_lock, kWaitTime); + EXPECT_TRUE(delegate_->check_value()); +} + +TEST_F( + ThreadTest, + StartCreatedThreadWithDefaultOptions_ExpectZeroStackAndJoinableThreadStarted) { + AutoLock test_lock(test_mutex_); + // Start thread with default options (Stack size = 0 & thread is joinable) + thread_->start(threads::ThreadOptions()); + // Check thread is joinable + EXPECT_TRUE(thread_->is_joinable()); + // Check thread stack size is minimum value. Stack can not be 0 + EXPECT_EQ(Thread::kMinStackSize, thread_->stack_size()); + cond_var_.WaitFor(test_lock, kWaitTime); + EXPECT_TRUE(delegate_->check_value()); +} + +// TODO (AN): Test should be reworked because we cannot use +// "test_lock_" and "cond_var_" global variables in detached thread +TEST_F( + ThreadTest, + DISABLED_StartThreadWithZeroStackAndDetached_ExpectMinimumStackAndDetachedThreadStarted) { + AutoLock test_lock(test_mutex_); + // Start thread with default options (Stack size = 0 & thread is detached) + thread_->start(threads::ThreadOptions(0, false)); + // Check thread is detached + EXPECT_FALSE(thread_->is_joinable()); + // Check thread stack size is 0 + EXPECT_EQ(Thread::kMinStackSize, thread_->stack_size()); + cond_var_.WaitFor(test_lock, kWaitTime); + EXPECT_TRUE(delegate_->check_value()); +} + +TEST_F(ThreadTest, StartThread_ExpectThreadStarted) { + AutoLock test_lock(test_mutex_); + // Start created thread + EXPECT_TRUE( + thread_->start(threads::ThreadOptions(threads::Thread::kMinStackSize))); + cond_var_.WaitFor(test_lock, kWaitTime); + EXPECT_TRUE(delegate_->check_value()); +} + +TEST_F(ThreadTest, StartOneThreadTwice_ExpectTheSameThreadStartedTwice) { + // Arrange + uint64_t thread1_id; + uint64_t thread2_id; + AutoLock test_lock(test_mutex_); + // Start created thread + EXPECT_TRUE( + thread_->start(threads::ThreadOptions(threads::Thread::kMinStackSize))); + thread1_id = thread_->CurrentId(); + thread_->stop(); + // Try to start thread again + EXPECT_TRUE( + thread_->start(threads::ThreadOptions(threads::Thread::kMinStackSize))); + thread2_id = thread_->CurrentId(); + EXPECT_EQ(thread1_id, thread2_id); + cond_var_.WaitFor(test_lock, kWaitTime); + EXPECT_TRUE(delegate_->check_value()); +} + +} // namespace utils +} // namespace components +} // namespace test diff --git a/src/components/utils/test/timer_test.cc b/src/components/utils/test/timer_test.cc index 940dbbe3a4..ea5c9f0b84 100644 --- a/src/components/utils/test/timer_test.cc +++ b/src/components/utils/test/timer_test.cc @@ -244,7 +244,7 @@ TEST(TimerTest, Restart_Loop_3Calls) { // Restart from call -TEST(TimerTest, DISABLED_Restart_NoLoop_FromCall) { +TEST(TimerTest, Restart_NoLoop_FromCall) { sync_primitives::AutoLock auto_lock(shot_lock); TestTask* task = new TestTaskWithStart(); timer::Timer timer(kTimerName, task); @@ -263,7 +263,7 @@ TEST(TimerTest, DISABLED_Restart_NoLoop_FromCall) { EXPECT_EQ(1u, task->calls_count()); } -TEST(TimerTest, DISABLED_Restart_Loop_FromCall) { +TEST(TimerTest, Restart_Loop_FromCall) { sync_primitives::AutoLock auto_lock(shot_lock); TestTask* task = new TestTaskWithStart(); timer::Timer timer(kTimerName, task); @@ -284,7 +284,7 @@ TEST(TimerTest, DISABLED_Restart_Loop_FromCall) { // Stop from call -TEST(TimerTest, DISABLED_Stop_Loop_FromCall) { +TEST(TimerTest, Stop_Loop_FromCall) { sync_primitives::AutoLock auto_lock(shot_lock); TestTask* task = new TestTaskWithStop(); timer::Timer timer(kTimerName, task); @@ -305,7 +305,7 @@ TEST(TimerTest, DISABLED_Stop_Loop_FromCall) { // Delete running -TEST(TimerTest, DISABLED_Delete_Running_NoLoop) { +TEST(TimerTest, Delete_Running_NoLoop) { MockTimerTask* mock_task = new MockTimerTask(); EXPECT_CALL(*mock_task, run()).Times(0); |