|
8 | 8 | LEGACY_HELLO_COMMAND,
|
9 | 9 | type MongoClient
|
10 | 10 | } from '../../mongodb';
|
| 11 | +import { sleep } from '../../tools/utils'; |
11 | 12 |
|
12 | 13 | type EnvironmentVariables = Array<[string, string]>;
|
13 | 14 |
|
@@ -194,3 +195,161 @@ describe('Handshake Prose Tests', function () {
|
194 | 195 | });
|
195 | 196 | });
|
196 | 197 | });
|
| 198 | + |
| 199 | +describe('Client Metadata Update Prose Tests', function () { |
| 200 | + let client: MongoClient; |
| 201 | + |
| 202 | + afterEach(async function () { |
| 203 | + await client?.close(); |
| 204 | + sinon.restore(); |
| 205 | + }); |
| 206 | + |
| 207 | + describe('Test 1: Test that the driver updates metadata', function () { |
| 208 | + let initialClientMetadata; |
| 209 | + let updatedClientMetadata; |
| 210 | + |
| 211 | + const tests = [ |
| 212 | + { testCase: 1, name: 'framework', version: '2.0', platform: 'Framework Platform' }, |
| 213 | + { testCase: 2, name: 'framework', version: '2.0' }, |
| 214 | + { testCase: 3, name: 'framework', platform: 'Framework Platform' }, |
| 215 | + { testCase: 4, name: 'framework' } |
| 216 | + ]; |
| 217 | + |
| 218 | + for (const { testCase, name, version, platform } of tests) { |
| 219 | + context(`Case: ${testCase}`, function () { |
| 220 | + // 1. Create a `MongoClient` instance with the following: |
| 221 | + // - `maxIdleTimeMS` set to `1ms` |
| 222 | + // - Wrapping library metadata: |
| 223 | + // | Field | Value | |
| 224 | + // | -------- | ---------------- | |
| 225 | + // | name | library | |
| 226 | + // | version | 1.2 | |
| 227 | + // | platform | Library Platform | |
| 228 | + // 2. Send a `ping` command to the server and verify that the command succeeds. |
| 229 | + // 3. Save intercepted `client` document as `initialClientMetadata`. |
| 230 | + // 4. Wait 5ms for the connection to become idle. |
| 231 | + beforeEach(async function () { |
| 232 | + client = this.configuration.newClient( |
| 233 | + {}, |
| 234 | + { |
| 235 | + maxIdleTimeMS: 1, |
| 236 | + driverInfo: { name: 'library', version: '1.2', platform: 'Library Platform' } |
| 237 | + } |
| 238 | + ); |
| 239 | + |
| 240 | + sinon.stub(Connection.prototype, 'command').callsFake(async function (ns, cmd, options) { |
| 241 | + // @ts-expect-error: sinon will place wrappedMethod on the command method. |
| 242 | + const command = Connection.prototype.command.wrappedMethod.bind(this); |
| 243 | + |
| 244 | + if (cmd.hello || cmd[LEGACY_HELLO_COMMAND]) { |
| 245 | + if (!initialClientMetadata) { |
| 246 | + initialClientMetadata = cmd.client; |
| 247 | + } else { |
| 248 | + updatedClientMetadata = cmd.client; |
| 249 | + } |
| 250 | + } |
| 251 | + return command(ns, cmd, options); |
| 252 | + }); |
| 253 | + |
| 254 | + await client.db('test').command({ ping: 1 }); |
| 255 | + await sleep(5); |
| 256 | + }); |
| 257 | + |
| 258 | + it('appends the metadata', async function () { |
| 259 | + // 1. Append the `DriverInfoOptions` from the selected test case to the `MongoClient` metadata. |
| 260 | + // 2. Send a `ping` command to the server and verify: |
| 261 | + // - The command succeeds. |
| 262 | + // - The framework metadata is appended to the existing `DriverInfoOptions` in the `client.driver` fields of the `hello` |
| 263 | + // command, with values separated by a pipe `|`. |
| 264 | + client.appendMetadata({ name, version, platform }); |
| 265 | + await client.db('test').command({ ping: 1 }); |
| 266 | + |
| 267 | + // Since we have our own driver metadata getting added, we really want to just |
| 268 | + // assert that the last driver info values are appended at the end. |
| 269 | + expect(updatedClientMetadata.driver.name).to.match(/^.*\|framework$/); |
| 270 | + expect(updatedClientMetadata.driver.version).to.match( |
| 271 | + new RegExp(`^.*\\|${version ? version : '1.2'}$`) |
| 272 | + ); |
| 273 | + expect(updatedClientMetadata.platform).to.match( |
| 274 | + new RegExp(`^.*\\|${platform ? platform : 'Library Platform'}$`) |
| 275 | + ); |
| 276 | + // - All other subfields in the client document remain unchanged from initialClientMetadata. |
| 277 | + // (Note os is the only one getting set in these tests) |
| 278 | + expect(updatedClientMetadata.os).to.deep.equal(initialClientMetadata.os); |
| 279 | + }); |
| 280 | + }); |
| 281 | + } |
| 282 | + }); |
| 283 | + |
| 284 | + describe('Test 2: Multiple Successive Metadata Updates', function () { |
| 285 | + let initialClientMetadata; |
| 286 | + let updatedClientMetadata; |
| 287 | + |
| 288 | + const tests = [ |
| 289 | + { testCase: 1, name: 'framework', version: '2.0', platform: 'Framework Platform' }, |
| 290 | + { testCase: 2, name: 'framework', version: '2.0' }, |
| 291 | + { testCase: 3, name: 'framework', platform: 'Framework Platform' }, |
| 292 | + { testCase: 4, name: 'framework' } |
| 293 | + ]; |
| 294 | + |
| 295 | + for (const { testCase, name, version, platform } of tests) { |
| 296 | + context(`Case: ${testCase}`, function () { |
| 297 | + // 1. Create a `MongoClient` instance with the following: |
| 298 | + // - `maxIdleTimeMS` set to `1ms` |
| 299 | + // 2. Append the following `DriverInfoOptions` to the `MongoClient` metadata: |
| 300 | + // | Field | Value | |
| 301 | + // | -------- | ---------------- | |
| 302 | + // | name | library | |
| 303 | + // | version | 1.2 | |
| 304 | + // | platform | Library Platform | |
| 305 | + // 3. Send a `ping` command to the server and verify that the command succeeds. |
| 306 | + // 4. Save intercepted `client` document as `updatedClientMetadata`. |
| 307 | + // 5. Wait 5ms for the connection to become idle. |
| 308 | + beforeEach(async function () { |
| 309 | + client = this.configuration.newClient({}, { maxIdleTimeMS: 1 }); |
| 310 | + client.appendMetadata({ name: 'library', version: '1.2', platform: 'Library Platform' }); |
| 311 | + |
| 312 | + sinon.stub(Connection.prototype, 'command').callsFake(async function (ns, cmd, options) { |
| 313 | + // @ts-expect-error: sinon will place wrappedMethod on the command method. |
| 314 | + const command = Connection.prototype.command.wrappedMethod.bind(this); |
| 315 | + |
| 316 | + if (cmd.hello || cmd[LEGACY_HELLO_COMMAND]) { |
| 317 | + if (!initialClientMetadata) { |
| 318 | + initialClientMetadata = cmd.client; |
| 319 | + } else { |
| 320 | + updatedClientMetadata = cmd.client; |
| 321 | + } |
| 322 | + } |
| 323 | + return command(ns, cmd, options); |
| 324 | + }); |
| 325 | + |
| 326 | + await client.db('test').command({ ping: 1 }); |
| 327 | + await sleep(5); |
| 328 | + }); |
| 329 | + |
| 330 | + it('appends the metadata', async function () { |
| 331 | + // 1. Append the `DriverInfoOptions` from the selected test case to the `MongoClient` metadata. |
| 332 | + // 2. Send a `ping` command to the server and verify: |
| 333 | + // - The command succeeds. |
| 334 | + // - The framework metadata is appended to the existing `DriverInfoOptions` in the `client.driver` fields of the `hello` |
| 335 | + // command, with values separated by a pipe `|`. |
| 336 | + client.appendMetadata({ name, version, platform }); |
| 337 | + await client.db('test').command({ ping: 1 }); |
| 338 | + |
| 339 | + // Since we have our own driver metadata getting added, we really want to just |
| 340 | + // assert that the last driver info values are appended at the end. |
| 341 | + expect(updatedClientMetadata.driver.name).to.match(/^.*\|framework$/); |
| 342 | + expect(updatedClientMetadata.driver.version).to.match( |
| 343 | + new RegExp(`^.*\\|${version ? version : '1.2'}$`) |
| 344 | + ); |
| 345 | + expect(updatedClientMetadata.platform).to.match( |
| 346 | + new RegExp(`^.*\\|${platform ? platform : 'Library Platform'}$`) |
| 347 | + ); |
| 348 | + // - All other subfields in the client document remain unchanged from initialClientMetadata. |
| 349 | + // (Note os is the only one getting set in these tests) |
| 350 | + expect(updatedClientMetadata.os).to.deep.equal(initialClientMetadata.os); |
| 351 | + }); |
| 352 | + }); |
| 353 | + } |
| 354 | + }); |
| 355 | +}); |
0 commit comments