Skip to content

Commit bdbfec2

Browse files
committed
CSHARP-1357: Find operations should send read preference correctly when connected to a sharded cluster.
1 parent afabfd2 commit bdbfec2

10 files changed

+90
-84
lines changed

src/MongoDB.Driver.Core.Tests/Core/Operations/FindCommandOperationTests.cs

Lines changed: 25 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2015 MongoDB Inc.
1+
/* Copyright 2015-2016 MongoDB Inc.
22
*
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
@@ -164,7 +164,7 @@ public void CreateCommand_should_return_expected_result()
164164
var reflector = new Reflector(subject);
165165
var serverDescription = CreateServerDescription();
166166

167-
var result = reflector.CreateCommand(serverDescription, null);
167+
var result = reflector.CreateCommand(serverDescription);
168168

169169
result.Should().Be($"{{ find : '{_collectionNamespace.CollectionName}' }}");
170170
}
@@ -179,7 +179,7 @@ public void CreateCommand_should_return_expected_result_when_allowPartialResults
179179
var reflector = new Reflector(subject);
180180
var serverDescription = CreateServerDescription(type: ServerType.ShardRouter);
181181

182-
var result = reflector.CreateCommand(serverDescription, null);
182+
var result = reflector.CreateCommand(serverDescription);
183183

184184
result.Should().Be($"{{ find : '{_collectionNamespace.CollectionName}', allowPartialResults : {(value ? "true" : "false")} }}");
185185
}
@@ -194,7 +194,7 @@ public void CreateCommand_should_return_expected_result_when_comment_is_provided
194194
var reflector = new Reflector(subject);
195195
var serverDescription = CreateServerDescription();
196196

197-
var result = reflector.CreateCommand(serverDescription, null);
197+
var result = reflector.CreateCommand(serverDescription);
198198

199199
result.Should().Be($"{{ find : '{_collectionNamespace.CollectionName}', comment : '{value}' }}");
200200
}
@@ -208,7 +208,7 @@ public void CreateCommand_should_return_expected_result_when_cursor_is_tailableA
208208
var reflector = new Reflector(subject);
209209
var serverDescription = CreateServerDescription();
210210

211-
var result = reflector.CreateCommand(serverDescription, null);
211+
var result = reflector.CreateCommand(serverDescription);
212212

213213
result.Should().Be($"{{ find : '{_collectionNamespace.CollectionName}', tailable : true{awaitJson} }}");
214214
}
@@ -223,7 +223,7 @@ public void CreateCommand_should_return_expected_result_when_filter_is_provided(
223223
var reflector = new Reflector(subject);
224224
var serverDescription = CreateServerDescription();
225225

226-
var result = reflector.CreateCommand(serverDescription, null);
226+
var result = reflector.CreateCommand(serverDescription);
227227

228228
result.Should().Be($"{{ find : '{_collectionNamespace.CollectionName}', filter : {json} }}");
229229
}
@@ -238,7 +238,7 @@ public void CreateCommand_should_return_expected_result_when_firstBatchSize_is_p
238238
var reflector = new Reflector(subject);
239239
var serverDescription = CreateServerDescription();
240240

241-
var result = reflector.CreateCommand(serverDescription, null);
241+
var result = reflector.CreateCommand(serverDescription);
242242

243243
result.Should().Be($"{{ find : '{_collectionNamespace.CollectionName}', batchSize : {value} }}");
244244
}
@@ -253,7 +253,7 @@ public void CreateCommand_should_return_expected_result_when_hint_is_provided(
253253
var reflector = new Reflector(subject);
254254
var serverDescription = CreateServerDescription();
255255

256-
var result = reflector.CreateCommand(serverDescription, null);
256+
var result = reflector.CreateCommand(serverDescription);
257257

258258
result.Should().Be($"{{ find : '{_collectionNamespace.CollectionName}', hint : {subject.Hint.ToJson()} }}");
259259
}
@@ -269,7 +269,7 @@ public void CreateCommand_should_return_expected_result_when_limit_is_provided(i
269269
var reflector = new Reflector(subject);
270270
var serverDescription = CreateServerDescription();
271271

272-
var result = reflector.CreateCommand(serverDescription, null);
272+
var result = reflector.CreateCommand(serverDescription);
273273

274274
result.Should().Be($"{{ find : '{_collectionNamespace.CollectionName}'{json} }}");
275275
}
@@ -284,7 +284,7 @@ public void CreateCommand_should_return_expected_result_when_max_is_provided(
284284
var reflector = new Reflector(subject);
285285
var serverDescription = CreateServerDescription();
286286

287-
var result = reflector.CreateCommand(serverDescription, null);
287+
var result = reflector.CreateCommand(serverDescription);
288288

289289
result.Should().Be($"{{ find : '{_collectionNamespace.CollectionName}', max : {json} }}");
290290
}
@@ -299,7 +299,7 @@ public void CreateCommand_should_return_expected_result_when_maxScan_is_provided
299299
var reflector = new Reflector(subject);
300300
var serverDescription = CreateServerDescription();
301301

302-
var result = reflector.CreateCommand(serverDescription, null);
302+
var result = reflector.CreateCommand(serverDescription);
303303

304304
result.Should().Be($"{{ find : '{_collectionNamespace.CollectionName}', maxScan : {value} }}");
305305
}
@@ -314,7 +314,7 @@ public void CreateCommand_should_return_expected_result_when_maxTime_is_provided
314314
var reflector = new Reflector(subject);
315315
var serverDescription = CreateServerDescription();
316316

317-
var result = reflector.CreateCommand(serverDescription, null);
317+
var result = reflector.CreateCommand(serverDescription);
318318

319319
result.Should().Be($"{{ find : '{_collectionNamespace.CollectionName}', maxTimeMS : {value * 1000} }}");
320320
}
@@ -329,7 +329,7 @@ public void CreateCommand_should_return_expected_result_when_min_is_provided(
329329
var reflector = new Reflector(subject);
330330
var serverDescription = CreateServerDescription();
331331

332-
var result = reflector.CreateCommand(serverDescription, null);
332+
var result = reflector.CreateCommand(serverDescription);
333333

334334
result.Should().Be($"{{ find : '{_collectionNamespace.CollectionName}', min : {json} }}");
335335
}
@@ -344,7 +344,7 @@ public void CreateCommand_should_return_expected_result_when_noCursorTimeout_is_
344344
var reflector = new Reflector(subject);
345345
var serverDescription = CreateServerDescription();
346346

347-
var result = reflector.CreateCommand(serverDescription, null);
347+
var result = reflector.CreateCommand(serverDescription);
348348

349349
result.Should().Be($"{{ find : '{_collectionNamespace.CollectionName}', noCursorTimeout : {(value ? "true" : "false")} }}");
350350
}
@@ -359,7 +359,7 @@ public void CreateCommand_should_return_expected_result_when_oplogReplay_is_prov
359359
var reflector = new Reflector(subject);
360360
var serverDescription = CreateServerDescription();
361361

362-
var result = reflector.CreateCommand(serverDescription, null);
362+
var result = reflector.CreateCommand(serverDescription);
363363

364364
result.Should().Be($"{{ find : '{_collectionNamespace.CollectionName}', oplogReplay : {(value ? "true" : "false")} }}");
365365
}
@@ -374,7 +374,7 @@ public void CreateCommand_should_return_expected_result_when_projection_is_provi
374374
var reflector = new Reflector(subject);
375375
var serverDescription = CreateServerDescription();
376376

377-
var result = reflector.CreateCommand(serverDescription, null);
377+
var result = reflector.CreateCommand(serverDescription);
378378

379379
result.Should().Be($"{{ find : '{_collectionNamespace.CollectionName}', projection : {json} }}");
380380
}
@@ -390,28 +390,11 @@ public void CreateCommand_should_return_expected_result_when_readConcern_is_prov
390390
var reflector = new Reflector(subject);
391391
var serverDescription = CreateServerDescription();
392392

393-
var result = reflector.CreateCommand(serverDescription, null);
393+
var result = reflector.CreateCommand(serverDescription);
394394

395395
result.Should().Be($"{{ find : '{_collectionNamespace.CollectionName}', readConcern : {readConcernJson} }}");
396396
}
397397

398-
[Test]
399-
public void CreateCommand_should_return_expected_result_when_readPreference_is_provided(
400-
[Values(ReadPreferenceMode.PrimaryPreferred, ReadPreferenceMode.Secondary)]
401-
ReadPreferenceMode value)
402-
{
403-
var subject = new FindCommandOperation<BsonDocument>(_collectionNamespace, BsonDocumentSerializer.Instance, _messageEncoderSettings);
404-
var readPreference = new ReadPreference(value);
405-
var reflector = new Reflector(subject);
406-
var serverDescription = CreateServerDescription(type: ServerType.ShardRouter);
407-
408-
var result = reflector.CreateCommand(serverDescription, readPreference);
409-
410-
var mode = value.ToString();
411-
var camelCaseMode = char.ToLower(mode[0]) + mode.Substring(1);
412-
result.Should().Be($"{{ find : '{_collectionNamespace.CollectionName}', readPreference : {{ mode : '{camelCaseMode}' }} }}");
413-
}
414-
415398
[Test]
416399
public void CreateCommand_should_return_expected_result_when_returnKey_is_provided(
417400
[Values(false, true)]
@@ -422,7 +405,7 @@ public void CreateCommand_should_return_expected_result_when_returnKey_is_provid
422405
var reflector = new Reflector(subject);
423406
var serverDescription = CreateServerDescription();
424407

425-
var result = reflector.CreateCommand(serverDescription, null);
408+
var result = reflector.CreateCommand(serverDescription);
426409

427410
result.Should().Be($"{{ find : '{_collectionNamespace.CollectionName}', returnKey : {(value ? "true" : "false")} }}");
428411
}
@@ -437,7 +420,7 @@ public void CreateCommand_should_return_expected_result_when_showRecordId_is_pro
437420
var reflector = new Reflector(subject);
438421
var serverDescription = CreateServerDescription();
439422

440-
var result = reflector.CreateCommand(serverDescription, null);
423+
var result = reflector.CreateCommand(serverDescription);
441424

442425
result.Should().Be($"{{ find : '{_collectionNamespace.CollectionName}', showRecordId : {(value ? "true" : "false")} }}");
443426
}
@@ -452,7 +435,7 @@ public void CreateCommand_should_return_expected_result_when_singleBatch_is_prov
452435
var reflector = new Reflector(subject);
453436
var serverDescription = CreateServerDescription();
454437

455-
var result = reflector.CreateCommand(serverDescription, null);
438+
var result = reflector.CreateCommand(serverDescription);
456439

457440
result.Should().Be($"{{ find : '{_collectionNamespace.CollectionName}', singleBatch : {(value ? "true" : "false")} }}");
458441
}
@@ -467,7 +450,7 @@ public void CreateCommand_should_return_expected_result_when_skip_is_provided(
467450
var reflector = new Reflector(subject);
468451
var serverDescription = CreateServerDescription();
469452

470-
var result = reflector.CreateCommand(serverDescription, null);
453+
var result = reflector.CreateCommand(serverDescription);
471454

472455
result.Should().Be($"{{ find : '{_collectionNamespace.CollectionName}', skip : {value} }}");
473456
}
@@ -482,7 +465,7 @@ public void CreateCommand_should_return_expected_result_when_snapshot_is_provide
482465
var reflector = new Reflector(subject);
483466
var serverDescription = CreateServerDescription();
484467

485-
var result = reflector.CreateCommand(serverDescription, null);
468+
var result = reflector.CreateCommand(serverDescription);
486469

487470
result.Should().Be($"{{ find : '{_collectionNamespace.CollectionName}', snapshot : {(value ? "true" : "false")} }}");
488471
}
@@ -497,7 +480,7 @@ public void CreateCommand_should_return_expected_result_when_sort_is_provided(
497480
var reflector = new Reflector(subject);
498481
var serverDescription = CreateServerDescription();
499482

500-
var result = reflector.CreateCommand(serverDescription, null);
483+
var result = reflector.CreateCommand(serverDescription);
501484

502485
result.Should().Be($"{{ find : '{_collectionNamespace.CollectionName}', sort : {json} }}");
503486
}
@@ -910,10 +893,10 @@ public Reflector(FindCommandOperation<BsonDocument> instance)
910893
}
911894

912895
// public methods
913-
public BsonDocument CreateCommand(ServerDescription serverDescription, ReadPreference readPreference)
896+
public BsonDocument CreateCommand(ServerDescription serverDescription)
914897
{
915898
var methodInfo = _instance.GetType().GetMethod("CreateCommand", BindingFlags.NonPublic | BindingFlags.Instance);
916-
return (BsonDocument)methodInfo.Invoke(_instance, new object[] { serverDescription, readPreference });
899+
return (BsonDocument)methodInfo.Invoke(_instance, new object[] { serverDescription });
917900
}
918901
}
919902
}

src/MongoDB.Driver.Core.Tests/Core/Operations/FindOperationTests.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2013-2015 MongoDB Inc.
1+
/* Copyright 2013-2016 MongoDB Inc.
22
*
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
@@ -333,6 +333,20 @@ public void Execute_should_find_all_the_documents_matching_the_query(
333333
result.Should().HaveCount(5);
334334
}
335335

336+
[Test]
337+
[RequiresServer("EnsureTestData")]
338+
public void Execute_should_find_all_the_documents_matching_the_query_when_read_preference_is_used(
339+
[Values(false, true)]
340+
bool async)
341+
{
342+
var subject = new FindOperation<BsonDocument>(_collectionNamespace, BsonDocumentSerializer.Instance, _messageEncoderSettings);
343+
344+
var cursor = ExecuteOperation(subject, ReadPreference.PrimaryPreferred, async); // note: SecondaryPreferred doesn't test $readPreference because it is encoded as slaveOk = true
345+
var result = ReadCursorToEnd(cursor, async);
346+
347+
result.Should().HaveCount(5);
348+
}
349+
336350
[Test]
337351
[RequiresServer("EnsureTestData")]
338352
public void Execute_should_find_all_the_documents_matching_the_query_when_split_across_batches(

src/MongoDB.Driver.Core.Tests/Core/Operations/ListDatabasesOperationTests.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2013-2015 MongoDB Inc.
1+
/* Copyright 2013-2016 MongoDB Inc.
22
*
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
@@ -77,8 +77,9 @@ public void Execute_should_throw_when_binding_is_null(
7777
bool async)
7878
{
7979
var subject = new ListDatabasesOperation(_messageEncoderSettings);
80+
IReadBinding binding = null;
8081

81-
Action action = () => ExecuteOperation(subject, null, async);
82+
Action action = () => ExecuteOperation(subject, binding, async);
8283

8384
action.ShouldThrow<ArgumentNullException>().And.ParamName.Should().Be("binding");
8485
}

src/MongoDB.Driver.Core.Tests/Core/Operations/ListIndexesOperationTests.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2013-2015 MongoDB Inc.
1+
/* Copyright 2013-2016 MongoDB Inc.
22
*
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
@@ -107,8 +107,9 @@ public void Execute_should_throw_when_binding_is_null(
107107
bool async)
108108
{
109109
var subject = new ListIndexesOperation(_collectionNamespace, _messageEncoderSettings);
110+
IReadBinding binding = null;
110111

111-
Action action = () => ExecuteOperation(subject, null, async);
112+
Action action = () => ExecuteOperation(subject, binding, async);
112113

113114
action.ShouldThrow<ArgumentNullException>().And.ParamName.Should().Be("binding");
114115
}

src/MongoDB.Driver.Core.Tests/Core/Operations/ListIndexesUsingCommandOperationTests.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2013-2015 MongoDB Inc.
1+
/* Copyright 2013-2016 MongoDB Inc.
22
*
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
@@ -107,8 +107,9 @@ public void Execute_should_throw_when_binding_is_null(
107107
bool async)
108108
{
109109
var subject = new ListIndexesUsingCommandOperation(_collectionNamespace, _messageEncoderSettings);
110+
IReadBinding binding = null;
110111

111-
Action action = () => ExecuteOperation(subject, null, async);
112+
Action action = () => ExecuteOperation(subject, binding, async);
112113

113114
action.ShouldThrow<ArgumentNullException>().And.ParamName.Should().Be("binding");
114115
}

src/MongoDB.Driver.Core.Tests/Core/Operations/ListIndexesUsingQueryOperationTests.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2013-2015 MongoDB Inc.
1+
/* Copyright 2013-2016 MongoDB Inc.
22
*
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
@@ -107,8 +107,9 @@ public void Execute_should_throw_when_binding_is_null(
107107
bool async)
108108
{
109109
var subject = new ListIndexesUsingQueryOperation(_collectionNamespace, _messageEncoderSettings);
110+
IReadBinding binding = null;
110111

111-
Action action = () => ExecuteOperation(subject, null, async);
112+
Action action = () => ExecuteOperation(subject, binding, async);
112113

113114
action.ShouldThrow<ArgumentNullException>().And.ParamName.Should().Be("binding");
114115
}

src/MongoDB.Driver.Core.Tests/Core/Operations/MapReduceLegacyOperationTests.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2013-2015 MongoDB Inc.
1+
/* Copyright 2013-2016 MongoDB Inc.
22
*
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
@@ -22,6 +22,7 @@
2222
using MongoDB.Bson;
2323
using MongoDB.Bson.Serialization;
2424
using MongoDB.Bson.Serialization.Serializers;
25+
using MongoDB.Driver.Core.Bindings;
2526
using MongoDB.Driver.Core.Misc;
2627
using NUnit.Framework;
2728

@@ -126,8 +127,9 @@ public void Execute_should_throw_when_binding_is_null(
126127
bool async)
127128
{
128129
var subject = new MapReduceLegacyOperation(_collectionNamespace, _mapFunction, _reduceFunction, _messageEncoderSettings);
130+
IReadBinding binding = null;
129131

130-
Action act = () => ExecuteOperation(subject, null, async);
132+
Action act = () => ExecuteOperation(subject, binding, async);
131133

132134
act.ShouldThrow<ArgumentNullException>().And.ParamName.Should().Be("binding");
133135
}

src/MongoDB.Driver.Core.Tests/Core/Operations/MapReduceOperationTests.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2013-2015 MongoDB Inc.
1+
/* Copyright 2013-2016 MongoDB Inc.
22
*
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
@@ -22,6 +22,7 @@
2222
using MongoDB.Bson;
2323
using MongoDB.Bson.Serialization;
2424
using MongoDB.Bson.Serialization.Serializers;
25+
using MongoDB.Driver.Core.Bindings;
2526
using MongoDB.Driver.Core.Misc;
2627
using NUnit.Framework;
2728

@@ -136,8 +137,9 @@ public void Execute_should_throw_when_binding_is_null(
136137
bool async)
137138
{
138139
var subject = new MapReduceOperation<BsonDocument>(_collectionNamespace, _mapFunction, _reduceFunction, _resultSerializer, _messageEncoderSettings);
140+
IReadBinding binding = null;
139141

140-
Action act = () => ExecuteOperation(subject, null, async);
142+
Action act = () => ExecuteOperation(subject, binding, async);
141143

142144
act.ShouldThrow<ArgumentNullException>().And.ParamName.Should().Be("binding");
143145
}

0 commit comments

Comments
 (0)