Fix intermittent failure in concurrent_skip_list_test
authorTudor Bosman <tudorb@fb.com>
Wed, 13 Jun 2012 00:08:04 +0000 (17:08 -0700)
committerTudor Bosman <tudorb@fb.com>
Wed, 13 Jun 2012 00:45:35 +0000 (17:45 -0700)
Summary: It creates too many threads, stop when we run out of resources.

Test Plan: by hand, in a loop, in a memory-constrained environment

Reviewed By: delong.j@fb.com

FB internal diff: D493239

folly/test/ConcurrentSkipListTest.cpp

index fb558f70de79d321511409f66dabec843bf68e90..c7bb99772059e422d41eaac53aa76b46f5b03e35 100644 (file)
 #include <set>
 #include <vector>
 #include <thread>
+#include <system_error>
 
 #include <glog/logging.h>
 #include <gflags/gflags.h>
 #include "folly/ConcurrentSkipList.h"
 #include "folly/Foreach.h"
+#include "folly/String.h"
 #include "gtest/gtest.h"
 
 DEFINE_int32(num_threads, 12, "num concurrent threads to test");
@@ -210,9 +212,16 @@ void testConcurrentAdd(int numThreads) {
 
   vector<std::thread> threads;
   vector<SetType> verifiers(numThreads);
-  for (int i = 0; i < numThreads; ++i) {
-    threads.push_back(std::thread(
-          &randomAdding, 100, skipList, &verifiers[i], kMaxValue));
+  try {
+    for (int i = 0; i < numThreads; ++i) {
+      threads.push_back(std::thread(
+            &randomAdding, 100, skipList, &verifiers[i], kMaxValue));
+    }
+  } catch (const std::system_error& e) {
+    LOG(WARNING)
+      << "Caught " << exceptionStr(e)
+      << ": could only create " << threads.size() << " threads out of "
+      << numThreads;
   }
   for (int i = 0; i < threads.size(); ++i) {
     threads[i].join();
@@ -240,9 +249,16 @@ void testConcurrentRemoval(int numThreads, int maxValue) {
 
   vector<std::thread> threads;
   vector<SetType > verifiers(numThreads);
-  for (int i = 0; i < numThreads; ++i) {
-    threads.push_back(std::thread(
-          &randomRemoval, 100, skipList, &verifiers[i], maxValue));
+  try {
+    for (int i = 0; i < numThreads; ++i) {
+      threads.push_back(std::thread(
+            &randomRemoval, 100, skipList, &verifiers[i], maxValue));
+    }
+  } catch (const std::system_error& e) {
+    LOG(WARNING)
+      << "Caught " << exceptionStr(e)
+      << ": could only create " << threads.size() << " threads out of "
+      << numThreads;
   }
   FOR_EACH(t, threads) {
     (*t).join();