summaryrefslogtreecommitdiff
path: root/src/mongo/unittest
diff options
context:
space:
mode:
authorJudah Schvimer <judah@mongodb.com>2016-10-21 15:30:51 -0400
committerJudah Schvimer <judah@mongodb.com>2016-10-21 15:30:51 -0400
commitb6fdf20fc99c416c45b3200b541fb4f90c30a3fd (patch)
treeb52eb72463f4bbdc36b9c04df221f2b7e68d3ba9 /src/mongo/unittest
parentff45562f1578bd6f2ad55c85f9c4b69dd2887130 (diff)
downloadmongo-b6fdf20fc99c416c45b3200b541fb4f90c30a3fd.tar.gz
SERVER-26707 Always call tearDown after unittests end
Diffstat (limited to 'src/mongo/unittest')
-rw-r--r--src/mongo/unittest/unittest.cpp34
1 files changed, 28 insertions, 6 deletions
diff --git a/src/mongo/unittest/unittest.cpp b/src/mongo/unittest/unittest.cpp
index dc21394fe25..94eb315d23a 100644
--- a/src/mongo/unittest/unittest.cpp
+++ b/src/mongo/unittest/unittest.cpp
@@ -128,6 +128,33 @@ public:
Result* Result::cur = 0;
+namespace {
+
+/**
+ * This unsafe scope guard allows exceptions in its destructor. Thus, if it goes out of scope when
+ * an exception is active and the guard function also throws an exception, the program will call
+ * std::terminate. This should only be used in unittests where termination on exception is okay.
+ */
+template <typename F>
+class UnsafeScopeGuard {
+public:
+ UnsafeScopeGuard(F fun) : _fun(fun) {}
+
+ ~UnsafeScopeGuard() noexcept(false) {
+ _fun();
+ }
+
+private:
+ F _fun;
+};
+
+template <typename F>
+inline UnsafeScopeGuard<F> MakeUnsafeScopeGuard(F fun) {
+ return UnsafeScopeGuard<F>(std::move(fun));
+}
+
+} // namespace
+
Test::Test() : _isCapturingLogMessages(false) {}
Test::~Test() {
@@ -138,6 +165,7 @@ Test::~Test() {
void Test::run() {
setUp();
+ auto guard = MakeUnsafeScopeGuard([this] { tearDown(); });
// An uncaught exception does not prevent the tear down from running. But
// such an event still constitutes an error. To test this behavior we use a
@@ -146,14 +174,8 @@ void Test::run() {
try {
_doTest();
} catch (FixtureExceptionForTesting&) {
- tearDown();
return;
- } catch (TestAssertionFailureException&) {
- tearDown();
- throw;
}
-
- tearDown();
}
void Test::setUp() {}