21
21
22
22
import java .time .Duration ;
23
23
import java .util .List ;
24
+ import java .util .concurrent .TimeUnit ;
24
25
25
26
import org .bson .Document ;
26
27
import org .springframework .core .env .Environment ;
30
31
import org .springframework .util .ObjectUtils ;
31
32
32
33
import com .mongodb .ConnectionString ;
34
+ import com .mongodb .MongoClientSettings ;
33
35
import com .mongodb .ReadPreference ;
34
36
import com .mongodb .WriteConcern ;
35
37
import com .mongodb .client .MongoClient ;
@@ -68,6 +70,10 @@ public static MongoClient client(String host, int port) {
68
70
}
69
71
70
72
public static MongoClient client (ConnectionString connectionString ) {
73
+ MongoClientSettings settings = MongoClientSettings .builder ().applyConnectionString (connectionString )
74
+ .applyToSocketSettings (builder -> {
75
+ builder .connectTimeout (120 , TimeUnit .SECONDS );
76
+ }).build ();
71
77
return com .mongodb .client .MongoClients .create (connectionString , SpringDataMongoDB .driverInformation ());
72
78
}
73
79
@@ -176,11 +182,10 @@ public static void dropCollectionNow(String dbName, String collectionName,
176
182
* @param collectionName must not be {@literal null}.
177
183
* @param client must not be {@literal null}.
178
184
*/
179
- public static void dropCollectionNow (String dbName , String collectionName ,
180
- com .mongodb .client .MongoClient client ) {
185
+ public static void dropCollectionNow (String dbName , String collectionName , com .mongodb .client .MongoClient client ) {
181
186
182
- com .mongodb .client .MongoDatabase database = client .getDatabase (dbName )
183
- .withWriteConcern ( WriteConcern . MAJORITY ). withReadPreference (ReadPreference .primary ());
187
+ com .mongodb .client .MongoDatabase database = client .getDatabase (dbName ). withWriteConcern ( WriteConcern . MAJORITY )
188
+ .withReadPreference (ReadPreference .primary ());
184
189
185
190
database .getCollection (collectionName ).drop ();
186
191
}
@@ -205,11 +210,10 @@ public static void flushCollection(String dbName, String collectionName,
205
210
.verifyComplete ();
206
211
}
207
212
208
- public static void flushCollection (String dbName , String collectionName ,
209
- com .mongodb .client .MongoClient client ) {
213
+ public static void flushCollection (String dbName , String collectionName , com .mongodb .client .MongoClient client ) {
210
214
211
- com .mongodb .client .MongoDatabase database = client .getDatabase (dbName )
212
- .withWriteConcern ( WriteConcern . MAJORITY ). withReadPreference (ReadPreference .primary ());
215
+ com .mongodb .client .MongoDatabase database = client .getDatabase (dbName ). withWriteConcern ( WriteConcern . MAJORITY )
216
+ .withReadPreference (ReadPreference .primary ());
213
217
214
218
database .getCollection (collectionName ).deleteMany (new Document ());
215
219
}
@@ -267,14 +271,19 @@ public static boolean serverIsReplSet() {
267
271
@ SuppressWarnings ("unchecked" )
268
272
public static boolean isVectorSearchEnabled () {
269
273
try (MongoClient client = MongoTestUtils .client ()) {
274
+ return isVectorSearchEnabled (client );
275
+ }
276
+ }
270
277
278
+ public static boolean isVectorSearchEnabled (MongoClient client ) {
279
+ try {
271
280
return client .getDatabase ("admin" ).runCommand (new Document ("getCmdLineOpts" , "1" )).get ("argv" , List .class )
272
- .stream ().anyMatch (it -> {
273
- if (it instanceof String cfgString ) {
274
- return cfgString .startsWith ("searchIndexManagementHostAndPort" );
275
- }
276
- return false ;
277
- });
281
+ .stream ().anyMatch (it -> {
282
+ if (it instanceof String cfgString ) {
283
+ return cfgString .startsWith ("searchIndexManagementHostAndPort" );
284
+ }
285
+ return false ;
286
+ });
278
287
} catch (Exception e ) {
279
288
return false ;
280
289
}
@@ -297,10 +306,11 @@ private static void giveTheServerALittleTimeToThink() {
297
306
298
307
public static CollectionInfo readCollectionInfo (MongoDatabase db , String collectionName ) {
299
308
300
- List <Document > list = db .runCommand (new Document ().append ("listCollections" , 1 ).append ("filter" , new Document ("name" , collectionName )))
309
+ List <Document > list = db
310
+ .runCommand (new Document ().append ("listCollections" , 1 ).append ("filter" , new Document ("name" , collectionName )))
301
311
.get ("cursor" , Document .class ).get ("firstBatch" , List .class );
302
312
303
- if (list .isEmpty ()) {
313
+ if (list .isEmpty ()) {
304
314
throw new IllegalStateException (String .format ("Collection %s not found." , collectionName ));
305
315
}
306
316
return CollectionInfo .from (list .get (0 ));
0 commit comments