19 #include <glog/logging.h> 45 TEST(ScopeGuard, DifferentWaysToBind) {
94 auto g =
makeGuard([v]()
mutable { v.push_back(6); });
133 TEST(ScopeGuard, GuardException) {
136 [] {
throw std::runtime_error(
"dtors should never throw!"); }),
137 "dtors should never throw!");
202 bool cleanupOccurred =
false;
210 throw std::runtime_error(
"throwing an expected error");
212 throw "never throw raw strings";
214 }
catch (
const std::runtime_error&) {
223 TEST(ScopeGuard, TryCatchFinally) {
229 TEST(ScopeGuard, TEST_SCOPE_EXIT) {
245 auto e = std::current_exception();
254 }
catch (
const std::exception& ex) {
255 LOG(FATAL) <<
"Unexpected exception: " << ex.what();
260 TEST(ScopeGuard, TEST_SCOPE_FAILURE2) {
263 throw std::runtime_error(
"test");
269 bool scopeFailExecuted =
false;
270 bool scopeSuccessExecuted =
false;
274 scopeFailExecuted =
true;
277 scopeSuccessExecuted =
true;
282 throw std::runtime_error(
"throwing an expected error");
284 throw "never throw raw strings";
286 }
catch (
const std::runtime_error&) {
292 EXPECT_EQ(expectFail, scopeFailExecuted);
293 EXPECT_EQ(!expectFail, scopeSuccessExecuted);
296 TEST(ScopeGuard, TEST_SCOPE_FAIL_AND_SCOPE_SUCCESS) {
302 TEST(ScopeGuard, TEST_SCOPE_SUCCESS_THROW) {
305 throw std::runtime_error(
"ehm");
311 TEST(ScopeGuard, TEST_THROWING_CLEANUP_ACTION) {
312 struct ThrowingCleanupAction {
314 explicit ThrowingCleanupAction(
int& scopeExitExecuted)
315 : scopeExitExecuted_(scopeExitExecuted) {}
316 [[noreturn]] ThrowingCleanupAction(
const ThrowingCleanupAction& other)
317 : scopeExitExecuted_(other.scopeExitExecuted_) {
318 throw std::runtime_error(
"whoa");
322 ++scopeExitExecuted_;
326 int& scopeExitExecuted_;
328 int scopeExitExecuted = 0;
329 ThrowingCleanupAction onExit(scopeExitExecuted);
#define EXPECT_THROW(statement, expected_exception)
void testScopeFailAndScopeSuccess(ErrorBehavior error, bool expectFail)
#define EXPECT_EQ(val1, val2)
static uint64_t test(std::string name, bool fc_, bool dedicated_, bool tc_, bool syncops_, uint64_t base)
TEST(ScopeGuard, DifferentWaysToBind)
requires And< SemiMovable< VN >... > &&SemiMovable< E > auto error(E e)
void BENCHFUN() push_back(size_t iters, size_t arg)
void testFinally(ErrorBehavior error)
GuardImpl guard(ErrorHandler &&handler)
#define EXPECT_TRUE(condition)
FOLLY_NODISCARD detail::ScopeGuardImplDecay< F, true > makeGuard(F &&f) noexcept(noexcept(detail::ScopeGuardImplDecay< F, true >(static_cast< F && >(f))))
void testUndoAction(bool failure)
int bind(NetworkSocket s, const sockaddr *name, socklen_t namelen)