diff options
author | Amirsaman Memaripour <amirsaman.memaripour@mongodb.com> | 2021-05-26 15:08:34 +0000 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2021-11-08 18:53:32 +0000 |
commit | 28136c4dd97e017e9b95c5f8d3f2c4de518a4a91 (patch) | |
tree | b1474ac6bc49b05cec28573e9fdc53a2875dd6df | |
parent | ce921f75b2ea9f520726c321cf87a3e83514bb81 (diff) | |
download | mongo-28136c4dd97e017e9b95c5f8d3f2c4de518a4a91.tar.gz |
SERVER-57171 Make the killop test more resilient
(cherry picked from commit 3a48403f63ed65e076e0cd44cccf0f89123f8e86)
-rw-r--r-- | src/mongo/embedded/mongo_embedded/mongo_embedded_test.cpp | 80 |
1 files changed, 42 insertions, 38 deletions
diff --git a/src/mongo/embedded/mongo_embedded/mongo_embedded_test.cpp b/src/mongo/embedded/mongo_embedded/mongo_embedded_test.cpp index 70a8eb6663e..955fbf62fff 100644 --- a/src/mongo/embedded/mongo_embedded/mongo_embedded_test.cpp +++ b/src/mongo/embedded/mongo_embedded/mongo_embedded_test.cpp @@ -44,11 +44,13 @@ #include "mongo/rpc/op_msg.h" #include "mongo/stdx/thread.h" #include "mongo/unittest/temp_dir.h" +#include "mongo/unittest/thread_assertion_monitor.h" #include "mongo/unittest/unittest.h" #include "mongo/util/options_parser/environment.h" #include "mongo/util/options_parser/option_section.h" #include "mongo/util/options_parser/options_parser.h" #include "mongo/util/quick_exit.h" +#include "mongo/util/scopeguard.h" #include "mongo/util/shared_buffer.h" #include "mongo/util/signal_handlers_synchronous.h" #include "mongo/util/text.h" @@ -347,52 +349,54 @@ TEST_F(MongodbCAPITest, InsertMultipleDocuments) { } TEST_F(MongodbCAPITest, KillOp) { - auto client = createClient(); - - mongo::stdx::thread killOpThread([this]() { + mongo::unittest::threadAssertionMonitoredTest([&](auto& assertionMonitor) { auto client = createClient(); - mongo::BSONObj currentOpObj = mongo::fromjson("{currentOp: 1}"); - auto currentOpMsg = mongo::OpMsgRequest::fromDBAndBody("admin", currentOpObj); - mongo::BSONObj outputBSON; - - // Wait for the sleep command to start in the main test thread. - int opid = -1; - do { - outputBSON = performRpc(client, currentOpMsg); - auto inprog = outputBSON.getObjectField("inprog"); - - // See if we find the sleep command among the running commands - for (const auto& elt : inprog) { - auto inprogObj = inprog.getObjectField(elt.fieldNameStringData()); - std::string ns = inprogObj.getStringField("ns"); - if (ns == "admin.$cmd") { - opid = inprogObj.getIntField("opid"); - break; + auto killOpThread = assertionMonitor.spawn([&]() { + auto client = createClient(); + + mongo::BSONObj currentOpObj = mongo::fromjson("{currentOp: 1}"); + auto currentOpMsg = mongo::OpMsgRequest::fromDBAndBody("admin", currentOpObj); + mongo::BSONObj outputBSON; + + // Wait for the sleep command to start in the main test thread. + int opid = -1; + do { + outputBSON = performRpc(client, currentOpMsg); + auto inprog = outputBSON.getObjectField("inprog"); + + // See if we find the sleep command among the running commands + for (const auto& elt : inprog) { + auto inprogObj = inprog.getObjectField(elt.fieldNameStringData()); + std::string ns = inprogObj.getStringField("ns"); + if (ns == "admin.$cmd") { + opid = inprogObj.getIntField("opid"); + break; + } } - } - } while (opid == -1); + } while (opid == -1); - // Sleep command found, kill it. - std::stringstream ss; - ss << "{'killOp': 1, 'op': " << opid << "}"; - mongo::BSONObj killOpObj = mongo::fromjson(ss.str()); - auto killOpMsg = mongo::OpMsgRequest::fromDBAndBody("admin", killOpObj); - outputBSON = performRpc(client, killOpMsg); + // Sleep command found, kill it. + std::stringstream ss; + ss << "{'killOp': 1, 'op': " << opid << "}"; + mongo::BSONObj killOpObj = mongo::fromjson(ss.str()); + auto killOpMsg = mongo::OpMsgRequest::fromDBAndBody("admin", killOpObj); + outputBSON = performRpc(client, killOpMsg); - ASSERT(outputBSON.hasField("ok")); - ASSERT(outputBSON.getField("ok").numberDouble() == 1.0); - }); + ASSERT(outputBSON.hasField("ok")); + ASSERT(outputBSON.getField("ok").numberDouble() == 1.0); + }); - mongo::BSONObj sleepObj = mongo::fromjson("{'sleep': {'secs': 1000}}"); - auto sleepOpMsg = mongo::OpMsgRequest::fromDBAndBody("admin", sleepObj); - auto outputBSON = performRpc(client, sleepOpMsg); + auto guard = mongo::makeGuard([&] { killOpThread.join(); }); - ASSERT(outputBSON.hasField("ok")); - ASSERT(outputBSON.getField("ok").numberDouble() != 1.0); - ASSERT(outputBSON.getIntField("code") == mongo::ErrorCodes::Interrupted); + mongo::BSONObj sleepObj = mongo::fromjson("{'sleep': {'secs': 1000}}"); + auto sleepOpMsg = mongo::OpMsgRequest::fromDBAndBody("admin", sleepObj); + auto outputBSON = performRpc(client, sleepOpMsg); - killOpThread.join(); + ASSERT(outputBSON.hasField("ok")); + ASSERT(outputBSON.getField("ok").numberDouble() != 1.0); + ASSERT(outputBSON.getIntField("code") == mongo::ErrorCodes::Interrupted); + }); } TEST_F(MongodbCAPITest, ReadDB) { |