Skip to content

Commit c8a81c4

Browse files
committed
JAVA-2666: Fix a racy test
1 parent ef8b75d commit c8a81c4

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

driver/src/test/functional/com/mongodb/MongoClientsSpecification.groovy

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import static com.mongodb.ClusterFixture.serverVersionAtLeast
3333
import static com.mongodb.Fixture.getDefaultDatabaseName
3434
import static com.mongodb.Fixture.getMongoClientURI
3535

36-
class MongoClientsSpecification extends FunctionalSpecification {
36+
class MongoClientsSpecification extends FunctionalSpecification {
3737
@IgnoreIf({ !serverVersionAtLeast(3, 4) || !isStandalone() })
3838
def 'application name should appear in the system.profile collection'() {
3939
given:
@@ -64,17 +64,22 @@ class MongoClientsSpecification extends FunctionalSpecification {
6464
@IgnoreIf({ !isDiscoverableReplicaSet() })
6565
def 'should use server selector from MongoClientOptions'() {
6666
given:
67-
def expectedWinner
67+
def expectedWinningAddresses = [] as Set
6868
def actualWinningAddresses = [] as Set
6969
def optionsBuilder = MongoClientOptions.builder()
7070
// select the suitable server with the highest port number
7171
.serverSelector { ClusterDescription clusterDescription ->
72+
def highestPortServer
7273
for (ServerDescription cur : clusterDescription.getServerDescriptions()) {
73-
if (expectedWinner == null || cur.address.port > expectedWinner.address.port) {
74-
expectedWinner = cur
74+
if (highestPortServer == null || cur.address.port > highestPortServer.address.port) {
75+
highestPortServer = cur
7576
}
7677
}
77-
expectedWinner == null ? [] : [expectedWinner]
78+
if (highestPortServer == null) {
79+
return []
80+
}
81+
expectedWinningAddresses.add(highestPortServer.address)
82+
[highestPortServer]
7883
}.addCommandListener(new CommandListener() {
7984
// record each address actually used
8085
@Override
@@ -101,7 +106,6 @@ class MongoClientsSpecification extends FunctionalSpecification {
101106
}
102107

103108
then:
104-
actualWinningAddresses.size() == 1
105-
actualWinningAddresses.contains(expectedWinner.address)
109+
actualWinningAddresses == expectedWinningAddresses
106110
}
107111
}

0 commit comments

Comments
 (0)