summaryrefslogtreecommitdiff
path: root/jstests/core/timeseries/timeseries_out_of_order.js
blob: ef89104b2890197c6863cdfe53e660d77b0a45b8 (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
/**
 * Tests that time-series inserts occurring before a bucket is inserted go into the same bucket if
 * they are within the time range, regardless of the order in which they are inserted.
 *
 * @tags: [
 *     assumes_no_implicit_collection_creation_after_drop,
 *     does_not_support_retryable_writes,  # Batches containing more than one measurement
 *     does_not_support_stepdowns,
 *     requires_fcv_49,
 *     requires_find_command,
 *     requires_getmore,
 *     sbe_incompatible,
 * ]
 */
(function() {
'use strict';

load('jstests/core/timeseries/libs/timeseries.js');

if (!TimeseriesTest.timeseriesCollectionsEnabled(db.getMongo())) {
    jsTestLog('Skipping test because the time-series collection feature flag is disabled');
    return;
}

const testDB = db.getSiblingDB(jsTestName());

const timeFieldName = 'time';
const times = [
    ISODate('2021-01-01T01:00:00Z'),
    ISODate('2021-01-01T01:30:00Z'),
    ISODate('2021-01-01T02:00:00Z')
];
let docs = [{_id: 0, [timeFieldName]: times[1]}, {_id: 1, [timeFieldName]: times[0]}];

let collCount = 0;
const runTest = function(bucketsFn) {
    const coll = testDB.getCollection('t_' + collCount++);
    const bucketsColl = testDB.getCollection('system.buckets.' + coll.getName());
    coll.drop();

    assert.commandWorked(
        testDB.createCollection(coll.getName(), {timeseries: {timeField: timeFieldName}}));
    assert.contains(bucketsColl.getName(), testDB.getCollectionNames());

    assert.commandWorked(coll.insert(docs, {ordered: false}));
    assert.docEq(coll.find().sort({_id: 1}).toArray(), docs);

    const buckets = bucketsColl.find().sort({_id: 1}).toArray();
    jsTestLog('Checking buckets:' + tojson(buckets));
    bucketsFn(buckets);
};

runTest(buckets => {
    assert.eq(buckets.length, 1);
    assert.eq(buckets[0].control.min[timeFieldName], times[0]);
    assert.eq(buckets[0].control.max[timeFieldName], times[1]);
});

docs.push({_id: 2, [timeFieldName]: times[2]});
runTest(buckets => {
    assert.eq(buckets.length, 2);
    assert.eq(buckets[0].control.min[timeFieldName], times[0]);
    assert.eq(buckets[0].control.max[timeFieldName], times[1]);
    assert.eq(buckets[1].control.min[timeFieldName], times[2]);
    assert.eq(buckets[1].control.max[timeFieldName], times[2]);
});

docs = [{_id: 0, [timeFieldName]: times[2]}, {_id: 1, [timeFieldName]: times[0]}];
runTest(buckets => {
    assert.eq(buckets.length, 2);
    assert.eq(buckets[0].control.min[timeFieldName], times[0]);
    assert.eq(buckets[0].control.max[timeFieldName], times[0]);
    assert.eq(buckets[1].control.min[timeFieldName], times[2]);
    assert.eq(buckets[1].control.max[timeFieldName], times[2]);
});
})();