diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..efcf36b --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,125 @@ +**HIPPOCRATIC LICENSE** + +**Version 3.0, October 2021** + + + +**TERMS AND CONDITIONS** + +TERMS AND CONDITIONS FOR USE, COPY, MODIFICATION, PREPARATION OF DERIVATIVE WORK, REPRODUCTION, AND DISTRIBUTION: + +**[1.](#1) DEFINITIONS:** + +_This section defines certain terms used throughout this license agreement._ + +[1.1.](#1.1) “License” means the terms and conditions, as stated herein, for use, copy, modification, preparation of derivative work, reproduction, and distribution of Software (as defined below). + +[1.2.](#1.2) “Licensor” means the copyright and/or patent owner or entity authorized by the copyright and/or patent owner that is granting the License. + +[1.3.](#1.3) “Licensee” means the individual or entity exercising permissions granted by this License, including the use, copy, modification, preparation of derivative work, reproduction, and distribution of Software (as defined below). + +[1.4.](#1.4) “Software” means any copyrighted work, including but not limited to software code, authored by Licensor and made available under this License. + +[1.5.](#1.5) “Supply Chain” means the sequence of processes involved in the production and/or distribution of a commodity, good, or service offered by the Licensee. + +[1.6.](#1.6) “Supply Chain Impacted Party” or “Supply Chain Impacted Parties” means any person(s) directly impacted by any of Licensee’s Supply Chain, including the practices of all persons or entities within the Supply Chain prior to a good or service reaching the Licensee. + +[1.7.](#1.7) “Duty of Care” is defined by its use in tort law, delict law, and/or similar bodies of law closely related to tort and/or delict law, including without limitation, a requirement to act with the watchfulness, attention, caution, and prudence that a reasonable person in the same or similar circumstances would use towards any Supply Chain Impacted Party. + +[1.8.](#1.8) “Worker” is defined to include any and all permanent, temporary, and agency workers, as well as piece-rate, salaried, hourly paid, legal young (minors), part-time, night, and migrant workers. + +**[2.](#2) INTELLECTUAL PROPERTY GRANTS:** + +_This section identifies intellectual property rights granted to a Licensee_. + +[2.1.](#2.1) _Grant of Copyright License_: Subject to the terms and conditions of this License, Licensor hereby grants to Licensee a worldwide, non-exclusive, no-charge, royalty-free copyright license to use, copy, modify, prepare derivative work, reproduce, or distribute the Software, Licensor authored modified software, or other work derived from the Software. + +[2.2.](#2.2) _Grant of Patent License_: Subject to the terms and conditions of this License, Licensor hereby grants Licensee a worldwide, non-exclusive, no-charge, royalty-free patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer Software. + +**[3.](#3) ETHICAL STANDARDS:** + +_This section lists conditions the Licensee must comply with in order to have rights under this License._ + +The rights granted to the Licensee by this License are expressly made subject to the Licensee’s ongoing compliance with the following conditions: + +* [3.1.](#3.1) The Licensee SHALL NOT, whether directly or indirectly, through agents or assigns: + * [3.1.1.](#3.1.1) Infringe upon any person’s right to life or security of person, engage in extrajudicial killings, or commit murder, without lawful cause (See Article 3, _United Nations Universal Declaration of Human Rights_; Article 6, _International Covenant on Civil and Political Rights_) + * [3.1.2.](#3.1.2) Hold any person in slavery, servitude, or forced labor (See Article 4, _United Nations Universal Declaration of Human Rights_; Article 8, _International Covenant on Civil and Political Rights_); + * [3.1.3.](#3.1.3) Contribute to the institution of slavery, slave trading, forced labor, or unlawful child labor (See Article 4, _United Nations Universal Declaration of Human Rights_; Article 8, _International Covenant on Civil and Political Rights_); + * [3.1.4.](#3.1.4) Torture or subject any person to cruel, inhumane, or degrading treatment or punishment (See Article 5, _United Nations Universal Declaration of Human Rights_; Article 7, _International Covenant on Civil and Political Rights_); + * [3.1.5.](#3.1.5) Discriminate on the basis of sex, gender, sexual orientation, race, ethnicity, nationality, religion, caste, age, medical disability or impairment, and/or any other like circumstances (See Article 7, _United Nations Universal Declaration of Human Rights_; Article 2, _International Covenant on Economic, Social and Cultural Rights_; Article 26, _International Covenant on Civil and Political Rights_); + * [3.1.6.](#3.1.6) Prevent any person from exercising his/her/their right to seek an effective remedy by a competent court or national tribunal (including domestic judicial systems, international courts, arbitration bodies, and other adjudicating bodies) for actions violating the fundamental rights granted to him/her/them by applicable constitutions, applicable laws, or by this License (See Article 8, _United Nations Universal Declaration of Human Rights_; Articles 9 and 14, _International Covenant on Civil and Political Rights_); + * [3.1.7.](#3.1.7) Subject any person to arbitrary arrest, detention, or exile (See Article 9, _United Nations Universal Declaration of Human Rights_; Article 9, _International Covenant on Civil and Political Rights_); + * [3.1.8.](#3.1.8) Subject any person to arbitrary interference with a person’s privacy, family, home, or correspondence without the express written consent of the person (See Article 12, _United Nations Universal Declaration of Human Rights_; Article 17, _International Covenant on Civil and Political Rights_); + * [3.1.9.](#3.1.9) Arbitrarily deprive any person of his/her/their property (See Article 17, _United Nations Universal Declaration of Human Rights_); + * [3.1.10.](#3.1.10) Forcibly remove indigenous peoples from their lands or territories or take any action with the aim or effect of dispossessing indigenous peoples from their lands, territories, or resources, including without limitation the intellectual property or traditional knowledge of indigenous peoples, without the free, prior, and informed consent of indigenous peoples concerned (See Articles 8 and 10, _United Nations Declaration on the Rights of Indigenous Peoples_); + * [3.1.11.](#3.1.11) _Taliban_: Be an individual or entity that: + * [3.1.11.1.](#3.1.11.1) engages in any commercial transactions with the Taliban; or + * [3.1.11.2.](#3.1.11.2) is a representative, agent, affiliate, successor, attorney, or assign of the Taliban; + * [3.1.12.](#3.1.12) _Myanmar_: Be an individual or entity that: + * [3.1.12.1.](#3.1.12.1) engages in any commercial transactions with the Myanmar/Burmese military junta; or + * [3.1.12.2.](#3.1.12.2) is a representative, agent, affiliate, successor, attorney, or assign of the Myanmar/Burmese government; + * [3.1.13.](#3.1.13) _Xinjiang Uygur Autonomous Region_: Be an individual or entity, or a representative, agent, affiliate, successor, attorney, or assign of any individual or entity, that does business in, purchases goods from, or otherwise benefits from goods produced in the Xinjiang Uygur Autonomous Region of China; + * [3.1.14.](#3.1.14) _Military Activities_: Be an entity or a representative, agent, affiliate, successor, attorney, or assign of an entity which conducts military activities; + * [3.1.15.](#3.1.15) _Media_: Be an individual or entity, or a representative, agent, affiliate, successor, attorney, or assign of an individual or entity, that broadcasts messages promoting killing, torture, or other forms of extreme violence; + * [3.1.16.](#3.1.16) Interfere with Workers' free exercise of the right to organize and associate (See Article 20, United Nations Universal Declaration of Human Rights; C087 - Freedom of Association and Protection of the Right to Organise Convention, 1948 (No. 87), International Labour Organization; Article 8, International Covenant on Economic, Social and Cultural Rights); and + * [3.1.17.](#3.1.17) Harm the environment in a manner inconsistent with local, state, national, or international law. +* [3.2.](#3.2) The Licensee SHALL: + * [3.2.1.](#3.2.1) Provide equal pay for equal work where the performance of such work requires equal skill, effort, and responsibility, and which are performed under similar working conditions, except where such payment is made pursuant to: + * [3.2.1.1.](#3.2.1.1) A seniority system; + * [3.2.1.2.](#3.2.1.2) A merit system; + * [3.2.1.3.](#3.2.1.3) A system which measures earnings by quantity or quality of production; or + * [3.2.1.4.](#3.2.1.4) A differential based on any other factor other than sex, gender, sexual orientation, race, ethnicity, nationality, religion, caste, age, medical disability or impairment, and/or any other like circumstances (See 29 U.S.C.A. § 206(d)(1); Article 23, _United Nations Universal Declaration of Human Rights_; Article 7, _International Covenant on Economic, Social and Cultural Rights_; Article 26, _International Covenant on Civil and Political Rights_); and + * [3.2.2.](#3.2.2) Allow for reasonable limitation of working hours and periodic holidays with pay (See Article 24, _United Nations Universal Declaration of Human Rights_; Article 7, _International Covenant on Economic, Social and Cultural Rights_). + +**[4.](#4) SUPPLY CHAIN IMPACTED PARTIES:** + +_This section identifies additional individuals or entities that a Licensee could harm as a result of violating the Ethical Standards section, the condition that the Licensee must voluntarily accept a Duty of Care for those individuals or entities, and the right to a private right of action that those individuals or entities possess as a result of violations of the Ethical Standards section._ + +[4.1.](#4.1) In addition to the above Ethical Standards, Licensee voluntarily accepts a Duty of Care for Supply Chain Impacted Parties of this License, including individuals and communities impacted by violations of the Ethical Standards. The Duty of Care is breached when a provision within the Ethical Standards section is violated by a Licensee, one of its successors or assigns, or by an individual or entity that exists within the Supply Chain prior to a good or service reaching the Licensee. + +[4.2.](#4.2) Breaches of the Duty of Care, as stated within this section, shall create a private right of action, allowing any Supply Chain Impacted Party harmed by the Licensee to take legal action against the Licensee in accordance with applicable negligence laws, whether they be in tort law, delict law, and/or similar bodies of law closely related to tort and/or delict law, regardless if Licensee is directly responsible for the harms suffered by a Supply Chain Impacted Party. Nothing in this section shall be interpreted to include acts committed by individuals outside of the scope of his/her/their employment. + +[5.](#5) **NOTICE:** _This section explains when a Licensee must notify others of the License._ + +[5.1.](#5.1) _Distribution of Notice_: Licensee must ensure that everyone who receives a copy of or uses any part of Software from Licensee, with or without changes, also receives the License and the copyright notice included with Software (and if included by the Licensor, patent, trademark, and attribution notice). Licensee must ensure that License is prominently displayed so that any individual or entity seeking to download, copy, use, or otherwise receive any part of Software from Licensee is notified of this License and its terms and conditions. Licensee must cause any modified versions of the Software to carry prominent notices stating that Licensee changed the Software. + +[5.2.](#5.2) _Modified Software_: Licensee is free to create modifications of the Software and distribute only the modified portion created by Licensee, however, any derivative work stemming from the Software or its code must be distributed pursuant to this License, including this Notice provision. + +[5.3.](#5.3) _Recipients as Licensees_: Any individual or entity that uses, copies, modifies, reproduces, distributes, or prepares derivative work based upon the Software, all or part of the Software’s code, or a derivative work developed by using the Software, including a portion of its code, is a Licensee as defined above and is subject to the terms and conditions of this License. + +**[6.](#6) REPRESENTATIONS AND WARRANTIES:** + +[6.1.](#6.1) _Disclaimer of Warranty_: TO THE FULL EXTENT ALLOWED BY LAW, THIS SOFTWARE COMES “AS IS,” WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED, AND LICENSOR SHALL NOT BE LIABLE TO ANY PERSON OR ENTITY FOR ANY DAMAGES OR OTHER LIABILITY ARISING FROM, OUT OF, OR IN CONNECTION WITH THE SOFTWARE OR THIS LICENSE, UNDER ANY LEGAL CLAIM. + +[6.2.](#6.2) _Limitation of Liability_: LICENSEE SHALL HOLD LICENSOR HARMLESS AGAINST ANY AND ALL CLAIMS, DEBTS, DUES, LIABILITIES, LIENS, CAUSES OF ACTION, DEMANDS, OBLIGATIONS, DISPUTES, DAMAGES, LOSSES, EXPENSES, ATTORNEYS' FEES, COSTS, LIABILITIES, AND ALL OTHER CLAIMS OF EVERY KIND AND NATURE WHATSOEVER, WHETHER KNOWN OR UNKNOWN, ANTICIPATED OR UNANTICIPATED, FORESEEN OR UNFORESEEN, ACCRUED OR UNACCRUED, DISCLOSED OR UNDISCLOSED, ARISING OUT OF OR RELATING TO LICENSEE’S USE OF THE SOFTWARE. NOTHING IN THIS SECTION SHOULD BE INTERPRETED TO REQUIRE LICENSEE TO INDEMNIFY LICENSOR, NOR REQUIRE LICENSOR TO INDEMNIFY LICENSEE. + +**[7.](#7) TERMINATION** + +[7.1.](#7.1) _Violations of Ethical Standards or Breaching Duty of Care_: If Licensee violates the Ethical Standards section or Licensee, or any other person or entity within the Supply Chain prior to a good or service reaching the Licensee, breaches its Duty of Care to Supply Chain Impacted Parties, Licensee must remedy the violation or harm caused by Licensee within 30 days of being notified of the violation or harm. If Licensee fails to remedy the violation or harm within 30 days, all rights in the Software granted to Licensee by License will be null and void as between Licensor and Licensee. + +[7.2.](#7.2) _Failure of Notice_: If any person or entity notifies Licensee in writing that Licensee has not complied with the Notice section of this License, Licensee can keep this License by taking all practical steps to comply within 30 days after the notice of noncompliance. If Licensee does not do so, Licensee’s License (and all rights licensed hereunder) will end immediately. + +[7.3.](#7.3) _Judicial Findings_: In the event Licensee is found by a civil, criminal, administrative, or other court of competent jurisdiction, or some other adjudicating body with legal authority, to have committed actions which are in violation of the Ethical Standards or Supply Chain Impacted Party sections of this License, all rights granted to Licensee by this License will terminate immediately. + +[7.4.](#7.4) _Patent Litigation_: If Licensee institutes patent litigation against any entity (including a cross-claim or counterclaim in a suit) alleging that the Software, all or part of the Software’s code, or a derivative work developed using the Software, including a portion of its code, constitutes direct or contributory patent infringement, then any patent license, along with all other rights, granted to Licensee under this License will terminate as of the date such litigation is filed. + +[7.5.](#7.5) _Additional Remedies_: Termination of the License by failing to remedy harms in no way prevents Licensor or Supply Chain Impacted Party from seeking appropriate remedies at law or in equity. + +**[8.](#8) MISCELLANEOUS:** + +[8.1.](#8.1) _Conditions_: Sections 3, 4.1, 5.1, 5.2, 7.1, 7.2, 7.3, and 7.4 are conditions of the rights granted to Licensee in the License. + +[8.2.](#8.2) _Equitable Relief_: Licensor and any Supply Chain Impacted Party shall be entitled to equitable relief, including injunctive relief or specific performance of the terms hereof, in addition to any other remedy to which they are entitled at law or in equity. + +[8.3.](#8.3) _Severability_: If any term or provision of this License is determined to be invalid, illegal, or unenforceable by a court of competent jurisdiction, any such determination of invalidity, illegality, or unenforceability shall not affect any other term or provision of this License or invalidate or render unenforceable such term or provision in any other jurisdiction. If the determination of invalidity, illegality, or unenforceability by a court of competent jurisdiction pertains to the terms or provisions contained in the Ethical Standards section of this License, all rights in the Software granted to Licensee shall be deemed null and void as between Licensor and Licensee. + +[8.4.](#8.4) _Section Titles_: Section titles are solely written for organizational purposes and should not be used to interpret the language within each section. + +[8.5.](#8.5) _Citations_: Citations are solely written to provide context for the source of the provisions in the Ethical Standards. + +[8.6.](#8.6) _Section Summaries_: Some sections have a brief _italicized description_ which is provided for the sole purpose of briefly describing the section and should not be used to interpret the terms of the License. + +[8.7.](#8.7) _Entire License_: This is the entire License between the Licensor and Licensee with respect to the claims released herein and that the consideration stated herein is the only consideration or compensation to be paid or exchanged between them for this License. This License cannot be modified or amended except in a writing signed by Licensor and Licensee. + +[8.8.](#8.8) _Successors and Assigns_: This License shall be binding upon and inure to the benefit of the Licensor’s and Licensee’s respective heirs, successors, and assigns. \ No newline at end of file diff --git a/Package.swift b/Package.swift new file mode 100644 index 0000000..2727677 --- /dev/null +++ b/Package.swift @@ -0,0 +1,21 @@ +// swift-tools-version: 5.9 +// The swift-tools-version declares the minimum version of Swift required to build this package. + +import PackageDescription + +let package = Package( + name: "CircuitBreaker", + platforms: [.iOS(.v15), .macOS(.v13), .watchOS(.v9)], + products: [ + .library( + name: "CircuitBreaker", + targets: ["CircuitBreaker"]), + ], + targets: [ + .target( + name: "CircuitBreaker"), + .testTarget( + name: "CircuitBreakerTests", + dependencies: ["CircuitBreaker"]), + ] +) diff --git a/Sources/CircuitBreaker/CircuitBreaker.swift b/Sources/CircuitBreaker/CircuitBreaker.swift new file mode 100644 index 0000000..9f51231 --- /dev/null +++ b/Sources/CircuitBreaker/CircuitBreaker.swift @@ -0,0 +1,178 @@ +import Foundation + + +/// Global Actor to execute all Circuit Breaker relevant manipulations on +/// This is to guarantee thread safety +@globalActor public actor CircuitBreakerActor: GlobalActor { + public static let shared = CircuitBreakerActor() +} + +/** +CircuitBreaker is a class that implements the circuit breaker pattern to manage the resiliency of network calls +or other unreliable operations. It has three states: Closed, Open, and Half-Open. + +If the circuit is open, it throws an error. If half-open, it allows one attempt and resets if successful, +otherwise opens again. If closed, it attempts normal operation and records failures. + +# Configuration: # +- `name` / `group`: Strings to identify the circuit breaker +- `maxFailures`: The number of failures before the circuit breaker trips to the open state. +- `rollingWindow`: Time in seconds where failures are recorded. +- `recoveryTimeout`: Time in seconds to wait before transitioning from open to half-open state. +- `ErrorStrategy`: can be passed to trip on specific errors e.g. ignore network timeout / connectivity issues +# Example: # +maxFailures = 3, rollingWindow = 5sec, recoveryTimeout = 30sec +If 3 failures happen within 5 seconds the breaker will open and all successive task will fail for the next 30 seconds. +After the recovery time the breaker will be set to half open. +*/ +@CircuitBreakerActor +public final class CircuitBreaker { + + enum State { + case open + case halfopen + case closed + } + + private(set) var state: State = .closed + private var currentFailureTimestamps: [TimeInterval] = [] + private var config: Config + private var lastOpenedTime: TimeInterval? + private var lastError: Error? + private var errorStrategy: ErrorStrategy + private var currentTime: () -> TimeInterval + + public convenience init(config: Config, errorStrategy: ErrorStrategy = DefaultErrorStrategy()) { + self.init(config: config, errorStrategy: errorStrategy) { + Date.timeIntervalSinceReferenceDate + } + } + + init(config: Config, errorStrategy: ErrorStrategy = DefaultErrorStrategy(), currentTime: @escaping () -> TimeInterval ) { + self.config = config + self.errorStrategy = errorStrategy + self.currentTime = currentTime + } + + + /// Executes a the task and updates the circuit state + /// e.g. if task fails it will record a failure according to the configuration and `ErrorStrategy`. + /// On successive failures - once the circuit opens, this will throw a `FastFailError` for + /// a certain time window until the circuit is automatically closed again. + /// - throws: `FastFailError` if circuit is open or any other error from the task + func run(_ task: @escaping () async throws -> T) async throws -> T { + openIfResetTimeoutPassed() + switch state { + case .open: + throw CircuitOpenError(lastError: lastError, name: config.name, group: config.group) + case .closed, .halfopen: + return try await handle(task: task) + } + } + + private func handle(task: @escaping () async throws -> T) async rethrows -> T { + let timeStamp = currentTime() + // TODO: handle timeout + do { + let result = try await task() + if state == .halfopen { + close() + } + return result + } catch { + guard errorStrategy.shouldTrip(on: error) else { + throw error + } + currentFailureTimestamps = Array(currentFailureTimestamps.prefix(config.maxFailures)) + currentFailureTimestamps.append(timeStamp) + if state == .halfopen { + // If failure on half open circuit - break immediately + open() + } else if let timeWindow = currentFailureTimeWindow, + currentFailureTimestamps.count >= config.maxFailures, + timeWindow <= config.rollingWindow { + // Reached maximum number of failures allowed + // in time window before tripping circuit + open() + } + + throw error + } + } + + private var currentFailureTimeWindow: TimeInterval? { + guard let firstTimestamp = currentFailureTimestamps.first, + let lastTimestamp = currentFailureTimestamps.last + else { + return nil + } + + return lastTimestamp - firstTimestamp + } + + private func openIfResetTimeoutPassed() { + guard let lastOpenedTime else { return } + let timeWindow = currentTime() - lastOpenedTime + guard timeWindow >= config.recoveryTimeout else { return } + self.lastOpenedTime = nil + state = .halfopen + } + + public func open() { + state = .open + lastOpenedTime = currentTime() + } + + public func close() { + currentFailureTimestamps.removeAll() + lastOpenedTime = nil + state = .closed + } +} + + +public extension CircuitBreaker { + + struct Config { + /// Name of the service or category of tasks + var name: String + /// Optional group name for debugging purposes + var group: String? + /// Time to wait before transitioning from open to half-open state + var recoveryTimeout: TimeInterval + /// Maximum number of failures allowed within the rolling window + var maxFailures: Int + /// Time period within which failures are counted to trigger the open state + var rollingWindow: TimeInterval + + public init( + name: String, + group: String?, + recoveryTimeout: TimeInterval, + maxFailures: Int, + rollingWindow: TimeInterval + ) { + self.name = name + self.group = group + self.recoveryTimeout = recoveryTimeout + self.maxFailures = max(0, maxFailures) + self.rollingWindow = rollingWindow + } + } + + protocol ErrorStrategy { + func shouldTrip(on error: Error) -> Bool + } + + struct DefaultErrorStrategy: ErrorStrategy { + public func shouldTrip(on error: any Error) -> Bool { true } + public init() {} + } +} + +/// Error thrown if the circuit breaker is open +struct CircuitOpenError: Error { + let lastError: Error? + let name: String + let group: String? +} diff --git a/Sources/CircuitBreaker/CircuitBreakerGroup.swift b/Sources/CircuitBreaker/CircuitBreakerGroup.swift new file mode 100644 index 0000000..d5ad26d --- /dev/null +++ b/Sources/CircuitBreaker/CircuitBreakerGroup.swift @@ -0,0 +1,55 @@ +import Foundation + +@CircuitBreakerActor +public final class CircuitBreakerGroup { + + let name: String + let baseConfig: CircuitBreaker.Config + private var collection: [Key : CircuitBreaker] = [:] + private var purgeTask: Task? + + init(name: String, baseConfig: CircuitBreaker.Config) { + self.name = name + self.baseConfig = baseConfig + } + + func run(key: Key, task: @escaping () async throws -> T) async throws -> T { + let breaker = getBreaker(key: key) + let result = try await breaker.run(task) + + return result + } + + private func getBreaker(key: Key) -> CircuitBreaker { + let circuitBreaker: CircuitBreaker + + if let breaker = collection[key] { + circuitBreaker = breaker + } else { + circuitBreaker = newCircuitBreaker(for: key) + collection[key] = circuitBreaker + } + + return circuitBreaker + } + + private func newCircuitBreaker(for key: Key) -> CircuitBreaker { + var config = baseConfig + config.group = name + config.name = key.description + let breaker = CircuitBreaker(config: config) + return breaker + } + + private func purge() { + + } + + public func openAll() { + collection.values.forEach { $0.open() } + } + + public func closeAll() { + collection.values.forEach { $0.close() } + } +} diff --git a/Tests/CircuitBreakerTests/CircuitBreakerTests.swift b/Tests/CircuitBreakerTests/CircuitBreakerTests.swift new file mode 100644 index 0000000..439a184 --- /dev/null +++ b/Tests/CircuitBreakerTests/CircuitBreakerTests.swift @@ -0,0 +1,482 @@ +import XCTest +import Combine +@testable import CircuitBreaker + + +final class CircuitBreakerTests: XCTestCase { + + var config: CircuitBreaker.Config = .init( + name: "test", + group: "xctests", + recoveryTimeout: 30, + maxFailures: 5, + rollingWindow: 15 + ) + + var shortTripConfig: CircuitBreaker.Config = .init( + name: "test", + group: "xctests", + recoveryTimeout: 20, + maxFailures: 3, + rollingWindow: 10 + ) + + override func setUp() async throws { + try await super.setUp() + } + + override func tearDown() async throws { + try await super.tearDown() + } + + func testClosedStateOneTask() async throws { + + // Given + var currentTime: TimeInterval = 0 + let sut = await CircuitBreaker(config: config) { + currentTime + } + + // When + let results = await runTasks( + on: sut, + currentTime: ¤tTime, + results: [ + (.success("ok"), 1), + ] + ) + + // Then + XCTAssertEqual(results, [.success("ok")]) + } + + // test trip on x failure in n time + + func testClosedStateManyTasksNotOpen() async throws { + + // Given + var currentTime: TimeInterval = 0 + let sut = await CircuitBreaker(config: config) { + currentTime + } + + // When + let results = await runTasks( + on: sut, + currentTime: ¤tTime, + results: [ + (.success("0"), 1), + (.success("1"), 1), + (.success("2"), 1), + (.success("3"), 1), + (.success("4"), 1), + (.success("5"), 1), + ] + ) + + // Then + XCTAssertEqual(results, [ + .success("0"), + .success("1"), + .success("2"), + .success("3"), + .success("4"), + .success("5"), + ] + ) + } + + func testOpenOnAllFailuresWithinWindow() async throws { + + // Given + var currentTime: TimeInterval = 0 + let sut = await CircuitBreaker(config: config) { + return currentTime + } + + // When + let results = await runTasks( + on: sut, + currentTime: ¤tTime, + results: [ + (.failure(SubTaskError()), 1), + (.failure(SubTaskError()), 1), + (.failure(SubTaskError()), 1), + (.failure(SubTaskError()), 1), + (.failure(SubTaskError()), 1), + (.failure(SubTaskError()), 1), + ] + ) + + + // Then + XCTAssertEqual(results, [ + .failure(SubTaskError().equatable), + .failure(SubTaskError().equatable), + .failure(SubTaskError().equatable), + .failure(SubTaskError().equatable), + .failure(SubTaskError().equatable), + .failure(CircuitOpenError(lastError: nil, name: "", group: nil).equatable), + ] + ) + } + + func testOpenOnSomeFailuresWithinWindow() async throws { + + // Given + var currentTime: TimeInterval = 0 + let sut = await CircuitBreaker(config: config) { + return currentTime + } + + // When + let results = await runTasks( + on: sut, + currentTime: ¤tTime, + results: [ + (.failure(SubTaskError()), 1), + (.failure(SubTaskError()), 1), + (.success("ok"), 1), + (.failure(SubTaskError()), 1), + (.success("ok"), 1), + (.failure(SubTaskError()), 1), + (.failure(SubTaskError()), 1), // Trips + (.success("ok"), 1), + ] + ) + + + // Then + XCTAssertEqual(results, [ + .failure(SubTaskError().equatable), + .failure(SubTaskError().equatable), + .success("ok"), + .failure(SubTaskError().equatable), + .success("ok"), + .failure(SubTaskError().equatable), + .failure(SubTaskError().equatable), + .failure(CircuitOpenError(lastError: nil, name: "", group: nil).equatable), + ] + ) + } + + + func testNotOpenOnManyFailuresOutsideWindow() async throws { + + // Given + var currentTime: TimeInterval = 0 + let sut = await CircuitBreaker(config: config) { + return currentTime + } + + // When + let delayToTripOn6th = 3.75 + let results = await runTasks( + on: sut, + currentTime: ¤tTime, + results: [ + (.failure(SubTaskError()), delayToTripOn6th), + (.failure(SubTaskError()), delayToTripOn6th), + (.failure(SubTaskError()), delayToTripOn6th), + (.failure(SubTaskError()), delayToTripOn6th), + (.failure(SubTaskError()), delayToTripOn6th), + (.failure(SubTaskError()), delayToTripOn6th), + ] + ) + + // Then + XCTAssertEqual(results, [ + .failure(SubTaskError().equatable), + .failure(SubTaskError().equatable), + .failure(SubTaskError().equatable), + .failure(SubTaskError().equatable), + .failure(SubTaskError().equatable), + .failure(CircuitOpenError(lastError: nil, name: "", group: nil).equatable), + ] + ) + } + + func testOpenThenRecoverToHalfOpenThenOpen() async throws { + + // Given + var currentTime: TimeInterval = 0 + let sut = await CircuitBreaker(config: shortTripConfig) { + return currentTime + } + + // When + let results = await runTasks( + on: sut, + currentTime: ¤tTime, + results: [ + (.failure(SubTaskError()), 1), + (.failure(SubTaskError()), 1), + (.failure(SubTaskError()), 1), // Trips + (.success("ok"), 20), // Recover + (.failure(SubTaskError()), 1), // Trips again + (.success("ok"), 1), + ] + ) + + // Then + XCTAssertEqual(results, [ + .failure(SubTaskError().equatable), + .failure(SubTaskError().equatable), + .failure(SubTaskError().equatable), + .failure(CircuitOpenError(lastError: nil, name: "", group: nil).equatable), + .failure(SubTaskError().equatable), + .failure(CircuitOpenError(lastError: nil, name: "", group: nil).equatable), + ]) + } + + func testOpenThenRecoverToHalfOpenThenClose() async throws { + + // Given + var currentTime: TimeInterval = 0 + let sut = await CircuitBreaker(config: shortTripConfig) { + return currentTime + } + + // When + let results = await runTasks( + on: sut, + currentTime: ¤tTime, + results: [ + (.failure(SubTaskError()), 1), + (.failure(SubTaskError()), 1), + (.failure(SubTaskError()), 1), // Trips + (.success("ok"), 20), // Recover + (.success("ok"), 1), + ] + ) + + // Then + XCTAssertEqual(results, [ + .failure(SubTaskError().equatable), + .failure(SubTaskError().equatable), + .failure(SubTaskError().equatable), + .failure(CircuitOpenError(lastError: nil, name: "", group: nil).equatable), + .success("ok"), + ]) + } + + + func testCancelTasksNotOpen() async throws { + + // Given + let currentTime: TimeInterval = 0 + let sut = await CircuitBreaker(config: shortTripConfig) { + return currentTime + } + + // When + let (t1, t1r) = await submitTask(to: sut, result: .failure(SubTaskError())) + let (t2, t2r) = await submitTask(to: sut, result: .failure(SubTaskError())) + let (t3, t3r) = await submitTask(to: sut, result: .failure(SubTaskError())) + let (t4, t4r) = await submitTask(to: sut, result: .success("ok")) + + await t1.cancel() + await t2.cancel() + await t3.cancel() + await t4.unblock() + + let breakerResults = [ + await t1r.result.mapError(\.equatable), + await t2r.result.mapError(\.equatable), + await t3r.result.mapError(\.equatable), + await t4r.result.mapError(\.equatable), + ] + + // Then + XCTAssertEqual(breakerResults, [ + .failure(CancellationError().equatable), + .failure(CancellationError().equatable), + .failure(CancellationError().equatable), + .success("ok"), + ]) + } + + // test error strategy + + func testErrorStrategyNotOpen() async throws { + + // Given + struct CustomErrorStrategy: CircuitBreaker.ErrorStrategy { + func shouldTrip(on error: any Error) -> Bool { + guard error is SubTaskError else { + return true + } + + return false + } + } + + var currentTime: TimeInterval = 0 + let sut = await CircuitBreaker(config: shortTripConfig, errorStrategy: CustomErrorStrategy()) { + return currentTime + } + + // When + let results = await runTasks( + on: sut, + currentTime: ¤tTime, + results: [ + (.failure(SubTaskError()), 1), + (.failure(SubTaskError()), 1), + (.failure(SubTaskError()), 1), + (.failure(SubTaskError()), 1), + (.success("ok"), 1), + ] + ) + + // Then + XCTAssertEqual(results, [ + .failure(SubTaskError().equatable), + .failure(SubTaskError().equatable), + .failure(SubTaskError().equatable), + .failure(SubTaskError().equatable), + .success("ok"), + ]) + } + + func testPerformanceAndMemory() throws { + throw XCTSkip("Run on device only") + + self.measure(metrics: [XCTMemoryMetric(), XCTClockMetric()]) { + let exp = expectation(description: "Finished") + Task { + var currentTime: TimeInterval = 0 + let sut = await CircuitBreaker(config: config) { + currentTime + } + + for i in (0..<100_000) { + let (t, r) = await submitTask(to: sut, result: .success("\(i)")) + await t.unblock() + _ = await r.result + currentTime += 1.0 + } + exp.fulfill() + } + wait(for: [exp], timeout: 200.0) + } + } + + private struct SubTaskError: CustomNSError { + static let errorDomain = "SubTaskError" + } +} + +private func runTasks( + on breaker: CircuitBreaker, + currentTime: inout TimeInterval, + results: [(mockResult: Result, delay: TimeInterval)] +) async -> [Result] { + + var breakerResults: [Result] = [] + + for (result, delay) in results { + let (t1, t1r) = await submitTask(to: breaker, result: result) + await t1.unblock() + + breakerResults.append( + await t1r.result.mapError(\.equatable) + ) + + currentTime += delay + } + + return breakerResults +} + +private func submitTask( + to breaker: CircuitBreaker, + result: Result +) async -> (subTask: BarrierTask, breakerResultTask: Task) { + + let barrierTask = await BarrierTask(result: result) + let resultTask = Task { + do { + return try await breaker.run { + try await barrierTask.run() + } + } catch { + throw error + } + } + + return (barrierTask, resultTask) +} + + +extension Result: CustomDebugStringConvertible where + Success: CustomDebugStringConvertible, + Failure: CustomDebugStringConvertible +{ + + public var debugDescription: String { + switch self { + case let .success(value): "success(\(value.debugDescription))" + case let .failure(error): "failure(\(error.debugDescription))" + } + } +} + +private actor BarrierTask where Success: Sendable { + + private var task: Task! + private var isBlocked: Bool = true + + init(result: Result) async { + self.task = Task { + while isBlocked { + try Task.checkCancellation() + await Task.yield() + } + return try result.get() + } + } + + func run() async throws -> Success { + try await task.result.get() + } + + func unblock() async { + isBlocked = false + } + + func cancel() async { + task.cancel() + _ = try? await task.result.get() + } +} + +private struct EquatableError: Error, Equatable, CustomDebugStringConvertible { + let error: Error + let equalTo: (Error) -> Bool + + init(_ error: Error) { + self.error = error + self.equalTo = { other in + let lhs = other as NSError + let rhs = error as NSError + return (lhs.domain, lhs.code) == (rhs.domain, rhs.code) + } + } + + var debugDescription: String { + let nsError = error as NSError + return "\(nsError.domain):\(nsError.code)" + } + + static func == (lhs: Self, rhs: Self) -> Bool { + lhs.equalTo(rhs.error) + } + +} + +private extension Error { + var equatable: EquatableError { + EquatableError(self) + } +}