proxygen
|
#include <folly/ScopeGuard.h>
#include <glog/logging.h>
#include <functional>
#include <stdexcept>
#include <folly/portability/GTest.h>
Go to the source code of this file.
Classes | |
class | MyFunctor |
class | Foo |
Enumerations | |
enum | ErrorBehavior { ErrorBehavior::SUCCESS, ErrorBehavior::HANDLED_ERROR, ErrorBehavior::UNHANDLED_ERROR } |
Functions | |
double | returnsDouble () |
TEST (ScopeGuard, DifferentWaysToBind) | |
TEST (ScopeGuard, GuardException) | |
void | testUndoAction (bool failure) |
TEST (ScopeGuard, UndoAction) | |
void | testFinally (ErrorBehavior error) |
TEST (ScopeGuard, TryCatchFinally) | |
TEST (ScopeGuard, TEST_SCOPE_EXIT) | |
TEST (ScopeGuard, TEST_SCOPE_FAILURE2) | |
void | testScopeFailAndScopeSuccess (ErrorBehavior error, bool expectFail) |
TEST (ScopeGuard, TEST_SCOPE_FAIL_AND_SCOPE_SUCCESS) | |
TEST (ScopeGuard, TEST_SCOPE_SUCCESS_THROW) | |
TEST (ScopeGuard, TEST_THROWING_CLEANUP_ACTION) | |
|
strong |
Sometimes in a try catch block we want to execute a piece of code regardless if an exception happened or not. For example, you want to close a db connection regardless if an exception was thrown during insertion. In Java and other languages there is a finally clause that helps accomplish this:
try { dbConn.doInsert(sql); } catch (const DbException& dbe) { dbConn.recordFailure(dbe); } catch (const CriticalException& e) { throw e; // re-throw the exception } finally { dbConn.closeConnection(); // executes no matter what! }
We can approximate this behavior in C++ with ScopeGuard.
Enumerator | |
---|---|
SUCCESS | |
HANDLED_ERROR | |
UNHANDLED_ERROR |
Definition at line 195 of file ScopeGuardTest.cpp.
double returnsDouble | ( | ) |
TEST | ( | ScopeGuard | , |
DifferentWaysToBind | |||
) |
Definition at line 45 of file ScopeGuardTest.cpp.
References folly::netops::bind(), EXPECT_EQ, f, g(), folly::makeGuard(), MyFunctor::MyFunctor(), push_back(), returnsDouble(), and v.
TEST | ( | ScopeGuard | , |
GuardException | |||
) |
Definition at line 133 of file ScopeGuardTest.cpp.
References folly::makeGuard().
TEST | ( | ScopeGuard | , |
UndoAction | |||
) |
TEST | ( | ScopeGuard | , |
TryCatchFinally | |||
) |
Definition at line 223 of file ScopeGuardTest.cpp.
References HANDLED_ERROR, SUCCESS, testFinally(), and UNHANDLED_ERROR.
TEST | ( | ScopeGuard | , |
TEST_SCOPE_EXIT | |||
) |
Definition at line 229 of file ScopeGuardTest.cpp.
References EXPECT_EQ, SCOPE_EXIT, and x.
TEST | ( | ScopeGuard | , |
TEST_SCOPE_FAILURE2 | |||
) |
TEST | ( | ScopeGuard | , |
TEST_SCOPE_FAIL_AND_SCOPE_SUCCESS | |||
) |
Definition at line 296 of file ScopeGuardTest.cpp.
References HANDLED_ERROR, SUCCESS, testScopeFailAndScopeSuccess(), and UNHANDLED_ERROR.
TEST | ( | ScopeGuard | , |
TEST_SCOPE_SUCCESS_THROW | |||
) |
TEST | ( | ScopeGuard | , |
TEST_THROWING_CLEANUP_ACTION | |||
) |
Definition at line 311 of file ScopeGuardTest.cpp.
References EXPECT_EQ, EXPECT_THROW, folly::makeGuard(), and MyFunctor::operator()().
void testFinally | ( | ErrorBehavior | error | ) |
Definition at line 201 of file ScopeGuardTest.cpp.
References EXPECT_TRUE, folly::gen::guard(), HANDLED_ERROR, folly::makeGuard(), and UNHANDLED_ERROR.
Referenced by TEST().
void testScopeFailAndScopeSuccess | ( | ErrorBehavior | error, |
bool | expectFail | ||
) |
Definition at line 268 of file ScopeGuardTest.cpp.
References EXPECT_EQ, HANDLED_ERROR, and UNHANDLED_ERROR.
Referenced by TEST().
void testUndoAction | ( | bool | failure | ) |
Add an integer to a vector iff it was inserted into the db successfuly. Here is a schematic of how you would accomplish this with scope guard.
Definition at line 145 of file ScopeGuardTest.cpp.
References EXPECT_EQ, folly::gen::guard(), folly::makeGuard(), and v.
Referenced by TEST().