Skip to content

Commit 565d502

Browse files
committed
Make DefaultServer.invalidate a no-op when server is closed
This method can easily be called in situations where the server instance is already called, and having it throw an IllegalStateException in that situation is not the desired behavior. JAVA-3130
1 parent 468bcb9 commit 565d502

File tree

3 files changed

+43
-8
lines changed

3 files changed

+43
-8
lines changed

driver-core/src/main/com/mongodb/internal/connection/DefaultServer.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -122,14 +122,14 @@ public ServerDescription getDescription() {
122122

123123
@Override
124124
public void invalidate() {
125-
isTrue("open", !isClosed());
126-
127-
serverStateListener.stateChanged(new ChangeEvent<ServerDescription>(description, ServerDescription.builder()
128-
.state(CONNECTING)
129-
.address(serverId.getAddress())
130-
.build()));
131-
connectionPool.invalidate();
132-
connect();
125+
if (!isClosed()) {
126+
serverStateListener.stateChanged(new ChangeEvent<ServerDescription>(description, ServerDescription.builder()
127+
.state(CONNECTING)
128+
.address(serverId.getAddress())
129+
.build()));
130+
connectionPool.invalidate();
131+
connect();
132+
}
133133
}
134134

135135
@Override

driver-core/src/test/unit/com/mongodb/internal/connection/DefaultConnectionPoolSpecification.groovy

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,19 @@ class DefaultConnectionPoolSpecification extends Specification {
466466
thrown(MongoWaitQueueFullException)
467467
}
468468

469+
def 'invalidate should do nothing when pool is closed'() {
470+
given:
471+
pool = new DefaultConnectionPool(SERVER_ID, connectionFactory,
472+
builder().maxSize(1).maxWaitQueueSize(1).build())
473+
pool.close()
474+
475+
when:
476+
pool.invalidate()
477+
478+
then:
479+
noExceptionThrown()
480+
}
481+
469482
def selectConnectionAsyncAndGet(DefaultConnectionPool pool) {
470483
selectConnectionAsync(pool).get()
471484
}

driver-core/src/test/unit/com/mongodb/internal/connection/DefaultServerSpecification.groovy

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,28 @@ class DefaultServerSpecification extends Specification {
136136
server?.close()
137137
}
138138

139+
def 'invalidate should do nothing when server is closed'() {
140+
given:
141+
def clusterTime = new ClusterClock()
142+
def connectionPool = Mock(ConnectionPool)
143+
def connectionFactory = Mock(ConnectionFactory)
144+
def serverMonitorFactory = Stub(ServerMonitorFactory)
145+
def serverMonitor = Mock(ServerMonitor)
146+
connectionPool.get() >> { throw exceptionToThrow }
147+
serverMonitorFactory.create(_) >> { serverMonitor }
148+
149+
def server = new DefaultServer(serverId, SINGLE, connectionPool, connectionFactory, serverMonitorFactory,
150+
NO_OP_SERVER_LISTENER, null, clusterTime)
151+
server.close()
152+
153+
when:
154+
server.invalidate()
155+
156+
then:
157+
0 * connectionPool.invalidate()
158+
0 * serverMonitor.connect()
159+
}
160+
139161
def 'failed open should invalidate the server'() {
140162
given:
141163
def clusterTime = new ClusterClock()

0 commit comments

Comments
 (0)