summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/lookup_unwind_killcursor.js
blob: 0e3e547b9206d547d907607058a5aa5578ffae8d (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
94
/**
 * Tests that the cursor underlying the $lookup stage is killed when the cursor returned to the
 * client for the aggregation pipeline is killed.
 *
 * This test was designed to reproduce SERVER-24386.
 */
(function() {
'use strict';

const options = {
    setParameter: 'internalDocumentSourceCursorBatchSizeBytes=1'
};
const conn = MongoRunner.runMongod(options);
assert.neq(null, conn, 'mongod was unable to start up with options: ' + tojson(options));

const testDB = conn.getDB('test');

function runTest(pipeline) {
    // We use a batch size of 2 to ensure that the mongo shell does not exhaust the cursor on
    // its first batch.
    const batchSize = 2;

    testDB.source.drop();
    assert.commandWorked(testDB.source.insert({x: 1}));

    testDB.dest.drop();
    for (let i = 0; i < 5; ++i) {
        assert.commandWorked(testDB.dest.insert({x: 1}));
    }

    const res = assert.commandWorked(testDB.runCommand({
        aggregate: 'source',
        pipeline: pipeline,
        cursor: {
            batchSize: batchSize,
        },
    }));

    const cursor = new DBCommandCursor(testDB, res, batchSize);
    cursor.close();  // Closing the cursor will issue the "killCursors" command.

    const serverStatus = assert.commandWorked(testDB.adminCommand({serverStatus: 1}));
    assert.eq(0, serverStatus.metrics.cursor.open.total, tojson(serverStatus.metrics.cursor));
}

runTest([
        {
          $lookup: {
              from: 'dest',
              localField: 'x',
              foreignField: 'x',
              as: 'matches',
          }
        },
        {
          $unwind: {
              path: '$matches',
          },
        },
    ]);

runTest([
        {
          $lookup: {
              from: 'dest',
              let : {x1: "$x"},
              pipeline: [
                  {$match: {$expr: {$eq: ["$$x1", "$x"]}}},
                  {
                    $lookup: {
                        from: "dest",
                        as: "matches2",
                        let : {x2: "$x"},
                        pipeline: [{$match: {$expr: {$eq: ["$$x2", "$x"]}}}]
                    }
                  },
                  {
                    $unwind: {
                        path: '$matches2',
                    },
                  },
              ],
              as: 'matches1',
          }
        },
        {
          $unwind: {
              path: '$matches1',
          },
        },
    ]);

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