Skip to content

Commit 7cb8e93

Browse files
committed
Add coverage for valid maxStalenessSeconds with no viable servers
JAVA-3760
1 parent bd52222 commit 7cb8e93

File tree

4 files changed

+31
-10
lines changed

4 files changed

+31
-10
lines changed

driver-core/src/main/com/mongodb/TaggableReadPreference.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -255,10 +255,12 @@ protected List<ServerDescription> selectFreshServers(final ClusterDescription cl
255255
}
256256
} else {
257257
ServerDescription mostUpToDateSecondary = findMostUpToDateSecondary(clusterDescription);
258-
for (ServerDescription cur : servers) {
259-
if (getLastWriteDateNonNull(mostUpToDateSecondary).getTime() - getLastWriteDateNonNull(cur).getTime()
260-
+ heartbeatFrequencyMS <= maxStaleness) {
261-
freshServers.add(cur);
258+
if (mostUpToDateSecondary != null) {
259+
for (ServerDescription cur : servers) {
260+
if (getLastWriteDateNonNull(mostUpToDateSecondary).getTime() - getLastWriteDateNonNull(cur).getTime()
261+
+ heartbeatFrequencyMS <= maxStaleness) {
262+
freshServers.add(cur);
263+
}
262264
}
263265
}
264266
}
@@ -293,9 +295,6 @@ private ServerDescription findMostUpToDateSecondary(final ClusterDescription clu
293295
}
294296
}
295297
}
296-
if (mostUpdateToDateSecondary == null) {
297-
throw new MongoInternalException("Expected at least one secondary in cluster description: " + clusterDescription);
298-
}
299298
return mostUpdateToDateSecondary;
300299
}
301300

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"topology_description": {
3+
"type": "ReplicaSetNoPrimary",
4+
"servers": [
5+
{
6+
"address": "a:27017",
7+
"type": "Unknown"
8+
},
9+
{
10+
"address": "b:27017",
11+
"type": "Unknown"
12+
}
13+
]
14+
},
15+
"read_preference": {
16+
"mode": "Nearest",
17+
"maxStalenessSeconds": 1
18+
},
19+
"error": true
20+
}

driver-core/src/test/resources/max-staleness/server_selection/ReplicaSetNoPrimary/NoKnownServers.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
},
1515
"read_preference": {
1616
"mode": "Nearest",
17-
"maxStalenessSeconds": 1
17+
"maxStalenessSeconds": 90
1818
},
19-
"error": true
19+
"suitable_servers": [],
20+
"in_latency_window": []
2021
}

driver-core/src/test/resources/max-staleness/server_selection/Unknown/SmallMaxStaleness.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"servers": [
66
{
77
"address": "a:27017",
8-
"type": "Unknown"
8+
"type": "Unknown",
9+
"maxWireVersion": 5
910
}
1011
]
1112
},

0 commit comments

Comments
 (0)