|
1 | 1 | import MongoSwiftSync |
2 | 2 | import TestsCommon |
3 | 3 |
|
4 | | -/// Protocol that test cases which configure fail points during their execution conform to. |
5 | | -internal protocol FailPointConfigured { |
6 | | - /// The fail point currently set, if one exists. |
7 | | - var activeFailPoint: FailPoint? { get set } |
8 | | - |
9 | | - /// The address of the host in which this failpoint was set on, if applicable. |
10 | | - var targetedHost: ServerAddress? { get set } |
11 | | -} |
12 | | - |
13 | 4 | extension FailPointConfigured { |
14 | 5 | /// Sets the active fail point to the provided fail point and enables it. |
15 | 6 | internal mutating func activateFailPoint( |
@@ -51,37 +42,7 @@ class FailPointGuard { |
51 | 42 | /// |
52 | 43 | /// - Note: if a fail point results in a connection being closed / interrupted, libmongoc built in debug mode will print |
53 | 44 | /// a warning. |
54 | | -internal struct FailPoint: Decodable { |
55 | | - private var failPoint: BSONDocument |
56 | | - |
57 | | - /// The fail point being configured. |
58 | | - internal var name: String { |
59 | | - self.failPoint["configureFailPoint"]?.stringValue ?? "" |
60 | | - } |
61 | | - |
62 | | - private init(_ document: BSONDocument) { |
63 | | - self.failPoint = document |
64 | | - } |
65 | | - |
66 | | - public init(from decoder: Decoder) throws { |
67 | | - let container = try decoder.singleValueContainer() |
68 | | - let unordered = try container.decode(BSONDocument.self) |
69 | | - guard let command = unordered["configureFailPoint"] else { |
70 | | - throw DecodingError.dataCorruptedError( |
71 | | - in: container, |
72 | | - debugDescription: "fail point \(unordered) did not contain \"configureFailPoint\" command" |
73 | | - ) |
74 | | - } |
75 | | - var ordered: BSONDocument = ["configureFailPoint": command] |
76 | | - for (k, v) in unordered { |
77 | | - guard k != "configureFailPoint" else { |
78 | | - continue |
79 | | - } |
80 | | - ordered[k] = v |
81 | | - } |
82 | | - self.failPoint = ordered |
83 | | - } |
84 | | - |
| 45 | +extension FailPoint { |
85 | 46 | internal func enable( |
86 | 47 | using client: MongoClient, |
87 | 48 | options: RunCommandOptions? = nil |
@@ -119,64 +80,4 @@ internal struct FailPoint: Decodable { |
119 | 80 | print("Failed to disable failpoint: \(error)") |
120 | 81 | } |
121 | 82 | } |
122 | | - |
123 | | - /// Enum representing the options for the "mode" field of a `configureFailPoint` command. |
124 | | - public enum Mode { |
125 | | - case times(Int) |
126 | | - case alwaysOn |
127 | | - case off |
128 | | - case activationProbability(Double) |
129 | | - |
130 | | - internal func toBSON() -> BSON { |
131 | | - switch self { |
132 | | - case let .times(i): |
133 | | - return ["times": BSON(i)] |
134 | | - case let .activationProbability(d): |
135 | | - return ["activationProbability": .double(d)] |
136 | | - default: |
137 | | - return .string(String(describing: self)) |
138 | | - } |
139 | | - } |
140 | | - } |
141 | | - |
142 | | - /// Factory function for creating a `failCommand` failpoint. |
143 | | - /// Note: enabling a `failCommand` failpoint will override any other `failCommand` failpoint that is currently |
144 | | - /// enabled. |
145 | | - /// For more information, see the wiki: https://github.com/mongodb/mongo/wiki/The-%22failCommand%22-fail-point |
146 | | - public static func failCommand( |
147 | | - failCommands: [String], |
148 | | - mode: Mode, |
149 | | - blockTimeMS: Int? = nil, |
150 | | - closeConnection: Bool? = nil, |
151 | | - errorCode: Int? = nil, |
152 | | - errorLabels: [String]? = nil, |
153 | | - writeConcernError: BSONDocument? = nil |
154 | | - ) -> FailPoint { |
155 | | - var data: BSONDocument = [ |
156 | | - "failCommands": .array(failCommands.map { .string($0) }) |
157 | | - ] |
158 | | - if let blockTime = blockTimeMS { |
159 | | - data["blockTimeMS"] = BSON(blockTime) |
160 | | - data["blockConnection"] = true |
161 | | - } |
162 | | - if let close = closeConnection { |
163 | | - data["closeConnection"] = .bool(close) |
164 | | - } |
165 | | - if let code = errorCode { |
166 | | - data["errorCode"] = BSON(code) |
167 | | - } |
168 | | - if let labels = errorLabels { |
169 | | - data["errorLabels"] = .array(labels.map { .string($0) }) |
170 | | - } |
171 | | - if let writeConcernError = writeConcernError { |
172 | | - data["writeConcernError"] = .document(writeConcernError) |
173 | | - } |
174 | | - |
175 | | - let command: BSONDocument = [ |
176 | | - "configureFailPoint": "failCommand", |
177 | | - "mode": mode.toBSON(), |
178 | | - "data": .document(data) |
179 | | - ] |
180 | | - return FailPoint(command) |
181 | | - } |
182 | 83 | } |
0 commit comments