|
1 | 1 | import { expect } from 'chai'; |
2 | 2 |
|
3 | | -import { |
4 | | - Code, |
5 | | - enumToString, |
6 | | - type MongoClient, |
7 | | - ProfilingLevel, |
8 | | - ReturnDocument |
9 | | -} from '../../mongodb'; |
| 3 | +import { Code, type MongoClient, ProfilingLevel, ReturnDocument } from '../../../src'; |
| 4 | +import { enumToString } from '../../../src/utils'; |
10 | 5 | import { sleep as delay } from '../../tools/utils'; |
11 | 6 | import { setupDatabase } from '../shared'; |
12 | 7 |
|
@@ -3989,77 +3984,47 @@ describe('Operations', function () { |
3989 | 3984 | }); |
3990 | 3985 |
|
3991 | 3986 | /** |
3992 | | - * A simple example showing the listening to a capped collection using a Promise. |
| 3987 | + * A simple example showing the listening to a capped collection. |
3993 | 3988 | * |
3994 | 3989 | * example-class Db |
3995 | 3990 | * example-method createCollection |
3996 | 3991 | */ |
3997 | | - it('Should correctly add capped collection options to cursor With Promises', { |
3998 | | - metadata: { requires: { topology: ['single'] } }, |
3999 | | - |
4000 | | - test: function (done) { |
4001 | | - const configuration = this.configuration; |
4002 | | - const client = configuration.newClient(configuration.writeConcernMax(), { |
4003 | | - maxPoolSize: 1 |
4004 | | - }); |
4005 | | - |
4006 | | - client.connect().then(function (client) { |
4007 | | - const db = client.db(configuration.db); |
4008 | | - // LINE var MongoClient = require('mongodb').MongoClient, |
4009 | | - // LINE test = require('assert'); |
4010 | | - // LINE const client = new MongoClient('mongodb://localhost:27017/test'); |
4011 | | - // LINE client.connect().then(() => { |
4012 | | - // LINE var db = client.db('test); |
4013 | | - // REPLACE configuration.writeConcernMax() WITH {w:1} |
4014 | | - // REMOVE-LINE done(); |
4015 | | - // BEGIN |
4016 | | - // Create a capped collection with a maximum of 1000 documents |
4017 | | - let collection; |
4018 | | - |
4019 | | - db.createCollection('a_simple_collection_2_with_promise', { |
4020 | | - capped: true, |
4021 | | - size: 100000, |
4022 | | - max: 1000, |
4023 | | - writeConcern: { w: 1 } |
4024 | | - }) |
4025 | | - .then(function (_collection) { |
4026 | | - collection = _collection; |
4027 | | - |
4028 | | - const docs: Array<{ a: number }> = []; |
4029 | | - for (let i = 0; i < 1000; i++) docs.push({ a: i }); |
| 3992 | + it('Should correctly add capped collection options to cursor', async function () { |
| 3993 | + const configuration = this.configuration; |
| 3994 | + const client = configuration.newClient(configuration.writeConcernMax(), { |
| 3995 | + maxPoolSize: 1 |
| 3996 | + }); |
4030 | 3997 |
|
4031 | | - // Insert a document in the capped collection |
4032 | | - return collection.insertMany(docs, configuration.writeConcernMax()); |
4033 | | - }) |
4034 | | - .then(function (result) { |
4035 | | - expect(result).to.exist; |
| 3998 | + await client.connect(); |
| 3999 | + const db = client.db(configuration.db); |
4036 | 4000 |
|
4037 | | - let total = 0; |
| 4001 | + const collection = await db.createCollection('a_simple_collection_2_with_promise', { |
| 4002 | + capped: true, |
| 4003 | + size: 100000, |
| 4004 | + max: 1000, |
| 4005 | + writeConcern: { w: 1 } |
| 4006 | + }); |
| 4007 | + const docs: Array<{ a: number }> = []; |
| 4008 | + for (let i = 0; i < 10000; i++) docs.push({ a: i }); |
4038 | 4009 |
|
4039 | | - // Get the cursor |
4040 | | - const cursor = collection |
4041 | | - .find({ a: { $gte: 0 } }) |
4042 | | - .addCursorFlag('tailable', true) |
4043 | | - .addCursorFlag('awaitData', true); |
| 4010 | + // Insert a document in the capped collection |
| 4011 | + await collection.insertMany(docs, configuration.writeConcernMax()); |
| 4012 | + let total = 0; |
4044 | 4013 |
|
4045 | | - const stream = cursor.stream(); |
4046 | | - stream.on('data', function (d) { |
4047 | | - expect(d).to.exist; |
4048 | | - total = total + 1; |
| 4014 | + // Get the cursor |
| 4015 | + const cursor = collection |
| 4016 | + .find({ a: { $gte: 0 } }) |
| 4017 | + .addCursorFlag('tailable', true) |
| 4018 | + .addCursorFlag('awaitData', true); |
4049 | 4019 |
|
4050 | | - if (total === 1000) { |
4051 | | - cursor.close(); |
4052 | | - } |
4053 | | - }); |
| 4020 | + const stream = cursor.stream(); |
4054 | 4021 |
|
4055 | | - cursor.on('close', function () { |
4056 | | - // TODO: forced because the cursor is still open/active |
4057 | | - client.close(true, done); |
4058 | | - }); |
4059 | | - }); |
4060 | | - }); |
4061 | | - // END |
| 4022 | + for await (const d of stream) { |
| 4023 | + expect(d).to.have.property('_id'); |
| 4024 | + total = total + 1; |
| 4025 | + if (total === 1000) await cursor.close(); |
4062 | 4026 | } |
| 4027 | + await client.close(); |
4063 | 4028 | }); |
4064 | 4029 |
|
4065 | 4030 | describe('Transaction Examples', function () { |
|
0 commit comments