summaryrefslogtreecommitdiff
path: root/src/mongo/base/status_test.cpp
diff options
context:
space:
mode:
authorMathias Stearn <mathias@10gen.com>2017-12-11 19:35:05 -0500
committerMathias Stearn <mathias@10gen.com>2018-01-04 14:52:27 -0500
commit7f56cb7f21a4d13dd4c4d39d85f23ec77cd28f9b (patch)
tree6ebe17c53fba868d4ce80b3ae1516db3412ee485 /src/mongo/base/status_test.cpp
parent6b1a6cfe77e3c5a3b2d62a3346d19d26694ed2ed (diff)
downloadmongo-7f56cb7f21a4d13dd4c4d39d85f23ec77cd28f9b.tar.gz
SERVER-31734 Core ErrorExtraInfo implementation
Diffstat (limited to 'src/mongo/base/status_test.cpp')
-rw-r--r--src/mongo/base/status_test.cpp52
1 files changed, 52 insertions, 0 deletions
diff --git a/src/mongo/base/status_test.cpp b/src/mongo/base/status_test.cpp
index 149f712cd15..f99b0844a7b 100644
--- a/src/mongo/base/status_test.cpp
+++ b/src/mongo/base/status_test.cpp
@@ -33,6 +33,9 @@
#include <boost/exception/exception.hpp>
#include "mongo/base/status.h"
+#include "mongo/config.h"
+#include "mongo/db/json.h"
+#include "mongo/unittest/death_test.h"
#include "mongo/unittest/unittest.h"
#include "mongo/util/assert_util.h"
@@ -265,5 +268,54 @@ TEST(Transformers, ExceptionToStatus) {
ASSERT_TRUE(fromBoostExcept.reason().find("boost::exception") != std::string::npos);
}
+// TODO enable this once the next ErrorExtraInfo subclass is registered
+#if false
+DEATH_TEST(ErrorExtraInfo, InvariantAllRegistered, "Invariant failure parser_for::") {
+ ErrorExtraInfo::invariantHaveAllParsers();
+}
+#else
+TEST(ErrorExtraInfo, MakeSureInvariantAllRegisteredGetsEnabled) {
+ // This will be the first "real" use of ErrorExtraInfo, and it won't be linked into this test.
+ // This just exists to ensure that once that work is done, the above test gets enabled.
+ invariant(
+ !ErrorCodes::shouldHaveExtraInfo(ErrorCodes::CommandOnShardedViewNotSupportedOnMongod));
+}
+#endif
+
+#ifdef MONGO_CONFIG_DEBUG_BUILD
+DEATH_TEST(ErrorExtraInfo, DassertShouldHaveExtraInfo, "Fatal Assertion 40680") {
+ Status(ErrorCodes::ForTestingErrorExtraInfo, "");
+}
+#else
+TEST(ErrorExtraInfo, ConvertCodeOnMissingExtraInfo) {
+ auto status = Status(ErrorCodes::ForTestingErrorExtraInfo, "");
+ ASSERT_EQ(status, ErrorCodes::duplicateCodeForTest(40671));
+}
+#endif
+
+TEST(ErrorExtraInfo, TypedConstructorWorks) {
+ const auto status = Status(ErrorExtraInfoExample(123), "");
+ ASSERT_EQ(status, ErrorCodes::ForTestingErrorExtraInfo);
+ ASSERT(status.extraInfo());
+ ASSERT(status.extraInfo<ErrorExtraInfoExample>());
+ ASSERT_EQ(status.extraInfo<ErrorExtraInfoExample>()->data, 123);
+}
+
+TEST(ErrorExtraInfo, StatusWhenParserThrows) {
+ auto status = Status(ErrorCodes::ForTestingErrorExtraInfo, "", fromjson("{data: 123}"));
+ ASSERT_EQ(status, ErrorCodes::duplicateCodeForTest(40681));
+ ASSERT(!status.extraInfo());
+ ASSERT(!status.extraInfo<ErrorExtraInfoExample>());
+}
+
+TEST(ErrorExtraInfo, StatusParserWorks) {
+ ErrorExtraInfoExample::EnableParserForTest whenInScope;
+ auto status = Status(ErrorCodes::ForTestingErrorExtraInfo, "", fromjson("{data: 123}"));
+ ASSERT_EQ(status, ErrorCodes::ForTestingErrorExtraInfo);
+ ASSERT(status.extraInfo());
+ ASSERT(status.extraInfo<ErrorExtraInfoExample>());
+ ASSERT_EQ(status.extraInfo<ErrorExtraInfoExample>()->data, 123);
+}
+
} // namespace
} // namespace mongo