Skip to content

Commit b0a4a0c

Browse files
committed
test(ChangeStream): attempt to stabilize changeStream tests
Attempt to remove client session leaks by encapsulating all insert behavior into a class to manage the resource.
1 parent af3243d commit b0a4a0c

File tree

1 file changed

+41
-12
lines changed

1 file changed

+41
-12
lines changed

test/examples/change_streams.js

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,36 @@ describe('examples(change-stream):', function() {
2525
db = undefined;
2626
});
2727

28+
class Looper {
29+
constructor(lambda, interval) {
30+
this._run = false;
31+
this._lambda = lambda;
32+
this._interval = interval || 50;
33+
}
34+
35+
async _go() {
36+
this._run = true;
37+
while (this._run) {
38+
await new Promise(r => setTimeout(r, this._interval));
39+
await this._lambda();
40+
}
41+
}
42+
43+
run() {
44+
this._p = this._go().catch(() => {});
45+
}
46+
47+
stop() {
48+
this._run = false;
49+
return this._p;
50+
}
51+
}
52+
2853
it('Open A Change Stream', {
2954
metadata: { requires: { topology: ['replicaset'], mongodb: '>=3.6.0' } },
3055
test: async function() {
31-
await db.collection('inventory').insertOne({ a: 1 });
32-
const interval = setInterval(async function() {
33-
await db.collection('inventory').insertOne({ a: 1 });
34-
}, 500);
56+
const looper = new Looper(() => db.collection('inventory').insertOne({ a: 1 }));
57+
looper.run();
3558

3659
// Start Changestream Example 1
3760
const collection = db.collection('inventory');
@@ -48,7 +71,7 @@ describe('examples(change-stream):', function() {
4871

4972
await changeStream.close();
5073
await changeStreamIterator.close();
51-
clearInterval(interval);
74+
await looper.stop();
5275

5376
expect(next)
5477
.to.have.property('operationType')
@@ -60,9 +83,10 @@ describe('examples(change-stream):', function() {
6083
metadata: { requires: { topology: ['replicaset'], mongodb: '>=3.6.0' } },
6184
test: async function() {
6285
await db.collection('inventory').insertOne({ a: 1, b: 2 });
63-
setTimeout(async function() {
64-
await db.collection('inventory').updateOne({ a: 1 }, { $set: { a: 2 } });
65-
}, 250);
86+
const looper = new Looper(() =>
87+
db.collection('inventory').updateOne({ a: 1 }, { $set: { a: 2 } })
88+
);
89+
looper.run();
6690

6791
// Start Changestream Example 2
6892
const collection = db.collection('inventory');
@@ -79,6 +103,7 @@ describe('examples(change-stream):', function() {
79103

80104
await changeStream.close();
81105
await changeStreamIterator.close();
106+
await looper.stop();
82107

83108
expect(next)
84109
.to.have.property('operationType')
@@ -92,10 +117,11 @@ describe('examples(change-stream):', function() {
92117
it('Resume a Change Stream', {
93118
metadata: { requires: { topology: ['replicaset'], mongodb: '>=3.6.0' } },
94119
test: async function() {
95-
setTimeout(async function() {
120+
const looper = new Looper(async () => {
96121
await db.collection('inventory').insertOne({ a: 1 });
97122
await db.collection('inventory').insertOne({ b: 2 });
98-
}, 250);
123+
});
124+
looper.run();
99125

100126
// Start Changestream Example 3
101127
const collection = db.collection('inventory');
@@ -126,6 +152,7 @@ describe('examples(change-stream):', function() {
126152

127153
await newChangeStreamIterator.close();
128154
await newChangeStream.close();
155+
await looper.stop();
129156

130157
expect(change1).to.have.nested.property('fullDocument.a', 1);
131158
expect(change2).to.have.nested.property('fullDocument.b', 2);
@@ -135,9 +162,10 @@ describe('examples(change-stream):', function() {
135162
it('Modify Change Stream Output', {
136163
metadata: { requires: { topology: ['replicaset'], mongodb: '>=3.6.0' } },
137164
test: async function() {
138-
setTimeout(async function() {
165+
const looper = new Looper(async () => {
139166
await db.collection('inventory').insertOne({ username: 'alice' });
140-
}, 250);
167+
});
168+
looper.run();
141169

142170
// Start Changestream Example 4
143171
const pipeline = [
@@ -159,6 +187,7 @@ describe('examples(change-stream):', function() {
159187

160188
await changeStream.close();
161189
await changeStreamIterator.close();
190+
await looper.stop();
162191

163192
expect(next).to.have.nested.property('fullDocument.username', 'alice');
164193
expect(next).to.have.property('newField', 'this is an added field!');

0 commit comments

Comments
 (0)