@@ -156,14 +156,41 @@ class DefaultServerSpecification extends Specification {
156
156
157
157
where :
158
158
exceptionToThrow << [
159
- new MongoSecurityException (createCredential(' jeff' , ' admin' , ' 123' . toCharArray()), ' Auth failed' ),
160
159
new MongoSocketOpenException (' open failed' , new ServerAddress (), new IOException ()),
161
160
new MongoSocketWriteException (' Write failed' , new ServerAddress (), new IOException ()),
162
161
new MongoSocketReadException (' Read failed' , new ServerAddress (), new IOException ()),
163
162
new MongoSocketReadTimeoutException (' Read timed out' , new ServerAddress (), new IOException ()),
164
163
]
165
164
}
166
165
166
+ def ' failed authentication should invalidate the connection pool' () {
167
+ given :
168
+ def clusterTime = new ClusterClock ()
169
+ def connectionPool = Mock (ConnectionPool )
170
+ def connectionFactory = Mock (ConnectionFactory )
171
+ def serverMonitorFactory = Stub (ServerMonitorFactory )
172
+ def serverMonitor = Mock (ServerMonitor )
173
+ connectionPool. get() >> { throw exceptionToThrow }
174
+ serverMonitorFactory. create(_) >> { serverMonitor }
175
+
176
+ def server = new DefaultServer (serverId, SINGLE , connectionPool, connectionFactory, serverMonitorFactory,
177
+ NO_OP_SERVER_LISTENER , null , clusterTime)
178
+
179
+ when :
180
+ server. getConnection()
181
+
182
+ then :
183
+ def e = thrown(MongoSecurityException )
184
+ e. is(exceptionToThrow)
185
+ 1 * connectionPool. invalidate()
186
+ 0 * serverMonitor. connect()
187
+
188
+ where :
189
+ exceptionToThrow << [
190
+ new MongoSecurityException (createCredential(' jeff' , ' admin' , ' 123' . toCharArray()), ' Auth failed' ),
191
+ ]
192
+ }
193
+
167
194
def ' failed open should invalidate the server asynchronously' () {
168
195
given :
169
196
def clusterTime = new ClusterClock ()
@@ -192,14 +219,45 @@ class DefaultServerSpecification extends Specification {
192
219
193
220
where :
194
221
exceptionToThrow << [
195
- new MongoSecurityException (createCredential(' jeff' , ' admin' , ' 123' . toCharArray()), ' Auth failed' ),
196
222
new MongoSocketOpenException (' open failed' , new ServerAddress (), new IOException ()),
197
223
new MongoSocketWriteException (' Write failed' , new ServerAddress (), new IOException ()),
198
224
new MongoSocketReadException (' Read failed' , new ServerAddress (), new IOException ()),
199
225
new MongoSocketReadTimeoutException (' Read timed out' , new ServerAddress (), new IOException ()),
200
226
]
201
227
}
202
228
229
+ def ' failed auth should invalidate the connection pool asynchronously' () {
230
+ given :
231
+ def clusterTime = new ClusterClock ()
232
+ def connectionPool = Mock (ConnectionPool )
233
+ def connectionFactory = Mock (ConnectionFactory )
234
+ def serverMonitorFactory = Stub (ServerMonitorFactory )
235
+ def serverMonitor = Mock (ServerMonitor )
236
+ connectionPool. getAsync(_) >> { it[0 ]. onResult(null , exceptionToThrow) }
237
+ serverMonitorFactory. create(_) >> { serverMonitor }
238
+ def server = new DefaultServer (serverId, SINGLE , connectionPool, connectionFactory,
239
+ serverMonitorFactory, NO_OP_SERVER_LISTENER , null , clusterTime)
240
+
241
+ when :
242
+ def latch = new CountDownLatch (1 )
243
+ def receivedConnection = null
244
+ def receivedThrowable = null
245
+ server. getConnectionAsync { result , throwable -> receivedConnection = result; receivedThrowable = throwable; latch. countDown() }
246
+ latch. await()
247
+
248
+ then :
249
+ ! receivedConnection
250
+ receivedThrowable. is(exceptionToThrow)
251
+ 1 * connectionPool. invalidate()
252
+ 0 * serverMonitor. connect()
253
+
254
+
255
+ where :
256
+ exceptionToThrow << [
257
+ new MongoSecurityException (createCredential(' jeff' , ' admin' , ' 123' . toCharArray()), ' Auth failed' ),
258
+ ]
259
+ }
260
+
203
261
def ' should invalidate on MongoNotPrimaryException' () {
204
262
given :
205
263
def clusterTime = new ClusterClock ()
0 commit comments