Skip to content

Commit a30ad32

Browse files
authored
Run killAllSessions less in unified runner (#659)
1 parent 3c39cb5 commit a30ad32

File tree

1 file changed

+55
-56
lines changed

1 file changed

+55
-56
lines changed

Tests/MongoSwiftSyncTests/UnifiedTestRunner/UnifiedTestRunner.swift

Lines changed: 55 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@ struct UnifiedTestRunner {
1717
self.serverVersion = try self.internalClient.serverVersion()
1818
self.topologyType = try self.internalClient.topologyType()
1919
self.serverParameters = try self.internalClient.serverParameters()
20-
21-
// The test runner SHOULD terminate any open transactions using the internal MongoClient before executing any
22-
// tests.
23-
try self.terminateOpenTransactions()
2420
}
2521

2622
func terminateOpenTransactions() throws {
@@ -57,17 +53,6 @@ struct UnifiedTestRunner {
5753
/// strings indicating cases to skip. If the array contains a single string "*" all tests in the file will be
5854
/// skipped.
5955
func runFiles(_ files: [UnifiedTestFile], skipTests: [String: [String]] = [:]) throws {
60-
// Test runners SHOULD terminate all open transactions after each failed test by killing all sessions in the
61-
// cluster. Since we stop running the provided files as soon we encounter a failure, we just always run this on
62-
// method exit for simplicity.
63-
defer {
64-
do {
65-
try self.terminateOpenTransactions()
66-
} catch {
67-
print("Failed to terminate open transactions: \(error)")
68-
}
69-
}
70-
7156
for file in files {
7257
// Upon loading a file, the test runner MUST read the schemaVersion field and determine if the test file
7358
// can be processed further.
@@ -122,8 +107,6 @@ struct UnifiedTestRunner {
122107
}
123108
}
124109

125-
fileLevelLog("Running test \"\(test.description)\" from file \"\(file.description)\"")
126-
127110
// If initialData is specified, for each collectionData therein the test runner MUST drop the
128111
// collection and insert the specified documents (if any) using a "majority" write concern. If no
129112
// documents are specified, the test runner MUST create the collection with a "majority" write concern.
@@ -168,54 +151,70 @@ struct UnifiedTestRunner {
168151
}
169152
}
170153

171-
for (i, operation) in test.operations.enumerated() {
172-
try context.withPushedElt("Operation \(i) (\(operation.name))") {
173-
try operation.executeAndCheckResult(context: context)
174-
}
175-
}
154+
fileLevelLog("Running test \"\(test.description)\" from file \"\(file.description)\"")
176155

177-
var clientEvents = [String: [CommandEvent]]()
178-
// If any event listeners were enabled on any client entities, the test runner MUST now disable those
179-
// event listeners.
180-
for (id, client) in context.entities.compactMapValues({ try? $0.asTestClient() }) {
181-
clientEvents[id] = try client.stopCapturingEvents()
182-
}
156+
do {
157+
for (i, operation) in test.operations.enumerated() {
158+
try context.withPushedElt("Operation \(i) (\(operation.name))") {
159+
try operation.executeAndCheckResult(context: context)
160+
}
161+
}
183162

184-
if let expectEvents = test.expectEvents {
185-
for expectedEventList in expectEvents {
186-
let clientId = expectedEventList.client
163+
var clientEvents = [String: [CommandEvent]]()
164+
// If any event listeners were enabled on any client entities, the test runner MUST now disable
165+
// those event listeners.
166+
for (id, client) in context.entities.compactMapValues({ try? $0.asTestClient() }) {
167+
clientEvents[id] = try client.stopCapturingEvents()
168+
}
187169

188-
guard let actualEvents = clientEvents[clientId] else {
189-
throw TestError(message: "No client entity found with id \(clientId)")
170+
if let expectEvents = test.expectEvents {
171+
for expectedEventList in expectEvents {
172+
let clientId = expectedEventList.client
173+
174+
guard let actualEvents = clientEvents[clientId] else {
175+
throw TestError(message: "No client entity found with id \(clientId)")
176+
}
177+
178+
try context.withPushedElt("Expected events for client \(clientId)") {
179+
try matchesEvents(
180+
expected: expectedEventList.events,
181+
actual: actualEvents,
182+
context: context
183+
)
184+
}
190185
}
186+
}
191187

192-
try context.withPushedElt("Expected events for client \(clientId)") {
193-
try matchesEvents(
194-
expected: expectedEventList.events,
195-
actual: actualEvents,
196-
context: context
188+
if let expectedOutcome = test.outcome {
189+
for collectionData in expectedOutcome {
190+
let collection = self.internalClient
191+
.db(collectionData.databaseName)
192+
.collection(collectionData.collectionName)
193+
let opts = FindOptions(
194+
readConcern: .local,
195+
readPreference: .primary,
196+
sort: ["_id": 1]
197197
)
198+
let documents = try collection.find(options: opts).map { try $0.get() }
199+
200+
expect(documents.count).to(equal(collectionData.documents.count))
201+
for (expected, actual) in zip(collectionData.documents, documents) {
202+
expect(actual).to(
203+
sortedEqual(expected),
204+
description: "Test outcome did not match expected"
205+
)
206+
}
198207
}
199208
}
200-
}
201-
202-
if let expectedOutcome = test.outcome {
203-
for collectionData in expectedOutcome {
204-
let collection = self.internalClient
205-
.db(collectionData.databaseName)
206-
.collection(collectionData.collectionName)
207-
let opts = FindOptions(
208-
readConcern: .local,
209-
readPreference: .primary,
210-
sort: ["_id": 1]
211-
)
212-
let documents = try collection.find(options: opts).map { try $0.get() }
213-
214-
expect(documents.count).to(equal(collectionData.documents.count))
215-
for (expected, actual) in zip(collectionData.documents, documents) {
216-
expect(actual).to(sortedEqual(expected), description: "Test outcome did not match expected")
217-
}
209+
} catch let testErr {
210+
// Test runners SHOULD terminate all open transactions after each failed test by killing all
211+
// sessions in the cluster.
212+
do {
213+
try self.terminateOpenTransactions()
214+
} catch {
215+
print("Failed to terminate open transactions: \(error)")
218216
}
217+
throw testErr
219218
}
220219
}
221220
}

0 commit comments

Comments
 (0)