Skip to content

Commit a42ede2

Browse files
committed
CSHARP-1005: Fix ReadPreference to provide correct GetHashCode() values.
1 parent 672bd04 commit a42ede2

File tree

6 files changed

+222
-6
lines changed

6 files changed

+222
-6
lines changed

MongoDB.Driver/ReadPreference.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using System.Collections.Generic;
1818
using System.Collections.ObjectModel;
1919
using System.Linq;
20+
using MongoDB.Shared;
2021

2122
namespace MongoDB.Driver
2223
{
@@ -320,11 +321,10 @@ public override int GetHashCode()
320321
return _frozenHashCode;
321322
}
322323

323-
// see Effective Java by Joshua Bloch
324-
int hash = 17;
325-
hash = 37 * hash + _readPreferenceMode.GetHashCode();
326-
hash = 37 * hash + ((_tagSets == null) ? 0 : _tagSets.GetHashCode());
327-
return hash;
324+
return new Hasher()
325+
.Hash(_readPreferenceMode)
326+
.HashElements(_tagSets)
327+
.GetHashCode();
328328
}
329329

330330
/// <summary>

MongoDB.Driver/ReplicaSetTagSet.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
using System.Collections.Generic;
1919
using System.Collections.ObjectModel;
2020
using System.Linq;
21+
using MongoDB.Shared;
2122

2223
namespace MongoDB.Driver
2324
{
@@ -203,7 +204,7 @@ public override int GetHashCode()
203204
return _frozenHashCode;
204205
}
205206

206-
return _tags.GetHashCode();
207+
return new Hasher().HashElements(_tags).GetHashCode();
207208
}
208209

209210
/// <summary>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/* Copyright 2010-2014 MongoDB Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
using System;
17+
using System.Linq;
18+
using MongoDB.Bson;
19+
using MongoDB.Driver;
20+
using NUnit.Framework;
21+
22+
namespace MongoDB.DriverUnitTests
23+
{
24+
[TestFixture]
25+
public class MongoClientTests
26+
{
27+
[Test]
28+
public void UsesSameMongoServerForIdenticalSettings()
29+
{
30+
var client1 = new MongoClient("mongodb://localhost");
31+
var server1 = client1.GetServer();
32+
33+
var client2 = new MongoClient("mongodb://localhost");
34+
var server2 = client2.GetServer();
35+
36+
Assert.AreSame(server1, server2);
37+
}
38+
39+
[Test]
40+
public void UsesSameMongoServerWhenReadPreferenceTagsAreTheSame()
41+
{
42+
var client1 = new MongoClient("mongodb://localhost/?readPreferenceTags=dc:ny");
43+
var server1 = client1.GetServer();
44+
45+
var client2 = new MongoClient("mongodb://localhost/?readPreferenceTags=dc:ny");
46+
var server2 = client2.GetServer();
47+
48+
Assert.AreSame(server1, server2);
49+
}
50+
}
51+
}

MongoDB.DriverUnitTests/MongoDB.DriverUnitTests.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,11 @@
147147
<Compile Include="Jira\CSharp900Tests.cs" />
148148
<Compile Include="MongoClientSettingsTests.cs" />
149149
<Compile Include="MongoServerBuildInfoTests.cs" />
150+
<Compile Include="MongoClientTests.cs" />
150151
<Compile Include="Operations\BulkWriteBatchResultCombinerTests.cs" />
151152
<Compile Include="Operations\BulkWriteOperationTests.cs" />
152153
<Compile Include="Operations\DeleteCommandEmulatorTests.cs" />
154+
<Compile Include="ReplicaSetTagSetTests.cs" />
153155
<Compile Include="ReadPreferenceTests.cs" />
154156
<Compile Include="MongoCollectionSettingsTests.cs" />
155157
<Compile Include="MongoCollectionTests.cs" />

MongoDB.DriverUnitTests/ReadPreferenceTests.cs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* limitations under the License.
1414
*/
1515

16+
using System.Collections.Generic;
1617
using MongoDB.Driver;
1718
using NUnit.Framework;
1819

@@ -37,5 +38,77 @@ public void TestCopyConstructor()
3738

3839
Assert.AreEqual(subject, other);
3940
}
41+
42+
[Test]
43+
public void TestGetHashCodeIsSameWhenEverythingIsTheSame()
44+
{
45+
var tagSets1 = new List<ReplicaSetTagSet>()
46+
{
47+
new ReplicaSetTagSet
48+
{
49+
new ReplicaSetTag("dc", "ny")
50+
}
51+
};
52+
var rp1 = new ReadPreference(ReadPreferenceMode.Nearest, tagSets1);
53+
54+
var tagSets2 = new List<ReplicaSetTagSet>()
55+
{
56+
new ReplicaSetTagSet
57+
{
58+
new ReplicaSetTag("dc", "ny")
59+
}
60+
};
61+
var rp2 = new ReadPreference(ReadPreferenceMode.Nearest, tagSets2);
62+
63+
Assert.AreEqual(rp1.GetHashCode(), rp2.GetHashCode());
64+
}
65+
66+
[Test]
67+
public void TestGetHashCodeIsDifferentWhenTagsAreDifferent()
68+
{
69+
var tagSets1 = new List<ReplicaSetTagSet>()
70+
{
71+
new ReplicaSetTagSet
72+
{
73+
new ReplicaSetTag("dc", "ny")
74+
}
75+
};
76+
var rp1 = new ReadPreference(ReadPreferenceMode.Nearest, tagSets1);
77+
78+
var tagSets2 = new List<ReplicaSetTagSet>()
79+
{
80+
new ReplicaSetTagSet
81+
{
82+
new ReplicaSetTag("dc", "tx")
83+
}
84+
};
85+
var rp2 = new ReadPreference(ReadPreferenceMode.Nearest, tagSets2);
86+
87+
Assert.AreNotEqual(rp1.GetHashCode(), rp2.GetHashCode());
88+
}
89+
90+
[Test]
91+
public void TestEquality()
92+
{
93+
var tagSets1 = new List<ReplicaSetTagSet>()
94+
{
95+
new ReplicaSetTagSet
96+
{
97+
new ReplicaSetTag("dc", "ny")
98+
}
99+
};
100+
var rp1 = new ReadPreference(ReadPreferenceMode.Nearest, tagSets1);
101+
102+
var tagSets2 = new List<ReplicaSetTagSet>()
103+
{
104+
new ReplicaSetTagSet
105+
{
106+
new ReplicaSetTag("dc", "ny")
107+
}
108+
};
109+
var rp2 = new ReadPreference(ReadPreferenceMode.Nearest, tagSets1);
110+
111+
Assert.AreEqual(rp1, rp2);
112+
}
40113
}
41114
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/* Copyright 2010-2014 MongoDB Inc.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
using System.Collections.Generic;
17+
using MongoDB.Driver;
18+
using NUnit.Framework;
19+
20+
namespace MongoDB.DriverUnitTests
21+
{
22+
[TestFixture]
23+
public class ReplicaSetTagSetTests
24+
{
25+
[Test]
26+
public void TestGetHashCodeIsSameWhenTagsAreTheSame()
27+
{
28+
var tagSet1 = new ReplicaSetTagSet
29+
{
30+
new ReplicaSetTag("dc", "ny")
31+
};
32+
33+
var tagSet2 = new ReplicaSetTagSet
34+
{
35+
new ReplicaSetTag("dc", "ny")
36+
};
37+
38+
Assert.AreEqual(tagSet1.GetHashCode(), tagSet2.GetHashCode());
39+
}
40+
41+
[Test]
42+
public void TestGetHashCodeIsSameWhenTagsAreDifferent()
43+
{
44+
var tagSet1 = new ReplicaSetTagSet
45+
{
46+
new ReplicaSetTag("dc", "ny")
47+
};
48+
49+
var tagSet2 = new ReplicaSetTagSet
50+
{
51+
new ReplicaSetTag("dc", "tx")
52+
};
53+
54+
Assert.AreNotEqual(tagSet1.GetHashCode(), tagSet2.GetHashCode());
55+
}
56+
57+
[Test]
58+
public void TestAreEqualWhenTagsAreEqual()
59+
{
60+
var tagSet1 = new ReplicaSetTagSet
61+
{
62+
new ReplicaSetTag("dc", "ny")
63+
};
64+
65+
var tagSet2 = new ReplicaSetTagSet
66+
{
67+
new ReplicaSetTag("dc", "ny")
68+
};
69+
70+
Assert.AreEqual(tagSet1, tagSet2);
71+
}
72+
73+
[Test]
74+
public void TestAreNotEqualWhenTagsAreNotEqual()
75+
{
76+
var tagSet1 = new ReplicaSetTagSet
77+
{
78+
new ReplicaSetTag("dc", "ny")
79+
};
80+
81+
var tagSet2 = new ReplicaSetTagSet
82+
{
83+
new ReplicaSetTag("dc", "tx")
84+
};
85+
86+
Assert.AreNotEqual(tagSet1, tagSet2);
87+
}
88+
}
89+
}

0 commit comments

Comments
 (0)