|
1 | 1 | import { expect } from 'chai';
|
2 | 2 |
|
3 |
| -import { ReadPreference } from '../../mongodb'; |
| 3 | +import { type MongoClient, ObjectId, ReadPreference } from '../../mongodb'; |
4 | 4 | import { filterForCommands, ignoreNsNotFound, setupDatabase } from '../shared';
|
5 | 5 |
|
6 | 6 | describe('Command Monitoring', function () {
|
| 7 | + let client: MongoClient; |
| 8 | + |
7 | 9 | before(function () {
|
8 | 10 | return setupDatabase(this.configuration);
|
9 | 11 | });
|
10 | 12 |
|
| 13 | + afterEach(async function () { |
| 14 | + await client?.close(); |
| 15 | + }); |
| 16 | + |
11 | 17 | it('should correctly receive the APM events for an insert', {
|
12 | 18 | metadata: { requires: { topology: ['single', 'replicaset', 'sharded'] } },
|
13 | 19 |
|
@@ -65,81 +71,75 @@ describe('Command Monitoring', function () {
|
65 | 71 | }
|
66 | 72 | });
|
67 | 73 |
|
68 |
| - it('should correctly receive the APM events for a listCollections command', { |
69 |
| - metadata: { requires: { topology: ['replicaset'], mongodb: '>=3.0.0' } }, |
| 74 | + it('records APM events for a listIndexes command', async function () { |
| 75 | + const started = []; |
| 76 | + const succeeded = []; |
| 77 | + client = this.configuration.newClient( |
| 78 | + { writeConcern: { w: 'majority' } }, |
| 79 | + { maxPoolSize: 1, monitorCommands: true } |
| 80 | + ); |
70 | 81 |
|
71 |
| - test: function () { |
72 |
| - const started = []; |
73 |
| - const succeeded = []; |
74 |
| - const client = this.configuration.newClient( |
75 |
| - { writeConcern: { w: 1 } }, |
76 |
| - { maxPoolSize: 1, monitorCommands: true } |
77 |
| - ); |
| 82 | + const desiredEvents = ['listIndexes']; |
| 83 | + client.on('commandStarted', filterForCommands(desiredEvents, started)); |
| 84 | + client.on('commandSucceeded', filterForCommands(desiredEvents, succeeded)); |
78 | 85 |
|
79 |
| - client.on('commandStarted', filterForCommands('listCollections', started)); |
80 |
| - client.on('commandSucceeded', filterForCommands('listCollections', succeeded)); |
| 86 | + const db = client.db(new ObjectId().toHexString()); |
81 | 87 |
|
82 |
| - const db = client.db(this.configuration.db); |
| 88 | + const collection = db.collection('apm_test_list_collections'); |
| 89 | + const session = client.startSession({ causalConsistency: true }); |
83 | 90 |
|
84 |
| - return db |
85 |
| - .collection('apm_test_list_collections') |
86 |
| - .insertOne({ a: 1 }, this.configuration.writeConcernMax()) |
87 |
| - .then(r => { |
88 |
| - expect(r).property('insertedId').to.exist; |
89 |
| - return db.listCollections({}, { readPreference: ReadPreference.primary }).toArray(); |
90 |
| - }) |
91 |
| - .then(() => db.listCollections({}, { readPreference: ReadPreference.secondary }).toArray()) |
92 |
| - .then(() => { |
93 |
| - expect(started).to.have.lengthOf(2); |
94 |
| - expect(started[0]).property('address').to.not.equal(started[1].address); |
| 91 | + const r = await collection.insertOne({ a: 1 }, { writeConcern: { w: 'majority' }, session }); |
| 92 | + expect(r).property('insertedId').to.exist; |
95 | 93 |
|
96 |
| - return client.close(); |
97 |
| - }); |
98 |
| - } |
99 |
| - }); |
| 94 | + expect(await collection.listIndexes({ session }).toArray()).to.have.lengthOf(1); |
100 | 95 |
|
101 |
| - it('should correctly receive the APM events for a listIndexes command', { |
102 |
| - metadata: { requires: { topology: ['replicaset'], mongodb: '>=3.0.0' } }, |
| 96 | + const [{ commandName }] = started; |
| 97 | + expect(commandName).to.equal('listIndexes'); |
| 98 | + }); |
103 | 99 |
|
104 |
| - test: function () { |
| 100 | + it( |
| 101 | + 'records APM events for reads on secondaries', |
| 102 | + { requires: { topology: ['replicaset'] } }, |
| 103 | + async function () { |
105 | 104 | const started = [];
|
106 | 105 | const succeeded = [];
|
107 |
| - const client = this.configuration.newClient( |
108 |
| - { writeConcern: { w: 1 } }, |
| 106 | + client = this.configuration.newClient( |
| 107 | + { writeConcern: { w: 'majority' } }, |
109 | 108 | { maxPoolSize: 1, monitorCommands: true }
|
110 | 109 | );
|
111 | 110 |
|
112 | 111 | const desiredEvents = ['listIndexes', 'find'];
|
113 | 112 | client.on('commandStarted', filterForCommands(desiredEvents, started));
|
114 | 113 | client.on('commandSucceeded', filterForCommands(desiredEvents, succeeded));
|
115 | 114 |
|
116 |
| - const db = client.db(this.configuration.db); |
| 115 | + const db = client.db(new ObjectId().toHexString()); |
117 | 116 |
|
118 |
| - return db |
119 |
| - .collection('apm_test_list_collections') |
120 |
| - .insertOne({ a: 1 }, this.configuration.writeConcernMax()) |
121 |
| - .then(r => { |
122 |
| - expect(r).property('insertedId').to.exist; |
| 117 | + const collection = db.collection('apm_test_list_collections'); |
| 118 | + const session = client.startSession({ causalConsistency: true }); |
123 | 119 |
|
124 |
| - return db |
125 |
| - .collection('apm_test_list_collections') |
126 |
| - .listIndexes({ readPreference: ReadPreference.PRIMARY }) |
127 |
| - .toArray(); |
128 |
| - }) |
129 |
| - .then(() => |
130 |
| - db |
131 |
| - .collection('apm_test_list_collections') |
132 |
| - .listIndexes({ readPreference: ReadPreference.SECONDARY }) |
133 |
| - .toArray() |
134 |
| - ) |
135 |
| - .then(() => { |
136 |
| - expect(started).to.have.lengthOf(2); |
137 |
| - expect(started[0]).property('address').to.not.equal(started[1].address); |
| 120 | + const r = await collection.insertOne({ a: 1 }, { writeConcern: { w: 'majority' }, session }); |
| 121 | + expect(r).property('insertedId').to.exist; |
138 | 122 |
|
139 |
| - return client.close(); |
| 123 | + await collection |
| 124 | + .listIndexes({ readPreference: ReadPreference.PRIMARY, session }) |
| 125 | + .toArray() |
| 126 | + .catch(e => { |
| 127 | + throw new Error('primary listIndexes failed', { cause: e }); |
140 | 128 | });
|
| 129 | + |
| 130 | + await collection |
| 131 | + .listIndexes({ readPreference: ReadPreference.SECONDARY, session }) |
| 132 | + .toArray() |
| 133 | + .catch(() => { |
| 134 | + // reading with secondary read preference means the data may or may not have been propagated to the seconary |
| 135 | + // node yet. for this test, we are asserting that we did correctly send commands to different nodes, so |
| 136 | + // the actual outcome of this listIndexes doesn't matter. |
| 137 | + }); |
| 138 | + |
| 139 | + const [{ address: primaryAddress }, { address: secondaryAddress }] = started; |
| 140 | + expect(primaryAddress).not.to.equal(secondaryAddress); |
141 | 141 | }
|
142 |
| - }); |
| 142 | + ); |
143 | 143 |
|
144 | 144 | it('should correctly receive the APM events for a find with getmore and killcursor', {
|
145 | 145 | metadata: { requires: { topology: ['single', 'replicaset'] } },
|
|
0 commit comments