Skip to content

Commit b1e8f4b

Browse files
Make selectServer async (#742)
1 parent 62eaa14 commit b1e8f4b

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

Sources/MongoSwift/ServerSelection.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@ internal struct Server {
1919
}
2020
}
2121

22+
#if compiler(>=5.5.2) && canImport(_Concurrency)
2223
extension MongoClient {
24+
@available(macOS 10.15.0, *)
2325
internal func selectServer(
2426
readPreference: ReadPreference = ReadPreference.primary,
2527
topology: TopologyDescription,
2628
servers: [ServerAddress: Server]
27-
) throws -> Server {
29+
) async throws -> Server {
2830
let startTime = Date()
2931
let serverSelectionTimeoutMS = self.connectionString.serverSelectionTimeoutMS
3032
?? SDAMConstants.defaultServerSelectionTimeoutMS
@@ -50,9 +52,8 @@ extension MongoClient {
5052

5153
let selectedServer: Server
5254
if inWindowServers.isEmpty {
53-
// When pure Swift SDAM is implemented, this should instead block on a topology change occurring for
54-
// endTime - Date() seconds.
55-
// TODO: add an async sleep here once the driver has been updated to permit async/await on 10.15+
55+
let nanosecondsRemaining = endTime.timeIntervalSince(Date()) * 1_000_000_000
56+
try await Task.sleep(nanoseconds: UInt64(nanosecondsRemaining))
5657
continue
5758
} else if inWindowServers.count == 1 {
5859
selectedServer = inWindowServers[0]
@@ -78,6 +79,7 @@ extension MongoClient {
7879
+ " read preference: \(readPreference)\nTopology: \(topology)")
7980
}
8081
}
82+
#endif
8183

8284
extension Array where Element == ServerDescription {
8385
/// Filters servers according to their latency. A server is considered to be within the latency window if its

Tests/MongoSwiftTests/ServerSelectionTests.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,15 +160,17 @@ final class ServerSelectionTests: MongoSwiftTestCase {
160160
}
161161
}
162162

163-
func testSelectionWithinLatencyWindow() throws {
163+
#if compiler(>=5.5.2) && canImport(_Concurrency)
164+
@available(macOS 10.15.0, *)
165+
func testSelectionWithinLatencyWindow() async throws {
164166
let tests = try retrieveSpecTestFiles(
165167
specName: "server-selection",
166168
subdirectory: "in_window",
167169
asType: SelectionWithinLatencyWindowTest.self
168170
)
169171
for (filename, test) in tests {
170172
print("Running test from \(filename)...")
171-
try self.withTestClient { client in
173+
try await self.withTestClient { client in
172174
var selectedServerCounts: [ServerAddress: Int] = [:]
173175
let readPreference = ReadPreference.nearest
174176
for _ in 1...test.iterations {
@@ -177,7 +179,7 @@ final class ServerSelectionTests: MongoSwiftTestCase {
177179
let servers = test.mockedTopologyState.reduce(into: [ServerAddress: Server]()) {
178180
$0[$1.address] = Server(address: $1.address, operationCount: $1.operationCount)
179181
}
180-
let selectedServer = try client.selectServer(
182+
let selectedServer = try await client.selectServer(
181183
readPreference: readPreference,
182184
topology: test.topologyDescription,
183185
servers: servers
@@ -209,6 +211,7 @@ final class ServerSelectionTests: MongoSwiftTestCase {
209211
}
210212
}
211213
}
214+
#endif
212215

213216
func testReadPreferenceValidation() throws {
214217
var readPreference = ReadPreference.primary

0 commit comments

Comments
 (0)