@@ -3,17 +3,42 @@ const { withClient, setupDatabase } = require('./shared');
3
3
const test = require ( './shared' ) . assert ;
4
4
const { expect } = require ( 'chai' ) ;
5
5
const { ServerHeartbeatStartedEvent, MongoClient } = require ( '../../src' ) ;
6
+ const { Topology } = require ( '../../src/sdam/topology' ) ;
6
7
7
8
describe ( 'Connection - functional' , function ( ) {
9
+ let client ;
10
+ let testClient ;
11
+
8
12
before ( function ( ) {
9
13
return setupDatabase ( this . configuration ) ;
10
14
} ) ;
11
15
16
+ afterEach ( async ( ) => {
17
+ let savedError ;
18
+ if ( client ) {
19
+ try {
20
+ await client . close ( ) ;
21
+ } catch ( err ) {
22
+ savedError = err ;
23
+ }
24
+ }
25
+ if ( testClient ) {
26
+ try {
27
+ await testClient . close ( ) ;
28
+ } catch ( err ) {
29
+ savedError = err ;
30
+ }
31
+ }
32
+ if ( savedError ) {
33
+ throw savedError ;
34
+ }
35
+ } ) ;
36
+
12
37
it ( 'should correctly start monitoring for single server connection' , {
13
38
metadata : { requires : { topology : 'single' , os : '!win32' } } ,
14
- test ( ) {
39
+ test : function ( done ) {
15
40
var configuration = this . configuration ;
16
- var client = configuration . newClient (
41
+ client = configuration . newClient (
17
42
`mongodb://${ encodeURIComponent ( '/tmp/mongodb-27017.sock' ) } ?w=1` ,
18
43
{
19
44
maxPoolSize : 1 ,
@@ -27,12 +52,10 @@ describe('Connection - functional', function () {
27
52
isMonitoring = event instanceof ServerHeartbeatStartedEvent ;
28
53
} ) ;
29
54
30
- return client
31
- . connect ( )
32
- . then ( ( ) => {
33
- expect ( isMonitoring ) ;
34
- } )
35
- . finally ( ( ) => client . close ( ) ) ;
55
+ client . connect ( ) . then ( ( ) => {
56
+ expect ( isMonitoring ) ;
57
+ done ( ) ;
58
+ } ) ;
36
59
}
37
60
} ) ;
38
61
@@ -41,7 +64,7 @@ describe('Connection - functional', function () {
41
64
42
65
test : function ( done ) {
43
66
var configuration = this . configuration ;
44
- var client = configuration . newClient (
67
+ client = configuration . newClient (
45
68
`mongodb://${ encodeURIComponent ( '/tmp/mongodb-27017.sock' ) } ?w=1` ,
46
69
{ maxPoolSize : 1 }
47
70
) ;
@@ -62,22 +85,37 @@ describe('Connection - functional', function () {
62
85
expect ( err ) . to . not . exist ;
63
86
test . equal ( 1 , items . length ) ;
64
87
65
- client . close ( done ) ;
88
+ done ( ) ;
66
89
} ) ;
67
90
}
68
91
) ;
69
92
} ) ;
70
93
}
71
94
} ) ;
72
95
96
+ it ( 'should only pass one argument (topology and not error) for topology "open" events' , function ( done ) {
97
+ const configuration = this . configuration ;
98
+ client = configuration . newClient ( { w : 1 } , { maxPoolSize : 1 } ) ;
99
+
100
+ client . on ( 'topologyOpening' , ( ) => {
101
+ client . topology . on ( 'open' , ( ...args ) => {
102
+ expect ( args ) . to . have . lengthOf ( 1 ) ;
103
+ expect ( args [ 0 ] ) . to . be . instanceOf ( Topology ) ;
104
+ done ( ) ;
105
+ } ) ;
106
+ } ) ;
107
+
108
+ client . connect ( ) ;
109
+ } ) ;
110
+
73
111
it ( 'should correctly connect to server using just events' , function ( done ) {
74
112
var configuration = this . configuration ;
75
- var client = configuration . newClient ( { w : 1 } , { maxPoolSize : 1 } ) ;
113
+ client = configuration . newClient ( { w : 1 } , { maxPoolSize : 1 } ) ;
76
114
77
115
client . on ( 'open' , clientFromEvent => {
78
116
expect ( clientFromEvent ) . to . be . instanceOf ( MongoClient ) ;
79
117
expect ( clientFromEvent ) . to . equal ( client ) ;
80
- clientFromEvent . close ( done ) ;
118
+ done ( ) ;
81
119
} ) ;
82
120
83
121
client . connect ( ) ;
@@ -90,9 +128,9 @@ describe('Connection - functional', function () {
90
128
91
129
test : function ( done ) {
92
130
var configuration = this . configuration ;
93
- var client = configuration . newClient ( { w : 1 } , { maxPoolSize : 2000 } ) ;
131
+ client = configuration . newClient ( { w : 1 } , { maxPoolSize : 2000 } ) ;
94
132
client . on ( 'open' , function ( ) {
95
- client . close ( done ) ;
133
+ done ( ) ;
96
134
} ) ;
97
135
98
136
client . connect ( ) ;
@@ -130,11 +168,11 @@ describe('Connection - functional', function () {
130
168
131
169
test : function ( done ) {
132
170
const configuration = this . configuration ;
133
- const client = configuration . newClient ( ) ;
171
+ client = configuration . newClient ( ) ;
134
172
135
173
client . connect (
136
- connectionTester ( configuration , 'testConnectNoOptions' , function ( client ) {
137
- client . close ( done ) ;
174
+ connectionTester ( configuration , 'testConnectNoOptions' , function ( ) {
175
+ done ( ) ;
138
176
} )
139
177
) ;
140
178
}
@@ -148,24 +186,24 @@ describe('Connection - functional', function () {
148
186
const username = 'testConnectGoodAuth' ;
149
187
const password = 'password' ;
150
188
151
- const setupClient = configuration . newClient ( ) ;
189
+ client = configuration . newClient ( ) ;
152
190
153
191
// First add a user.
154
- setupClient . connect ( function ( err , client ) {
192
+ client . connect ( function ( err , client ) {
155
193
expect ( err ) . to . not . exist ;
156
194
var db = client . db ( configuration . db ) ;
157
195
158
196
db . addUser ( username , password , function ( err ) {
159
197
expect ( err ) . to . not . exist ;
160
- client . close ( restOfTest ) ;
198
+ restOfTest ( ) ;
161
199
} ) ;
162
200
} ) ;
163
201
164
202
function restOfTest ( ) {
165
- const testClient = configuration . newClient ( configuration . url ( { username, password } ) ) ;
203
+ testClient = configuration . newClient ( configuration . url ( { username, password } ) ) ;
166
204
testClient . connect (
167
- connectionTester ( configuration , 'testConnectGoodAuth' , function ( client ) {
168
- client . close ( done ) ;
205
+ connectionTester ( configuration , 'testConnectGoodAuth' , function ( ) {
206
+ done ( ) ;
169
207
} )
170
208
) ;
171
209
}
@@ -181,25 +219,25 @@ describe('Connection - functional', function () {
181
219
const password = 'password' ;
182
220
183
221
// First add a user.
184
- const setupClient = configuration . newClient ( ) ;
185
- setupClient . connect ( function ( err , client ) {
222
+ client = configuration . newClient ( ) ;
223
+ client . connect ( function ( err , client ) {
186
224
expect ( err ) . to . not . exist ;
187
225
var db = client . db ( configuration . db ) ;
188
226
189
227
db . addUser ( username , password , { roles : [ 'read' ] } , function ( err ) {
190
228
expect ( err ) . to . not . exist ;
191
- client . close ( restOfTest ) ;
229
+ restOfTest ( ) ;
192
230
} ) ;
193
231
} ) ;
194
232
195
233
function restOfTest ( ) {
196
234
var opts = { auth : { username, password } , authSource : configuration . db } ;
197
235
198
- const testClient = configuration . newClient ( opts ) ;
236
+ testClient = configuration . newClient ( opts ) ;
199
237
200
238
testClient . connect (
201
- connectionTester ( configuration , 'testConnectGoodAuthAsOption' , function ( client ) {
202
- client . close ( done ) ;
239
+ connectionTester ( configuration , 'testConnectGoodAuthAsOption' , function ( ) {
240
+ done ( ) ;
203
241
} )
204
242
) ;
205
243
}
@@ -211,7 +249,7 @@ describe('Connection - functional', function () {
211
249
212
250
test : function ( done ) {
213
251
var configuration = this . configuration ;
214
- const client = configuration . newClient (
252
+ client = configuration . newClient (
215
253
configuration . url ( { username : 'slithy' , password : 'toves' } )
216
254
) ;
217
255
client . connect ( function ( err , client ) {
0 commit comments