Skip to content

Commit 1b5b59f

Browse files
committed
JAVA-2737: Add functional test
1 parent c103e68 commit 1b5b59f

File tree

3 files changed

+73
-77
lines changed

3 files changed

+73
-77
lines changed

driver-core/src/test/functional/com/mongodb/ClusterFixture.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,15 +318,33 @@ public static SslSettings getSslSettings(final ConnectionString connectionString
318318
}
319319

320320
@SuppressWarnings("deprecation")
321-
public static ServerAddress getPrimary() throws InterruptedException {
321+
public static ServerAddress getPrimary() {
322322
List<ServerDescription> serverDescriptions = getCluster().getDescription().getPrimaries();
323323
while (serverDescriptions.isEmpty()) {
324-
sleep(100);
324+
try {
325+
sleep(100);
326+
} catch (InterruptedException e) {
327+
throw new RuntimeException(e);
328+
}
325329
serverDescriptions = getCluster().getDescription().getPrimaries();
326330
}
327331
return serverDescriptions.get(0).getAddress();
328332
}
329333

334+
@SuppressWarnings("deprecation")
335+
public static ServerAddress getSecondary() {
336+
List<ServerDescription> serverDescriptions = getCluster().getDescription().getSecondaries();
337+
while (serverDescriptions.isEmpty()) {
338+
try {
339+
sleep(100);
340+
} catch (InterruptedException e) {
341+
throw new RuntimeException(e);
342+
}
343+
serverDescriptions = getCluster().getDescription().getSecondaries();
344+
}
345+
return serverDescriptions.get(0).getAddress();
346+
}
347+
330348
@SuppressWarnings("deprecation")
331349
public static List<MongoCredential> getCredentialList() {
332350
return getConnectionString().getCredentialList();

driver-core/src/test/functional/com/mongodb/FunctionalTest.java

Lines changed: 0 additions & 61 deletions
This file was deleted.

driver-core/src/test/functional/com/mongodb/connection/SingleServerClusterTest.java

Lines changed: 53 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,41 +17,51 @@
1717
package com.mongodb.connection;
1818

1919
import com.mongodb.MongoCompressor;
20+
import com.mongodb.ReadPreference;
21+
import com.mongodb.ServerAddress;
22+
import com.mongodb.internal.connection.NoOpSessionContext;
23+
import com.mongodb.internal.validator.NoOpFieldNameValidator;
2024
import com.mongodb.selector.ServerSelector;
25+
import org.bson.BsonDocument;
26+
import org.bson.BsonDouble;
27+
import org.bson.BsonString;
28+
import org.bson.codecs.BsonDocumentCodec;
2129
import org.junit.After;
22-
import org.junit.Before;
2330
import org.junit.Test;
2431

2532
import java.util.Collections;
2633
import java.util.List;
2734

2835
import static com.mongodb.ClusterFixture.getCredentialList;
36+
import static com.mongodb.ClusterFixture.getDefaultDatabaseName;
2937
import static com.mongodb.ClusterFixture.getPrimary;
38+
import static com.mongodb.ClusterFixture.getSecondary;
3039
import static com.mongodb.ClusterFixture.getSslSettings;
3140
import static java.util.Collections.singletonList;
41+
import static org.junit.Assert.assertEquals;
3242
import static org.junit.Assert.assertNotNull;
3343
import static org.junit.Assert.assertTrue;
3444

3545
public class SingleServerClusterTest {
3646
private SingleServerCluster cluster;
3747

38-
@Before
39-
public void setUp() throws Exception {
48+
49+
private void setUpCluster(final ServerAddress serverAddress) {
4050
SocketStreamFactory streamFactory = new SocketStreamFactory(SocketSettings.builder().build(),
41-
getSslSettings());
51+
getSslSettings());
4252
ClusterId clusterId = new ClusterId();
4353
ClusterSettings clusterSettings = ClusterSettings.builder()
44-
.mode(ClusterConnectionMode.SINGLE)
45-
.hosts(singletonList(getPrimary()))
46-
.build();
54+
.mode(ClusterConnectionMode.SINGLE)
55+
.hosts(singletonList(serverAddress))
56+
.build();
4757
cluster = new SingleServerCluster(clusterId,
48-
clusterSettings,
49-
new DefaultClusterableServerFactory(clusterId, clusterSettings, ServerSettings.builder().build(),
50-
ConnectionPoolSettings.builder().maxSize(1).build(),
51-
streamFactory, streamFactory, getCredentialList(),
58+
clusterSettings,
59+
new DefaultClusterableServerFactory(clusterId, clusterSettings, ServerSettings.builder().build(),
60+
ConnectionPoolSettings.builder().maxSize(1).build(),
61+
streamFactory, streamFactory, getCredentialList(),
5262

53-
null, null, null,
54-
Collections.<MongoCompressor>emptyList()));
63+
null, null, null,
64+
Collections.<MongoCompressor>emptyList()));
5565
}
5666

5767
@After
@@ -61,25 +71,54 @@ public void tearDown() {
6171

6272
@Test
6373
public void shouldGetDescription() {
74+
// given
75+
setUpCluster(getPrimary());
76+
77+
// expect
6478
assertNotNull(cluster.getDescription());
6579
}
6680

6781
@Test
6882
public void descriptionShouldIncludeSettings() {
83+
// given
84+
setUpCluster(getPrimary());
85+
86+
// expect
6987
assertNotNull(cluster.getDescription().getClusterSettings());
7088
assertNotNull(cluster.getDescription().getServerSettings());
7189
}
7290

7391
@Test
7492
@SuppressWarnings("deprecation")
75-
public void shouldGetServerWithOkDescription() throws InterruptedException {
93+
public void shouldGetServerWithOkDescription() {
94+
// given
95+
setUpCluster(getPrimary());
96+
97+
// when
7698
Server server = cluster.selectServer(new ServerSelector() {
7799
@Override
78100
public List<ServerDescription> select(final ClusterDescription clusterDescription) {
79101
return clusterDescription.getPrimaries();
80102
}
81103
});
104+
105+
// then
82106
assertTrue(server.getDescription().isOk());
83107
}
84108

109+
@Test
110+
public void shouldSuccessfullyQueryASecondaryWithPrimaryReadPreference() {
111+
// given
112+
ServerAddress secondary = getSecondary();
113+
setUpCluster(secondary);
114+
String collectionName = getClass().getName();
115+
Connection connection = cluster.getServer(secondary).getConnection();
116+
117+
// when
118+
BsonDocument result = connection.command(getDefaultDatabaseName(), new BsonDocument("find", new BsonString(collectionName)),
119+
new NoOpFieldNameValidator(), ReadPreference.primary(), new BsonDocumentCodec(), NoOpSessionContext.INSTANCE);
120+
121+
// then
122+
assertEquals(new BsonDouble(1.0).intValue(), result.getNumber("ok").intValue());
123+
}
85124
}

0 commit comments

Comments
 (0)