diff options
author | Andrew Morrow <acm@mongodb.com> | 2014-10-14 08:23:38 -0400 |
---|---|---|
committer | Andrew Morrow <acm@mongodb.com> | 2014-10-15 20:51:31 -0400 |
commit | 82c79ceed40f66b4e4c260d4b7d5cef5061893ac (patch) | |
tree | 5496a8ba71ad9beccddee90dcf0db6cf31c5c9df | |
parent | cea7003c9124435452b2d1f9828907e857bcd26b (diff) | |
download | mongo-82c79ceed40f66b4e4c260d4b7d5cef5061893ac.tar.gz |
SERVER-15641 Fix memory leaks in C++ unit tests
24 files changed, 352 insertions, 136 deletions
diff --git a/src/mongo/db/auth/authz_manager_external_state_mock.cpp b/src/mongo/db/auth/authz_manager_external_state_mock.cpp index 430d2535619..638e6f9465a 100644 --- a/src/mongo/db/auth/authz_manager_external_state_mock.cpp +++ b/src/mongo/db/auth/authz_manager_external_state_mock.cpp @@ -309,7 +309,7 @@ namespace { if (!parseResult.isOK()) { return parseResult.getStatus(); } - MatchExpression* matcher = parseResult.getValue(); + const boost::scoped_ptr<MatchExpression> matcher(parseResult.getValue()); NamespaceDocumentMap::iterator mapIt = _documents.find(collectionName); if (mapIt == _documents.end()) diff --git a/src/mongo/db/auth/user_set_test.cpp b/src/mongo/db/auth/user_set_test.cpp index 0f06c8b4452..bf65fa6ae3a 100644 --- a/src/mongo/db/auth/user_set_test.cpp +++ b/src/mongo/db/auth/user_set_test.cpp @@ -46,6 +46,10 @@ namespace { User* p2 = new User(UserName("George", "test")); User* p3 = new User(UserName("Bob", "test2")); + const boost::scoped_ptr<User> delp1(p1); + const boost::scoped_ptr<User> delp2(p2); + const boost::scoped_ptr<User> delp3(p3); + ASSERT_NULL(set.lookup(UserName("Bob", "test"))); ASSERT_NULL(set.lookup(UserName("George", "test"))); ASSERT_NULL(set.lookup(UserName("Bob", "test2"))); @@ -98,7 +102,8 @@ namespace { UserNameIterator iter = pset.getNames(); ASSERT(!iter.more()); - ASSERT_NULL(pset.add(new User(UserName("bob", "test")))); + boost::scoped_ptr<User> user(new User(UserName("bob", "test"))); + ASSERT_NULL(pset.add(user.get())); iter = pset.getNames(); ASSERT(iter.more()); diff --git a/src/mongo/db/concurrency/lock_mgr_new.cpp b/src/mongo/db/concurrency/lock_mgr_new.cpp index fc6e9211653..7ce283cb6a7 100644 --- a/src/mongo/db/concurrency/lock_mgr_new.cpp +++ b/src/mongo/db/concurrency/lock_mgr_new.cpp @@ -186,7 +186,7 @@ namespace mongo { // LockManager // - LockManager::LockManager() : _noCheckForLeakedLocksTestOnly(false) { + LockManager::LockManager() { // Have more buckets than CPUs to reduce contention on lock and caches _numLockBuckets = 128; _lockBuckets = new LockBucket[_numLockBuckets]; @@ -199,9 +199,7 @@ namespace mongo { LockBucket* bucket = &_lockBuckets[i]; // TODO: dump more information about the non-empty bucket to see what locks were leaked - if (!_noCheckForLeakedLocksTestOnly) { - invariant(bucket->data.empty()); - } + invariant(bucket->data.empty()); } delete[] _lockBuckets; @@ -439,10 +437,6 @@ namespace mongo { } } - void LockManager::setNoCheckForLeakedLocksTestOnly(bool newValue) { - _noCheckForLeakedLocksTestOnly = newValue; - } - void LockManager::_onLockModeChanged(LockHead* lock, bool checkConflictQueue) { // Unblock any converting requests (because conversions are still counted as granted and // are on the granted queue). diff --git a/src/mongo/db/concurrency/lock_mgr_new.h b/src/mongo/db/concurrency/lock_mgr_new.h index 84224eec1a5..8420218b526 100644 --- a/src/mongo/db/concurrency/lock_mgr_new.h +++ b/src/mongo/db/concurrency/lock_mgr_new.h @@ -335,9 +335,6 @@ namespace mongo { unsigned _numLockBuckets; LockBucket* _lockBuckets; - - // This is for tests only and removes the validation for leaked locks in the destructor - bool _noCheckForLeakedLocksTestOnly; }; } // namespace mongo diff --git a/src/mongo/db/concurrency/lock_mgr_new_test.cpp b/src/mongo/db/concurrency/lock_mgr_new_test.cpp index 7a9448199d8..99fd55f0440 100644 --- a/src/mongo/db/concurrency/lock_mgr_new_test.cpp +++ b/src/mongo/db/concurrency/lock_mgr_new_test.cpp @@ -587,7 +587,6 @@ namespace mongo { static void checkConflict(LockMode existingMode, LockMode newMode, bool hasConflict) { LockManager lockMgr; - lockMgr.setNoCheckForLeakedLocksTestOnly(true); const ResourceId resId(RESOURCE_COLLECTION, std::string("TestDB.collection")); diff --git a/src/mongo/db/matcher/expression_parser_array_test.cpp b/src/mongo/db/matcher/expression_parser_array_test.cpp index 9230298a019..e2987efb74c 100644 --- a/src/mongo/db/matcher/expression_parser_array_test.cpp +++ b/src/mongo/db/matcher/expression_parser_array_test.cpp @@ -48,6 +48,7 @@ namespace mongo { ASSERT( result.getValue()->matchesBSON( BSON( "x" << BSON_ARRAY( 1 << 2 ) ) ) ); ASSERT( !result.getValue()->matchesBSON( BSON( "x" << BSON_ARRAY( 1 ) ) ) ); ASSERT( !result.getValue()->matchesBSON( BSON( "x" << BSON_ARRAY( 1 << 2 << 3 ) ) ) ); + delete result.getValue(); } TEST( MatchExpressionParserArrayTest, SizeAsString ) { @@ -59,6 +60,7 @@ namespace mongo { ASSERT( !result.getValue()->matchesBSON( BSON( "x" << BSON_ARRAY( 1 << 2 ) ) ) ); ASSERT( result.getValue()->matchesBSON( BSON( "x" << BSONArray() ) ) ); ASSERT( !result.getValue()->matchesBSON( BSON( "x" << BSON_ARRAY( 1 ) ) ) ); + delete result.getValue(); } TEST( MatchExpressionParserArrayTest, SizeWithDouble ) { @@ -71,6 +73,7 @@ namespace mongo { ASSERT( !result.getValue()->matchesBSON( BSON( "x" << BSON_ARRAY( 1 ) ) ) ); ASSERT( !result.getValue()->matchesBSON( BSON( "x" << BSONArray() ) ) ); ASSERT( !result.getValue()->matchesBSON( BSON( "x" << BSON_ARRAY( 1 << 2 << 3 ) ) ) ); + delete result.getValue(); } TEST( MatchExpressionParserArrayTest, SizeBad ) { @@ -91,6 +94,7 @@ namespace mongo { ASSERT( !result.getValue()->matchesBSON( BSON( "x" << BSON_ARRAY( BSON( "x" << 1 ) ) ) ) ); ASSERT( result.getValue()->matchesBSON( BSON( "x" << BSON_ARRAY( BSON( "x" << 1 << "y" << 2 ) ) ) ) ); + delete result.getValue(); } @@ -106,6 +110,7 @@ namespace mongo { ASSERT( !result.getValue()->matchesBSON( BSON( "x" << BSON_ARRAY( BSON( "x" << 1 ) ) ) ) ); ASSERT( result.getValue()->matchesBSON( BSON( "x" << BSON_ARRAY( BSON( "x" << 1 << "y" << 2 ) ) ) ) ); + delete result.getValue(); } @@ -121,6 +126,7 @@ namespace mongo { ASSERT( !result.getValue()->matchesBSON( BSON( "x" << BSON_ARRAY( BSON( "x" << 1 ) ) ) ) ); ASSERT( result.getValue()->matchesBSON( BSON( "x" << BSON_ARRAY( BSON( "x" << 2 << "y" << 2 ) ) ) ) ); + delete result.getValue(); } @@ -136,6 +142,7 @@ namespace mongo { ASSERT( !result.getValue()->matchesBSON( BSON( "x" << BSON_ARRAY( BSON( "x" << 1 ) ) ) ) ); ASSERT( result.getValue()->matchesBSON( BSON( "x" << BSON_ARRAY( BSON( "x" << 1 << "y" << 2 ) ) ) ) ); + delete result.getValue(); } @@ -147,6 +154,7 @@ namespace mongo { ASSERT( !result.getValue()->matchesBSON( BSON( "x" << 1 ) ) ); ASSERT( !result.getValue()->matchesBSON( BSON( "x" << BSON_ARRAY( 4 ) ) ) ); ASSERT( result.getValue()->matchesBSON( BSON( "x" << BSON_ARRAY( 6 ) ) ) ); + delete result.getValue(); } // with explicit $eq @@ -163,6 +171,7 @@ namespace mongo { ASSERT( !result.getValue()->matchesBSON( BSON( "x" << match ) ) ); ASSERT( !result.getValue()->matchesBSON( BSON( "x" << BSON_ARRAY( notMatch ) ) ) ); ASSERT( result.getValue()->matchesBSON( BSON( "x" << BSON_ARRAY( match ) ) ) ); + delete result.getValue(); } TEST( MatchExpressionParserArrayTest, ElemMatchDBRef2 ) { @@ -178,6 +187,7 @@ namespace mongo { ASSERT( !result.getValue()->matchesBSON( BSON( "x" << match ) ) ); ASSERT( !result.getValue()->matchesBSON( BSON( "x" << BSON_ARRAY( notMatch ) ) ) ); ASSERT( result.getValue()->matchesBSON( BSON( "x" << BSON_ARRAY( match ) ) ) ); + delete result.getValue(); } // Additional fields after $ref and $id. @@ -198,6 +208,7 @@ namespace mongo { // Document contains fields not referred to in $elemMatch query. ASSERT( result.getValue()->matchesBSON( BSON( "x" << BSON_ARRAY( BSON( "$ref" << "coll" << "$id" << oid << "foo" << 12345 << "bar" << 678 ) ) ) ) ); + delete result.getValue(); } // Query with DBRef fields out of order. @@ -215,6 +226,7 @@ namespace mongo { ASSERT( !result.getValue()->matchesBSON( BSON( "x" << match ) ) ); ASSERT( !result.getValue()->matchesBSON( BSON( "x" << BSON_ARRAY( notMatch ) ) ) ); ASSERT( result.getValue()->matchesBSON( BSON( "x" << BSON_ARRAY( match ) ) ) ); + delete result.getValue(); } // Query with DBRef fields out of order. @@ -237,6 +249,7 @@ namespace mongo { // Document contains fields not referred to in $elemMatch query. ASSERT( result.getValue()->matchesBSON( BSON( "x" << BSON_ARRAY( BSON( "$ref" << "coll" << "$id" << oid << "foo" << 12345 << "bar" << 678 ) ) ) ) ); + delete result.getValue(); } // Incomplete DBRef - $id missing. @@ -257,6 +270,7 @@ namespace mongo { // Document contains fields not referred to in $elemMatch query. ASSERT( result.getValue()->matchesBSON( BSON( "x" << BSON_ARRAY( BSON( "$ref" << "coll" << "$id" << oid << "foo" << 12345 << "bar" << 678 ) ) ) ) ); + delete result.getValue(); } // Incomplete DBRef - $ref missing. @@ -278,6 +292,7 @@ namespace mongo { // Document contains fields not referred to in $elemMatch query. ASSERT( result.getValue()->matchesBSON( BSON( "x" << BSON_ARRAY( BSON( "$ref" << "coll" << "$id" << oid << "foo" << 12345 << "bar" << 678 ) ) ) ) ); + delete result.getValue(); } // Incomplete DBRef - $db only. @@ -301,6 +316,7 @@ namespace mongo { ASSERT( result.getValue()->matchesBSON( BSON( "x" << BSON_ARRAY( BSON( "$ref" << "coll" << "$id" << oid << "$db" << "db" << "foo" << 12345 << "bar" << 678 ) ) ) ) ); + delete result.getValue(); } TEST( MatchExpressionParserArrayTest, All1 ) { @@ -314,6 +330,7 @@ namespace mongo { ASSERT( result.getValue()->matchesBSON( BSON( "x" << BSON_ARRAY( 1 << 2 ) ) ) ); ASSERT( result.getValue()->matchesBSON( BSON( "x" << BSON_ARRAY( 1 << 2 << 3 ) ) ) ); ASSERT( !result.getValue()->matchesBSON( BSON( "x" << BSON_ARRAY( 2 << 3 ) ) ) ); + delete result.getValue(); } TEST( MatchExpressionParserArrayTest, AllNull ) { @@ -325,6 +342,7 @@ namespace mongo { ASSERT( !result.getValue()->matchesBSON( BSON( "x" << BSON_ARRAY( 1 ) ) ) ); ASSERT( result.getValue()->matchesBSON( BSON( "x" << BSONNULL ) ) ); ASSERT( result.getValue()->matchesBSON( BSON( "x" << BSON_ARRAY( BSONNULL ) ) ) ); + delete result.getValue(); } TEST( MatchExpressionParserArrayTest, AllBadArg ) { @@ -365,6 +383,7 @@ namespace mongo { ASSERT( !result.getValue()->matchesSingleElement( notMatchFirst[ "a" ] ) ); ASSERT( !result.getValue()->matchesSingleElement( notMatchSecond[ "a" ] ) ); ASSERT( result.getValue()->matchesSingleElement( matchesBoth[ "a" ] ) ); + delete result.getValue(); } TEST( MatchExpressionParserArrayTest, AllRegex2 ) { @@ -383,6 +402,7 @@ namespace mongo { ASSERT( !result.getValue()->matchesSingleElement( notMatchFirst[ "a" ] ) ); ASSERT( result.getValue()->matchesSingleElement( matchesBoth[ "a" ] ) ); + delete result.getValue(); } TEST( MatchExpressionParserArrayTest, AllNonArray ) { @@ -394,6 +414,7 @@ namespace mongo { ASSERT( result.getValue()->matchesBSON( BSON( "x" << BSON_ARRAY( 5 ) ) ) ); ASSERT( !result.getValue()->matchesBSON( BSON( "x" << 4 ) ) ); ASSERT( !result.getValue()->matchesBSON( BSON( "x" << BSON_ARRAY( 4 ) ) ) ); + delete result.getValue(); } @@ -408,6 +429,7 @@ namespace mongo { ASSERT( !result.getValue()->matchesBSON( BSON( "x" << BSON_ARRAY( BSON( "x" << 1 ) ) ) ) ); ASSERT( result.getValue()->matchesBSON( BSON( "x" << BSON_ARRAY( BSON( "x" << 1 << "y" << 2 ) ) ) ) ); + delete result.getValue(); } @@ -457,6 +479,7 @@ namespace mongo { BSON( "y" << BSON_ARRAY( BSON( "x" << 1 << "z" << 1 ) ) ) ) ) ) ); + delete result.getValue(); } TEST( MatchExpressionParserArrayTest, AllElemMatchBad ) { @@ -484,6 +507,7 @@ namespace mongo { ASSERT( result.getValue()->matchesBSON( BSON( "x" << "" ) ) ); ASSERT( result.getValue()->matchesBSON( BSON( "x" << BSON_ARRAY( BSONNULL << "" ) ) ) ); ASSERT( result.getValue()->matchesBSON( BSON( "x" << BSON_ARRAY( BSONObj() << "" ) ) ) ); + delete result.getValue(); } // $all with ISO date. @@ -510,6 +534,7 @@ namespace mongo { match ) ) ) ); ASSERT( result.getValue()->matchesBSON( BSON( "x" << BSON_ARRAY( BSONObj() << match ) ) ) ); + delete result.getValue(); } // $all on array element with empty string. @@ -527,6 +552,7 @@ namespace mongo { ASSERT( !result.getValue()->matchesBSON( BSON( "x" << "" ) ) ); ASSERT( result.getValue()->matchesBSON( BSON( "x" << BSON_ARRAY( BSONNULL << "" ) ) ) ); ASSERT( result.getValue()->matchesBSON( BSON( "x" << BSON_ARRAY( BSONObj() << "" ) ) ) ); + delete result.getValue(); } // $all on array element with ISO date. @@ -557,6 +583,7 @@ namespace mongo { match ) ) ) ); ASSERT( result.getValue()->matchesBSON( BSON( "x" << BSON_ARRAY( BSONObj() << match ) ) ) ); + delete result.getValue(); } } diff --git a/src/mongo/db/matcher/expression_parser_geo_test.cpp b/src/mongo/db/matcher/expression_parser_geo_test.cpp index bb3bb158677..a7636390403 100644 --- a/src/mongo/db/matcher/expression_parser_geo_test.cpp +++ b/src/mongo/db/matcher/expression_parser_geo_test.cpp @@ -44,6 +44,7 @@ namespace mongo { StatusWithMatchExpression result = MatchExpressionParser::parse( query ); ASSERT_TRUE( result.isOK() ); + boost::scoped_ptr<MatchExpression> destroy(result.getValue()); ASSERT(!result.getValue()->matchesBSON(fromjson("{a: [3,4]}"))); ASSERT(result.getValue()->matchesBSON(fromjson("{a: [4,4]}"))); @@ -59,6 +60,7 @@ namespace mongo { StatusWithMatchExpression result = MatchExpressionParser::parse( query ); ASSERT_TRUE( result.isOK() ); + boost::scoped_ptr<MatchExpression> destroy(result.getValue()); MatchExpression* exp = result.getValue(); ASSERT_EQUALS(MatchExpression::GEO_NEAR, exp->matchType()); diff --git a/src/mongo/db/matcher/expression_parser_leaf_test.cpp b/src/mongo/db/matcher/expression_parser_leaf_test.cpp index 46b2ca0535b..507b781fdd9 100644 --- a/src/mongo/db/matcher/expression_parser_leaf_test.cpp +++ b/src/mongo/db/matcher/expression_parser_leaf_test.cpp @@ -46,6 +46,7 @@ namespace mongo { BSONObj query = BSON( "x" << BSON( "$eq" << 2 ) ); StatusWithMatchExpression result = MatchExpressionParser::parse( query ); ASSERT_TRUE( result.isOK() ); + boost::scoped_ptr<MatchExpression> destroy(result.getValue()); ASSERT( !result.getValue()->matchesBSON( BSON( "x" << 1 ) ) ); ASSERT( result.getValue()->matchesBSON( BSON( "x" << 2 ) ) ); @@ -62,6 +63,7 @@ namespace mongo { BSONObj query = BSON( "x" << BSON( "$gt" << 2 ) ); StatusWithMatchExpression result = MatchExpressionParser::parse( query ); ASSERT_TRUE( result.isOK() ); + boost::scoped_ptr<MatchExpression> destroy(result.getValue()); ASSERT( !result.getValue()->matchesBSON( BSON( "x" << 2 ) ) ); ASSERT( result.getValue()->matchesBSON( BSON( "x" << 3 ) ) ); @@ -71,6 +73,7 @@ namespace mongo { BSONObj query = BSON( "x" << BSON( "$lt" << 2 ) ); StatusWithMatchExpression result = MatchExpressionParser::parse( query ); ASSERT_TRUE( result.isOK() ); + boost::scoped_ptr<MatchExpression> destroy(result.getValue()); ASSERT( result.getValue()->matchesBSON( BSON( "x" << 1 ) ) ); ASSERT( !result.getValue()->matchesBSON( BSON( "x" << 2 ) ) ); @@ -81,6 +84,7 @@ namespace mongo { BSONObj query = BSON( "x" << BSON( "$gte" << 2 ) ); StatusWithMatchExpression result = MatchExpressionParser::parse( query ); ASSERT_TRUE( result.isOK() ); + boost::scoped_ptr<MatchExpression> destroy(result.getValue()); ASSERT( !result.getValue()->matchesBSON( BSON( "x" << 1 ) ) ); ASSERT( result.getValue()->matchesBSON( BSON( "x" << 2 ) ) ); @@ -91,6 +95,7 @@ namespace mongo { BSONObj query = BSON( "x" << BSON( "$lte" << 2 ) ); StatusWithMatchExpression result = MatchExpressionParser::parse( query ); ASSERT_TRUE( result.isOK() ); + boost::scoped_ptr<MatchExpression> destroy(result.getValue()); ASSERT( result.getValue()->matchesBSON( BSON( "x" << 1 ) ) ); ASSERT( result.getValue()->matchesBSON( BSON( "x" << 2 ) ) ); @@ -101,6 +106,7 @@ namespace mongo { BSONObj query = BSON( "x" << BSON( "$ne" << 2 ) ); StatusWithMatchExpression result = MatchExpressionParser::parse( query ); ASSERT_TRUE( result.isOK() ); + boost::scoped_ptr<MatchExpression> destroy(result.getValue()); ASSERT( result.getValue()->matchesBSON( BSON( "x" << 1 ) ) ); ASSERT( !result.getValue()->matchesBSON( BSON( "x" << 2 ) ) ); @@ -111,6 +117,7 @@ namespace mongo { BSONObj query = BSON( "x" << BSON( "$mod" << BSON_ARRAY( 3 << 2 ) ) ); StatusWithMatchExpression result = MatchExpressionParser::parse( query ); ASSERT_TRUE( result.isOK() ); + boost::scoped_ptr<MatchExpression> destroy1(result.getValue()); query = BSON( "x" << BSON( "$mod" << BSON_ARRAY( 3 ) ) ); result = MatchExpressionParser::parse( query ); @@ -137,6 +144,7 @@ namespace mongo { BSONObj query = BSON( "x" << BSON( "$mod" << BSON_ARRAY( 3 << 2 ) ) ); StatusWithMatchExpression result = MatchExpressionParser::parse( query ); ASSERT_TRUE( result.isOK() ); + boost::scoped_ptr<MatchExpression> destroy(result.getValue()); ASSERT( result.getValue()->matchesBSON( BSON( "x" << 5 ) ) ); ASSERT( !result.getValue()->matchesBSON( BSON( "x" << 4 ) ) ); @@ -147,6 +155,7 @@ namespace mongo { BSONObj query = BSON( "x" << BSON( "$mod" << BSON_ARRAY( 2 << "r" ) ) ); StatusWithMatchExpression result = MatchExpressionParser::parse( query ); ASSERT_TRUE( result.isOK() ); + boost::scoped_ptr<MatchExpression> destroy(result.getValue()); ASSERT( result.getValue()->matchesBSON( BSON( "x" << 2 ) ) ); ASSERT( result.getValue()->matchesBSON( BSON( "x" << 4 ) ) ); @@ -159,6 +168,7 @@ namespace mongo { BSONObj query = BSON( "x" << BSON( "$in" << BSON_ARRAY( 2 << 3 ) ) ); StatusWithMatchExpression result = MatchExpressionParser::parse( query ); ASSERT_TRUE( result.isOK() ); + boost::scoped_ptr<MatchExpression> destroy(result.getValue()); ASSERT( !result.getValue()->matchesBSON( BSON( "x" << 1 ) ) ); ASSERT( result.getValue()->matchesBSON( BSON( "x" << 2 ) ) ); @@ -172,6 +182,7 @@ namespace mongo { BSON( "$ref" << "coll" << "$id" << oid << "$db" << "db" ) ) ) ); StatusWithMatchExpression result = MatchExpressionParser::parse( query ); ASSERT_TRUE( result.isOK() ); + boost::scoped_ptr<MatchExpression> destroy(result.getValue()); OID oidx = OID::gen(); ASSERT( !result.getValue()->matchesBSON( @@ -208,6 +219,7 @@ namespace mongo { BSON( "$ref" << "coll" << "$id" << oid << "$db" << "db" ) ) ) ); StatusWithMatchExpression result = MatchExpressionParser::parse( query ); ASSERT_TRUE( result.isOK() ); + boost::scoped_ptr<MatchExpression> destroy(result.getValue()); OID oidx = OID::gen(); ASSERT( !result.getValue()->matchesBSON( @@ -270,6 +282,7 @@ namespace mongo { BSON( "$ref" << "coll" << "$id" << oid << "foo" << 12345 ) ) ) ); StatusWithMatchExpression result = MatchExpressionParser::parse( query ); ASSERT_TRUE( result.isOK() ); + boost::scoped_ptr<MatchExpression> destroy(result.getValue()); OID oidx = OID::gen(); ASSERT( !result.getValue()->matchesBSON( @@ -368,6 +381,7 @@ namespace mongo { BSONObj query = BSON( "a" << operand.obj() ); StatusWithMatchExpression result = MatchExpressionParser::parse( query ); ASSERT_TRUE( result.isOK() ); + boost::scoped_ptr<MatchExpression> destroy(result.getValue()); BSONObj matchFirst = BSON( "a" << "ax" ); BSONObj matchFirstRegex = BSONObjBuilder().appendRegex( "a", "^a", "" ).obj(); @@ -390,6 +404,7 @@ namespace mongo { BSONObj query = BSON( "x" << BSON( "$nin" << BSON_ARRAY( 2 << 3 ) ) ); StatusWithMatchExpression result = MatchExpressionParser::parse( query ); ASSERT_TRUE( result.isOK() ); + boost::scoped_ptr<MatchExpression> destroy(result.getValue()); ASSERT( result.getValue()->matchesBSON( BSON( "x" << 1 ) ) ); ASSERT( !result.getValue()->matchesBSON( BSON( "x" << 2 ) ) ); @@ -409,6 +424,7 @@ namespace mongo { BSONObj query = b.obj(); StatusWithMatchExpression result = MatchExpressionParser::parse( query ); ASSERT_TRUE( result.isOK() ); + boost::scoped_ptr<MatchExpression> destroy(result.getValue()); ASSERT( result.getValue()->matchesBSON( BSON( "x" << "abc" ) ) ); ASSERT( result.getValue()->matchesBSON( BSON( "x" << "ABC" ) ) ); @@ -419,6 +435,7 @@ namespace mongo { BSONObj query = BSON( "x" << BSON( "$regex" << "abc" << "$options" << "i" ) ); StatusWithMatchExpression result = MatchExpressionParser::parse( query ); ASSERT_TRUE( result.isOK() ); + boost::scoped_ptr<MatchExpression> destroy(result.getValue()); ASSERT( result.getValue()->matchesBSON( BSON( "x" << "abc" ) ) ); ASSERT( result.getValue()->matchesBSON( BSON( "x" << "ABC" ) ) ); @@ -430,6 +447,7 @@ namespace mongo { StatusWithMatchExpression result = MatchExpressionParser::parse( query ); log() << "result: " << result << endl; ASSERT_TRUE( result.isOK() ); + boost::scoped_ptr<MatchExpression> destroy(result.getValue()); ASSERT( result.getValue()->matchesBSON( BSON( "x" << "abc" ) ) ); ASSERT( result.getValue()->matchesBSON( BSON( "x" << "ABC" ) ) ); @@ -466,6 +484,7 @@ namespace mongo { BSONObj query = BSON( "x" << b.obj() ); StatusWithMatchExpression result = MatchExpressionParser::parse( query ); ASSERT_TRUE( result.isOK() ); + boost::scoped_ptr<MatchExpression> destroy(result.getValue()); ASSERT( result.getValue()->matchesBSON( BSON( "x" << "abc" ) ) ); ASSERT( !result.getValue()->matchesBSON( BSON( "y" << "AC" ) ) ); @@ -477,6 +496,7 @@ namespace mongo { BSONObj query = BSON( "x" << b.obj() ); StatusWithMatchExpression result = MatchExpressionParser::parse( query ); ASSERT_TRUE( result.isOK() ); + boost::scoped_ptr<MatchExpression> destroy(result.getValue()); ASSERT( !result.getValue()->matchesBSON( BSON( "x" << "abc" ) ) ); ASSERT( result.getValue()->matchesBSON( BSON( "y" << "AC" ) ) ); @@ -486,6 +506,7 @@ namespace mongo { BSONObj query = BSON( "x" << BSON( "$type" << String ) ); StatusWithMatchExpression result = MatchExpressionParser::parse( query ); ASSERT_TRUE( result.isOK() ); + boost::scoped_ptr<MatchExpression> destroy(result.getValue()); ASSERT( result.getValue()->matchesBSON( BSON( "x" << "abc" ) ) ); ASSERT( !result.getValue()->matchesBSON( BSON( "x" << 5 ) ) ); @@ -495,6 +516,7 @@ namespace mongo { BSONObj query = BSON( "x" << BSON( "$type" << (double)NumberDouble ) ); StatusWithMatchExpression result = MatchExpressionParser::parse( query ); ASSERT_TRUE( result.isOK() ); + boost::scoped_ptr<MatchExpression> destroy(result.getValue()); ASSERT( result.getValue()->matchesBSON( BSON( "x" << 5.3 ) ) ); ASSERT( !result.getValue()->matchesBSON( BSON( "x" << 5 ) ) ); @@ -504,6 +526,7 @@ namespace mongo { BSONObj query = BSON( "x" << BSON( "$type" << 1.5 ) ); StatusWithMatchExpression result = MatchExpressionParser::parse( query ); ASSERT_TRUE( result.isOK() ); + boost::scoped_ptr<MatchExpression> destroy(result.getValue()); ASSERT( !result.getValue()->matchesBSON( BSON( "x" << 5.3 ) ) ); ASSERT( !result.getValue()->matchesBSON( BSON( "x" << 5 ) ) ); @@ -513,6 +536,7 @@ namespace mongo { BSONObj query = BSON( "x" << BSON( "$type" << jstNULL ) ); StatusWithMatchExpression result = MatchExpressionParser::parse( query ); ASSERT_TRUE( result.isOK() ); + boost::scoped_ptr<MatchExpression> destroy(result.getValue()); ASSERT( !result.getValue()->matchesBSON( BSONObj() ) ); ASSERT( !result.getValue()->matchesBSON( BSON( "x" << 5 ) ) ); @@ -527,6 +551,7 @@ namespace mongo { BSONObj query = BSON( "x" << b.obj() ); StatusWithMatchExpression result = MatchExpressionParser::parse( query ); ASSERT_TRUE( result.isOK() ); + boost::scoped_ptr<MatchExpression> destroy(result.getValue()); ASSERT( !result.getValue()->matchesBSON( BSON( "x" << 5.3 ) ) ); ASSERT( !result.getValue()->matchesBSON( BSON( "x" << 5 ) ) ); diff --git a/src/mongo/db/matcher/expression_parser_test.cpp b/src/mongo/db/matcher/expression_parser_test.cpp index 0512f83499b..291c3465c8f 100644 --- a/src/mongo/db/matcher/expression_parser_test.cpp +++ b/src/mongo/db/matcher/expression_parser_test.cpp @@ -46,6 +46,8 @@ namespace mongo { ASSERT( result.getValue()->matchesBSON( BSON( "x" << 2 ) ) ); ASSERT( !result.getValue()->matchesBSON( BSON( "x" << 3 ) ) ); + + delete result.getValue(); } TEST( MatchExpressionParserTest, Multiple1 ) { @@ -58,16 +60,20 @@ namespace mongo { ASSERT( !result.getValue()->matchesBSON( BSON( "x" << 6 << "y" << 7 ) ) ); ASSERT( !result.getValue()->matchesBSON( BSON( "x" << 5 << "y" << 9 ) ) ); ASSERT( !result.getValue()->matchesBSON( BSON( "x" << 5 << "y" << 4 ) ) ); + + delete result.getValue(); } TEST( AtomicMatchExpressionTest, Simple1 ) { BSONObj query = BSON( "x" << 5 << "$atomic" << BSON( "$gt" << 5 << "$lt" << 8 ) ); StatusWithMatchExpression result = MatchExpressionParser::parse( query ); ASSERT_TRUE( result.isOK() ); + delete result.getValue(); query = BSON( "x" << 5 << "$isolated" << 1 ); result = MatchExpressionParser::parse( query ); ASSERT_TRUE( result.isOK() ); + delete result.getValue(); query = BSON( "x" << 5 << "y" << BSON( "$isolated" << 1 ) ); result = MatchExpressionParser::parse( query ); diff --git a/src/mongo/db/matcher/expression_parser_text_test.cpp b/src/mongo/db/matcher/expression_parser_text_test.cpp index b0d7166e5fe..b2f42018178 100644 --- a/src/mongo/db/matcher/expression_parser_text_test.cpp +++ b/src/mongo/db/matcher/expression_parser_text_test.cpp @@ -51,6 +51,7 @@ namespace mongo { TextMatchExpression* textExp = static_cast<TextMatchExpression*>( exp ); ASSERT_EQUALS( textExp->getQuery(), "awesome" ); ASSERT_EQUALS( textExp->getLanguage(), "english" ); + delete exp; } TEST( MatchExpressionParserText, Parse2 ) { diff --git a/src/mongo/db/matcher/expression_parser_tree_test.cpp b/src/mongo/db/matcher/expression_parser_tree_test.cpp index aba286fb4c3..a5cc3356463 100644 --- a/src/mongo/db/matcher/expression_parser_tree_test.cpp +++ b/src/mongo/db/matcher/expression_parser_tree_test.cpp @@ -49,6 +49,8 @@ namespace mongo { ASSERT( result.getValue()->matchesBSON( BSON( "y" << 2 ) ) ); ASSERT( !result.getValue()->matchesBSON( BSON( "x" << 3 ) ) ); ASSERT( !result.getValue()->matchesBSON( BSON( "y" << 1 ) ) ); + + delete result.getValue(); } TEST( MatchExpressionParserTreeTest, OREmbedded ) { @@ -62,6 +64,8 @@ namespace mongo { ASSERT( result.getValue()->matchesBSON( BSON( "y" << 2 ) ) ); ASSERT( !result.getValue()->matchesBSON( BSON( "x" << 3 ) ) ); ASSERT( !result.getValue()->matchesBSON( BSON( "y" << 1 ) ) ); + + delete result.getValue(); } @@ -77,6 +81,8 @@ namespace mongo { ASSERT( !result.getValue()->matchesBSON( BSON( "y" << 1 ) ) ); ASSERT( result.getValue()->matchesBSON( BSON( "x" << 1 << "y" << 2 ) ) ); ASSERT( !result.getValue()->matchesBSON( BSON( "x" << 2 << "y" << 2 ) ) ); + + delete result.getValue(); } TEST( MatchExpressionParserTreeTest, NOREmbedded ) { @@ -89,6 +95,8 @@ namespace mongo { ASSERT( !result.getValue()->matchesBSON( BSON( "y" << 2 ) ) ); ASSERT( result.getValue()->matchesBSON( BSON( "x" << 3 ) ) ); ASSERT( result.getValue()->matchesBSON( BSON( "y" << 1 ) ) ); + + delete result.getValue(); } TEST( MatchExpressionParserTreeTest, NOT1 ) { @@ -98,6 +106,8 @@ namespace mongo { ASSERT( result.getValue()->matchesBSON( BSON( "x" << 2 ) ) ); ASSERT( !result.getValue()->matchesBSON( BSON( "x" << 8 ) ) ); + + delete result.getValue(); } // Test a deep match tree that is not deep enough to hit the maximum depth limit. @@ -116,6 +126,7 @@ namespace mongo { BSONObj query = fromjson( ss.str() ); StatusWithMatchExpression result = MatchExpressionParser::parse( query ); ASSERT( result.isOK() ); + delete result.getValue(); } // Test a tree that exceeds the maximum depth limit. @@ -183,6 +194,8 @@ namespace mongo { ASSERT( !result.getValue()->matchesBSON( BSON( "x" << "abc" ) ) ); ASSERT( !result.getValue()->matchesBSON( BSON( "x" << "ABC" ) ) ); ASSERT( result.getValue()->matchesBSON( BSON( "x" << "AC" ) ) ); + + delete result.getValue(); } } diff --git a/src/mongo/db/query/lite_parsed_query_test.cpp b/src/mongo/db/query/lite_parsed_query_test.cpp index aceea8c9933..83495cec026 100644 --- a/src/mongo/db/query/lite_parsed_query_test.cpp +++ b/src/mongo/db/query/lite_parsed_query_test.cpp @@ -48,6 +48,7 @@ namespace { false, // explain &lpq); ASSERT_OK(result); + delete lpq; } TEST(LiteParsedQueryTest, InitSortOrderString) { @@ -71,6 +72,7 @@ namespace { &lpq); ASSERT_OK(result); ASSERT_EQUALS(BSON("x" << 5 ), lpq->getFilter()); + delete lpq; } TEST(LiteParsedQueryTest, NumToReturn) { @@ -84,6 +86,7 @@ namespace { ASSERT_OK(result); ASSERT_EQUALS(6, lpq->getNumToReturn()); ASSERT(lpq->wantMore()); + delete lpq; lpq = NULL; result = LiteParsedQuery::make("testns", 5, -6, 9, BSON( "x" << 5 ), BSONObj(), @@ -95,6 +98,7 @@ namespace { ASSERT_OK(result); ASSERT_EQUALS(6, lpq->getNumToReturn()); ASSERT(!lpq->wantMore()); + delete lpq; } TEST(LiteParsedQueryTest, MinFieldsNotPrefixOfMax) { diff --git a/src/mongo/db/query/parsed_projection_test.cpp b/src/mongo/db/query/parsed_projection_test.cpp index 20beaa70a6c..df36aef927b 100644 --- a/src/mongo/db/query/parsed_projection_test.cpp +++ b/src/mongo/db/query/parsed_projection_test.cpp @@ -45,21 +45,21 @@ namespace { // creation function // - ParsedProjection* createParsedProjection(const BSONObj& query, const BSONObj& projObj) { + auto_ptr<ParsedProjection> createParsedProjection(const BSONObj& query, const BSONObj& projObj) { StatusWithMatchExpression swme = MatchExpressionParser::parse(query); ASSERT(swme.isOK()); - MatchExpression* queryMatchExpr = swme.getValue(); + boost::scoped_ptr<MatchExpression> queryMatchExpr(swme.getValue()); ParsedProjection* out = NULL; - Status status = ParsedProjection::make(projObj, queryMatchExpr, &out); + Status status = ParsedProjection::make(projObj, queryMatchExpr.get(), &out); if (!status.isOK()) { FAIL(mongoutils::str::stream() << "failed to parse projection " << projObj << " (query: " << query << "): " << status.toString()); } ASSERT(out); - return out; + return auto_ptr<ParsedProjection>(out); } - ParsedProjection* createParsedProjection(const char* queryStr, const char* projStr) { + auto_ptr<ParsedProjection> createParsedProjection(const char* queryStr, const char* projStr) { BSONObj query = fromjson(queryStr); BSONObj projObj = fromjson(projStr); return createParsedProjection(query, projObj); @@ -74,9 +74,10 @@ namespace { BSONObj projObj = fromjson(projStr); StatusWithMatchExpression swme = MatchExpressionParser::parse(query); ASSERT(swme.isOK()); - MatchExpression* queryMatchExpr = swme.getValue(); + boost::scoped_ptr<MatchExpression> queryMatchExpr(swme.getValue()); ParsedProjection* out = NULL; - Status status = ParsedProjection::make(projObj, queryMatchExpr, &out); + Status status = ParsedProjection::make(projObj, queryMatchExpr.get(), &out); + boost::scoped_ptr<ParsedProjection> destroy(out); ASSERT(!status.isOK()); } @@ -183,6 +184,7 @@ namespace { BSONObj projObj = fromjson("{'a.$': 1}"); Status status = ParsedProjection::make(projObj, queryMatchExpr.get(), &out); ASSERT(!status.isOK()); + boost::scoped_ptr<ParsedProjection> destroy(out); // Projecting onto empty field should fail. BSONObj emptyFieldProjObj = fromjson("{'.$': 1}"); diff --git a/src/mongo/db/query/plan_cache_test.cpp b/src/mongo/db/query/plan_cache_test.cpp index be7c098c955..726f6378883 100644 --- a/src/mongo/db/query/plan_cache_test.cpp +++ b/src/mongo/db/query/plan_cache_test.cpp @@ -358,7 +358,8 @@ namespace { PlanCache planCache; auto_ptr<CanonicalQuery> cq(canonicalize("{a: 1}")); std::vector<QuerySolution*> solns; - ASSERT_NOT_OK(planCache.add(*cq, solns, createDecision(1U))); + boost::scoped_ptr<PlanRankingDecision> decision(createDecision(1U)); + ASSERT_NOT_OK(planCache.add(*cq, solns, decision.get())); } TEST(PlanCacheTest, AddValidSolution) { @@ -444,6 +445,7 @@ namespace { class CachePlanSelectionTest : public mongo::unittest::Test { protected: void setUp() { + cq = NULL; params.options = QueryPlannerParams::INCLUDE_COLLSCAN; addIndex(BSON("_id" << 1)); } @@ -530,7 +532,18 @@ namespace { const BSONObj& minObj, const BSONObj& maxObj, bool snapshot) { + + // Clean up any previous state from a call to runQueryFull + delete cq; + cq = NULL; + + for (vector<QuerySolution*>::iterator it = solns.begin(); it != solns.end(); ++it) { + delete *it; + } + solns.clear(); + + Status s = CanonicalQuery::canonicalize(ns, query, sort, proj, skip, limit, hint, minObj, maxObj, snapshot, false, // explain @@ -630,6 +643,7 @@ namespace { s = QueryPlanner::planFromCache(*scopedCq.get(), params, cachedSoln, &out, &backupOut); ASSERT_OK(s); + std::auto_ptr<QuerySolution> cleanBackup(backupOut); return out; } @@ -701,6 +715,7 @@ namespace { QuerySolution* bestSoln = firstMatchingSolution(solnJson); QuerySolution* planSoln = planQueryFromCache(query, sort, proj, *bestSoln); assertSolutionMatches(planSoln, solnJson); + delete planSoln; } /** diff --git a/src/mongo/db/query/query_planner_test.cpp b/src/mongo/db/query/query_planner_test.cpp index 562f65ff054..8467a5a70ff 100644 --- a/src/mongo/db/query/query_planner_test.cpp +++ b/src/mongo/db/query/query_planner_test.cpp @@ -54,6 +54,7 @@ namespace { class QueryPlannerTest : public mongo::unittest::Test { protected: void setUp() { + cq = NULL; internalQueryPlannerEnableHashIntersection = true; params.options = QueryPlannerParams::INCLUDE_COLLSCAN; addIndex(BSON("_id" << 1)); @@ -148,6 +149,15 @@ namespace { const BSONObj& minObj, const BSONObj& maxObj, bool snapshot) { + + // Clean up any previous state from a call to runQueryFull + delete cq; + cq = NULL; + + for (vector<QuerySolution*>::iterator it = solns.begin(); it != solns.end(); ++it) { + delete *it; + } + solns.clear(); Status s = CanonicalQuery::canonicalize(ns, query, sort, proj, skip, limit, hint, minObj, maxObj, snapshot, @@ -200,6 +210,13 @@ namespace { const BSONObj& minObj, const BSONObj& maxObj, bool snapshot) { + delete cq; + cq = NULL; + + for (vector<QuerySolution*>::iterator it = solns.begin(); it != solns.end(); ++it) { + delete *it; + } + solns.clear(); Status s = CanonicalQuery::canonicalize(ns, query, sort, proj, skip, limit, hint, minObj, maxObj, snapshot, @@ -4996,13 +5013,13 @@ namespace { ASSERT_OK(cqStatus); boost::scoped_ptr<CanonicalQuery> scopedCq(cq); - PlanCacheIndexTree* indexTree = new PlanCacheIndexTree(); + boost::scoped_ptr<PlanCacheIndexTree> indexTree(new PlanCacheIndexTree()); indexTree->setIndexEntry(IndexEntry(BSON("a" << 1))); map<BSONObj, size_t> indexMap; // Null filter. - Status s = QueryPlanner::tagAccordingToCache(NULL, indexTree, indexMap); + Status s = QueryPlanner::tagAccordingToCache(NULL, indexTree.get(), indexMap); ASSERT_NOT_OK(s); // Null indexTree. @@ -5010,12 +5027,12 @@ namespace { ASSERT_NOT_OK(s); // Index not found. - s = QueryPlanner::tagAccordingToCache(scopedCq->root(), indexTree, indexMap); + s = QueryPlanner::tagAccordingToCache(scopedCq->root(), indexTree.get(), indexMap); ASSERT_NOT_OK(s); // Index found once added to the map. indexMap[BSON("a" << 1)] = 0; - s = QueryPlanner::tagAccordingToCache(scopedCq->root(), indexTree, indexMap); + s = QueryPlanner::tagAccordingToCache(scopedCq->root(), indexTree.get(), indexMap); ASSERT_OK(s); // Regenerate canonical query in order to clear tags. @@ -5027,7 +5044,7 @@ namespace { PlanCacheIndexTree* child = new PlanCacheIndexTree(); child->setIndexEntry(IndexEntry(BSON("a" << 1))); indexTree->children.push_back(child); - s = QueryPlanner::tagAccordingToCache(scopedCq->root(), indexTree, indexMap); + s = QueryPlanner::tagAccordingToCache(scopedCq->root(), indexTree.get(), indexMap); ASSERT_NOT_OK(s); } diff --git a/src/mongo/db/query/query_planner_test_lib.cpp b/src/mongo/db/query/query_planner_test_lib.cpp index 8b150aaf19c..a95f406721e 100644 --- a/src/mongo/db/query/query_planner_test_lib.cpp +++ b/src/mongo/db/query/query_planner_test_lib.cpp @@ -53,9 +53,9 @@ namespace { if (!swme.isOK()) { return false; } - MatchExpression* root = swme.getValue(); - CanonicalQuery::sortTree(root); - return trueFilterNode->filter->equivalent(root); + const boost::scoped_ptr<MatchExpression> root(swme.getValue()); + CanonicalQuery::sortTree(root.get()); + return trueFilterNode->filter->equivalent(root.get()); } void appendIntervalBound(BSONObjBuilder& bob, BSONElement& el) { diff --git a/src/mongo/db/query/query_planner_text_test.cpp b/src/mongo/db/query/query_planner_text_test.cpp index 488be093681..2925a2ff7da 100644 --- a/src/mongo/db/query/query_planner_text_test.cpp +++ b/src/mongo/db/query/query_planner_text_test.cpp @@ -51,6 +51,7 @@ namespace { class QueryPlannerTest : public mongo::unittest::Test { protected: void setUp() { + cq = NULL; params.options = QueryPlannerParams::INCLUDE_COLLSCAN; addIndex(BSON("_id" << 1)); } @@ -140,6 +141,15 @@ namespace { const BSONObj& minObj, const BSONObj& maxObj, bool snapshot) { + + // Clean up any previous state from a call to runQueryFull + delete cq; + cq = NULL; + + for (vector<QuerySolution*>::iterator it = solns.begin(); it != solns.end(); ++it) { + delete *it; + } + solns.clear(); Status s = CanonicalQuery::canonicalize(ns, query, sort, proj, skip, limit, hint, minObj, maxObj, snapshot, @@ -192,6 +202,14 @@ namespace { const BSONObj& minObj, const BSONObj& maxObj, bool snapshot) { + + delete cq; + cq = NULL; + + for (vector<QuerySolution*>::iterator it = solns.begin(); it != solns.end(); ++it) { + delete *it; + } + solns.clear(); Status s = CanonicalQuery::canonicalize(ns, query, sort, proj, skip, limit, hint, minObj, maxObj, snapshot, diff --git a/src/mongo/db/range_deleter_test.cpp b/src/mongo/db/range_deleter_test.cpp index 098d4e7bfaa..71bcb5e2267 100644 --- a/src/mongo/db/range_deleter_test.cpp +++ b/src/mongo/db/range_deleter_test.cpp @@ -63,8 +63,11 @@ namespace { // Should not be able to queue deletes if deleter workers were not started. TEST(QueueDelete, CantAfterStop) { - mongo::repl::setGlobalReplicationCoordinator( - new mongo::repl::ReplicationCoordinatorMock(replSettings)); + boost::scoped_ptr<mongo::repl::ReplicationCoordinatorMock> mock( + new mongo::repl::ReplicationCoordinatorMock(replSettings)); + + mongo::repl::setGlobalReplicationCoordinator(mock.get()); + RangeDeleterMockEnv* env = new RangeDeleterMockEnv(); RangeDeleter deleter(env); @@ -80,16 +83,23 @@ namespace { &errMsg)); ASSERT_FALSE(errMsg.empty()); ASSERT_FALSE(env->deleteOccured()); + + mongo::repl::setGlobalReplicationCoordinator(NULL); } // Should not start delete if the set of cursors that were open when the // delete was queued is still open. TEST(QueuedDelete, ShouldWaitCursor) { - mongo::repl::setGlobalReplicationCoordinator( - new mongo::repl::ReplicationCoordinatorMock(replSettings)); const string ns("test.user"); + + boost::scoped_ptr<mongo::repl::ReplicationCoordinatorMock> mock( + new mongo::repl::ReplicationCoordinatorMock(replSettings)); + + mongo::repl::setGlobalReplicationCoordinator(mock.get()); + RangeDeleterMockEnv* env = new RangeDeleterMockEnv(); RangeDeleter deleter(env); + deleter.startWorkers(); env->addCursorId(ns, 345); @@ -124,15 +134,22 @@ namespace { ASSERT_TRUE(deletedChunk.max.equal(BSON("x" << 10))); deleter.stopWorkers(); + + mongo::repl::setGlobalReplicationCoordinator(NULL); } // Should terminate when stop is requested. TEST(QueuedDelete, StopWhileWaitingCursor) { - mongo::repl::setGlobalReplicationCoordinator( - new mongo::repl::ReplicationCoordinatorMock(replSettings)); const string ns("test.user"); + + boost::scoped_ptr<mongo::repl::ReplicationCoordinatorMock> mock( + new mongo::repl::ReplicationCoordinatorMock(replSettings)); + + mongo::repl::setGlobalReplicationCoordinator(mock.get()); + RangeDeleterMockEnv* env = new RangeDeleterMockEnv(); RangeDeleter deleter(env); + deleter.startWorkers(); env->addCursorId(ns, 345); @@ -152,6 +169,8 @@ namespace { deleter.stopWorkers(); ASSERT_FALSE(env->deleteOccured()); + + mongo::repl::setGlobalReplicationCoordinator(NULL); } static void rangeDeleterDeleteNow(RangeDeleter* deleter, @@ -164,11 +183,16 @@ namespace { // Should not start delete if the set of cursors that were open when the // deleteNow method is called is still open. TEST(ImmediateDelete, ShouldWaitCursor) { - mongo::repl::setGlobalReplicationCoordinator( - new mongo::repl::ReplicationCoordinatorMock(replSettings)); const string ns("test.user"); + + boost::scoped_ptr<mongo::repl::ReplicationCoordinatorMock> mock( + new mongo::repl::ReplicationCoordinatorMock(replSettings)); + + mongo::repl::setGlobalReplicationCoordinator(mock.get()); + RangeDeleterMockEnv* env = new RangeDeleterMockEnv(); RangeDeleter deleter(env); + deleter.startWorkers(); env->addCursorId(ns, 345); @@ -210,15 +234,22 @@ namespace { ASSERT_TRUE(deletedChunk.shardKeyPattern.equal(BSON("x" << 1))); deleter.stopWorkers(); + + mongo::repl::setGlobalReplicationCoordinator(NULL); } // Should terminate when stop is requested. TEST(ImmediateDelete, StopWhileWaitingCursor) { - mongo::repl::setGlobalReplicationCoordinator( - new mongo::repl::ReplicationCoordinatorMock(replSettings)); const string ns("test.user"); + + boost::scoped_ptr<mongo::repl::ReplicationCoordinatorMock> mock( + new mongo::repl::ReplicationCoordinatorMock(replSettings)); + + mongo::repl::setGlobalReplicationCoordinator(mock.get()); + RangeDeleterMockEnv* env = new RangeDeleterMockEnv(); RangeDeleter deleter(env); + deleter.startWorkers(); env->addCursorId(ns, 345); @@ -250,6 +281,8 @@ namespace { boost::posix_time::seconds(MAX_IMMEDIATE_DELETE_WAIT_SECS))); ASSERT_FALSE(env->deleteOccured()); + + mongo::repl::setGlobalReplicationCoordinator(NULL); } // Tests the interaction of multiple deletes queued with different states. @@ -258,13 +291,17 @@ namespace { // other one is waiting for an open cursor. The test then makes sure that the // deletes are performed in the right order. TEST(MixedDeletes, MultipleDeletes) { - mongo::repl::setGlobalReplicationCoordinator( - new mongo::repl::ReplicationCoordinatorMock(replSettings)); const string blockedNS("foo.bar"); const string ns("test.user"); + boost::scoped_ptr<mongo::repl::ReplicationCoordinatorMock> mock( + new mongo::repl::ReplicationCoordinatorMock(replSettings)); + + mongo::repl::setGlobalReplicationCoordinator(mock.get()); + RangeDeleterMockEnv* env = new RangeDeleterMockEnv(); RangeDeleter deleter(env); + deleter.startWorkers(); env->addCursorId(blockedNS, 345); @@ -357,14 +394,20 @@ namespace { ASSERT_TRUE(deleted3.shardKeyPattern.equal(BSON("x" << 1))); deleter.stopWorkers(); + + mongo::repl::setGlobalReplicationCoordinator(NULL); } // Should not be able to delete ranges that overlaps with a black listed range. TEST(BlackList, CantDeleteBlackListed) { - mongo::repl::setGlobalReplicationCoordinator( - new mongo::repl::ReplicationCoordinatorMock(replSettings)); + boost::scoped_ptr<mongo::repl::ReplicationCoordinatorMock> mock( + new mongo::repl::ReplicationCoordinatorMock(replSettings)); + + mongo::repl::setGlobalReplicationCoordinator(mock.get()); + RangeDeleterMockEnv* env = new RangeDeleterMockEnv(); RangeDeleter deleter(env); + deleter.startWorkers(); const string ns("test.user"); @@ -399,10 +442,14 @@ namespace { // Should not be able to black list a range that overlaps with a range that is // already blacklisted. TEST(BlackList, CantDoubleBlackList) { - mongo::repl::setGlobalReplicationCoordinator( - new mongo::repl::ReplicationCoordinatorMock(replSettings)); + boost::scoped_ptr<mongo::repl::ReplicationCoordinatorMock> mock( + new mongo::repl::ReplicationCoordinatorMock(replSettings)); + + mongo::repl::setGlobalReplicationCoordinator(mock.get()); + RangeDeleterMockEnv* env = new RangeDeleterMockEnv(); RangeDeleter deleter(env); + const string ns("test.user"); string errMsg; @@ -418,16 +465,23 @@ namespace { ASSERT_FALSE(errMsg.empty()); deleter.stopWorkers(); + + mongo::repl::setGlobalReplicationCoordinator(NULL); } // Should not be able to black list a range that overlaps with a range that is already // queued for deletion. TEST(BlackList, CantBlackListQueued) { - mongo::repl::setGlobalReplicationCoordinator( - new mongo::repl::ReplicationCoordinatorMock(replSettings)); + boost::scoped_ptr<mongo::repl::ReplicationCoordinatorMock> mock( + new mongo::repl::ReplicationCoordinatorMock(replSettings)); + + mongo::repl::setGlobalReplicationCoordinator(mock.get()); + RangeDeleterMockEnv* env = new RangeDeleterMockEnv(); RangeDeleter deleter(env); + const string ns("test.user"); + deleter.startWorkers(); // Set cursors on NS so deletes cannot be processed immediately. @@ -454,15 +508,21 @@ namespace { ASSERT_TRUE(errMsg.empty()); deleter.stopWorkers(); + + mongo::repl::setGlobalReplicationCoordinator(NULL); } // Should not be able to black list a range that overlaps the range of an // immediate delete that is currently in progress. TEST(BlackList, CantBlackListImmediateInProgress) { - mongo::repl::setGlobalReplicationCoordinator( - new mongo::repl::ReplicationCoordinatorMock(replSettings)); + boost::scoped_ptr<mongo::repl::ReplicationCoordinatorMock> mock( + new mongo::repl::ReplicationCoordinatorMock(replSettings)); + + mongo::repl::setGlobalReplicationCoordinator(mock.get()); + RangeDeleterMockEnv* env = new RangeDeleterMockEnv(); RangeDeleter deleter(env); + const string ns("test.user"); env->pauseDeletes(); @@ -494,15 +554,21 @@ namespace { ASSERT_TRUE(blErrMsg.empty()); deleter.stopWorkers(); + + mongo::repl::setGlobalReplicationCoordinator(NULL); } // Undo black list should only work if the range given exactly match with an // existing black listed range. TEST(BlackList, UndoShouldBeExact) { - mongo::repl::setGlobalReplicationCoordinator( - new mongo::repl::ReplicationCoordinatorMock(replSettings)); + boost::scoped_ptr<mongo::repl::ReplicationCoordinatorMock> mock( + new mongo::repl::ReplicationCoordinatorMock(replSettings)); + + mongo::repl::setGlobalReplicationCoordinator(mock.get()); + RangeDeleterMockEnv* env = new RangeDeleterMockEnv(); RangeDeleter deleter(env); + const string ns("test.user"); ASSERT_TRUE(deleter.addToBlackList(ns, BSON("x" << 1234), BSON("x" << 8952), @@ -523,10 +589,14 @@ namespace { // Should be able to delete the range again once the black list has been undone. TEST(BlackList, UndoBlackList) { - mongo::repl::setGlobalReplicationCoordinator( - new mongo::repl::ReplicationCoordinatorMock(replSettings)); + boost::scoped_ptr<mongo::repl::ReplicationCoordinatorMock> mock( + new mongo::repl::ReplicationCoordinatorMock(replSettings)); + + mongo::repl::setGlobalReplicationCoordinator(mock.get()); + RangeDeleterMockEnv* env = new RangeDeleterMockEnv(); RangeDeleter deleter(env); + const string ns("test.user"); string errMsg; @@ -554,12 +624,17 @@ namespace { ASSERT_TRUE(errMsg.empty()); deleter.stopWorkers(); + + mongo::repl::setGlobalReplicationCoordinator(NULL); } // Black listing should only affect the specified namespace. TEST(BlackList, NSIsolation) { - mongo::repl::setGlobalReplicationCoordinator( - new mongo::repl::ReplicationCoordinatorMock(replSettings)); + boost::scoped_ptr<mongo::repl::ReplicationCoordinatorMock> mock( + new mongo::repl::ReplicationCoordinatorMock(replSettings)); + + mongo::repl::setGlobalReplicationCoordinator(mock.get()); + RangeDeleterMockEnv* env = new RangeDeleterMockEnv(); RangeDeleter deleter(env); @@ -574,6 +649,8 @@ namespace { NULL /* errMsg not needed */)); deleter.stopWorkers(); + + mongo::repl::setGlobalReplicationCoordinator(NULL); } } // unnamed namespace diff --git a/src/mongo/db/storage/record_store_test_manyiter.cpp b/src/mongo/db/storage/record_store_test_manyiter.cpp index 1391637ecf7..110db208a58 100644 --- a/src/mongo/db/storage/record_store_test_manyiter.cpp +++ b/src/mongo/db/storage/record_store_test_manyiter.cpp @@ -65,6 +65,8 @@ namespace mongo { ASSERT_EQUALS( DiskLoc(), rIter->getNext() ); ASSERT( rIter->isEOF() ); ASSERT_EQUALS( DiskLoc(), rIter->curr() ); + + delete rIter; } } } @@ -123,6 +125,8 @@ namespace mongo { ASSERT_EQUALS( DiskLoc(), rIter->getNext() ); ASSERT( rIter->isEOF() ); ASSERT_EQUALS( DiskLoc(), rIter->curr() ); + + delete rIter; } ASSERT( remain.empty() ); } diff --git a/src/mongo/db/storage/record_store_test_recorditer.cpp b/src/mongo/db/storage/record_store_test_recorditer.cpp index 81e92be431a..924586d2d15 100644 --- a/src/mongo/db/storage/record_store_test_recorditer.cpp +++ b/src/mongo/db/storage/record_store_test_recorditer.cpp @@ -99,6 +99,8 @@ namespace mongo { ASSERT_EQUALS( DiskLoc(), it->getNext() ); ASSERT( it->isEOF() ); ASSERT_EQUALS( DiskLoc(), it->curr() ); + + delete it; } } @@ -159,6 +161,8 @@ namespace mongo { ASSERT_EQUALS( DiskLoc(), it->getNext() ); ASSERT( it->isEOF() ); ASSERT_EQUALS( DiskLoc(), it->curr() ); + + delete it; } } @@ -219,6 +223,8 @@ namespace mongo { ASSERT_EQUALS( DiskLoc(), it->getNext() ); ASSERT( it->isEOF() ); ASSERT_EQUALS( DiskLoc(), it->curr() ); + + delete it; } } @@ -279,6 +285,8 @@ namespace mongo { ASSERT_EQUALS( DiskLoc(), it->getNext() ); ASSERT( it->isEOF() ); ASSERT_EQUALS( DiskLoc(), it->curr() ); + + delete it; } } diff --git a/src/mongo/db/storage/record_store_test_repairiter.cpp b/src/mongo/db/storage/record_store_test_repairiter.cpp index 6aaa91bc892..ee632bbce71 100644 --- a/src/mongo/db/storage/record_store_test_repairiter.cpp +++ b/src/mongo/db/storage/record_store_test_repairiter.cpp @@ -60,6 +60,8 @@ namespace mongo { ASSERT_EQUALS( DiskLoc(), it->getNext() ); ASSERT( it->isEOF() ); ASSERT_EQUALS( DiskLoc(), it->curr() ); + + delete it; } } @@ -114,6 +116,8 @@ namespace mongo { ASSERT_EQUALS( DiskLoc(), it->getNext() ); ASSERT( it->isEOF() ); ASSERT_EQUALS( DiskLoc(), it->curr() ); + + delete it; } } diff --git a/src/mongo/s/balancer_policy_tests.cpp b/src/mongo/s/balancer_policy_tests.cpp index 5f4fed5ead2..002fba80a77 100644 --- a/src/mongo/s/balancer_policy_tests.cpp +++ b/src/mongo/s/balancer_policy_tests.cpp @@ -70,9 +70,8 @@ namespace mongo { info["shard0"] = ShardInfo(0, 2, false); info["shard1"] = ShardInfo(0, 0, false); - MigrateInfo* c = NULL; DistributionStatus status(info, chunkMap.map()); - c = BalancerPolicy::balance( "ns", status, 1 ); + boost::scoped_ptr<MigrateInfo> c(BalancerPolicy::balance( "ns", status, 1 )); ASSERT( c ); } @@ -118,9 +117,8 @@ namespace mongo { info["shard0"] = ShardInfo(0, 2, false); info["shard1"] = ShardInfo(0, 0, false); - MigrateInfo* c = NULL; DistributionStatus status(info, chunkMap.map()); - c = BalancerPolicy::balance( "ns", status, 1 ); + boost::scoped_ptr<MigrateInfo> c(BalancerPolicy::balance( "ns", status, 1 )); ASSERT( c ); ASSERT_EQUALS( 30, c->chunk.max["x"].numberInt() ); } @@ -151,7 +149,7 @@ namespace mongo { limitsMap["shard1"] = ShardInfo(0LL, 0LL, false); DistributionStatus status(limitsMap, chunkMap.map()); - MigrateInfo* c = BalancerPolicy::balance( "ns", status, 0 ); + boost::scoped_ptr<MigrateInfo> c(BalancerPolicy::balance( "ns", status, 0 )); ASSERT( c ); ASSERT_EQUALS( c->to , "shard1" ); ASSERT_EQUALS( c->from , "shard0" ); @@ -182,7 +180,7 @@ namespace mongo { limitsMap["shard1"] = ShardInfo(0, 0, true); DistributionStatus status(limitsMap, chunkMap.map()); - MigrateInfo* c = BalancerPolicy::balance( "ns", status, 0 ); + boost::scoped_ptr<MigrateInfo> c(BalancerPolicy::balance( "ns", status, 0 )); ASSERT( ! c ); } @@ -213,7 +211,7 @@ namespace mongo { limitsMap["shard2"] = ShardInfo(0, 1, true); DistributionStatus status(limitsMap, chunkMap.map()); - MigrateInfo* c = BalancerPolicy::balance( "ns", status, 0 ); + boost::scoped_ptr<MigrateInfo> c(BalancerPolicy::balance( "ns", status, 0 )); ASSERT( ! c ); } @@ -276,7 +274,7 @@ namespace mongo { shards["shard2"] = ShardInfo(0, 5, false); DistributionStatus d(shards, chunks.map()); - MigrateInfo* m = BalancerPolicy::balance( "ns", d, 0 ); + boost::scoped_ptr<MigrateInfo> m(BalancerPolicy::balance( "ns", d, 0 )); ASSERT( m ); ASSERT_EQUALS( "shard2" , m->to ); } @@ -304,7 +302,7 @@ namespace mongo { d.addTagRange( TagRange( BSON( "x" << -1 ), BSON( "x" << 7 ) , "a" ) ); d.addTagRange( TagRange( BSON( "x" << 7 ), BSON( "x" << 1000 ) , "b" ) ); - MigrateInfo* m = BalancerPolicy::balance( "ns", d, 0 ); + boost::scoped_ptr<MigrateInfo> m(BalancerPolicy::balance( "ns", d, 0 )); if ( ! m ) break; @@ -315,7 +313,7 @@ namespace mongo { ASSERT_EQUALS( "shard2" , m->to ); } - moveChunk( chunks, m ); + moveChunk( chunks, m.get() ); } ASSERT_EQUALS( 7U , chunks.mutableMap()["shard0"]->size() ); @@ -343,11 +341,11 @@ namespace mongo { DistributionStatus d(shards, chunks.map()); d.addTagRange( TagRange( BSON( "x" << -1 ), BSON( "x" << 1000 ) , "a" ) ); - MigrateInfo* m = BalancerPolicy::balance( "ns", d, 0 ); + boost::scoped_ptr<MigrateInfo> m(BalancerPolicy::balance( "ns", d, 0 )); if ( ! m ) break; - moveChunk( chunks, m ); + moveChunk( chunks, m.get() ); } const size_t shard0Size = chunks.mutableMap()["shard0"]->size(); @@ -416,7 +414,7 @@ namespace mongo { shards["shard2"] = ShardInfo(0, 6, false); DistributionStatus d(shards, chunks.map()); - MigrateInfo* m = BalancerPolicy::balance( "ns", d, 0 ); + scoped_ptr<MigrateInfo> m(BalancerPolicy::balance( "ns", d, 0 )); ASSERT( m ); ASSERT_EQUALS( "shard2" , m->from ); ASSERT_EQUALS( "shard1" , m->to ); @@ -445,7 +443,7 @@ namespace mongo { shards["shard2"] = ShardInfo(0, 4, false); DistributionStatus d(shards, chunks.map()); - MigrateInfo* m = BalancerPolicy::balance( "ns", d, 0 ); + boost::scoped_ptr<MigrateInfo> m(BalancerPolicy::balance( "ns", d, 0 )); ASSERT( !m ); } @@ -512,14 +510,14 @@ namespace mongo { for (int i = 0; i < numChunks; i++) { DistributionStatus d(shards, chunks.map()); - MigrateInfo* m = BalancerPolicy::balance( "ns", d, i != 0 ); + boost::scoped_ptr<MigrateInfo> m(BalancerPolicy::balance( "ns", d, i != 0 )); if (!m) { log() << "Finished with test moves." << endl; break; } - moveChunk(chunks, m); + moveChunk(chunks, m.get()); { ShardInfo& info = shards[m->from]; diff --git a/src/mongo/scripting/bson_template_evaluator_test.cpp b/src/mongo/scripting/bson_template_evaluator_test.cpp index d56f9ebcce3..5e4929a6bfe 100644 --- a/src/mongo/scripting/bson_template_evaluator_test.cpp +++ b/src/mongo/scripting/bson_template_evaluator_test.cpp @@ -66,16 +66,16 @@ namespace mongo { TEST(BSONTemplateEvaluatorTest, RAND_INT) { - BsonTemplateEvaluator *t = new BsonTemplateEvaluator(); + BsonTemplateEvaluator t; int randValue1, randValue2; - common_rand_tests("#RAND_INT", t); + common_rand_tests("#RAND_INT", &t); // Test success with a single element BSONObjBuilder builder5; BSONObj randObj = BSON( "#RAND_INT" << BSON_ARRAY( 1 << 5 ) ); ASSERT_EQUALS( BsonTemplateEvaluator::StatusSuccess, - t->evaluate(BSON("randField" << randObj), builder5) ); + t.evaluate(BSON("randField" << randObj), builder5) ); BSONObj obj5 = builder5.obj(); ASSERT_EQUALS(obj5.nFields(), 1); randValue1 = obj5["randField"].numberInt(); @@ -86,14 +86,14 @@ namespace mongo { BSONObjBuilder builder6; randObj = BSON( "#RAND_INT" << BSON_ARRAY( 1 << 5 ) ); ASSERT_EQUALS( BsonTemplateEvaluator::StatusSuccess, - t->evaluate(BSON("randField1" << randObj << + t.evaluate(BSON("randField1" << randObj << "randField2" << randObj), builder6) ); // Test success with #RAND_INT as first element BSONObjBuilder builder8; randObj = BSON( "#RAND_INT" << BSON_ARRAY( 1 << 5 ) ); ASSERT_EQUALS( BsonTemplateEvaluator::StatusSuccess, - t->evaluate(BSON("randField" << randObj << "hello" << "world" << + t.evaluate(BSON("randField" << randObj << "hello" << "world" << "id" << 1), builder8) ); BSONObj obj8 = builder8.obj(); ASSERT_EQUALS(obj8.nFields(), 3); @@ -105,7 +105,7 @@ namespace mongo { BSONObjBuilder builder9; randObj = BSON( "#RAND_INT" << BSON_ARRAY( 1 << 5 ) ); ASSERT_EQUALS( BsonTemplateEvaluator::StatusSuccess, - t->evaluate(BSON("id" << 1 << "randField" << randObj << "hello" << + t.evaluate(BSON("id" << 1 << "randField" << randObj << "hello" << "world"), builder9) ); BSONObj obj9 = builder9.obj(); ASSERT_EQUALS(obj9.nFields(), 3); @@ -117,7 +117,7 @@ namespace mongo { BSONObjBuilder builder10; randObj = BSON( "#RAND_INT" << BSON_ARRAY( 1 << 5 ) ); ASSERT_EQUALS( BsonTemplateEvaluator::StatusSuccess, - t->evaluate(BSON("randField1" << randObj << "hello" << + t.evaluate(BSON("randField1" << randObj << "hello" << "world" << "randField2" << randObj), builder10) ); BSONObj obj10 = builder10.obj(); ASSERT_EQUALS(obj10.nFields(), 3); @@ -132,7 +132,7 @@ namespace mongo { BSONObjBuilder builder11; randObj = BSON( "#RAND_INT" << BSON_ARRAY( 1 << 5 ) ); ASSERT_EQUALS( BsonTemplateEvaluator::StatusSuccess, - t->evaluate(BSON("testArray" << BSON_ARRAY( 0 << 5 << 10 << 20 ) << + t.evaluate(BSON("testArray" << BSON_ARRAY( 0 << 5 << 10 << 20 ) << "hello" << "world" << "randField" << randObj), builder11) ); BSONObj obj11 = builder11.obj(); @@ -145,7 +145,7 @@ namespace mongo { BSONObjBuilder builder12; randObj = BSON( "#RAND_INT" << BSON_ARRAY( 1 << 5 << 4 ) ); ASSERT_EQUALS( BsonTemplateEvaluator::StatusSuccess, - t->evaluate(BSON("id" << randObj), builder12) ); + t.evaluate(BSON("id" << randObj), builder12) ); BSONObj obj12 = builder12.obj(); ASSERT_EQUALS(obj12.nFields(), 1); randValue1 = obj12["id"].numberInt(); @@ -156,17 +156,17 @@ namespace mongo { TEST(BSONTemplateEvaluatorTest, RAND_INT_PLUS_THREAD) { - BsonTemplateEvaluator *t = new BsonTemplateEvaluator(); - t->setId(1); + BsonTemplateEvaluator t; + t.setId(1); int randValue1, randValue2; - common_rand_tests("#RAND_INT_PLUS_THREAD", t); + common_rand_tests("#RAND_INT_PLUS_THREAD", &t); // Test success with a single element BSONObjBuilder builder5; BSONObj randObj = BSON( "#RAND_INT_PLUS_THREAD" << BSON_ARRAY( 1 << 5 ) ); ASSERT_EQUALS( BsonTemplateEvaluator::StatusSuccess, - t->evaluate(BSON("randField" << randObj), builder5) ); + t.evaluate(BSON("randField" << randObj), builder5) ); BSONObj obj5 = builder5.obj(); ASSERT_EQUALS(obj5.nFields(), 1); randValue1 = obj5["randField"].numberInt(); @@ -177,14 +177,14 @@ namespace mongo { BSONObjBuilder builder6; randObj = BSON( "#RAND_INT_PLUS_THREAD" << BSON_ARRAY( 1 << 5 ) ); ASSERT_EQUALS( BsonTemplateEvaluator::StatusSuccess, - t->evaluate(BSON("randField1" << randObj << + t.evaluate(BSON("randField1" << randObj << "randField2" << randObj), builder6) ); // Test success with #RAND_INT_PLUS_THREAD as first element BSONObjBuilder builder8; randObj = BSON( "#RAND_INT_PLUS_THREAD" << BSON_ARRAY( 1 << 5 ) ); ASSERT_EQUALS( BsonTemplateEvaluator::StatusSuccess, - t->evaluate(BSON("randField" << randObj << "hello" << "world" << + t.evaluate(BSON("randField" << randObj << "hello" << "world" << "id" << 1), builder8) ); BSONObj obj8 = builder8.obj(); ASSERT_EQUALS(obj8.nFields(), 3); @@ -196,7 +196,7 @@ namespace mongo { BSONObjBuilder builder9; randObj = BSON( "#RAND_INT_PLUS_THREAD" << BSON_ARRAY( 1 << 5 ) ); ASSERT_EQUALS( BsonTemplateEvaluator::StatusSuccess, - t->evaluate(BSON("id" << 1 << "randField" << randObj << "hello" << + t.evaluate(BSON("id" << 1 << "randField" << randObj << "hello" << "world"), builder9) ); BSONObj obj9 = builder9.obj(); ASSERT_EQUALS(obj9.nFields(), 3); @@ -208,7 +208,7 @@ namespace mongo { BSONObjBuilder builder10; randObj = BSON( "#RAND_INT_PLUS_THREAD" << BSON_ARRAY( 1 << 5 ) ); ASSERT_EQUALS( BsonTemplateEvaluator::StatusSuccess, - t->evaluate(BSON("randField1" << randObj << "hello" << + t.evaluate(BSON("randField1" << randObj << "hello" << "world" << "randField2" << randObj), builder10) ); BSONObj obj10 = builder10.obj(); ASSERT_EQUALS(obj10.nFields(), 3); @@ -225,7 +225,7 @@ namespace mongo { BSONObjBuilder builder11; randObj = BSON( "#RAND_INT_PLUS_THREAD" << BSON_ARRAY( 1 << 5 ) ); ASSERT_EQUALS( BsonTemplateEvaluator::StatusSuccess, - t->evaluate(BSON("testArray" << BSON_ARRAY( 0 << 5 << 10 << 20 ) << + t.evaluate(BSON("testArray" << BSON_ARRAY( 0 << 5 << 10 << 20 ) << "hello" << "world" << "randField" << randObj), builder11) ); BSONObj obj11 = builder11.obj(); @@ -238,7 +238,7 @@ namespace mongo { BSONObjBuilder builder12; randObj = BSON( "#RAND_INT_PLUS_THREAD" << BSON_ARRAY( 1 << 5 << 4 ) ); ASSERT_EQUALS( BsonTemplateEvaluator::StatusSuccess, - t->evaluate(BSON("id" << randObj), builder12) ); + t.evaluate(BSON("id" << randObj), builder12) ); BSONObj obj12 = builder12.obj(); ASSERT_EQUALS(obj12.nFields(), 1); randValue1 = obj12["id"].numberInt(); @@ -246,13 +246,13 @@ namespace mongo { ASSERT_LESS_THAN_OR_EQUALS(randValue1, 10); // Test success with a single element for a zero _id - BsonTemplateEvaluator *t2 = new BsonTemplateEvaluator(); - t2->setId(0); + BsonTemplateEvaluator t2; + t2.setId(0); BSONObjBuilder builder13; randObj = BSON( "#RAND_INT_PLUS_THREAD" << BSON_ARRAY( 1 << 5 ) ); ASSERT_EQUALS( BsonTemplateEvaluator::StatusSuccess, - t2->evaluate(BSON("randField" << randObj), builder13) ); + t2.evaluate(BSON("randField" << randObj), builder13) ); BSONObj obj13 = builder13.obj(); ASSERT_EQUALS(obj13.nFields(), 1); randValue1 = obj13["randField"].numberInt(); @@ -399,31 +399,31 @@ namespace mongo { TEST(BSONTemplateEvaluatorTest, RAND_STRING) { - BsonTemplateEvaluator *t = new BsonTemplateEvaluator(); + BsonTemplateEvaluator t; // Test failure when the arguments to RAND_STRING is not an integer BSONObjBuilder builder1; BSONObj randObj = BSON( "#RAND_STRING" << BSON_ARRAY("hello") ); ASSERT_EQUALS( BsonTemplateEvaluator::StatusOpEvaluationError, - t->evaluate(BSON("randField" << randObj), builder1) ); + t.evaluate(BSON("randField" << randObj), builder1) ); // Test failure when there is more than 1 argument to RAND_STRING BSONObjBuilder builder2; randObj = BSON( "#RAND_STRING" << BSON_ARRAY( 2 << 8 ) ); ASSERT_EQUALS( BsonTemplateEvaluator::StatusOpEvaluationError, - t->evaluate(BSON("randField" << randObj), builder2) ); + t.evaluate(BSON("randField" << randObj), builder2) ); // Test failure when length argument to RAND_STRING is 0 BSONObjBuilder builder3; randObj = BSON( "#RAND_STRING" << BSON_ARRAY(0 ) ); ASSERT_EQUALS( BsonTemplateEvaluator::StatusOpEvaluationError, - t->evaluate(BSON("randField" << randObj), builder3) ); + t.evaluate(BSON("randField" << randObj), builder3) ); // Test success with a single element BSONObjBuilder builder4; randObj = BSON( "#RAND_STRING" << BSON_ARRAY(5) ); ASSERT_EQUALS( BsonTemplateEvaluator::StatusSuccess, - t->evaluate(BSON("randField" << randObj), builder4) ); + t.evaluate(BSON("randField" << randObj), builder4) ); BSONObj obj4 = builder4.obj(); ASSERT_EQUALS(obj4.nFields(), 1); ASSERT_EQUALS(obj4.firstElement().str().length(), 5U); @@ -432,7 +432,7 @@ namespace mongo { BSONObjBuilder builder5; randObj = BSON( "#RAND_STRING" << BSON_ARRAY(5) ); ASSERT_EQUALS( BsonTemplateEvaluator::StatusSuccess, - t->evaluate(BSON("randField1" << randObj << + t.evaluate(BSON("randField1" << randObj << "randField2" << randObj), builder5) ); BSONObj obj5 = builder5.obj(); ASSERT_EQUALS(obj5.nFields(), 2); @@ -444,7 +444,7 @@ namespace mongo { BSONObjBuilder builder6; randObj = BSON( "#RAND_STRING" << BSON_ARRAY(5) ); ASSERT_EQUALS( BsonTemplateEvaluator::StatusSuccess, - t->evaluate(BSON("id" << 1 << "hello" << "world" << + t.evaluate(BSON("id" << 1 << "hello" << "world" << "randField" << randObj), builder6) ); BSONObj obj6 = builder6.obj(); ASSERT_EQUALS(obj6.nFields(), 3); @@ -456,7 +456,7 @@ namespace mongo { BSONObjBuilder builder7; randObj = BSON( "#RAND_STRING" << BSON_ARRAY(5) ); ASSERT_EQUALS( BsonTemplateEvaluator::StatusSuccess, - t->evaluate(BSON("randField" << randObj << "hello" << "world" << + t.evaluate(BSON("randField" << randObj << "hello" << "world" << "id" << 1), builder7) ); BSONObj obj7 = builder7.obj(); ASSERT_EQUALS(obj7.nFields(), 3); @@ -466,7 +466,7 @@ namespace mongo { BSONObjBuilder builder8; randObj = BSON( "#RAND_STRING" << BSON_ARRAY(5) ); ASSERT_EQUALS( BsonTemplateEvaluator::StatusSuccess, - t->evaluate(BSON("id" << 1 << "randField" << randObj << "hello" << + t.evaluate(BSON("id" << 1 << "randField" << randObj << "hello" << "world"), builder8) ); BSONObj obj8 = builder8.obj(); ASSERT_EQUALS(obj8.nFields(), 3); @@ -478,7 +478,7 @@ namespace mongo { BSONObjBuilder builder10; randObj = BSON( "#RAND_STRING" << BSON_ARRAY(5) ); ASSERT_EQUALS( BsonTemplateEvaluator::StatusSuccess, - t->evaluate(BSON("randField1" << randObj << "hello" << + t.evaluate(BSON("randField1" << randObj << "hello" << "world" << "randField2" << randObj), builder10) ); BSONObj obj10 = builder10.obj(); ASSERT_EQUALS(obj10.nFields(), 3); @@ -492,7 +492,7 @@ namespace mongo { BSONObjBuilder builder11; randObj = BSON( "#RAND_STRING" << BSON_ARRAY(5) ); ASSERT_EQUALS( BsonTemplateEvaluator::StatusSuccess, - t->evaluate(BSON("testArray" << BSON_ARRAY( 0 << 5 << 10 << 20 ) << + t.evaluate(BSON("testArray" << BSON_ARRAY( 0 << 5 << 10 << 20 ) << "hello" << "world" << "randField" << randObj), builder11) ); BSONObj obj11 = builder11.obj(); @@ -504,19 +504,19 @@ namespace mongo { TEST(BSONTemplateEvaluatorTest, CONCAT) { - BsonTemplateEvaluator *t = new BsonTemplateEvaluator(); + BsonTemplateEvaluator t; // Test failure when the arguments to #CONCAT has only one argument BSONObjBuilder builder1; BSONObj concatObj = BSON( "#CONCAT" << BSON_ARRAY("hello") ); ASSERT_EQUALS( BsonTemplateEvaluator::StatusOpEvaluationError, - t->evaluate(BSON("concatField" << concatObj), builder1) ); + t.evaluate(BSON("concatField" << concatObj), builder1) ); // Test success when all arguments to #CONCAT are strings BSONObjBuilder builder2; concatObj = BSON( "#CONCAT" << BSON_ARRAY("hello" << " " << "world")); ASSERT_EQUALS( BsonTemplateEvaluator::StatusSuccess, - t->evaluate(BSON("concatField" << concatObj), builder2) ); + t.evaluate(BSON("concatField" << concatObj), builder2) ); BSONObj obj2 = builder2.obj(); ASSERT_EQUALS(obj2.nFields(), 1); BSONObj expectedObj = BSON("concatField" << "hello world"); @@ -526,7 +526,7 @@ namespace mongo { BSONObjBuilder builder3; concatObj = BSON( "#CONCAT" << BSON_ARRAY("F" << 1 << "racing")); ASSERT_EQUALS( BsonTemplateEvaluator::StatusSuccess, - t->evaluate(BSON("concatField" << concatObj), builder3) ); + t.evaluate(BSON("concatField" << concatObj), builder3) ); BSONObj obj3 = builder3.obj(); ASSERT_EQUALS(obj3.nFields(), 1); expectedObj = BSON("concatField" << "F1racing"); @@ -536,7 +536,7 @@ namespace mongo { BSONObjBuilder builder4; concatObj = BSON( "#CONCAT" << BSON_ARRAY("hello" << " " << "world")); ASSERT_EQUALS( BsonTemplateEvaluator::StatusSuccess, - t->evaluate(BSON("concatField1" << concatObj << + t.evaluate(BSON("concatField1" << concatObj << "middleKey" << 1 << "concatField2" << concatObj), builder4) ); BSONObj obj4 = builder4.obj(); @@ -550,7 +550,7 @@ namespace mongo { BSONObjBuilder builder5; concatObj = BSON( "#CONCAT" << BSON_ARRAY("hello" << BSON_ARRAY(1 << 10) << "world")); ASSERT_EQUALS( BsonTemplateEvaluator::StatusSuccess, - t->evaluate(BSON("concatField" << concatObj), builder5) ); + t.evaluate(BSON("concatField" << concatObj), builder5) ); BSONObj obj5 = builder5.obj(); ASSERT_EQUALS(obj5.nFields(), 1); expectedObj = BSON("concatField" << "hello[ 1, 10 ]world"); @@ -559,32 +559,32 @@ namespace mongo { TEST(BSONTemplateEvaluatorTest, OID) { - boost::scoped_ptr<BsonTemplateEvaluator> t(new BsonTemplateEvaluator()); + BsonTemplateEvaluator t; BSONObj oidObj = BSON( "#OID" << 1 ); // Error: field must be "_id" BSONObjBuilder builder1; ASSERT_EQUALS( BsonTemplateEvaluator::StatusOpEvaluationError, - t->evaluate(BSON("notIdField" << oidObj), builder1) ); + t.evaluate(BSON("notIdField" << oidObj), builder1) ); // Success. BSONObjBuilder builder2; ASSERT_EQUALS( BsonTemplateEvaluator::StatusSuccess, - t->evaluate(BSON("_id" << oidObj), builder2) ); + t.evaluate(BSON("_id" << oidObj), builder2) ); } TEST(BSONTemplateEvaluatorTest, COMBINED_OPERATORS) { - BsonTemplateEvaluator *t = new BsonTemplateEvaluator(); + BsonTemplateEvaluator t; BSONObj randIntObj = BSON( "#RAND_INT" << BSON_ARRAY( 0 << 5 ) ); BSONObj randStrObj = BSON( "#RAND_STRING" << BSON_ARRAY(5) ); // Test success when #RAND_INT, and #RAND_STRING are combined BSONObjBuilder builder1; ASSERT_EQUALS( BsonTemplateEvaluator::StatusSuccess, - t->evaluate(BSON("randInt" << randIntObj << - "randStr" << randStrObj), builder1) ); + t.evaluate(BSON("randInt" << randIntObj << + "randStr" << randStrObj), builder1) ); BSONObj obj1 = builder1.obj(); ASSERT_EQUALS(obj1.nFields(), 2); BSONObjIterator iter1(obj1); @@ -599,7 +599,7 @@ namespace mongo { BSONObj concatObj = BSON("#CONCAT" << BSON_ARRAY(randIntObj << " hello world " << randStrObj)); ASSERT_EQUALS( BsonTemplateEvaluator::StatusSuccess, - t->evaluate(BSON("concatField" << concatObj), builder2) ); + t.evaluate(BSON("concatField" << concatObj), builder2) ); BSONObj obj2 = builder2.obj(); ASSERT_EQUALS(obj2.nFields(), 1); // check that the resulting string has a length of 19. @@ -610,7 +610,7 @@ namespace mongo { // Test #VARIABLE TEST(BSONTemplateEvaluatorTest, VARIABLE) { - BsonTemplateEvaluator *t = new BsonTemplateEvaluator(); + BsonTemplateEvaluator t; int value1; // Test failure when the variable has not been set @@ -618,16 +618,16 @@ namespace mongo { BSONObjBuilder builder1; BSONObj innerObj = BSON( "#VARIABLE" << "foo" ); ASSERT_EQUALS( BsonTemplateEvaluator::StatusOpEvaluationError, - t->evaluate(BSON("id" << innerObj), builder1) ); + t.evaluate(BSON("id" << innerObj), builder1) ); // Test success when the variable has been set // test2 := 42 // {id: { #VARIABLE: "test2" } } - t->setVariable("test2", BSON( "test2" << 42 ).getField("test2") ); + t.setVariable("test2", BSON( "test2" << 42 ).getField("test2") ); BSONObjBuilder builder2; innerObj = BSON( "#VARIABLE" << "test2" ); ASSERT_EQUALS( BsonTemplateEvaluator::StatusSuccess, - t->evaluate(BSON("id" << innerObj), builder2) ); + t.evaluate(BSON("id" << innerObj), builder2) ); BSONObj obj2 = builder2.obj(); value1 = obj2["id"].numberInt(); ASSERT_EQUALS(value1, 42); @@ -636,7 +636,7 @@ namespace mongo { // Test template recursion and other general features TEST(BSONTemplateEvaluatorTest, NESTING) { - BsonTemplateEvaluator *t = new BsonTemplateEvaluator(); + BsonTemplateEvaluator t; int randValue1, randValue2; // Test failure when operators are arbitrarily nested @@ -645,7 +645,7 @@ namespace mongo { BSONObj innerObj = BSON( "#RAND_INT" << BSON_ARRAY( 0 << 5 ) ); BSONObj outerObj = BSON( "#RAND_INT" << BSON_ARRAY( innerObj << 10 ) ); ASSERT_EQUALS( BsonTemplateEvaluator::StatusSuccess, - t->evaluate(BSON("randField" << outerObj), builder1) ); + t.evaluate(BSON("randField" << outerObj), builder1) ); // Test success when operators are arbitrarily nested // {foo: { bar: { #op: [1, 5] } } } @@ -653,7 +653,7 @@ namespace mongo { innerObj = BSON( "#RAND_INT" << BSON_ARRAY( 1 << 5 ) ); outerObj = BSON( "bar" << innerObj ); ASSERT_EQUALS( BsonTemplateEvaluator::StatusSuccess, - t->evaluate(BSON("foo" << outerObj), builder2) ); + t.evaluate(BSON("foo" << outerObj), builder2) ); BSONObj obj2 = builder2.obj(); BSONElement obj2_foo = obj2["foo"]; randValue1 = obj2_foo["bar"].numberInt(); @@ -667,7 +667,7 @@ namespace mongo { BSONObj bazObj = BSON( "baz" << innerObj); outerObj = BSON( "foo" << "hi" << "bar" << bazObj ); ASSERT_EQUALS( BsonTemplateEvaluator::StatusSuccess, - t->evaluate(BSON("id" << outerObj), builder3) ); + t.evaluate(BSON("id" << outerObj), builder3) ); BSONObj obj3 = builder3.obj(); BSONElement obj3_id = obj3["id"]; BSONElement obj3_bar = obj3_id["bar"]; @@ -684,7 +684,7 @@ namespace mongo { BSONObj bazObj4 = BSON( "baz_a" << bazObj4a << "baz_b" << bazObj4b << "baz_c" << "bye" ); outerObj = BSON("foo" << "hi" << "bar" << barObj4 << "baz" << bazObj4 ); ASSERT_EQUALS( BsonTemplateEvaluator::StatusSuccess, - t->evaluate(BSON("id" << outerObj), builder4) ); + t.evaluate(BSON("id" << outerObj), builder4) ); BSONObj obj4 = builder4.obj(); BSONElement obj4_id = obj4["id"]; randValue1 = obj4_id["bar"].numberInt(); @@ -704,7 +704,7 @@ namespace mongo { innerObj = BSON( "#NOT_A_VALID_OP" << BSON_ARRAY( 0 << 1000 ) ); outerObj = BSON( "op" << "let" << "target" << "x" << "value" << innerObj ); ASSERT_EQUALS( BsonTemplateEvaluator::StatusBadOperator, - t->evaluate(outerObj, builder5) ); + t.evaluate(outerObj, builder5) ); // Test success for elements in an array that need evaluation // { foo: "hi", bar: [ { #op: [1, 5] }, { #op: [5, 10], { baz: 42 }, 7 ] } @@ -714,7 +714,7 @@ namespace mongo { BSONObj elem3 = BSON( "baz" << 42 ); outerObj = BSON( "foo" << "hi" << "bar" << BSON_ARRAY( elem1 << elem2 << elem3 << 7) ); ASSERT_EQUALS( BsonTemplateEvaluator::StatusSuccess, - t->evaluate(outerObj, builder6) ); + t.evaluate(outerObj, builder6) ); BSONObj obj6 = builder6.obj(); BSONElement obj6_bar = obj6["bar"]; randValue1 = obj6_bar.Obj()[0].numberInt(); @@ -730,7 +730,7 @@ namespace mongo { innerObj = BSON( "#CONCAT" << BSON_ARRAY( "a" << "b" ) ); outerObj = BSON( "foo" << innerObj << "bar" << "hi" ); ASSERT_EQUALS( BsonTemplateEvaluator::StatusSuccess, - t->evaluate(outerObj, builder7) ); + t.evaluate(outerObj, builder7) ); BSONObj obj7 = builder7.obj(); BSONElement obj7_foo = obj7["foo"]; ASSERT_EQUALS(obj7_foo.String(), "ab"); diff --git a/src/mongo/util/options_parser/environment_test.cpp b/src/mongo/util/options_parser/environment_test.cpp index 8fbf7c94011..20c54466b6f 100644 --- a/src/mongo/util/options_parser/environment_test.cpp +++ b/src/mongo/util/options_parser/environment_test.cpp @@ -88,8 +88,8 @@ namespace { TEST(Environment, MutuallyExclusive) { moe::Environment environment; - environment.addKeyConstraint(new moe::MutuallyExclusiveKeyConstraint(moe::Key("key"), - moe::Key("otherKey"))); + moe::MutuallyExclusiveKeyConstraint constraint(moe::Key("key"),moe::Key("otherKey")); + environment.addKeyConstraint(&constraint); ASSERT_OK(environment.set(moe::Key("key"), moe::Value(1))); ASSERT_OK(environment.set(moe::Key("otherKey"), moe::Value(1))); ASSERT_NOT_OK(environment.validate()); @@ -97,8 +97,8 @@ namespace { TEST(Environment, RequiresOther) { moe::Environment environment; - environment.addKeyConstraint(new moe::RequiresOtherKeyConstraint(moe::Key("key"), - moe::Key("otherKey"))); + moe::RequiresOtherKeyConstraint constraint(moe::Key("key"),moe::Key("otherKey")); + environment.addKeyConstraint(&constraint); ASSERT_OK(environment.set(moe::Key("key"), moe::Value(1))); ASSERT_NOT_OK(environment.validate()); ASSERT_OK(environment.set(moe::Key("otherKey"), moe::Value(1))); @@ -107,8 +107,8 @@ namespace { TEST(Environment, StringFormat) { moe::Environment environment; - environment.addKeyConstraint(new moe::StringFormatKeyConstraint(moe::Key("key"), "[0-9]", - "[0-9]")); + moe::StringFormatKeyConstraint constraint(moe::Key("key"), "[0-9]","[0-9]"); + environment.addKeyConstraint(&constraint); ASSERT_OK(environment.set(moe::Key("key"), moe::Value(1))); ASSERT_NOT_OK(environment.validate()); ASSERT_OK(environment.set(moe::Key("key"), moe::Value(std::string("a")))); |