[libcxx] r245918 - Refactor and fix more flaky shared_mutex tests

Eric Fiselier via cfe-commits cfe-commits at lists.llvm.org
Mon Aug 24 18:28:53 PDT 2015


Author: ericwf
Date: Mon Aug 24 20:28:52 2015
New Revision: 245918

URL: http://llvm.org/viewvc/llvm-project?rev=245918&view=rev
Log:
Refactor and fix more flaky shared_mutex tests

Modified:
    libcxx/trunk/test/std/thread/thread.mutex/thread.mutex.requirements/thread.sharedtimedmutex.requirements/thread.sharedtimedmutex.class/lock.pass.cpp
    libcxx/trunk/test/std/thread/thread.mutex/thread.mutex.requirements/thread.sharedtimedmutex.requirements/thread.sharedtimedmutex.class/lock_shared.pass.cpp
    libcxx/trunk/test/std/thread/thread.mutex/thread.mutex.requirements/thread.sharedtimedmutex.requirements/thread.sharedtimedmutex.class/try_lock_for.pass.cpp
    libcxx/trunk/test/std/thread/thread.mutex/thread.mutex.requirements/thread.sharedtimedmutex.requirements/thread.sharedtimedmutex.class/try_lock_shared_for.pass.cpp
    libcxx/trunk/test/std/thread/thread.mutex/thread.mutex.requirements/thread.sharedtimedmutex.requirements/thread.sharedtimedmutex.class/try_lock_shared_until.pass.cpp
    libcxx/trunk/test/std/thread/thread.mutex/thread.mutex.requirements/thread.sharedtimedmutex.requirements/thread.sharedtimedmutex.class/try_lock_until.pass.cpp

Modified: libcxx/trunk/test/std/thread/thread.mutex/thread.mutex.requirements/thread.sharedtimedmutex.requirements/thread.sharedtimedmutex.class/lock.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/thread/thread.mutex/thread.mutex.requirements/thread.sharedtimedmutex.requirements/thread.sharedtimedmutex.class/lock.pass.cpp?rev=245918&r1=245917&r2=245918&view=diff
==============================================================================
--- libcxx/trunk/test/std/thread/thread.mutex/thread.mutex.requirements/thread.sharedtimedmutex.requirements/thread.sharedtimedmutex.class/lock.pass.cpp (original)
+++ libcxx/trunk/test/std/thread/thread.mutex/thread.mutex.requirements/thread.sharedtimedmutex.requirements/thread.sharedtimedmutex.class/lock.pass.cpp Mon Aug 24 20:28:52 2015
@@ -21,6 +21,8 @@
 #include <cstdlib>
 #include <cassert>
 
+#include "test_macros.h"
+
 std::shared_timed_mutex m;
 
 typedef std::chrono::system_clock Clock;
@@ -29,6 +31,19 @@ typedef Clock::duration duration;
 typedef std::chrono::milliseconds ms;
 typedef std::chrono::nanoseconds ns;
 
+
+ms WaitTime = ms(250);
+
+// Thread sanitizer causes more overhead and will sometimes cause this test
+// to fail. To prevent this we give Thread sanitizer more time to complete the
+// test.
+#if !TEST_HAS_FEATURE(thread_sanitizer)
+ms Tolerance = ms(50);
+#else
+ms Tolerance = ms(100);
+#endif
+
+
 void f()
 {
     time_point t0 = Clock::now();

Modified: libcxx/trunk/test/std/thread/thread.mutex/thread.mutex.requirements/thread.sharedtimedmutex.requirements/thread.sharedtimedmutex.class/lock_shared.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/thread/thread.mutex/thread.mutex.requirements/thread.sharedtimedmutex.requirements/thread.sharedtimedmutex.class/lock_shared.pass.cpp?rev=245918&r1=245917&r2=245918&view=diff
==============================================================================
--- libcxx/trunk/test/std/thread/thread.mutex/thread.mutex.requirements/thread.sharedtimedmutex.requirements/thread.sharedtimedmutex.class/lock_shared.pass.cpp (original)
+++ libcxx/trunk/test/std/thread/thread.mutex/thread.mutex.requirements/thread.sharedtimedmutex.requirements/thread.sharedtimedmutex.class/lock_shared.pass.cpp Mon Aug 24 20:28:52 2015
@@ -22,6 +22,8 @@
 #include <cstdlib>
 #include <cassert>
 
+#include "test_macros.h"
+
 std::shared_timed_mutex m;
 
 typedef std::chrono::system_clock Clock;
@@ -30,14 +32,27 @@ typedef Clock::duration duration;
 typedef std::chrono::milliseconds ms;
 typedef std::chrono::nanoseconds ns;
 
+
+ms WaitTime = ms(250);
+
+// Thread sanitizer causes more overhead and will sometimes cause this test
+// to fail. To prevent this we give Thread sanitizer more time to complete the
+// test.
+#if !TEST_HAS_FEATURE(thread_sanitizer)
+ms Tolerance = ms(50);
+#else
+ms Tolerance = ms(100);
+#endif
+
+
 void f()
 {
     time_point t0 = Clock::now();
     m.lock_shared();
     time_point t1 = Clock::now();
     m.unlock_shared();
-    ns d = t1 - t0 - ms(250);
-    assert(d < ms(50));  // within 50ms
+    ns d = t1 - t0 - WaitTime;
+    assert(d < Tolerance);  // within tolerance
 }
 
 void g()
@@ -47,7 +62,7 @@ void g()
     time_point t1 = Clock::now();
     m.unlock_shared();
     ns d = t1 - t0;
-    assert(d < ms(50));  // within 50ms
+    assert(d < Tolerance);  // within tolerance
 }
 
 
@@ -57,7 +72,7 @@ int main()
     std::vector<std::thread> v;
     for (int i = 0; i < 5; ++i)
         v.push_back(std::thread(f));
-    std::this_thread::sleep_for(ms(250));
+    std::this_thread::sleep_for(WaitTime);
     m.unlock();
     for (auto& t : v)
         t.join();
@@ -65,7 +80,7 @@ int main()
     for (auto& t : v)
         t = std::thread(g);
     std::thread q(f);
-    std::this_thread::sleep_for(ms(250));
+    std::this_thread::sleep_for(WaitTime);
     m.unlock_shared();
     for (auto& t : v)
         t.join();

Modified: libcxx/trunk/test/std/thread/thread.mutex/thread.mutex.requirements/thread.sharedtimedmutex.requirements/thread.sharedtimedmutex.class/try_lock_for.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/thread/thread.mutex/thread.mutex.requirements/thread.sharedtimedmutex.requirements/thread.sharedtimedmutex.class/try_lock_for.pass.cpp?rev=245918&r1=245917&r2=245918&view=diff
==============================================================================
--- libcxx/trunk/test/std/thread/thread.mutex/thread.mutex.requirements/thread.sharedtimedmutex.requirements/thread.sharedtimedmutex.class/try_lock_for.pass.cpp (original)
+++ libcxx/trunk/test/std/thread/thread.mutex/thread.mutex.requirements/thread.sharedtimedmutex.requirements/thread.sharedtimedmutex.class/try_lock_for.pass.cpp Mon Aug 24 20:28:52 2015
@@ -22,6 +22,8 @@
 #include <cstdlib>
 #include <cassert>
 
+#include "test_macros.h"
+
 std::shared_timed_mutex m;
 
 typedef std::chrono::steady_clock Clock;
@@ -30,23 +32,36 @@ typedef Clock::duration duration;
 typedef std::chrono::milliseconds ms;
 typedef std::chrono::nanoseconds ns;
 
+
+ms WaitTime = ms(250);
+
+// Thread sanitizer causes more overhead and will sometimes cause this test
+// to fail. To prevent this we give Thread sanitizer more time to complete the
+// test.
+#if !TEST_HAS_FEATURE(thread_sanitizer)
+ms Tolerance = ms(50);
+#else
+ms Tolerance = ms(100);
+#endif
+
+
 void f1()
 {
     time_point t0 = Clock::now();
-    assert(m.try_lock_for(ms(300)) == true);
+    assert(m.try_lock_for(WaitTime + Tolerance) == true);
     time_point t1 = Clock::now();
     m.unlock();
-    ns d = t1 - t0 - ms(250);
-    assert(d < ms(50));  // within 50ms
+    ns d = t1 - t0 - WaitTime;
+    assert(d < Tolerance);  // within tolerance
 }
 
 void f2()
 {
     time_point t0 = Clock::now();
-    assert(m.try_lock_for(ms(250)) == false);
+    assert(m.try_lock_for(WaitTime) == false);
     time_point t1 = Clock::now();
-    ns d = t1 - t0 - ms(250);
-    assert(d < ms(50));  // within 50ms
+    ns d = t1 - t0 - WaitTime;
+    assert(d < Tolerance);  // within tolerance
 }
 
 int main()
@@ -54,14 +69,14 @@ int main()
     {
         m.lock();
         std::thread t(f1);
-        std::this_thread::sleep_for(ms(250));
+        std::this_thread::sleep_for(WaitTime);
         m.unlock();
         t.join();
     }
     {
         m.lock();
         std::thread t(f2);
-        std::this_thread::sleep_for(ms(300));
+        std::this_thread::sleep_for(WaitTime + Tolerance);
         m.unlock();
         t.join();
     }

Modified: libcxx/trunk/test/std/thread/thread.mutex/thread.mutex.requirements/thread.sharedtimedmutex.requirements/thread.sharedtimedmutex.class/try_lock_shared_for.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/thread/thread.mutex/thread.mutex.requirements/thread.sharedtimedmutex.requirements/thread.sharedtimedmutex.class/try_lock_shared_for.pass.cpp?rev=245918&r1=245917&r2=245918&view=diff
==============================================================================
--- libcxx/trunk/test/std/thread/thread.mutex/thread.mutex.requirements/thread.sharedtimedmutex.requirements/thread.sharedtimedmutex.class/try_lock_shared_for.pass.cpp (original)
+++ libcxx/trunk/test/std/thread/thread.mutex/thread.mutex.requirements/thread.sharedtimedmutex.requirements/thread.sharedtimedmutex.class/try_lock_shared_for.pass.cpp Mon Aug 24 20:28:52 2015
@@ -23,6 +23,8 @@
 #include <cstdlib>
 #include <cassert>
 
+#include "test_macros.h"
+
 std::shared_timed_mutex m;
 
 typedef std::chrono::steady_clock Clock;
@@ -31,23 +33,35 @@ typedef Clock::duration duration;
 typedef std::chrono::milliseconds ms;
 typedef std::chrono::nanoseconds ns;
 
+ms WaitTime = ms(250);
+
+// Thread sanitizer causes more overhead and will sometimes cause this test
+// to fail. To prevent this we give Thread sanitizer more time to complete the
+// test.
+#if !TEST_HAS_FEATURE(thread_sanitizer)
+ms Tolerance = ms(50);
+#else
+ms Tolerance = ms(100);
+#endif
+
+
 void f1()
 {
     time_point t0 = Clock::now();
-    assert(m.try_lock_shared_for(ms(300)) == true);
+    assert(m.try_lock_shared_for(WaitTime + Tolerance) == true);
     time_point t1 = Clock::now();
     m.unlock_shared();
-    ns d = t1 - t0 - ms(250);
-    assert(d < ms(50));  // within 50ms
+    ns d = t1 - t0 - WaitTime;
+    assert(d < Tolerance);  // within 50ms
 }
 
 void f2()
 {
     time_point t0 = Clock::now();
-    assert(m.try_lock_shared_for(ms(250)) == false);
+    assert(m.try_lock_shared_for(WaitTime) == false);
     time_point t1 = Clock::now();
-    ns d = t1 - t0 - ms(250);
-    assert(d < ms(50));  // within 50ms
+    ns d = t1 - t0 - WaitTime;
+    assert(d < Tolerance);  // within 50ms
 }
 
 int main()
@@ -57,7 +71,7 @@ int main()
         std::vector<std::thread> v;
         for (int i = 0; i < 5; ++i)
             v.push_back(std::thread(f1));
-        std::this_thread::sleep_for(ms(250));
+        std::this_thread::sleep_for(WaitTime);
         m.unlock();
         for (auto& t : v)
             t.join();
@@ -67,7 +81,7 @@ int main()
         std::vector<std::thread> v;
         for (int i = 0; i < 5; ++i)
             v.push_back(std::thread(f2));
-        std::this_thread::sleep_for(ms(300));
+        std::this_thread::sleep_for(WaitTime + Tolerance);
         m.unlock();
         for (auto& t : v)
             t.join();

Modified: libcxx/trunk/test/std/thread/thread.mutex/thread.mutex.requirements/thread.sharedtimedmutex.requirements/thread.sharedtimedmutex.class/try_lock_shared_until.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/thread/thread.mutex/thread.mutex.requirements/thread.sharedtimedmutex.requirements/thread.sharedtimedmutex.class/try_lock_shared_until.pass.cpp?rev=245918&r1=245917&r2=245918&view=diff
==============================================================================
--- libcxx/trunk/test/std/thread/thread.mutex/thread.mutex.requirements/thread.sharedtimedmutex.requirements/thread.sharedtimedmutex.class/try_lock_shared_until.pass.cpp (original)
+++ libcxx/trunk/test/std/thread/thread.mutex/thread.mutex.requirements/thread.sharedtimedmutex.requirements/thread.sharedtimedmutex.class/try_lock_shared_until.pass.cpp Mon Aug 24 20:28:52 2015
@@ -23,6 +23,8 @@
 #include <cstdlib>
 #include <cassert>
 
+#include "test_macros.h"
+
 std::shared_timed_mutex m;
 
 typedef std::chrono::steady_clock Clock;
@@ -31,23 +33,35 @@ typedef Clock::duration duration;
 typedef std::chrono::milliseconds ms;
 typedef std::chrono::nanoseconds ns;
 
+ms WaitTime = ms(250);
+
+// Thread sanitizer causes more overhead and will sometimes cause this test
+// to fail. To prevent this we give Thread sanitizer more time to complete the
+// test.
+#if !TEST_HAS_FEATURE(thread_sanitizer)
+ms Tolerance = ms(50);
+#else
+ms Tolerance = ms(100);
+#endif
+
+
 void f1()
 {
     time_point t0 = Clock::now();
-    assert(m.try_lock_shared_until(Clock::now() + ms(300)) == true);
+    assert(m.try_lock_shared_until(Clock::now() + WaitTime + Tolerance) == true);
     time_point t1 = Clock::now();
     m.unlock_shared();
-    ns d = t1 - t0 - ms(250);
-    assert(d < ms(50));  // within 50ms
+    ns d = t1 - t0 - WaitTime;
+    assert(d < Tolerance);  // within 50ms
 }
 
 void f2()
 {
     time_point t0 = Clock::now();
-    assert(m.try_lock_shared_until(Clock::now() + ms(250)) == false);
+    assert(m.try_lock_shared_until(Clock::now() + WaitTime) == false);
     time_point t1 = Clock::now();
-    ns d = t1 - t0 - ms(250);
-    assert(d < ms(50));  // within 50ms
+    ns d = t1 - t0 - WaitTime;
+    assert(d < Tolerance);  // within tolerance
 }
 
 int main()
@@ -57,7 +71,7 @@ int main()
         std::vector<std::thread> v;
         for (int i = 0; i < 5; ++i)
             v.push_back(std::thread(f1));
-        std::this_thread::sleep_for(ms(250));
+        std::this_thread::sleep_for(WaitTime);
         m.unlock();
         for (auto& t : v)
             t.join();
@@ -67,7 +81,7 @@ int main()
         std::vector<std::thread> v;
         for (int i = 0; i < 5; ++i)
             v.push_back(std::thread(f2));
-        std::this_thread::sleep_for(ms(300));
+        std::this_thread::sleep_for(WaitTime + Tolerance);
         m.unlock();
         for (auto& t : v)
             t.join();

Modified: libcxx/trunk/test/std/thread/thread.mutex/thread.mutex.requirements/thread.sharedtimedmutex.requirements/thread.sharedtimedmutex.class/try_lock_until.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/thread/thread.mutex/thread.mutex.requirements/thread.sharedtimedmutex.requirements/thread.sharedtimedmutex.class/try_lock_until.pass.cpp?rev=245918&r1=245917&r2=245918&view=diff
==============================================================================
--- libcxx/trunk/test/std/thread/thread.mutex/thread.mutex.requirements/thread.sharedtimedmutex.requirements/thread.sharedtimedmutex.class/try_lock_until.pass.cpp (original)
+++ libcxx/trunk/test/std/thread/thread.mutex/thread.mutex.requirements/thread.sharedtimedmutex.requirements/thread.sharedtimedmutex.class/try_lock_until.pass.cpp Mon Aug 24 20:28:52 2015
@@ -32,23 +32,36 @@ typedef Clock::duration duration;
 typedef std::chrono::milliseconds ms;
 typedef std::chrono::nanoseconds ns;
 
+
+ms WaitTime = ms(250);
+
+// Thread sanitizer causes more overhead and will sometimes cause this test
+// to fail. To prevent this we give Thread sanitizer more time to complete the
+// test.
+#if !TEST_HAS_FEATURE(thread_sanitizer)
+ms Tolerance = ms(50);
+#else
+ms Tolerance = ms(100);
+#endif
+
+
 void f1()
 {
     time_point t0 = Clock::now();
-    assert(m.try_lock_until(Clock::now() + ms(300)) == true);
+    assert(m.try_lock_until(Clock::now() + WaitTime + Tolerance) == true);
     time_point t1 = Clock::now();
     m.unlock();
-    ns d = t1 - t0 - ms(250);
-    assert(d < ms(50));  // within 50ms
+    ns d = t1 - t0 - WaitTime;
+    assert(d < Tolerance);  // within tolerance
 }
 
 void f2()
 {
     time_point t0 = Clock::now();
-    assert(m.try_lock_until(Clock::now() + ms(250)) == false);
+    assert(m.try_lock_until(Clock::now() + WaitTime) == false);
     time_point t1 = Clock::now();
-    ns d = t1 - t0 - ms(250);
-    assert(d < ms(50));  // within 50ms
+    ns d = t1 - t0 - WaitTime;
+    assert(d < Tolerance);  // within tolerance
 }
 
 int main()
@@ -56,14 +69,14 @@ int main()
     {
         m.lock();
         std::thread t(f1);
-        std::this_thread::sleep_for(ms(250));
+        std::this_thread::sleep_for(WaitTime);
         m.unlock();
         t.join();
     }
     {
         m.lock();
         std::thread t(f2);
-        std::this_thread::sleep_for(ms(300));
+        std::this_thread::sleep_for(WaitTime + Tolerance);
         m.unlock();
         t.join();
     }




More information about the cfe-commits mailing list