0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-12-01 09:32:32 +01:00
mongodb/jstests/capped8.js

107 lines
3.1 KiB
JavaScript
Raw Normal View History

// Test NamespaceDetails::cappedTruncateAfter with empty extents
Random.setRandomSeed();
t = db.jstests_capped8;
function debug( x ) {
// printjson( x );
}
/** Generate an object with a string field of specified length */
2011-05-04 18:37:35 +02:00
function obj( size, x ) {
return {X:x, a:new Array( size + 1 ).toString()};;
}
function withinOne( a, b ) {
assert( Math.abs( a - b ) <= 1, "not within one: " + a + ", " + b )
}
2011-05-04 18:37:35 +02:00
var X = 0;
/**
* Insert enough documents of the given size spec that the collection will
* contain only documents having this size spec.
*/
2011-05-04 18:37:35 +02:00
function insertManyRollingOver( objsize ) {
// Add some variability, as the precise number can trigger different cases.
2011-05-04 18:37:35 +02:00
X++;
n = 250 + Random.randInt(10);
assert(t.count() == 0 || t.findOne().X != X);
for( i = 0; i < n; ++i ) {
2011-05-04 18:37:35 +02:00
t.save( obj( objsize, X ) );
debug( t.count() );
}
2011-05-04 18:37:35 +02:00
if (t.findOne().X != X) {
printjson(t.findOne());
print("\n\nERROR didn't roll over in insertManyRollingOver " + objsize);
print("approx amountwritten: " + (objsize * n));
printjson(t.stats());
assert(false);
}
}
/**
* Insert some documents in such a way that there may be an empty extent, then
* truncate the capped collection.
*/
function insertAndTruncate( first ) {
myInitialCount = t.count();
// Insert enough documents to make the capped allocation loop over.
2011-05-04 18:37:35 +02:00
insertManyRollingOver( 150 );
myFiftyCount = t.count();
// Insert documents that are too big to fit in the smaller extents.
2011-05-04 18:37:35 +02:00
insertManyRollingOver( 5000 );
myTwokCount = t.count();
if ( first ) {
initialCount = myInitialCount;
fiftyCount = myFiftyCount;
twokCount = myTwokCount;
// Sanity checks for collection count
assert( fiftyCount > initialCount );
assert( fiftyCount > twokCount );
} else {
// Check that we are able to insert roughly the same number of documents
// after truncating. The exact values are slightly variable as a result
// of the capped allocation algorithm.
withinOne( initialCount, myInitialCount );
withinOne( fiftyCount, myFiftyCount );
withinOne( twokCount, myTwokCount );
}
count = t.count();
// Check that we can truncate the collection successfully.
assert.commandWorked( db.runCommand( { captrunc:"jstests_capped8", n:count - 1, inc:false } ) );
}
/** Test truncating and subsequent inserts */
function testTruncate() {
insertAndTruncate( true );
insertAndTruncate( false );
insertAndTruncate( false );
}
2011-05-04 18:37:35 +02:00
var pass = 1;
print("pass " + pass++);
t.drop();
2011-05-04 18:37:35 +02:00
db._dbCommand( { create:"jstests_capped8", capped: true, $nExtents: [ 10000, 10000, 4000 ] } );
testTruncate();
2011-05-04 18:37:35 +02:00
print("pass " + pass++);
t.drop();
2011-05-04 18:37:35 +02:00
db._dbCommand( { create:"jstests_capped8", capped: true, $nExtents: [ 10000, 1000, 4000 ] } );
testTruncate();
2011-05-04 18:37:35 +02:00
print("pass " + pass++);
t.drop();
2011-05-04 18:37:35 +02:00
db._dbCommand( { create:"jstests_capped8", capped: true, $nExtents: [ 10000, 4000 ] } );
testTruncate();
2011-05-04 18:37:35 +02:00
print("pass " + pass++);
t.drop();
db._dbCommand( { create:"jstests_capped8", capped: true, $nExtents: [ 10000 ] } );
testTruncate();