diff options
Diffstat (limited to 'client/examples/rs.cpp')
-rw-r--r-- | client/examples/rs.cpp | 80 |
1 files changed, 70 insertions, 10 deletions
diff --git a/client/examples/rs.cpp b/client/examples/rs.cpp index 7813ec6..3307d87 100644 --- a/client/examples/rs.cpp +++ b/client/examples/rs.cpp @@ -21,11 +21,62 @@ #include "client/dbclient.h" #include <iostream> +#include <vector> using namespace mongo; using namespace std; +void workerThread( string collName , bool print , DBClientReplicaSet * conn ) { + + while ( true ) { + try { + conn->update( collName , BSONObj() , BSON( "$inc" << BSON( "x" << 1 ) ) , true ); + + BSONObj x = conn->findOne( collName , BSONObj() ); + + if ( print ) { + cout << x << endl; + } + + BSONObj a = conn->slaveConn().findOne( collName , BSONObj() , 0 , QueryOption_SlaveOk ); + BSONObj b = conn->findOne( collName , BSONObj() , 0 , QueryOption_SlaveOk ); + + if ( print ) { + cout << "\t A " << a << endl; + cout << "\t B " << b << endl; + } + } + catch ( std::exception& e ) { + cout << "ERROR: " << e.what() << endl; + } + sleepmillis( 10 ); + } +} + int main( int argc , const char ** argv ) { + + unsigned nThreads = 1; + bool print = false; + bool testTimeout = false; + + for ( int i=1; i<argc; i++ ) { + if ( mongoutils::str::equals( "--threads" , argv[i] ) ) { + nThreads = atoi( argv[++i] ); + } + else if ( mongoutils::str::equals( "--print" , argv[i] ) ) { + print = true; + } + // Run a special mode to demonstrate the DBClientReplicaSet so_timeout option. + else if ( mongoutils::str::equals( "--testTimeout" , argv[i] ) ) { + testTimeout = true; + } + else { + cerr << "unknown option: " << argv[i] << endl; + return 1; + } + + } + string errmsg; ConnectionString cs = ConnectionString::parse( "foo/127.0.0.1" , errmsg ); if ( ! cs.isValid() ) { @@ -33,7 +84,7 @@ int main( int argc , const char ** argv ) { return 1; } - DBClientReplicaSet * conn = (DBClientReplicaSet*)cs.connect( errmsg ); + DBClientReplicaSet * conn = dynamic_cast<DBClientReplicaSet*>(cs.connect( errmsg, testTimeout ? 10 : 0 )); if ( ! conn ) { cout << "error connecting: " << errmsg << endl; return 2; @@ -42,17 +93,26 @@ int main( int argc , const char ** argv ) { string collName = "test.rs1"; conn->dropCollection( collName ); - while ( true ) { + + if ( testTimeout ) { + conn->insert( collName, BSONObj() ); try { - conn->update( collName , BSONObj() , BSON( "$inc" << BSON( "x" << 1 ) ) , true ); - cout << conn->findOne( collName , BSONObj() ) << endl; - cout << "\t A" << conn->slaveConn().findOne( collName , BSONObj() , 0 , QueryOption_SlaveOk ) << endl; - cout << "\t B " << conn->findOne( collName , BSONObj() , 0 , QueryOption_SlaveOk ) << endl; - } - catch ( std::exception& e ) { - cout << "ERROR: " << e.what() << endl; + conn->count( collName, BSON( "$where" << "sleep(40000)" ) ); + } catch( DBException& ) { + return 0; } - sleepsecs( 1 ); + cout << "expected socket exception" << endl; + return 1; + } + + vector<boost::shared_ptr<boost::thread> > threads; + for ( unsigned i=0; i<nThreads; i++ ) { + string errmsg; + threads.push_back( boost::shared_ptr<boost::thread>( new boost::thread( boost::bind( workerThread , collName , print , (DBClientReplicaSet*)cs.connect(errmsg) ) ) ) ); + } + + for ( unsigned i=0; i<threads.size(); i++ ) { + threads[i]->join(); } } |