diff options
Diffstat (limited to 'src/mongo/util')
-rw-r--r-- | src/mongo/util/fail_point.cpp | 6 | ||||
-rw-r--r-- | src/mongo/util/fail_point.h | 7 | ||||
-rw-r--r-- | src/mongo/util/fail_point_test.cpp | 51 |
3 files changed, 64 insertions, 0 deletions
diff --git a/src/mongo/util/fail_point.cpp b/src/mongo/util/fail_point.cpp index e467ff2d9fb..34a5cb7454e 100644 --- a/src/mongo/util/fail_point.cpp +++ b/src/mongo/util/fail_point.cpp @@ -371,6 +371,12 @@ void FailPointRegistry::registerAllFailPointsAsServerParameters() { } } +void FailPointRegistry::disableAllFailpoints() { + for (auto& [_, fp] : _fpMap) { + fp->setMode(FailPoint::Mode::off); + } +} + static constexpr auto kFailPointServerParameterPrefix = "failpoint."_sd; FailPointServerParameter::FailPointServerParameter(StringData name, ServerParameterType spt) diff --git a/src/mongo/util/fail_point.h b/src/mongo/util/fail_point.h index af02e9f1622..64bcaa85804 100644 --- a/src/mongo/util/fail_point.h +++ b/src/mongo/util/fail_point.h @@ -465,6 +465,13 @@ public: */ void registerAllFailPointsAsServerParameters(); + /** + * Sets all registered FailPoints to Mode::off. Used primarily during unit test cleanup to + * reset the state of all FailPoints set by the unit test. Does not prevent FailPoints from + * being enabled again after. + */ + void disableAllFailpoints(); + private: bool _frozen; StringMap<FailPoint*> _fpMap; diff --git a/src/mongo/util/fail_point_test.cpp b/src/mongo/util/fail_point_test.cpp index a3c346594c9..da7b7d76e5d 100644 --- a/src/mongo/util/fail_point_test.cpp +++ b/src/mongo/util/fail_point_test.cpp @@ -54,6 +54,12 @@ using mongo::FailPointEnableBlock; namespace stdx = mongo::stdx; namespace mongo_test { +namespace { +// Used by tests in this file that need access to a failpoint that is a registered in the +// FailPointRegistry. +MONGO_FAIL_POINT_DEFINE(dummy2); +} // namespace + TEST(FailPoint, InitialState) { FailPoint failPoint("testFP"); ASSERT_FALSE(failPoint.shouldFail()); @@ -140,6 +146,51 @@ TEST(FailPoint, SetGetParam) { failPoint.execute([&](const BSONObj& data) { ASSERT_EQUALS(20, data["x"].numberInt()); }); } +TEST(FailPoint, DisableAllFailpoints) { + auto& registry = mongo::globalFailPointRegistry(); + + FailPoint& fp1 = *registry.find("dummy"); + FailPoint& fp2 = *registry.find("dummy2"); + int counter1 = 0; + int counter2 = 0; + fp1.execute([&](const BSONObj&) { counter1++; }); + fp2.execute([&](const BSONObj&) { counter2++; }); + + ASSERT_EQ(0, counter1); + ASSERT_EQ(0, counter2); + + fp1.setMode(FailPoint::alwaysOn); + fp2.setMode(FailPoint::alwaysOn); + + fp1.execute([&](const BSONObj&) { counter1++; }); + fp2.execute([&](const BSONObj&) { counter2++; }); + + ASSERT_EQ(1, counter1); + ASSERT_EQ(1, counter2); + + registry.disableAllFailpoints(); + + fp1.execute([&](const BSONObj&) { counter1++; }); + fp2.execute([&](const BSONObj&) { counter2++; }); + + ASSERT_EQ(1, counter1); + ASSERT_EQ(1, counter2); + + // Check that you can still enable and continue using FailPoints after a call to + // disableAllFailpoints() + fp1.setMode(FailPoint::alwaysOn); + fp2.setMode(FailPoint::alwaysOn); + + fp1.execute([&](const BSONObj&) { counter1++; }); + fp2.execute([&](const BSONObj&) { counter2++; }); + + ASSERT_EQ(2, counter1); + ASSERT_EQ(2, counter2); + + // Reset the state for future tests. + registry.disableAllFailpoints(); +} + class FailPointStress : public mongo::unittest::Test { public: void setUp() { |