Skip to content

Commit c5fec5f

Browse files
Jessica Lordmbroadst
Jessica Lord
authored andcommitted
fix(change stream examples): use timeouts, cleanup
1 parent 258d935 commit c5fec5f

File tree

1 file changed

+45
-43
lines changed

1 file changed

+45
-43
lines changed

test/functional/operation_changestream_example_tests.js

+45-43
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
'use strict';
2-
var test = require('./shared').assert;
32
var setupDatabase = require('./shared').setupDatabase;
4-
// var f = require('util').format;
53
var expect = require('chai').expect;
64

75
// ./node_modules/.bin/mongodb-test-runner -l -e replicaset test/functional/operation_changestream_example_tests.js
8-
// ./node_modules/.bin/mongodb-test-runner -l -e -s replicaset test/functional/operation_changestream_example_tests.js
96

107
describe('Changestream Examples', function() {
118
before(function() {
129
return setupDatabase(this.configuration);
1310
});
1411

15-
it('has next', {
12+
it.only('has next', {
1613
metadata: {
1714
requires: {
1815
topology: ['replicaset']
@@ -29,26 +26,27 @@ describe('Changestream Examples', function() {
2926

3027
// Start Changestream Example 1
3128
changeStream.next(function(err, next) {
32-
if (err) console.log(err);
29+
if (err) return console.log(err);
3330
expect(err).to.equal(null);
3431
expect(next).to.exist;
35-
client.close(function() {
36-
done();
37-
});
32+
client.close();
33+
done();
3834
});
3935
// End Changestream Example 1
4036

4137
// Insert something
42-
collection.insertOne({ a: 1 }, function(err, result) {
43-
if (err) return console.log(err);
44-
expect(err).to.equal(null);
45-
expect(result).to.exist;
38+
setTimeout(function() {
39+
collection.insertOne({ a: 1 }, function(err, result) {
40+
if (err) return console.log(err);
41+
expect(err).to.equal(null);
42+
expect(result).to.exist;
43+
});
4644
});
4745
});
4846
}
4947
});
5048

51-
it('event emitter api', {
49+
it.only('uses an event emitter api', {
5250
metadata: {
5351
requires: {
5452
topology: ['replicaset']
@@ -64,27 +62,26 @@ describe('Changestream Examples', function() {
6462
const collection = db.collection('changeStreamExample1b');
6563
const changeStream = collection.watch();
6664

67-
// Use event emitter API
65+
// Using event emitter API
6866
changeStream.on('change', function(change) {
69-
console.log('change', change)
7067
expect(change).to.exist;
71-
client.close(function() {
72-
done();
73-
});
68+
client.close();
69+
done();
7470
});
7571

7672
// Insert something
77-
collection.insertOne({ a: 1 }, function(err, result) {
78-
console.log('inserting');
79-
if (err) return console.log(err);
80-
expect(err).to.equal(null);
81-
expect(result).to.exist;
73+
setTimeout(function() {
74+
collection.insertOne({ a: 1 }, function(err, result) {
75+
if (err) return console.log(err);
76+
expect(err).to.equal(null);
77+
expect(result).to.exist;
78+
});
8279
});
8380
});
8481
}
8582
});
8683

87-
it.only('streams changestream', {
84+
it.only('streams a changestream', {
8885
metadata: {
8986
requires: {
9087
topology: ['replicaset']
@@ -105,16 +102,18 @@ describe('Changestream Examples', function() {
105102
});
106103

107104
// Insert something
108-
collection.insertOne({ a: 1 }, function(err, result) {
109-
if (err) return console.log(err);
110-
expect(err).to.equal(null);
111-
expect(result).to.exist;
105+
setTimeout(function() {
106+
collection.insertOne({ a: 1 }, function(err, result) {
107+
if (err) return console.log(err);
108+
expect(err).to.equal(null);
109+
expect(result).to.exist;
110+
});
112111
});
113112
});
114113
}
115114
});
116115

117-
it('full document update', {
116+
it.only('specifies a full document update', {
118117
metadata: {
119118
requires: {
120119
topology: ['replicaset']
@@ -125,31 +124,32 @@ describe('Changestream Examples', function() {
125124
test: function(done) {
126125
const configuration = this.configuration;
127126
const client = configuration.newClient(configuration.writeConcernMax(), { poolSize: 1 });
128-
// Start Changestream Example 2
129127
client.connect(function(err, client) {
130128
const db = client.db(configuration.db);
131129
const collection = db.collection('changeStreamExample1b');
132130
const changeStream = collection.watch({ fullDocument: 'updateLookup' });
133131

132+
// Start Changestream Example 2
134133
changeStream.on('change', function(change) {
135-
console.log(change);
136134
expect(change).to.exist;
137135
client.close();
138136
done();
139137
});
140138
// End Changestream Eample 2
141139

142140
// Insert something
143-
collection.insertOne({ a: 1 }, function(err, result) {
144-
if (err) return console.log(err);
145-
expect(err).to.equal(null);
146-
expect(result).to.exist;
141+
setTimeout(function() {
142+
collection.insertOne({ a: 1 }, function(err, result) {
143+
if (err) return console.log(err);
144+
expect(err).to.equal(null);
145+
expect(result).to.exist;
146+
});
147147
});
148148
});
149149
}
150150
});
151151

152-
it('creates and uses a resume token', {
152+
it.only('creates and uses a resume token', {
153153
metadata: {
154154
requires: {
155155
topology: ['replicaset']
@@ -159,11 +159,11 @@ describe('Changestream Examples', function() {
159159
test: function(done) {
160160
const configuration = this.configuration;
161161
const client = configuration.newClient(configuration.writeConcernMax(), { poolSize: 1 });
162-
// Start Changestream Example 3
163162
client.connect(function(err, client) {
164163
const db = client.db(configuration.db);
165164
const collection = db.collection('changeStreamExample3');
166165
const changeStream = collection.watch();
166+
// Start Changestream Example 3
167167
let resumeToken;
168168

169169
changeStream.hasNext(function(err, change) {
@@ -196,15 +196,17 @@ describe('Changestream Examples', function() {
196196
});
197197
});
198198
// Insert something
199-
collection.insertOne({ a: 1 }, function(err, result) {
200-
if (err) return console.log(err);
201-
expect(err).to.equal(null);
202-
expect(result).to.exist;
203-
// Insert something else
204-
collection.insertOne({ a: 2 }, function(err, result) {
199+
setTimeout(function() {
200+
collection.insertOne({ a: 1 }, function(err, result) {
205201
if (err) return console.log(err);
206202
expect(err).to.equal(null);
207203
expect(result).to.exist;
204+
// Insert something else
205+
collection.insertOne({ a: 2 }, function(err, result) {
206+
if (err) return console.log(err);
207+
expect(err).to.equal(null);
208+
expect(result).to.exist;
209+
});
208210
});
209211
});
210212
});

0 commit comments

Comments
 (0)