2009-01-23 16:17:29 +01:00
|
|
|
// repltests.cpp : Unit tests for replication
|
|
|
|
//
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copyright (C) 2009 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 "../db/repl.h"
|
|
|
|
|
|
|
|
#include "../db/db.h"
|
|
|
|
#include "../db/instance.h"
|
|
|
|
#include "../db/json.h"
|
|
|
|
|
|
|
|
#include "dbtests.h"
|
|
|
|
|
|
|
|
namespace mongo {
|
|
|
|
void createOplog();
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace ReplTests {
|
|
|
|
|
2009-01-23 23:49:44 +01:00
|
|
|
BSONObj f( const char *s ) {
|
|
|
|
return fromjson( s );
|
|
|
|
}
|
|
|
|
|
2009-01-23 16:17:29 +01:00
|
|
|
class Base {
|
|
|
|
public:
|
|
|
|
Base() {
|
|
|
|
master = true;
|
|
|
|
createOplog();
|
|
|
|
dblock lk;
|
|
|
|
setClient( ns() );
|
|
|
|
}
|
|
|
|
~Base() {
|
|
|
|
try {
|
|
|
|
master = false;
|
|
|
|
deleteAll( ns() );
|
|
|
|
deleteAll( logNs() );
|
|
|
|
} catch ( ... ) {
|
|
|
|
FAIL( "Exception while cleaning up test" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
protected:
|
|
|
|
static const char *ns() {
|
|
|
|
return "dbtests.repltests";
|
|
|
|
}
|
|
|
|
static const char *logNs() {
|
|
|
|
return "local.oplog.$main";
|
|
|
|
}
|
2009-01-23 21:14:27 +01:00
|
|
|
DBClientInterface *client() const { return &client_; }
|
2009-01-23 16:17:29 +01:00
|
|
|
BSONObj one( const BSONObj &query = emptyObj ) const {
|
|
|
|
return client()->findOne( ns(), query );
|
|
|
|
}
|
|
|
|
void checkOne( const BSONObj &o ) const {
|
2009-01-23 23:49:44 +01:00
|
|
|
check( o, one( o ) );
|
2009-01-23 16:17:29 +01:00
|
|
|
}
|
|
|
|
void checkAll( const BSONObj &o ) const {
|
|
|
|
auto_ptr< DBClientCursor > c = client()->query( ns(), o );
|
|
|
|
assert( c->more() );
|
|
|
|
while( c->more() ) {
|
2009-01-23 23:49:44 +01:00
|
|
|
check( o, c->next() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void check( const BSONObj &expected, const BSONObj &got ) const {
|
|
|
|
if ( expected.woCompare( got ) ) {
|
|
|
|
out() << "expected: " << expected.toString()
|
|
|
|
<< ", got: " << got.toString() << endl;
|
2009-01-23 16:17:29 +01:00
|
|
|
}
|
2009-01-23 23:49:44 +01:00
|
|
|
ASSERT( !expected.woCompare( got ) );
|
2009-01-23 16:17:29 +01:00
|
|
|
}
|
|
|
|
BSONObj oneOp() const {
|
|
|
|
return client()->findOne( logNs(), emptyObj );
|
|
|
|
}
|
|
|
|
int count() const {
|
|
|
|
int count = 0;
|
|
|
|
dblock lk;
|
|
|
|
setClient( ns() );
|
|
|
|
auto_ptr< Cursor > c = theDataFileMgr.findAll( ns() );
|
|
|
|
for(; c->ok(); c->advance(), ++count );
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
static void applyAllOperations() {
|
|
|
|
class Applier : public ReplSource {
|
|
|
|
public:
|
|
|
|
static void apply( const BSONObj &op ) {
|
|
|
|
ReplSource::applyOperation( op );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
dblock lk;
|
|
|
|
setClient( logNs() );
|
|
|
|
vector< BSONObj > ops;
|
|
|
|
for( auto_ptr< Cursor > c = theDataFileMgr.findAll( logNs() ); c->ok(); c->advance() )
|
|
|
|
ops.push_back( c->current() );
|
|
|
|
setClient( ns() );
|
|
|
|
for( vector< BSONObj >::iterator i = ops.begin(); i != ops.end(); ++i )
|
|
|
|
Applier::apply( *i );
|
|
|
|
}
|
2009-01-23 23:49:44 +01:00
|
|
|
static void printAll( const char *ns ) {
|
|
|
|
dblock lk;
|
|
|
|
setClient( ns );
|
|
|
|
auto_ptr< Cursor > c = theDataFileMgr.findAll( ns );
|
|
|
|
vector< DiskLoc > toDelete;
|
|
|
|
out() << "all for " << ns << endl;
|
|
|
|
for(; c->ok(); c->advance() ) {
|
|
|
|
out() << c->current().toString() << endl;
|
|
|
|
}
|
|
|
|
}
|
2009-01-23 16:17:29 +01:00
|
|
|
// These deletes don't get logged.
|
|
|
|
static void deleteAll( const char *ns ) {
|
|
|
|
dblock lk;
|
|
|
|
setClient( ns );
|
|
|
|
auto_ptr< Cursor > c = theDataFileMgr.findAll( ns );
|
|
|
|
vector< DiskLoc > toDelete;
|
|
|
|
for(; c->ok(); c->advance() ) {
|
|
|
|
toDelete.push_back( c->currLoc() );
|
|
|
|
}
|
|
|
|
for( vector< DiskLoc >::iterator i = toDelete.begin(); i != toDelete.end(); ++i ) {
|
|
|
|
theDataFileMgr.deleteRecord( ns, i->rec(), *i, true );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
static void insert( const BSONObj &o ) {
|
|
|
|
dblock lk;
|
|
|
|
setClient( ns() );
|
|
|
|
theDataFileMgr.insert( ns(), o.objdata(), o.objsize() );
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
static DBDirectClient client_;
|
|
|
|
};
|
|
|
|
DBDirectClient Base::client_;
|
|
|
|
|
|
|
|
class LogBasic : public Base {
|
|
|
|
public:
|
|
|
|
void run() {
|
|
|
|
ASSERT( !database->haveLogged() );
|
|
|
|
ASSERT( oneOp().isEmpty() );
|
|
|
|
|
|
|
|
client()->insert( ns(), fromjson( "{\"a\":\"b\"}" ) );
|
|
|
|
|
|
|
|
ASSERT( database->haveLogged() );
|
|
|
|
ASSERT( !oneOp().isEmpty() );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
namespace Idempotence {
|
|
|
|
|
|
|
|
class Base : public ReplTests::Base {
|
|
|
|
public:
|
|
|
|
void run() {
|
|
|
|
reset();
|
|
|
|
doIt();
|
|
|
|
check();
|
|
|
|
applyAllOperations();
|
|
|
|
check();
|
|
|
|
|
|
|
|
reset();
|
|
|
|
applyAllOperations();
|
|
|
|
check();
|
|
|
|
applyAllOperations();
|
|
|
|
check();
|
|
|
|
}
|
|
|
|
protected:
|
|
|
|
virtual void doIt() const = 0;
|
|
|
|
virtual void check() const = 0;
|
|
|
|
virtual void reset() const = 0;
|
|
|
|
};
|
|
|
|
|
2009-02-04 04:34:51 +01:00
|
|
|
class InsertAutoId : public Base {
|
2009-01-23 16:17:29 +01:00
|
|
|
public:
|
2009-02-04 04:34:51 +01:00
|
|
|
InsertAutoId() : o_( fromjson( "{\"a\":\"b\"}" ) ) {}
|
2009-01-23 16:17:29 +01:00
|
|
|
void doIt() const {
|
|
|
|
client()->insert( ns(), o_ );
|
|
|
|
}
|
|
|
|
void check() const {
|
|
|
|
ASSERT_EQUALS( 1, count() );
|
|
|
|
}
|
|
|
|
void reset() const {
|
|
|
|
deleteAll( ns() );
|
|
|
|
}
|
|
|
|
protected:
|
|
|
|
BSONObj o_;
|
|
|
|
};
|
|
|
|
|
2009-02-04 04:34:51 +01:00
|
|
|
class InsertWithId : public InsertAutoId {
|
2009-01-23 16:17:29 +01:00
|
|
|
public:
|
|
|
|
InsertWithId() {
|
|
|
|
o_ = fromjson( "{\"_id\":ObjectId(\"0f0f0f0f0f0f0f0f0f0f0f0f\"),\"a\":\"b\"}" );
|
|
|
|
}
|
2009-02-04 04:34:51 +01:00
|
|
|
void check() const {
|
|
|
|
ASSERT_EQUALS( 1, count() );
|
|
|
|
checkOne( o_ );
|
|
|
|
}
|
2009-01-23 16:17:29 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
class InsertTwo : public Base {
|
|
|
|
public:
|
|
|
|
InsertTwo() :
|
2009-02-04 04:34:51 +01:00
|
|
|
o_( fromjson( "{'_id':1,a:'b'}" ) ),
|
|
|
|
t_( fromjson( "{'_id':2,c:'d'}" ) ) {}
|
2009-01-23 16:17:29 +01:00
|
|
|
void doIt() const {
|
|
|
|
vector< BSONObj > v;
|
|
|
|
v.push_back( o_ );
|
|
|
|
v.push_back( t_ );
|
|
|
|
client()->insert( ns(), v );
|
|
|
|
}
|
|
|
|
void check() const {
|
|
|
|
ASSERT_EQUALS( 2, count() );
|
|
|
|
checkOne( o_ );
|
|
|
|
checkOne( t_ );
|
|
|
|
}
|
|
|
|
void reset() const {
|
|
|
|
deleteAll( ns() );
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
BSONObj o_;
|
|
|
|
BSONObj t_;
|
|
|
|
};
|
|
|
|
|
|
|
|
class InsertTwoIdentical : public Base {
|
|
|
|
public:
|
|
|
|
InsertTwoIdentical() : o_( fromjson( "{\"a\":\"b\"}" ) ) {}
|
|
|
|
void doIt() const {
|
|
|
|
client()->insert( ns(), o_ );
|
|
|
|
client()->insert( ns(), o_ );
|
|
|
|
}
|
|
|
|
void check() const {
|
|
|
|
ASSERT_EQUALS( 2, count() );
|
|
|
|
}
|
|
|
|
void reset() const {
|
|
|
|
deleteAll( ns() );
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
BSONObj o_;
|
|
|
|
};
|
|
|
|
|
|
|
|
// TODO Maybe make this a test suite?
|
|
|
|
class UpdateBase {
|
|
|
|
public:
|
|
|
|
void run() {
|
|
|
|
runOne< Update >();
|
2009-01-23 23:49:44 +01:00
|
|
|
runOne< UpdateMultiple >();
|
2009-01-23 16:17:29 +01:00
|
|
|
runOne< UpsertInsert >();
|
|
|
|
runOne< UpsertNoInsert >();
|
|
|
|
}
|
2009-01-23 23:49:44 +01:00
|
|
|
class UpdateSpec {
|
|
|
|
public:
|
|
|
|
virtual BSONObj o() const = 0;
|
|
|
|
virtual BSONObj q() const = 0;
|
|
|
|
virtual BSONObj u() const = 0;
|
|
|
|
virtual BSONObj ou() const = 0;
|
|
|
|
};
|
2009-01-23 16:17:29 +01:00
|
|
|
protected:
|
2009-01-23 23:49:44 +01:00
|
|
|
UpdateBase( UpdateSpec *s ) : s_( s ) {}
|
2009-01-23 16:17:29 +01:00
|
|
|
private:
|
|
|
|
template< class T >
|
|
|
|
void runOne() const {
|
2009-01-23 23:49:44 +01:00
|
|
|
T test( s_ );
|
2009-01-23 16:17:29 +01:00
|
|
|
test.run();
|
|
|
|
}
|
|
|
|
class Update : public Base {
|
|
|
|
public:
|
2009-01-23 23:49:44 +01:00
|
|
|
Update( UpdateSpec *s ) : s_( s ) {}
|
2009-01-23 16:17:29 +01:00
|
|
|
protected:
|
|
|
|
void doIt() const {
|
2009-01-23 23:49:44 +01:00
|
|
|
client()->update( ns(), s_->q(), s_->u() );
|
2009-01-23 16:17:29 +01:00
|
|
|
}
|
|
|
|
void check() const {
|
|
|
|
ASSERT_EQUALS( 1, count() );
|
2009-01-23 23:49:44 +01:00
|
|
|
checkOne( s_->ou() );
|
2009-01-23 16:17:29 +01:00
|
|
|
}
|
|
|
|
void reset() const {
|
|
|
|
deleteAll( ns() );
|
2009-01-23 23:49:44 +01:00
|
|
|
insert( s_->o() );
|
|
|
|
}
|
|
|
|
UpdateSpec *s_;
|
|
|
|
};
|
|
|
|
class UpdateMultiple : public Update {
|
|
|
|
public:
|
|
|
|
UpdateMultiple( UpdateSpec *s ) : Update( s ) {}
|
|
|
|
void check() const {
|
|
|
|
ASSERT_EQUALS( 2, count() );
|
|
|
|
checkOne( s_->ou() );
|
|
|
|
checkOne( s_->o() );
|
2009-01-23 16:17:29 +01:00
|
|
|
}
|
2009-01-23 23:49:44 +01:00
|
|
|
void reset() const {
|
|
|
|
deleteAll( ns() );
|
|
|
|
insert( s_->o() );
|
|
|
|
insert( s_->o() );
|
|
|
|
}
|
2009-01-23 16:17:29 +01:00
|
|
|
};
|
|
|
|
class UpsertInsert : public Update {
|
|
|
|
public:
|
2009-01-23 23:49:44 +01:00
|
|
|
UpsertInsert( UpdateSpec *s ) : Update( s ) {}
|
2009-01-23 16:17:29 +01:00
|
|
|
void doIt() const {
|
2009-01-23 23:49:44 +01:00
|
|
|
client()->update( ns(), s_->q(), s_->u(), true );
|
2009-01-23 16:17:29 +01:00
|
|
|
}
|
|
|
|
void reset() const {
|
|
|
|
deleteAll( ns() );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
class UpsertNoInsert : public Update {
|
|
|
|
public:
|
2009-01-23 23:49:44 +01:00
|
|
|
UpsertNoInsert( UpdateSpec *s ) : Update( s ) {}
|
2009-01-23 16:17:29 +01:00
|
|
|
void doIt() const {
|
2009-01-23 23:49:44 +01:00
|
|
|
client()->update( ns(), s_->q(), s_->u(), true );
|
2009-01-23 16:17:29 +01:00
|
|
|
}
|
|
|
|
};
|
2009-01-23 23:49:44 +01:00
|
|
|
UpdateSpec *s_;
|
2009-01-23 16:17:29 +01:00
|
|
|
};
|
|
|
|
|
2009-02-04 04:34:51 +01:00
|
|
|
class UpdateSameField : public Base {
|
2009-01-23 16:17:29 +01:00
|
|
|
public:
|
|
|
|
UpdateSameField() :
|
2009-02-04 04:34:51 +01:00
|
|
|
o_( fromjson( "{a:'b'}" ) ),
|
|
|
|
u_( fromjson( "{a:'c'}" ) ){}
|
|
|
|
void doIt() const {
|
|
|
|
client()->update( ns(), o_, u_ );
|
|
|
|
}
|
|
|
|
void check() const {
|
|
|
|
ASSERT_EQUALS( 2, count() );
|
|
|
|
ASSERT( !client()->findOne( ns(), o_ ).isEmpty() );
|
|
|
|
ASSERT( !client()->findOne( ns(), u_ ).isEmpty() );
|
|
|
|
}
|
|
|
|
void reset() const {
|
|
|
|
deleteAll( ns() );
|
|
|
|
insert( o_ );
|
|
|
|
insert( o_ );
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
BSONObj o_, u_;
|
|
|
|
};
|
|
|
|
|
|
|
|
class UpdateSameFieldWithId : public Base {
|
|
|
|
public:
|
|
|
|
UpdateSameFieldWithId() :
|
|
|
|
o_( fromjson( "{'_id':1,a:'b'}" ) ),
|
|
|
|
q_( fromjson( "{a:'b'}" ) ),
|
|
|
|
u_( fromjson( "{'_id':1,a:'c'}" ) ){}
|
|
|
|
void doIt() const {
|
|
|
|
client()->update( ns(), q_, u_ );
|
|
|
|
}
|
|
|
|
void check() const {
|
|
|
|
ASSERT_EQUALS( 2, count() );
|
|
|
|
ASSERT( !client()->findOne( ns(), q_ ).isEmpty() );
|
|
|
|
ASSERT( !client()->findOne( ns(), u_ ).isEmpty() );
|
|
|
|
}
|
|
|
|
void reset() const {
|
|
|
|
deleteAll( ns() );
|
|
|
|
insert( o_ );
|
|
|
|
insert( fromjson( "{'_id':2,a:'b'}" ) );
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
BSONObj o_, q_, u_;
|
|
|
|
};
|
|
|
|
|
|
|
|
class UpdateSameFieldExplicitId : public Base {
|
|
|
|
public:
|
|
|
|
UpdateSameFieldExplicitId() :
|
|
|
|
o_( fromjson( "{'_id':1,a:'b'}" ) ),
|
|
|
|
u_( fromjson( "{'_id':1,a:'c'}" ) ){}
|
|
|
|
void doIt() const {
|
|
|
|
client()->update( ns(), o_, u_ );
|
|
|
|
}
|
|
|
|
void check() const {
|
|
|
|
ASSERT_EQUALS( 1, count() );
|
|
|
|
checkOne( u_ );
|
|
|
|
}
|
|
|
|
void reset() const {
|
|
|
|
deleteAll( ns() );
|
|
|
|
insert( o_ );
|
|
|
|
}
|
|
|
|
protected:
|
|
|
|
BSONObj o_, u_;
|
|
|
|
};
|
|
|
|
|
|
|
|
class UpdateId : public UpdateSameFieldExplicitId {
|
|
|
|
public:
|
|
|
|
UpdateId() {
|
|
|
|
o_ = fromjson( "{'_id':1}" );
|
|
|
|
u_ = fromjson( "{'_id':2}" );
|
|
|
|
}
|
2009-01-23 16:17:29 +01:00
|
|
|
};
|
2009-02-04 04:34:51 +01:00
|
|
|
|
|
|
|
// class UpdateSameField : public UpdateBase {
|
|
|
|
// public:
|
|
|
|
// class Spec : public UpdateSpec {
|
|
|
|
// virtual BSONObj o() const { return f( "{\"a\":\"b\",\"m\":\"n\"}" ); }
|
|
|
|
// virtual BSONObj q() const { return f( "{\"a\":\"b\"}" ); }
|
|
|
|
// virtual BSONObj u() const { return f( "{\"a\":\"c\"}" ); }
|
|
|
|
// virtual BSONObj ou() const { return u(); }
|
|
|
|
// };
|
|
|
|
// static Spec spec;
|
|
|
|
// UpdateSameField() :
|
|
|
|
// UpdateBase( &spec ) {}
|
|
|
|
// };
|
|
|
|
// UpdateSameField::Spec UpdateSameField::spec;
|
2009-01-23 16:17:29 +01:00
|
|
|
|
2009-02-04 04:34:51 +01:00
|
|
|
class UpdateDifferentFieldExplicitId : public Base {
|
|
|
|
public:
|
|
|
|
UpdateDifferentFieldExplicitId() :
|
|
|
|
o_( fromjson( "{'_id':1,a:'b'}" ) ),
|
|
|
|
q_( fromjson( "{'_id':1}" ) ),
|
|
|
|
u_( fromjson( "{'_id':1,a:'c'}" ) ){}
|
|
|
|
void doIt() const {
|
|
|
|
client()->update( ns(), q_, u_ );
|
|
|
|
}
|
|
|
|
void check() const {
|
|
|
|
ASSERT_EQUALS( 1, count() );
|
|
|
|
checkOne( u_ );
|
|
|
|
}
|
|
|
|
void reset() const {
|
|
|
|
deleteAll( ns() );
|
|
|
|
insert( o_ );
|
|
|
|
}
|
|
|
|
protected:
|
|
|
|
BSONObj o_, q_, u_;
|
|
|
|
};
|
|
|
|
|
2009-01-23 16:17:29 +01:00
|
|
|
class UpdateDifferentField : public UpdateBase {
|
|
|
|
public:
|
2009-01-23 23:49:44 +01:00
|
|
|
class Spec : public UpdateSpec {
|
|
|
|
virtual BSONObj o() const { return f( "{\"a\":\"b\",\"m\":\"n\",\"x\":\"y\"}" ); }
|
|
|
|
virtual BSONObj q() const { return f( "{\"a\":\"b\"}" ); }
|
|
|
|
virtual BSONObj u() const { return f( "{\"a\":\"b\",\"x\":\"z\"}" ); }
|
|
|
|
virtual BSONObj ou() const { return u(); }
|
|
|
|
};
|
|
|
|
static Spec spec;
|
2009-01-23 16:17:29 +01:00
|
|
|
UpdateDifferentField() :
|
2009-01-23 23:49:44 +01:00
|
|
|
UpdateBase( &spec ) {}
|
|
|
|
};
|
|
|
|
UpdateDifferentField::Spec UpdateDifferentField::spec;
|
|
|
|
|
|
|
|
class Set : public UpdateBase {
|
|
|
|
public:
|
|
|
|
class Spec : public UpdateSpec {
|
|
|
|
virtual BSONObj o() const { return f( "{\"a\":\"b\",\"m\":1}" ); }
|
|
|
|
virtual BSONObj q() const { return f( "{\"a\":\"b\"}" ); }
|
|
|
|
virtual BSONObj u() const { return f( "{\"$set\":{\"m\":5}}" ); }
|
|
|
|
virtual BSONObj ou() const { return f( "{\"a\":\"b\",\"m\":5}" ); }
|
|
|
|
};
|
|
|
|
static Spec spec;
|
|
|
|
Set() :
|
|
|
|
UpdateBase( &spec ) {}
|
|
|
|
};
|
|
|
|
Set::Spec Set::spec;
|
|
|
|
|
|
|
|
class SetSame : public UpdateBase {
|
|
|
|
public:
|
|
|
|
class Spec : public UpdateSpec {
|
|
|
|
virtual BSONObj o() const { return f( "{\"a\":10,\"m\":1}" ); }
|
|
|
|
virtual BSONObj q() const { return f( "{\"a\":10}" ); }
|
|
|
|
virtual BSONObj u() const { return f( "{\"$set\":{\"a\":11}}" ); }
|
|
|
|
virtual BSONObj ou() const { return f( "{\"a\":11,\"m\":1}" ); }
|
|
|
|
};
|
|
|
|
static Spec spec;
|
|
|
|
SetSame() :
|
|
|
|
UpdateBase( &spec ) {}
|
|
|
|
};
|
|
|
|
SetSame::Spec SetSame::spec;
|
|
|
|
|
|
|
|
class Inc : public UpdateBase {
|
|
|
|
public:
|
|
|
|
class Spec : public UpdateSpec {
|
|
|
|
virtual BSONObj o() const { return f( "{\"a\":\"b\",\"m\":0}" ); }
|
|
|
|
virtual BSONObj q() const { return f( "{\"a\":\"b\"}" ); }
|
|
|
|
virtual BSONObj u() const { return f( "{\"$inc\":{\"m\":5}}" ); }
|
|
|
|
virtual BSONObj ou() const { return f( "{\"a\":\"b\",\"m\":5}" ); }
|
|
|
|
};
|
|
|
|
static Spec spec;
|
|
|
|
Inc() :
|
|
|
|
UpdateBase( &spec ) {}
|
|
|
|
};
|
|
|
|
Inc::Spec Inc::spec;
|
|
|
|
|
|
|
|
class IncSame : public UpdateBase {
|
|
|
|
public:
|
|
|
|
class Spec : public UpdateSpec {
|
|
|
|
virtual BSONObj o() const { return f( "{\"a\":0,\"m\":\"n\"}" ); }
|
|
|
|
virtual BSONObj q() const { return f( "{\"a\":0}" ); }
|
|
|
|
virtual BSONObj u() const { return f( "{\"$inc\":{\"a\":2}}" ); }
|
|
|
|
virtual BSONObj ou() const { return f( "{\"a\":2,\"m\":\"n\"}" ); }
|
|
|
|
};
|
|
|
|
static Spec spec;
|
|
|
|
IncSame() :
|
|
|
|
UpdateBase( &spec ) {}
|
2009-01-23 16:17:29 +01:00
|
|
|
};
|
2009-01-23 23:49:44 +01:00
|
|
|
IncSame::Spec IncSame::spec;
|
|
|
|
|
2009-01-23 21:14:27 +01:00
|
|
|
class Remove : public Base {
|
|
|
|
public:
|
2009-01-28 21:24:52 +01:00
|
|
|
Remove() :
|
|
|
|
o1_( f( "{\"_id\":\"010101010101010101010101\",\"a\":\"b\"}" ) ),
|
|
|
|
o2_( f( "{\"_id\":\"010101010101010101010102\",\"a\":\"b\"}" ) ),
|
|
|
|
q_( f( "{\"a\":\"b\"}" ) ) {}
|
2009-01-23 21:14:27 +01:00
|
|
|
void doIt() const {
|
2009-01-28 21:24:52 +01:00
|
|
|
client()->remove( ns(), q_ );
|
2009-01-23 21:14:27 +01:00
|
|
|
}
|
|
|
|
void check() const {
|
|
|
|
ASSERT_EQUALS( 0, count() );
|
|
|
|
}
|
|
|
|
void reset() const {
|
|
|
|
deleteAll( ns() );
|
2009-01-28 21:24:52 +01:00
|
|
|
insert( o1_ );
|
|
|
|
insert( o2_ );
|
2009-01-23 21:14:27 +01:00
|
|
|
}
|
|
|
|
protected:
|
2009-01-28 21:24:52 +01:00
|
|
|
BSONObj o1_, o2_, q_;
|
2009-01-23 21:14:27 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
class RemoveOne : public Remove {
|
|
|
|
void doIt() const {
|
2009-01-28 21:24:52 +01:00
|
|
|
client()->remove( ns(), q_, true );
|
2009-01-23 21:14:27 +01:00
|
|
|
}
|
|
|
|
void check() const {
|
|
|
|
ASSERT_EQUALS( 1, count() );
|
|
|
|
}
|
|
|
|
};
|
2009-01-23 23:49:44 +01:00
|
|
|
|
|
|
|
class FailingUpdate : public Base {
|
|
|
|
public:
|
|
|
|
FailingUpdate() :
|
2009-02-04 04:34:51 +01:00
|
|
|
o_( fromjson( "{'_id':1,a:'b'}" ) ),
|
|
|
|
u_( fromjson( "{'_id':1,c:'d'}" ) ) {}
|
2009-01-23 23:49:44 +01:00
|
|
|
void doIt() const {
|
|
|
|
client()->update( ns(), o_, u_ );
|
|
|
|
client()->insert( ns(), o_ );
|
|
|
|
}
|
|
|
|
void check() const {
|
|
|
|
ASSERT_EQUALS( 1, count() );
|
|
|
|
checkOne( o_ );
|
|
|
|
}
|
|
|
|
void reset() const {
|
|
|
|
deleteAll( ns() );
|
|
|
|
}
|
|
|
|
protected:
|
|
|
|
BSONObj o_, u_;
|
|
|
|
};
|
|
|
|
|
2009-01-23 16:17:29 +01:00
|
|
|
} // namespace Idempotence
|
|
|
|
|
|
|
|
class All : public UnitTest::Suite {
|
|
|
|
public:
|
|
|
|
All() {
|
|
|
|
add< LogBasic >();
|
2009-02-04 04:34:51 +01:00
|
|
|
add< Idempotence::InsertAutoId >();
|
2009-01-23 16:17:29 +01:00
|
|
|
add< Idempotence::InsertWithId >();
|
2009-02-04 04:34:51 +01:00
|
|
|
add< Idempotence::InsertTwo >();
|
|
|
|
add< Idempotence::InsertTwoIdentical >();
|
2009-01-23 16:17:29 +01:00
|
|
|
// add< Idempotence::UpdateSameField >();
|
2009-02-04 04:34:51 +01:00
|
|
|
add< Idempotence::UpdateSameFieldWithId >();
|
|
|
|
add< Idempotence::UpdateSameFieldExplicitId >();
|
|
|
|
add< Idempotence::UpdateId >();
|
2009-01-23 23:49:44 +01:00
|
|
|
// add< Idempotence::UpdateDifferentField >();
|
2009-02-04 04:34:51 +01:00
|
|
|
add< Idempotence::UpdateDifferentFieldExplicitId >();
|
2009-01-31 00:44:40 +01:00
|
|
|
// add< Idempotence::Set >();
|
2009-01-23 23:49:44 +01:00
|
|
|
// FIXME Decide what is correct & uncomment
|
|
|
|
// add< Idempotence::SetSame >();
|
2009-01-31 00:44:40 +01:00
|
|
|
// add< Idempotence::Inc >();
|
2009-01-23 23:49:44 +01:00
|
|
|
// FIXME Decide what is correct & uncomment
|
|
|
|
// add< Idempotence::IncSame >();
|
2009-01-23 21:14:27 +01:00
|
|
|
add< Idempotence::Remove >();
|
2009-01-28 21:24:52 +01:00
|
|
|
add< Idempotence::RemoveOne >();
|
2009-02-04 04:34:51 +01:00
|
|
|
add< Idempotence::FailingUpdate >();
|
2009-01-23 16:17:29 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace ReplTests
|
|
|
|
|
|
|
|
UnitTest::TestPtr replTests() {
|
|
|
|
return UnitTest::createSuite< ReplTests::All >();
|
2009-01-31 00:44:40 +01:00
|
|
|
}
|