Make Observer.Stress test not fail under load
[folly.git] / folly / test / ScopeGuardTest.cpp
index 5310eff765af8c001d3b6520b1ceff7982c83f5f..892a4352a20b9b3c0ceec7523fae39dade24f979 100644 (file)
 #include <folly/ScopeGuard.h>
 #include <folly/Portability.h>
 
-#include <gtest/gtest.h>
 #include <glog/logging.h>
 
 #include <functional>
 #include <stdexcept>
 
+#include <folly/portability/GTest.h>
+
 using folly::ScopeGuard;
 using folly::makeGuard;
 using std::vector;
@@ -289,3 +290,22 @@ TEST(ScopeGuard, TEST_SCOPE_SUCCESS_THROW) {
   };
   EXPECT_THROW(lambda(), std::runtime_error);
 }
+
+TEST(ScopeGuard, TEST_THROWING_CLEANUP_ACTION) {
+  struct ThrowingCleanupAction {
+    explicit ThrowingCleanupAction(int& scopeExitExecuted)
+        : scopeExitExecuted_(scopeExitExecuted) {}
+    ThrowingCleanupAction(const ThrowingCleanupAction& other)
+        : scopeExitExecuted_(other.scopeExitExecuted_) {
+      throw std::runtime_error("whoa");
+    }
+    void operator()() { ++scopeExitExecuted_; }
+
+   private:
+    int& scopeExitExecuted_;
+  };
+  int scopeExitExecuted = 0;
+  ThrowingCleanupAction onExit(scopeExitExecuted);
+  EXPECT_THROW(makeGuard(onExit), std::runtime_error);
+  EXPECT_EQ(scopeExitExecuted, 1);
+}