summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/indexbg2.js
blob: e89b91fa2cfaa084c0ce45d5e4ba41945ad8202b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// SERVER-13922
if (0) {
// Test background index creation w/ constraints

load( "jstests/libs/slow_weekly_util.js" )

testServer = new SlowWeeklyMongod( "indexbg2" )
db = testServer.getDB( "test" );

parallel = function() {
    return db[ baseName + "_parallelStatus" ];
}

resetParallel = function() {
    parallel().drop();
}

doParallel = function( work ) {
    resetParallel();
    startMongoProgramNoConnect( "mongo", "--eval", work + "; db." + baseName + "_parallelStatus.save( {done:1} );", db.getMongo().host );
}

doneParallel = function() {
    return !!parallel().findOne();
}

waitParallel = function() {
    assert.soon( function() { return doneParallel(); }, "parallel did not finish in time", 300000, 1000 );
}

doTest = function(dropDups) {

    size = 10000;
    while (1) { // if indexing finishes before we can run checks, try indexing w/ more data
        print("size: " + size);
        baseName = "jstests_index12";
        fullName = "db." + baseName;
        t = db[baseName];
        t.drop();

        db.eval(function(size) {
            for (i = 0; i < size; ++i) {
                db.jstests_index12.save({ i: i });
            }
        },
            size);
        assert.eq(size, t.count());

        doParallel(fullName + ".ensureIndex( {i:1}, {background:true, unique:true, dropDups:" + dropDups + "} )");
        try {
            // wait for indexing to start
            assert.soon(function() { return 2 == db.system.indexes.count({ ns: "test." + baseName }) }, "no index created", 30000, 50);
            t.save({ i: 0, n: true });
            //printjson(db.getLastError());
            t.save({ i: size - 1, n: true });
            //printjson(db.getLastError());
        } catch (e) {
            // only a failure if we're still indexing
            // wait for parallel status to update to reflect indexing status
            sleep(1000);
            if (!doneParallel()) {
                throw e;
            }
        }
        if (!doneParallel()) {
            break;
        }
        print("indexing finished too soon, retrying...");
        size *= 2;
        assert(size < 5000000, "unable to run checks in parallel with index creation");
    }

    waitParallel();

    if( dropDups == "true" ) {
        assert.eq(size, t.find().toArray().length, "full query failed");
        assert.eq(size, t.count(), "count failed");
    }
    else {
        /* without dropdups, it could be that there is more than size now but the index failed
        to build - which is valid.  we check index isn't there. 
        */
        if (t.count() != size) 
            assert.eq(1, t.getIndexes().length, "change in # of elems yet index is there");
    }

}

doTest( "false" );
doTest( "true" );

testServer.stop();
}  // if (0)