diff options
author | Henning Baldersheim <balder@yahoo-inc.com> | 2021-01-07 17:09:16 +0000 |
---|---|---|
committer | Henning Baldersheim <balder@yahoo-inc.com> | 2021-01-07 17:09:16 +0000 |
commit | 1969e5c0ff6ff19fcd87d12b6c738f1d49e0035b (patch) | |
tree | fdc3b4f044462e5f0f89098af4fec1045cae3537 /fastos/src/tests | |
parent | 8f0b0b284344dee93d6e4a86684b54da25315bc6 (diff) |
No need to test the std::mutex
Diffstat (limited to 'fastos/src/tests')
-rw-r--r-- | fastos/src/tests/CMakeLists.txt | 7 | ||||
-rw-r--r-- | fastos/src/tests/thread_mutex_test.cpp | 194 | ||||
-rw-r--r-- | fastos/src/tests/thread_stats_test.cpp | 27 | ||||
-rw-r--r-- | fastos/src/tests/thread_test_base.hpp | 11 |
4 files changed, 15 insertions, 224 deletions
diff --git a/fastos/src/tests/CMakeLists.txt b/fastos/src/tests/CMakeLists.txt index cc4928cc7d0..89b820d1482 100644 --- a/fastos/src/tests/CMakeLists.txt +++ b/fastos/src/tests/CMakeLists.txt @@ -26,13 +26,6 @@ vespa_add_executable(fastos_thread_sleep_test_app TEST fastos ) vespa_add_test(NAME fastos_thread_sleep_test_app NO_VALGRIND COMMAND fastos_thread_sleep_test_app) -vespa_add_executable(fastos_thread_mutex_test_app TEST - SOURCES - thread_mutex_test.cpp - DEPENDS - fastos -) -vespa_add_test(NAME fastos_thread_mutex_test_app NO_VALGRIND COMMAND fastos_thread_mutex_test_app) vespa_add_executable(fastos_thread_joinwait_test_app TEST SOURCES thread_joinwait_test.cpp diff --git a/fastos/src/tests/thread_mutex_test.cpp b/fastos/src/tests/thread_mutex_test.cpp deleted file mode 100644 index 6d3f8c3c5f0..00000000000 --- a/fastos/src/tests/thread_mutex_test.cpp +++ /dev/null @@ -1,194 +0,0 @@ -// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. - -#include "tests.h" -#include "job.h" -#include "thread_test_base.hpp" - -#define MUTEX_TEST_THREADS 6 -#define MAX_THREADS 7 - -class Thread_Mutex_Test : public ThreadTestBase -{ - int Main () override; - - void MutexTest (bool usingMutex) - { - if(usingMutex) - TestHeader("Mutex Test"); - else - TestHeader("Not Using Mutex Test"); - - - FastOS_ThreadPool *pool = new FastOS_ThreadPool(128*1024, MAX_THREADS); - - if(Progress(pool != nullptr, "Allocating ThreadPool")) - { - int i; - Job jobs[MUTEX_TEST_THREADS]; - std::mutex *myMutex=nullptr; - - if(usingMutex) { - myMutex = new std::mutex; - } - - for(i=0; i<MUTEX_TEST_THREADS; i++) - { - jobs[i].code = INCREASE_NUMBER; - jobs[i].mutex = myMutex; - } - - number = 0; - - for(i=0; i<MUTEX_TEST_THREADS; i++) - { - bool rc = (nullptr != pool->NewThread(this, - static_cast<void *>(&jobs[i]))); - Progress(rc, "Creating Thread with%s mutex", (usingMutex ? "" : "out")); - }; - - WaitForThreadsToFinish(jobs, MUTEX_TEST_THREADS); - - - for(i=0; i<MUTEX_TEST_THREADS; i++) - { - Progress(true, "Thread returned with resultcode %d", jobs[i].result); - } - - bool wasOk=true; - int concurrentHits=0; - - for(i=0; i<MUTEX_TEST_THREADS; i++) - { - int result = jobs[i].result; - - if(usingMutex) - { - if((result % INCREASE_NUMBER_AMOUNT) != 0) - { - wasOk = false; - Progress(false, "Mutex locking did not work (%d).", result); - break; - } - } - else - { - if((result != 0) && - (result != INCREASE_NUMBER_AMOUNT*MUTEX_TEST_THREADS) && - (result % INCREASE_NUMBER_AMOUNT) == 0) - { - if((++concurrentHits) == 2) - { - wasOk = false; - Progress(false, "Very unlikely that threads are running " - "concurrently (%d)", jobs[i].result); - break; - } - } - } - } - - if(wasOk) - { - if(usingMutex) - { - Progress(true, "Using the mutex, the returned numbers were alligned."); - } - else - { - Progress(true, "Returned numbers were not alligned. " - "This was the expected result."); - } - } - - if(myMutex != nullptr) - delete(myMutex); - - Progress(true, "Closing threadpool..."); - pool->Close(); - - Progress(true, "Deleting threadpool..."); - delete(pool); - } - PrintSeparator(); - } - - void TryLockTest () - { - TestHeader("Mutex TryLock Test"); - - FastOS_ThreadPool pool(128*1024); - Job job; - std::mutex mtx; - - job.code = HOLD_MUTEX_FOR2SEC; - job.result = -1; - job.mutex = &mtx; - job.ownThread = pool.NewThread(this, - static_cast<void *>(&job)); - - Progress(job.ownThread !=nullptr, "Creating thread"); - - if(job.ownThread != nullptr) - { - bool lockrc; - - std::this_thread::sleep_for(1s); - - for(int i=0; i<5; i++) - { - lockrc = mtx.try_lock(); - Progress(!lockrc, "We should not get the mutex lock just yet (%s)", - lockrc ? "got it" : "didn't get it"); - if(lockrc) { - mtx.unlock(); - break; - } - } - - std::this_thread::sleep_for(2s); - - lockrc = mtx.try_lock(); - Progress(lockrc, "We should get the mutex lock now (%s)", - lockrc ? "got it" : "didn't get it"); - - if(lockrc) - mtx.unlock(); - - Progress(true, "Attempting to do normal lock..."); - mtx.lock(); - Progress(true, "Got lock. Attempt to do normal unlock..."); - mtx.unlock(); - Progress(true, "Unlock OK."); - } - - Progress(true, "Waiting for threads to finish using pool.Close()..."); - pool.Close(); - Progress(true, "Pool closed."); - - PrintSeparator(); - } - -}; - -int Thread_Mutex_Test::Main () -{ - printf("grep for the string '%s' to detect failures.\n\n", failString); - time_t before = time(0); - - MutexTest(true); - { time_t now = time(0); printf("[%ld seconds]\n", now-before); before = now; } - MutexTest(false); - { time_t now = time(0); printf("[%ld seconds]\n", now-before); before = now; } - TryLockTest(); - { time_t now = time(0); printf("[%ld seconds]\n", now-before); before = now; } - - printf("END OF TEST (%s)\n", _argv[0]); - return allWasOk() ? 0 : 1; -} - -int main (int argc, char **argv) -{ - Thread_Mutex_Test app; - setvbuf(stdout, nullptr, _IOLBF, 8192); - return app.Entry(argc, argv); -} diff --git a/fastos/src/tests/thread_stats_test.cpp b/fastos/src/tests/thread_stats_test.cpp index 7753ad5686a..ff868383a5c 100644 --- a/fastos/src/tests/thread_stats_test.cpp +++ b/fastos/src/tests/thread_stats_test.cpp @@ -18,18 +18,14 @@ class Thread_Stats_Test : public ThreadTestBase Job job[2]; inactiveThreads = pool.GetNumInactiveThreads(); - Progress(inactiveThreads == 0, "Initial inactive threads = %d", - inactiveThreads); + Progress(inactiveThreads == 0, "Initial inactive threads = %d", inactiveThreads); activeThreads = pool.GetNumActiveThreads(); - Progress(activeThreads == 0, "Initial active threads = %d", - activeThreads); + Progress(activeThreads == 0, "Initial active threads = %d", activeThreads); startedThreads = pool.GetNumStartedThreads(); - Progress(startedThreads == 0, "Initial started threads = %d", - startedThreads); + Progress(startedThreads == 0, "Initial started threads = %d", startedThreads); job[0].code = WAIT_FOR_BREAK_FLAG; - job[0].ownThread = pool.NewThread(this, - static_cast<void *>(&job[0])); + job[0].ownThread = pool.NewThread(this, static_cast<void *>(&job[0])); inactiveThreads = pool.GetNumInactiveThreads(); Progress(inactiveThreads == 0, "Inactive threads = %d", inactiveThreads); @@ -39,8 +35,7 @@ class Thread_Stats_Test : public ThreadTestBase Progress(startedThreads == 1, "Started threads = %d", startedThreads); job[1].code = WAIT_FOR_BREAK_FLAG; - job[1].ownThread = pool.NewThread(this, - static_cast<void *>(&job[1])); + job[1].ownThread = pool.NewThread(this, static_cast<void *>(&job[1])); inactiveThreads = pool.GetNumInactiveThreads(); Progress(inactiveThreads == 0, "Inactive threads = %d", inactiveThreads); @@ -53,7 +48,8 @@ class Thread_Stats_Test : public ThreadTestBase job[0].ownThread->SetBreakFlag(); job[1].ownThread->SetBreakFlag(); - std::this_thread::sleep_for(3s); + job[0].ownThread->Join(); + job[1].ownThread->Join(); inactiveThreads = pool.GetNumInactiveThreads(); Progress(inactiveThreads == 2, "Inactive threads = %d", inactiveThreads); @@ -68,8 +64,6 @@ class Thread_Stats_Test : public ThreadTestBase job[0].code = WAIT_FOR_BREAK_FLAG; job[0].ownThread = pool.NewThread(this, static_cast<void *>(&job[0])); - std::this_thread::sleep_for(1s); - inactiveThreads = pool.GetNumInactiveThreads(); Progress(inactiveThreads == 1, "Inactive threads = %d", inactiveThreads); activeThreads = pool.GetNumActiveThreads(); @@ -80,8 +74,6 @@ class Thread_Stats_Test : public ThreadTestBase job[1].code = WAIT_FOR_BREAK_FLAG; job[1].ownThread = pool.NewThread(this, static_cast<void *>(&job[1])); - std::this_thread::sleep_for(1s); - inactiveThreads = pool.GetNumInactiveThreads(); Progress(inactiveThreads == 0, "Inactive threads = %d", inactiveThreads); activeThreads = pool.GetNumActiveThreads(); @@ -93,7 +85,9 @@ class Thread_Stats_Test : public ThreadTestBase job[0].ownThread->SetBreakFlag(); job[1].ownThread->SetBreakFlag(); - std::this_thread::sleep_for(3s); + job[0].ownThread->Join(); + job[1].ownThread->Join(); + std::this_thread::sleep_for(1s); inactiveThreads = pool.GetNumInactiveThreads(); Progress(inactiveThreads == 2, "Inactive threads = %d", inactiveThreads); @@ -102,7 +96,6 @@ class Thread_Stats_Test : public ThreadTestBase startedThreads = pool.GetNumStartedThreads(); Progress(startedThreads == 4, "Started threads = %d", startedThreads); - pool.Close(); Progress(true, "Pool closed."); diff --git a/fastos/src/tests/thread_test_base.hpp b/fastos/src/tests/thread_test_base.hpp index c4f7ed76ea7..60a3dfedb6f 100644 --- a/fastos/src/tests/thread_test_base.hpp +++ b/fastos/src/tests/thread_test_base.hpp @@ -20,7 +20,7 @@ public: : printMutex() { } - virtual ~ThreadTestBase() {}; + virtual ~ThreadTestBase() {} void PrintProgress (char *string) override { @@ -48,7 +48,7 @@ public: } } - std::this_thread::sleep_for(500ms); + std::this_thread::sleep_for(1us); if(threadsFinished) break; @@ -110,7 +110,7 @@ void ThreadTestBase::Run (FastOS_ThreadInterface *thread, void *arg) number = number + 2; if(i == sleepOn) - std::this_thread::sleep_for(1s); + std::this_thread::sleep_for(1ms); } guard = std::unique_lock<std::mutex>(); @@ -124,10 +124,9 @@ void ThreadTestBase::Run (FastOS_ThreadInterface *thread, void *arg) { for(;;) { - std::this_thread::sleep_for(1s); + std::this_thread::sleep_for(1us); - if(thread->GetBreakFlag()) - { + if (thread->GetBreakFlag()) { Progress(true, "Thread %p got breakflag", thread); break; } |