Skip to content

Commit 7466a67

Browse files
authored
SWIFT-549 Test that $readPreference isn't sent to standalone servers (#691)
1 parent 1227101 commit 7466a67

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

Sources/MongoSwift/SDAM.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ extension ServerDescription: Equatable {
249249
/// which servers are up, what type of servers they are, which is primary, and so on.
250250
public struct TopologyDescription: Equatable {
251251
/// The possible types for a topology.
252-
public struct TopologyType: RawRepresentable, Equatable {
252+
public struct TopologyType: RawRepresentable, Equatable, CustomStringConvertible {
253253
/// A single mongod server.
254254
public static let single = TopologyType(.single)
255255

@@ -295,6 +295,10 @@ public struct TopologyDescription: Equatable {
295295
}
296296
self._topologyType = _type
297297
}
298+
299+
public var description: String {
300+
self.rawValue
301+
}
298302
}
299303

300304
/// The type of this topology.

Tests/LinuxMain.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ extension ReadConcernTests {
268268
extension ReadPreferenceOperationTests {
269269
static var allTests = [
270270
("testOperationReadPreference", testOperationReadPreference),
271+
("testReadPreferenceIsntSentToStandalones", testReadPreferenceIsntSentToStandalones),
271272
]
272273
}
273274

Tests/MongoSwiftSyncTests/ReadPreferenceOperationTests.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,27 @@ final class ReadPreferenceOperationTests: MongoSwiftTestCase {
4141
options: DistinctOptions(readPreference: .secondaryPreferred)
4242
)).toNot(throwError())
4343
}
44+
45+
func testReadPreferenceIsntSentToStandalones() throws {
46+
try withTestNamespace { client, _, coll in
47+
guard try client.topologyType() == .single else {
48+
print(unsupportedTopologyMessage(testName: self.name))
49+
return
50+
}
51+
52+
let opts = FindOneOptions(readPreference: .secondary)
53+
let monitor = client.addCommandMonitor()
54+
try monitor.captureEvents {
55+
_ = try coll.findOne(options: opts)
56+
}
57+
58+
guard let event = monitor.commandStartedEvents(withNames: ["find"]).first else {
59+
XCTFail("Unexpectedly missing find event")
60+
return
61+
}
62+
63+
expect(event.command.keys)
64+
.toNot(contain("$readPreference"), description: "Command unexpectedly contained $readPreference key")
65+
}
66+
}
4467
}

0 commit comments

Comments
 (0)