diff options
author | Scott Hernandez <scotthernandez@gmail.com> | 2013-03-20 10:56:00 -0400 |
---|---|---|
committer | Scott Hernandez <scotthernandez@gmail.com> | 2013-03-25 17:24:34 -0400 |
commit | cb9f8b361e6559ed72fb302be273ea979541b246 (patch) | |
tree | 10938e23faadaf6034b38671e9aa399b622ef632 | |
parent | 568e95e3dad7ee511565056db4f03586cd5ad018 (diff) | |
download | mongo-cb9f8b361e6559ed72fb302be273ea979541b246.tar.gz |
SERVER-8984: gle c++ client helper should check for command failure
-rw-r--r-- | src/mongo/client/dbclient.cpp | 18 | ||||
-rw-r--r-- | src/mongo/dbtests/gle_test.cpp | 85 |
2 files changed, 98 insertions, 5 deletions
diff --git a/src/mongo/client/dbclient.cpp b/src/mongo/client/dbclient.cpp index 9f7a1a48ac7..984b5c8a44d 100644 --- a/src/mongo/client/dbclient.cpp +++ b/src/mongo/client/dbclient.cpp @@ -474,11 +474,19 @@ namespace mongo { return getLastErrorString( info ); } - string DBClientWithCommands::getLastErrorString( const BSONObj& info ) { - BSONElement e = info["err"]; - if( e.eoo() ) return ""; - if( e.type() == Object ) return e.toString(); - return e.str(); + string DBClientWithCommands::getLastErrorString(const BSONObj& info) { + if (info["ok"].trueValue()) { + BSONElement e = info["err"]; + if (e.eoo()) return ""; + if (e.type() == Object) return e.toString(); + return e.str(); + } else { + // command failure + BSONElement e = info["errmsg"]; + if (e.eoo()) return ""; + if (e.type() == Object) return "getLastError command failed: " + e.toString(); + return "getLastError command failed: " + e.str(); + } } const BSONObj getpreverrorcmdobj = fromjson("{getpreverror:1}"); diff --git a/src/mongo/dbtests/gle_test.cpp b/src/mongo/dbtests/gle_test.cpp new file mode 100644 index 00000000000..1a5f89ad1b2 --- /dev/null +++ b/src/mongo/dbtests/gle_test.cpp @@ -0,0 +1,85 @@ +/** + * Copyright (C) 2013 10gen Inc. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include "mongo/dbtests/dbtests.h" +#include "mongo/util/assert_util.h" + +using mongo::MsgAssertionException; + +/** + * Test getLastError client handling + */ +namespace { + DBDirectClient _client; + static const char* const _ns = "unittests.gle"; + + /** + * Verify that when the command fails we get back an error message. + */ + class GetLastErrorCommandFailure { + public: + void run() { + _client.insert(_ns, BSON( "test" << "test")); + // Cannot mix fsync + j, will make command fail + string gleString = _client.getLastError(true, true, 10, 10); + ASSERT_NOT_EQUALS(gleString, ""); + } + }; + + /** + * Verify that the write succeeds + */ + class GetLastErrorClean { + public: + void run() { + _client.insert(_ns, BSON( "test" << "test")); + // Make sure there was no error + string gleString = _client.getLastError(); + ASSERT_EQUALS(gleString, ""); + } + }; + + /** + * Verify that the write succeed first, then error on dup + */ + class GetLastErrorFromDup { + public: + void run() { + _client.insert(_ns, BSON( "_id" << 1)); + // Make sure there was no error + string gleString = _client.getLastError(); + ASSERT_EQUALS(gleString, ""); + + //insert dup + _client.insert(_ns, BSON( "_id" << 1)); + // Make sure there was an error + gleString = _client.getLastError(); + ASSERT_NOT_EQUALS(gleString, ""); + } + }; + + class All : public Suite { + public: + All() : Suite( "gle" ) { + } + + void setupTests() { + add< GetLastErrorClean >(); + add< GetLastErrorCommandFailure >(); + add< GetLastErrorFromDup >(); + } + } myall; +} |