From db05b9ff041bfc6050072442f95a92b00ffa3699 Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Fri, 23 Aug 2013 17:49:14 -0700 Subject: [PATCH] Add TracketMutex unit test --- .../beast_core/diagnostic/beast_UnitTest.cpp | 11 +++ .../beast_core/diagnostic/beast_UnitTest.h | 7 ++ .../beast_core/thread/impl/TrackedMutex.cpp | 85 +++++++++++++++++++ 3 files changed, 103 insertions(+) diff --git a/modules/beast_core/diagnostic/beast_UnitTest.cpp b/modules/beast_core/diagnostic/beast_UnitTest.cpp index 9969903df..e889ea0a2 100644 --- a/modules/beast_core/diagnostic/beast_UnitTest.cpp +++ b/modules/beast_core/diagnostic/beast_UnitTest.cpp @@ -102,6 +102,11 @@ void UnitTest::logMessage (String const& message) m_runner->logMessage (message); } +void UnitTest::logReport (StringArray const& report) +{ + m_runner->logReport (report); +} + void UnitTest::beginTestCase (String const& name) { finishCase (); @@ -364,6 +369,12 @@ void UnitTests::logMessage (const String& message) Logger::writeToLog (message); } +void UnitTests::logReport (StringArray const& report) +{ + for (int i = 0; i < report.size (); ++i) + logMessage (report [i]); +} + void UnitTests::runTest (UnitTest& test) { try diff --git a/modules/beast_core/diagnostic/beast_UnitTest.h b/modules/beast_core/diagnostic/beast_UnitTest.h index 3fe46e793..aa5e6a542 100644 --- a/modules/beast_core/diagnostic/beast_UnitTest.h +++ b/modules/beast_core/diagnostic/beast_UnitTest.h @@ -309,6 +309,8 @@ public: */ void logMessage (const String& message); + void logReport (StringArray const& report); + private: void finishCase (); @@ -490,6 +492,11 @@ protected: */ virtual void logMessage (String const& message); + /** Logs a report about the current test progress. + This calls logMessage for each String. + */ + virtual void logReport (StringArray const& report); + private: void runTest (UnitTest& test); diff --git a/modules/beast_core/thread/impl/TrackedMutex.cpp b/modules/beast_core/thread/impl/TrackedMutex.cpp index 1b0a44f74..d37a14881 100644 --- a/modules/beast_core/thread/impl/TrackedMutex.cpp +++ b/modules/beast_core/thread/impl/TrackedMutex.cpp @@ -474,3 +474,88 @@ String TrackedMutex::makeSourceLocation (char const* fileName, int lineNumber) n return sourceLocation; } + +//============================================================================== + +namespace detail +{ + +class TrackedMutexUnitTests : public UnitTest +{ +public: + typedef TrackedMutexType Mutex; + + struct LockingThread : public Thread + { + Mutex& m_m1; + Mutex& m_m2; + WaitableEvent m_start; + + explicit LockingThread (String name, Mutex& m1, Mutex& m2) + : Thread (name) + , m_m1 (m1) + , m_m2 (m2) + { + startThread (); + } + + void waitForStart () + { + m_start.wait (); + } + + void run () + { + Mutex::ScopedLockType l2 (m_m2, __FILE__, __LINE__); + { + Mutex::ScopedLockType l1 (m_m1, __FILE__, __LINE__); + m_start.signal (); + { + Mutex::ScopedUnlockType ul1 (m_m1, __FILE__, __LINE__); + this->wait (); + } + } + } + }; + + //-------------------------------------------------------------------------- + + void report (String name) + { + beginTestCase (name); + StringArray report; + TrackedMutex::generateGlobalBlockedReport (report); + logReport (report); + pass (); + } + + void runTest () + { + Mutex m1 ("M1", __FILE__, __LINE__); + Mutex m2 ("M2", __FILE__, __LINE__); + + { + Mutex::ScopedLockType l1 (m1, __FILE__, __LINE__); + LockingThread t1 ("T1", m1, m2); + { + Mutex::ScopedUnlockType ul1 (m1, __FILE__, __LINE__); + t1.waitForStart (); + } + report ("#1"); + { + t1.notify (); + Mutex::ScopedUnlockType ul1 (m1, __FILE__, __LINE__); + t1.waitForThreadToExit (); + } + } + } + + TrackedMutexUnitTests () : UnitTest ("TrackedMutex", "beast", runManual) + { + } +}; + +static TrackedMutexUnitTests trackedMutexUnitTests; + +} // namespace detail +