1717package com .mongodb .connection ;
1818
1919import 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 ;
2024import 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 ;
2129import org .junit .After ;
22- import org .junit .Before ;
2330import org .junit .Test ;
2431
2532import java .util .Collections ;
2633import java .util .List ;
2734
2835import static com .mongodb .ClusterFixture .getCredentialList ;
36+ import static com .mongodb .ClusterFixture .getDefaultDatabaseName ;
2937import static com .mongodb .ClusterFixture .getPrimary ;
38+ import static com .mongodb .ClusterFixture .getSecondary ;
3039import static com .mongodb .ClusterFixture .getSslSettings ;
3140import static java .util .Collections .singletonList ;
41+ import static org .junit .Assert .assertEquals ;
3242import static org .junit .Assert .assertNotNull ;
3343import static org .junit .Assert .assertTrue ;
3444
3545public 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