Skip to content

Commit a1bdaa2

Browse files
author
rstam
committed
Fixed some more problems when running unit tests against replica sets.
1 parent 7d30a79 commit a1bdaa2

File tree

5 files changed

+34
-7
lines changed

5 files changed

+34
-7
lines changed

MongoDB.Driver/Communication/Proxies/DiscoveringMongoServerProxy.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,23 @@ public ReadOnlyCollection<MongoServerInstance> Instances
9696
}
9797
}
9898

99+
/// <summary>
100+
/// Gets the name of the replica set (null if not connected to a replica set).
101+
/// </summary>
102+
public string ReplicaSetName
103+
{
104+
get
105+
{
106+
var replicaSetProxy = _serverProxy as ReplicaSetMongoServerProxy;
107+
if (replicaSetProxy != null)
108+
{
109+
return replicaSetProxy.ReplicaSetName;
110+
}
111+
112+
return null;
113+
}
114+
}
115+
99116
/// <summary>
100117
/// Gets the state.
101118
/// </summary>

MongoDB.Driver/MongoCollection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1780,7 +1780,7 @@ private TCommandResult RunCommandAs<TCommandResult>(
17801780
resultSerializationOptions,
17811781
resultSerializer);
17821782

1783-
var connection = _server.AcquireConnection(ReadPreference.Primary);
1783+
var connection = _server.AcquireConnection(readPreference);
17841784
try
17851785
{
17861786
return commandOperation.Execute(connection);

MongoDB.Driver/MongoServer.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -295,10 +295,16 @@ public virtual string ReplicaSetName
295295
{
296296
get
297297
{
298-
var instanceManager = _serverProxy as ReplicaSetMongoServerProxy;
299-
if (instanceManager != null)
298+
var replicaSetProxy = _serverProxy as ReplicaSetMongoServerProxy;
299+
if (replicaSetProxy != null)
300300
{
301-
return instanceManager.ReplicaSetName;
301+
return replicaSetProxy.ReplicaSetName;
302+
}
303+
304+
var discoveringProxy = _serverProxy as DiscoveringMongoServerProxy;
305+
if (discoveringProxy != null)
306+
{
307+
return discoveringProxy.ReplicaSetName;
302308
}
303309

304310
return null;

MongoDB.DriverUnitTests/Jira/CSharp269Tests.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
using System.Text;
1919
using System.Threading;
2020
using MongoDB.Driver;
21+
using MongoDB.Driver.GridFS;
2122
using NUnit.Framework;
23+
using MongoDB.Driver.Builders;
2224

2325
namespace MongoDB.DriverUnitTests.Jira.CSharp269
2426
{
@@ -43,19 +45,21 @@ public void TestFixtureSetup()
4345
[Test]
4446
public void TestUploadAndDownload()
4547
{
48+
MongoGridFSFileInfo uploadedFileInfo;
49+
4650
var text = "HelloWorld";
4751
var bytes = Encoding.UTF8.GetBytes(text);
4852
using (var stream = new MemoryStream(bytes))
4953
{
50-
_database.GridFS.Upload(stream, "HelloWorld.txt");
54+
uploadedFileInfo = _database.GridFS.Upload(stream, "HelloWorld.txt");
5155
}
5256

5357
// use RequestStart so that if we are running this test against a replica set we will bind to a specific secondary
5458
using (_server.RequestStart(_database, ReadPreference.SecondaryPreferred))
5559
{
5660
// wait for the GridFS file to be replicated before trying to Download it
5761
var timeoutAt = DateTime.UtcNow.AddSeconds(30);
58-
while (!_database.GridFS.Exists("HelloWorld.txt"))
62+
while (!_database.GridFS.Exists(Query.EQ("_id", uploadedFileInfo.Id)))
5963
{
6064
if (DateTime.UtcNow >= timeoutAt)
6165
{

MongoDB.DriverUnitTests/MongoServerTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ public void TestPrimary()
218218
public void TestReconnect()
219219
{
220220
_server.Reconnect();
221-
Assert.AreEqual(MongoServerState.Connected, _server.State);
221+
Assert.IsTrue(_server.State == MongoServerState.Connected || _server.State == MongoServerState.ConnectedToSubset);
222222
}
223223

224224
[Test]

0 commit comments

Comments
 (0)