@@ -156,14 +156,41 @@ class DefaultServerSpecification extends Specification {
156156
157157 where :
158158 exceptionToThrow << [
159- new MongoSecurityException (createCredential(' jeff' , ' admin' , ' 123' . toCharArray()), ' Auth failed' ),
160159 new MongoSocketOpenException (' open failed' , new ServerAddress (), new IOException ()),
161160 new MongoSocketWriteException (' Write failed' , new ServerAddress (), new IOException ()),
162161 new MongoSocketReadException (' Read failed' , new ServerAddress (), new IOException ()),
163162 new MongoSocketReadTimeoutException (' Read timed out' , new ServerAddress (), new IOException ()),
164163 ]
165164 }
166165
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+
167194 def ' failed open should invalidate the server asynchronously' () {
168195 given :
169196 def clusterTime = new ClusterClock ()
@@ -192,14 +219,45 @@ class DefaultServerSpecification extends Specification {
192219
193220 where :
194221 exceptionToThrow << [
195- new MongoSecurityException (createCredential(' jeff' , ' admin' , ' 123' . toCharArray()), ' Auth failed' ),
196222 new MongoSocketOpenException (' open failed' , new ServerAddress (), new IOException ()),
197223 new MongoSocketWriteException (' Write failed' , new ServerAddress (), new IOException ()),
198224 new MongoSocketReadException (' Read failed' , new ServerAddress (), new IOException ()),
199225 new MongoSocketReadTimeoutException (' Read timed out' , new ServerAddress (), new IOException ()),
200226 ]
201227 }
202228
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+
203261 def ' should invalidate on MongoNotPrimaryException' () {
204262 given :
205263 def clusterTime = new ClusterClock ()
0 commit comments