|
1 | 1 | 'use strict';
|
2 | 2 |
|
3 | 3 | const { expect } = require('chai');
|
| 4 | +const Sinon = require('sinon'); |
4 | 5 |
|
5 |
| -// TODO: unskip as part of NODE-2590 |
6 |
| -describe.skip('Cursor Async Iterator Tests', function () { |
7 |
| - let client, collection; |
8 |
| - before(async function () { |
9 |
| - client = this.configuration.newClient(); |
| 6 | +describe('Cursor Async Iterator Tests', function () { |
| 7 | + context('default promise library', function () { |
| 8 | + let client, collection; |
| 9 | + before(async function () { |
| 10 | + client = this.configuration.newClient(); |
10 | 11 |
|
11 |
| - await client.connect(); |
12 |
| - const docs = Array.from({ length: 1000 }).map((_, index) => ({ foo: index, bar: 1 })); |
| 12 | + await client.connect(); |
| 13 | + const docs = Array.from({ length: 1000 }).map((_, index) => ({ foo: index, bar: 1 })); |
13 | 14 |
|
14 |
| - collection = client.db(this.configuration.db).collection('async_cursor_tests'); |
| 15 | + collection = client.db(this.configuration.db).collection('async_cursor_tests'); |
15 | 16 |
|
16 |
| - await collection.deleteMany({}); |
17 |
| - await collection.insertMany(docs); |
18 |
| - await client.close(); |
19 |
| - }); |
| 17 | + await collection.deleteMany({}); |
| 18 | + await collection.insertMany(docs); |
| 19 | + await client.close(); |
| 20 | + }); |
20 | 21 |
|
21 |
| - beforeEach(async function () { |
22 |
| - client = this.configuration.newClient(); |
23 |
| - await client.connect(); |
24 |
| - collection = client.db(this.configuration.db).collection('async_cursor_tests'); |
25 |
| - }); |
| 22 | + beforeEach(async function () { |
| 23 | + client = this.configuration.newClient(); |
| 24 | + await client.connect(); |
| 25 | + collection = client.db(this.configuration.db).collection('async_cursor_tests'); |
| 26 | + }); |
| 27 | + |
| 28 | + afterEach(() => client.close()); |
26 | 29 |
|
27 |
| - afterEach(() => client.close()); |
| 30 | + it('should be able to use a for-await loop on a find command cursor', { |
| 31 | + metadata: { requires: { node: '>=10.5.0' } }, |
| 32 | + test: async function () { |
| 33 | + const cursor = collection.find({ bar: 1 }); |
28 | 34 |
|
29 |
| - it('should be able to use a for-await loop on a find command cursor', { |
30 |
| - metadata: { requires: { node: '>=10.5.0' } }, |
31 |
| - test: async function () { |
32 |
| - const cursor = collection.find({ bar: 1 }); |
| 35 | + let counter = 0; |
| 36 | + for await (const doc of cursor) { |
| 37 | + expect(doc).to.have.property('bar', 1); |
| 38 | + counter += 1; |
| 39 | + } |
33 | 40 |
|
34 |
| - let counter = 0; |
35 |
| - for await (const doc of cursor) { |
36 |
| - expect(doc).to.have.property('bar', 1); |
37 |
| - counter += 1; |
| 41 | + expect(counter).to.equal(1000); |
38 | 42 | }
|
| 43 | + }); |
39 | 44 |
|
40 |
| - expect(counter).to.equal(1000); |
41 |
| - } |
42 |
| - }); |
| 45 | + it('should be able to use a for-await loop on an aggregation cursor', { |
| 46 | + metadata: { requires: { node: '>=10.5.0' } }, |
| 47 | + test: async function () { |
| 48 | + const cursor = collection.aggregate([{ $match: { bar: 1 } }]); |
43 | 49 |
|
44 |
| - it('should be able to use a for-await loop on an aggregation cursor', { |
45 |
| - metadata: { requires: { node: '>=10.5.0' } }, |
46 |
| - test: async function () { |
47 |
| - const cursor = collection.aggregate([{ $match: { bar: 1 } }]); |
| 50 | + let counter = 0; |
| 51 | + for await (const doc of cursor) { |
| 52 | + expect(doc).to.have.property('bar', 1); |
| 53 | + counter += 1; |
| 54 | + } |
48 | 55 |
|
49 |
| - let counter = 0; |
50 |
| - for await (const doc of cursor) { |
51 |
| - expect(doc).to.have.property('bar', 1); |
52 |
| - counter += 1; |
| 56 | + expect(counter).to.equal(1000); |
53 | 57 | }
|
54 |
| - |
55 |
| - expect(counter).to.equal(1000); |
56 |
| - } |
57 |
| - }); |
58 |
| - |
59 |
| - it('should be able to use a for-await loop on a command cursor', { |
60 |
| - metadata: { requires: { node: '>=10.5.0', mongodb: '>=3.0.0' } }, |
61 |
| - test: async function () { |
62 |
| - const cursor1 = collection.listIndexes(); |
63 |
| - const cursor2 = collection.listIndexes(); |
64 |
| - |
65 |
| - const indexes = await cursor1.toArray(); |
66 |
| - let counter = 0; |
67 |
| - for await (const doc of cursor2) { |
68 |
| - expect(doc).to.exist; |
69 |
| - counter += 1; |
| 58 | + }); |
| 59 | + |
| 60 | + it('should be able to use a for-await loop on a command cursor', { |
| 61 | + metadata: { requires: { node: '>=10.5.0', mongodb: '>=3.0.0' } }, |
| 62 | + test: async function () { |
| 63 | + const cursor1 = collection.listIndexes(); |
| 64 | + const cursor2 = collection.listIndexes(); |
| 65 | + |
| 66 | + const indexes = await cursor1.toArray(); |
| 67 | + let counter = 0; |
| 68 | + for await (const doc of cursor2) { |
| 69 | + expect(doc).to.exist; |
| 70 | + counter += 1; |
| 71 | + } |
| 72 | + |
| 73 | + expect(counter).to.equal(indexes.length); |
70 | 74 | }
|
| 75 | + }); |
71 | 76 |
|
72 |
| - expect(counter).to.equal(indexes.length); |
73 |
| - } |
74 |
| - }); |
| 77 | + it('should properly stop when cursor is closed', { |
| 78 | + metadata: { requires: { node: '>=10.5.0' } }, |
| 79 | + test: async function () { |
| 80 | + const cursor = collection.find(); |
75 | 81 |
|
76 |
| - it('should properly stop when cursor is closed', { |
77 |
| - metadata: { requires: { node: '>=10.5.0' } }, |
78 |
| - test: async function () { |
79 |
| - const cursor = collection.find(); |
| 82 | + let count = 0; |
| 83 | + for await (const doc of cursor) { |
| 84 | + expect(doc).to.exist; |
| 85 | + count++; |
| 86 | + await cursor.close(); |
| 87 | + } |
80 | 88 |
|
81 |
| - let count = 0; |
82 |
| - for await (const doc of cursor) { |
83 |
| - expect(doc).to.exist; |
84 |
| - count++; |
85 |
| - await cursor.close(); |
| 89 | + expect(count).to.equal(1); |
86 | 90 | }
|
87 |
| - |
88 |
| - expect(count).to.equal(1); |
89 |
| - } |
| 91 | + }); |
| 92 | + }); |
| 93 | + context('custom promise library', () => { |
| 94 | + let client, collection, promiseSpy; |
| 95 | + before(async function () { |
| 96 | + class CustomPromise extends Promise {} |
| 97 | + promiseSpy = Sinon.spy(CustomPromise.prototype, 'then'); |
| 98 | + client = this.configuration.newClient({}, { promiseLibrary: CustomPromise }); |
| 99 | + |
| 100 | + await client.connect(); |
| 101 | + const docs = Array.from({ length: 1 }).map((_, index) => ({ foo: index, bar: 1 })); |
| 102 | + |
| 103 | + collection = client.db(this.configuration.db).collection('async_cursor_tests'); |
| 104 | + |
| 105 | + await collection.deleteMany({}); |
| 106 | + await collection.insertMany(docs); |
| 107 | + await client.close(); |
| 108 | + }); |
| 109 | + |
| 110 | + beforeEach(async function () { |
| 111 | + client = this.configuration.newClient(); |
| 112 | + await client.connect(); |
| 113 | + collection = client.db(this.configuration.db).collection('async_cursor_tests'); |
| 114 | + }); |
| 115 | + |
| 116 | + afterEach(() => { |
| 117 | + promiseSpy.restore(); |
| 118 | + return client.close(); |
| 119 | + }); |
| 120 | + |
| 121 | + it('should properly use custom promise', { |
| 122 | + metadata: { requires: { node: '>=10.5.0' } }, |
| 123 | + test: async function () { |
| 124 | + const cursor = collection.find(); |
| 125 | + const countBeforeIteration = promiseSpy.callCount; |
| 126 | + for await (const doc of cursor) { |
| 127 | + expect(doc).to.exist; |
| 128 | + } |
| 129 | + expect(countBeforeIteration).to.not.equal(promiseSpy.callCount); |
| 130 | + expect(promiseSpy.called).to.equal(true); |
| 131 | + } |
| 132 | + }); |
90 | 133 | });
|
91 | 134 | });
|
0 commit comments