Files
mongo/client/distlock_test.cpp

105 lines
3.3 KiB
C++
Raw Normal View History

2010-07-12 17:53:02 -04:00
// distlock_test.h
2010-07-12 17:50:26 -04:00
/* Copyright 2009 10gen Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "../pch.h"
#include "dbclient.h"
#include "distlock.h"
#include "../db/commands.h"
namespace mongo {
2011-01-04 00:40:41 -05:00
2010-07-12 17:50:26 -04:00
class TestDistLockWithSync : public Command {
public:
2011-01-04 00:40:41 -05:00
TestDistLockWithSync() : Command( "_testDistLockWithSyncCluster" ) {}
2010-07-12 17:50:26 -04:00
virtual void help( stringstream& help ) const {
help << "should not be calling this directly" << endl;
}
2011-01-04 00:40:41 -05:00
2010-07-12 17:50:26 -04:00
virtual bool slaveOk() const { return false; }
virtual bool adminOnly() const { return true; }
2011-01-04 00:40:41 -05:00
virtual LockType locktype() const { return NONE; }
2010-07-12 17:50:26 -04:00
2011-01-04 00:40:41 -05:00
static void runThread() {
2011-02-14 16:18:52 -05:00
while ( keepGoing ) {
2011-01-04 00:40:41 -05:00
if ( current->lock_try( "test" ) ) {
2011-02-14 16:18:52 -05:00
count++;
2011-02-14 12:39:53 -05:00
int before = count;
2011-02-14 16:18:52 -05:00
sleepmillis( 3 );
2011-02-14 12:39:53 -05:00
int after = count;
2011-02-14 16:18:52 -05:00
if ( after != before ) {
2011-02-14 12:39:53 -05:00
error() << " before: " << before << " after: " << after << endl;
}
2011-02-14 16:18:52 -05:00
2010-07-12 17:50:26 -04:00
current->unlock();
}
}
}
2011-02-14 12:39:53 -05:00
2011-01-04 00:40:41 -05:00
bool run(const string& , BSONObj& cmdObj, string& errmsg, BSONObjBuilder& result, bool) {
2011-02-14 12:39:53 -05:00
Timer t;
2010-07-12 17:50:26 -04:00
DistributedLock lk( ConnectionString( cmdObj["host"].String() , ConnectionString::SYNC ), "testdistlockwithsync" );
current = &lk;
count = 0;
gotit = 0;
2011-02-14 12:39:53 -05:00
errors = 0;
2011-02-14 16:18:52 -05:00
keepGoing = true;
2010-07-12 17:50:26 -04:00
vector<shared_ptr<boost::thread> > l;
2011-01-04 00:40:41 -05:00
for ( int i=0; i<4; i++ ) {
2010-07-12 17:50:26 -04:00
l.push_back( shared_ptr<boost::thread>( new boost::thread( runThread ) ) );
}
2011-02-14 12:39:53 -05:00
2011-02-14 16:18:52 -05:00
int secs = 10;
if ( cmdObj["secs"].isNumber() )
secs = cmdObj["secs"].numberInt();
sleepsecs( secs );
keepGoing = false;
2010-07-12 17:50:26 -04:00
for ( unsigned i=0; i<l.size(); i++ )
l[i]->join();
2011-02-14 12:39:53 -05:00
current = 0;
2010-07-12 17:50:26 -04:00
result.append( "count" , count );
result.append( "gotit" , gotit );
2011-02-14 12:39:53 -05:00
result.append( "errors" , errors );
result.append( "timeMS" , t.millis() );
2011-01-04 00:40:41 -05:00
2011-02-14 16:18:52 -05:00
return errors == 0;
2011-02-14 12:39:53 -05:00
}
// variables for test
2010-07-12 17:50:26 -04:00
static DistributedLock * current;
static int gotit;
2011-02-14 12:39:53 -05:00
static int errors;
2011-02-14 16:18:52 -05:00
static AtomicUInt count;
static bool keepGoing;
2010-07-12 17:50:26 -04:00
} testDistLockWithSyncCmd;
DistributedLock * TestDistLockWithSync::current;
2011-02-14 16:18:52 -05:00
AtomicUInt TestDistLockWithSync::count;
2010-07-12 17:50:26 -04:00
int TestDistLockWithSync::gotit;
2011-02-14 12:39:53 -05:00
int TestDistLockWithSync::errors;
2011-02-14 16:18:52 -05:00
bool TestDistLockWithSync::keepGoing;
2010-07-12 17:50:26 -04:00
}