summaryrefslogtreecommitdiff
path: root/src/mongo
diff options
context:
space:
mode:
authorRandolph Tan <randolph@10gen.com>2017-04-25 13:30:18 -0400
committerRandolph Tan <randolph@10gen.com>2017-04-25 15:23:44 -0400
commitcac5edbd6fa851beacedbede5140a358388087e9 (patch)
treec22ab24d4d3fd054784fca386122018b7c2e6ba5 /src/mongo
parent7ef8b55ec9149a7afca6d9b2bffc12cf80340474 (diff)
downloadmongo-cac5edbd6fa851beacedbede5140a358388087e9.tar.gz
SERVER-28436 Create a basic helper for enabling fail point in cpp test
Diffstat (limited to 'src/mongo')
-rw-r--r--src/mongo/db/commands/fail_point_cmd.cpp2
-rw-r--r--src/mongo/util/fail_point_registry.cpp2
-rw-r--r--src/mongo/util/fail_point_service.cpp14
-rw-r--r--src/mongo/util/fail_point_service.h17
-rw-r--r--src/mongo/util/fail_point_test.cpp16
5 files changed, 47 insertions, 4 deletions
diff --git a/src/mongo/db/commands/fail_point_cmd.cpp b/src/mongo/db/commands/fail_point_cmd.cpp
index 0f75d8c1316..ce8f1367f58 100644
--- a/src/mongo/db/commands/fail_point_cmd.cpp
+++ b/src/mongo/db/commands/fail_point_cmd.cpp
@@ -99,7 +99,7 @@ public:
FailPointRegistry* registry = getGlobalFailPointRegistry();
FailPoint* failPoint = registry->getFailPoint(failPointName);
- if (failPoint == NULL) {
+ if (failPoint == nullptr) {
errmsg = failPointName + " not found";
return false;
}
diff --git a/src/mongo/util/fail_point_registry.cpp b/src/mongo/util/fail_point_registry.cpp
index 7aea3357102..c428a29b349 100644
--- a/src/mongo/util/fail_point_registry.cpp
+++ b/src/mongo/util/fail_point_registry.cpp
@@ -55,7 +55,7 @@ Status FailPointRegistry::addFailPoint(const string& name, FailPoint* failPoint)
}
FailPoint* FailPointRegistry::getFailPoint(const string& name) const {
- return mapFindWithDefault(_fpMap, name, static_cast<FailPoint*>(NULL));
+ return mapFindWithDefault(_fpMap, name, static_cast<FailPoint*>(nullptr));
}
void FailPointRegistry::freeze() {
diff --git a/src/mongo/util/fail_point_service.cpp b/src/mongo/util/fail_point_service.cpp
index 79768792564..e246cc9c16b 100644
--- a/src/mongo/util/fail_point_service.cpp
+++ b/src/mongo/util/fail_point_service.cpp
@@ -26,8 +26,9 @@
* then also delete it in the license file.
*/
-#include "mongo/util/fail_point_service.h"
+#include "mongo/platform/basic.h"
+#include "mongo/util/fail_point_service.h"
namespace mongo {
@@ -52,4 +53,15 @@ MONGO_INITIALIZER_GENERAL(AllFailPointsRegistered, MONGO_NO_PREREQUISITES, MONGO
FailPointRegistry* getGlobalFailPointRegistry() {
return _fpRegistry.get();
}
+
+FailPointEnableBlock::FailPointEnableBlock(const std::string& failPointName) {
+ _failPoint = getGlobalFailPointRegistry()->getFailPoint(failPointName);
+ invariant(_failPoint != nullptr);
+ _failPoint->setMode(FailPoint::alwaysOn);
+}
+
+FailPointEnableBlock::~FailPointEnableBlock() {
+ _failPoint->setMode(FailPoint::off);
}
+
+} // namespace mongo
diff --git a/src/mongo/util/fail_point_service.h b/src/mongo/util/fail_point_service.h
index d06960d0aac..331dc485e32 100644
--- a/src/mongo/util/fail_point_service.h
+++ b/src/mongo/util/fail_point_service.h
@@ -33,6 +33,8 @@
namespace mongo {
+class FailPoint;
+
/**
* @return the global fail point registry.
*/
@@ -55,4 +57,17 @@ FailPointRegistry* getGlobalFailPointRegistry();
* Convenience macro for defining a fail point in a header scope.
*/
#define MONGO_FP_FORWARD_DECLARE(fp) extern FailPoint fp;
-}
+
+/**
+ * Convenience class for enabling a failpoint and disabling it as this goes out of scope.
+ */
+class FailPointEnableBlock {
+public:
+ FailPointEnableBlock(const std::string& failPointName);
+ ~FailPointEnableBlock();
+
+private:
+ FailPoint* _failPoint;
+};
+
+} // namespace mongo
diff --git a/src/mongo/util/fail_point_test.cpp b/src/mongo/util/fail_point_test.cpp
index 5374b879da9..37bf8936f21 100644
--- a/src/mongo/util/fail_point_test.cpp
+++ b/src/mongo/util/fail_point_test.cpp
@@ -38,11 +38,14 @@
#include "mongo/stdx/thread.h"
#include "mongo/unittest/unittest.h"
#include "mongo/util/fail_point.h"
+#include "mongo/util/fail_point_service.h"
#include "mongo/util/log.h"
#include "mongo/util/time_support.h"
+using mongo::getGlobalFailPointRegistry;
using mongo::BSONObj;
using mongo::FailPoint;
+using mongo::FailPointEnableBlock;
namespace stdx = mongo::stdx;
namespace mongo_test {
@@ -398,4 +401,17 @@ TEST(FailPoint, parseBSONValidDataSucceeds) {
<< BSON("a" << 1)));
ASSERT_TRUE(swTuple.isOK());
}
+
+TEST(FailPoint, FailPointBlockBasicTest) {
+ auto failPoint = getGlobalFailPointRegistry()->getFailPoint("dummy");
+
+ ASSERT_FALSE(failPoint->shouldFail());
+
+ {
+ FailPointEnableBlock dummyFp("dummy");
+ ASSERT_TRUE(failPoint->shouldFail());
+ }
+
+ ASSERT_FALSE(failPoint->shouldFail());
+}
}