summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/timeseries_expire.js
blob: 8f4ce8564e6e3cce08434374593b4678e1e0b24d (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
/**
 * Tests that a time-series collection created with the 'expireAfterSeconds' option will remove
 * buckets older than 'expireAfterSeconds' based on the bucket creation time.
 * @tags: [
 *   does_not_support_stepdowns,
 *   does_not_support_transactions,
 *   requires_getmore,
 * ]
 */
(function() {
"use strict";

load('jstests/libs/fixture_helpers.js');  // For 'FixtureHelpers'
load("jstests/core/timeseries/libs/timeseries.js");

const conn = MongoRunner.runMongod({setParameter: 'ttlMonitorSleepSecs=1'});
const testDB = conn.getDB(jsTestName());
assert.commandWorked(testDB.dropDatabase());

TimeseriesTest.run((insert) => {
    const coll = testDB.timeseries_expire;
    const bucketsColl = testDB.getCollection('system.buckets.' + coll.getName());

    coll.drop();

    const timeFieldName = 'time';
    const expireAfterSeconds = NumberLong(1);
    assert.commandWorked(testDB.createCollection(
        coll.getName(),
        {timeseries: {timeField: timeFieldName}, expireAfterSeconds: expireAfterSeconds}));
    assert.contains(bucketsColl.getName(), testDB.getCollectionNames());

    // Inserts a measurement with a time in the past to ensure the measurement will be removed
    // immediately.
    const t = ISODate("2020-11-13T01:00:00Z");
    let start = ISODate();
    assert.lt(t, start);

    const doc = {_id: 0, [timeFieldName]: t, x: 0};
    assert.commandWorked(insert(coll, doc), 'failed to insert doc: ' + tojson(doc));
    jsTestLog('Insertion took ' + ((new Date()).getTime() - start.getTime()) + ' ms.');

    // Wait for the document to be removed.
    start = ISODate();
    assert.soon(() => {
        return 0 == coll.find().itcount();
    });
    jsTestLog('Removal took ' + ((new Date()).getTime() - start.getTime()) + ' ms.');

    // Check bucket collection.
    const bucketDocs = bucketsColl.find().sort({'control.min._id': 1}).toArray();
    assert.eq(0, bucketDocs.length, bucketDocs);
});

MongoRunner.stopMongod(conn);
})();