@@ -136,6 +136,17 @@ class ClusterSettingsSpecification extends Specification {
136
136
thrown(IllegalArgumentException )
137
137
}
138
138
139
+ def ' when srvHost is specified and mode is SINGLE, should throw' () {
140
+ when :
141
+ ClusterSettings . builder()
142
+ .srvHost(' foo.bar.com' )
143
+ .mode(ClusterConnectionMode . SINGLE )
144
+ .build()
145
+
146
+ then :
147
+ thrown(IllegalArgumentException )
148
+ }
149
+
139
150
def ' when srvHost is specified, should set mode to MULTIPLE if mode is not configured' () {
140
151
when :
141
152
def builder = ClusterSettings . builder()
@@ -181,8 +192,10 @@ class ClusterSettingsSpecification extends Specification {
181
192
182
193
def ' when connection string is applied to builder, all properties should be set' () {
183
194
when :
184
- def settings = ClusterSettings . builder(). applyConnectionString(new ConnectionString (' mongodb://example.com:27018' ))
185
- .build()
195
+ def settings = ClusterSettings . builder()
196
+ .requiredReplicaSetName(" test" )
197
+ .applyConnectionString(new ConnectionString (' mongodb://example.com:27018' ))
198
+ .build()
186
199
187
200
then :
188
201
settings. mode == ClusterConnectionMode . SINGLE
@@ -192,6 +205,20 @@ class ClusterSettingsSpecification extends Specification {
192
205
settings. srvMaxHosts == null
193
206
settings. srvServiceName == ' mongodb'
194
207
208
+ when :
209
+ settings = ClusterSettings . builder()
210
+ .applyConnectionString(new ConnectionString (' mongodb://example.com:27018' ))
211
+ .requiredReplicaSetName(" test" )
212
+ .build()
213
+
214
+ then :
215
+ settings. mode == ClusterConnectionMode . MULTIPLE
216
+ settings. hosts == [new ServerAddress (' example.com:27018' )]
217
+ settings. requiredClusterType == ClusterType . REPLICA_SET
218
+ settings. requiredReplicaSetName == ' test'
219
+ settings. srvMaxHosts == null
220
+ settings. srvServiceName == ' mongodb'
221
+
195
222
when :
196
223
settings = ClusterSettings . builder(). applyConnectionString(new ConnectionString (' mongodb+srv://test5.test.build.10gen.cc/' )). build()
197
224
@@ -216,8 +243,10 @@ class ClusterSettingsSpecification extends Specification {
216
243
settings. srvServiceName == ' customname'
217
244
218
245
when :
219
- settings = ClusterSettings . builder(). applyConnectionString(new ConnectionString (' mongodb://example.com:27018/?replicaSet=test' ))
220
- .build()
246
+ settings = ClusterSettings . builder()
247
+ .mode(ClusterConnectionMode . SINGLE )
248
+ .applyConnectionString(new ConnectionString (' mongodb://example.com:27018/?replicaSet=test' ))
249
+ .build()
221
250
222
251
then :
223
252
settings. mode == ClusterConnectionMode . MULTIPLE
@@ -240,6 +269,19 @@ class ClusterSettingsSpecification extends Specification {
240
269
settings. srvMaxHosts == null
241
270
settings. srvServiceName == ' mongodb'
242
271
272
+ when :
273
+ settings = ClusterSettings . builder()
274
+ .applyConnectionString(new ConnectionString (' mongodb://example.com:27017,example.com:27018/?directConnection=false' ))
275
+ .build()
276
+
277
+ then :
278
+ settings. mode == ClusterConnectionMode . MULTIPLE
279
+ settings. hosts == [new ServerAddress (' example.com:27017' ), new ServerAddress (' example.com:27018' )]
280
+ settings. requiredClusterType == ClusterType . UNKNOWN
281
+ settings. requiredReplicaSetName == null
282
+ settings. srvMaxHosts == null
283
+ settings. srvServiceName == ' mongodb'
284
+
243
285
when :
244
286
settings = ClusterSettings . builder()
245
287
.applyConnectionString(new ConnectionString (' mongodb://example.com:27018/?directConnection=true' ))
@@ -288,15 +330,16 @@ class ClusterSettingsSpecification extends Specification {
288
330
settings. hosts == [new ServerAddress (' example.com:27018' )]
289
331
}
290
332
291
- def ' when cluster type is unknown and replica set name is specified , should set cluster type to ReplicaSet ' () {
333
+ def ' when cluster type is UNKNOWN and replica set name is set , should set cluster type to REPLICA_SET and mode to MULTIPLE ' () {
292
334
when :
293
335
def settings = ClusterSettings . builder(). hosts([new ServerAddress ()]). requiredReplicaSetName(' yeah' ). build()
294
336
295
337
then :
296
338
ClusterType . REPLICA_SET == settings. requiredClusterType
339
+ ClusterConnectionMode . MULTIPLE == settings. mode
297
340
}
298
341
299
- def ' connection mode should default to single if one host or multiple if more' () {
342
+ def ' connection mode should default to SINGLE if replica set name is not set and one host, or MULTIPLE if more' () {
300
343
when :
301
344
def settings = ClusterSettings . builder(). hosts([new ServerAddress ()]). build()
302
345
@@ -310,11 +353,28 @@ class ClusterSettingsSpecification extends Specification {
310
353
settings. mode == ClusterConnectionMode . MULTIPLE
311
354
}
312
355
356
+ def ' when a valid mode is specified, should use it' () {
357
+ when :
358
+ def mode = ClusterConnectionMode . LOAD_BALANCED
359
+ def settings = ClusterSettings . builder(). mode(mode). build()
360
+
361
+ then :
362
+ settings. mode == mode
363
+ }
364
+
313
365
def ' when mode is Single and hosts size is greater than one, should throw' () {
314
366
when :
315
367
ClusterSettings . builder(). hosts([new ServerAddress (), new ServerAddress (' other' )]). mode(ClusterConnectionMode . SINGLE ). build()
316
368
then :
317
369
thrown(IllegalArgumentException )
370
+
371
+ when :
372
+ ClusterSettings . builder()
373
+ .applyConnectionString(new ConnectionString (" mongodb://host1,host2/" ))
374
+ .mode(ClusterConnectionMode . SINGLE )
375
+ .build()
376
+ then :
377
+ thrown(IllegalArgumentException )
318
378
}
319
379
320
380
def ' when cluster type is Standalone and multiple hosts are specified, should throw' () {
0 commit comments