summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/ttl_with_restart.js
blob: 874a3bb30ce0a3d68bc4a693b1599b4757949d9e (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
/**
 * Verify the TTL index behavior after restart.
 * @tags: [requires_persistence]
 */
(function() {
'use strict';
let oldConn = MongoRunner.runMongod({setParameter: 'ttlMonitorSleepSecs=1'});
let db = oldConn.getDB('test');

let coll = db.ttl_coll;
coll.drop();
let now = (new Date()).getTime();

// Insert 50 docs with timestamp 'now - 24h'.
let past = new Date(now - (3600 * 1000 * 24));
for (let i = 0; i < 50; i++) {
    assert.commandWorked(db.runCommand({insert: 'ttl_coll', documents: [{x: past}]}));
}

assert.eq(coll.find().itcount(), 50);

// Create TTL index: expire docs older than 20000 seconds (~5.5h).
coll.createIndex({x: 1}, {expireAfterSeconds: 20000});

assert.soon(function() {
    return coll.find().itcount() == 0;
}, 'TTL index on x didn\'t delete');

// Restart the server.
MongoRunner.stopMongod(oldConn);
let newConn = MongoRunner.runMongod({
    restart: true,
    dbpath: oldConn.dbpath,
    cleanData: false,
    setParameter: "ttlMonitorSleepSecs=1"
});
db = newConn.getDB('test');
coll = db.ttl_coll;

// Re-insert 50 docs with timestamp 'now - 24h'.
for (let i = 0; i < 50; i++) {
    assert.commandWorked(db.runCommand({insert: 'ttl_coll', documents: [{x: past}]}));
}

assert.soon(function() {
    return coll.find().itcount() == 0;
}, 'TTL index on x didn\'t delete');

MongoRunner.stopMongod(newConn);
})();