Skip to content

Commit

Permalink
Merge pull request #98 from Infomaniak/fixPriorityInversion
Browse files Browse the repository at this point in the history
fix: Priority inversion could happen with TolerantDispatchGroup
  • Loading branch information
valentinperignon committed Jan 29, 2024
2 parents ae60c32 + 280312a commit 32b0630
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ import CocoaLumberjackSwift
import Foundation

public final class TolerantDispatchGroup {
private let syncQueue = DispatchQueue(label: "com.infomaniak.TolerantDispatchGroup")
let syncQueue: DispatchQueue
private let dispatchGroup = DispatchGroup()
private var callBalancer = 0

public init() {
// Meta: Keep Sonar Cloud happy
public init(qos: DispatchQoS = .default) {
syncQueue = DispatchQueue(label: "com.infomaniak.TolerantDispatchGroup", qos: qos)
}

public func enter() {
Expand Down
32 changes: 31 additions & 1 deletion Tests/InfomaniakCoreTests/UTTolerantDispatchGroup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,15 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import InfomaniakCore
@testable import InfomaniakCore
import XCTest

extension DispatchQoS: CaseIterable {
public static var allCases: [DispatchQoS] {
[.background, .utility, .default, .userInitiated, .userInteractive, .unspecified]
}
}

final class UTTolerantDispatchGroup: XCTestCase {
func testCanInit() {
// WHEN
Expand All @@ -27,4 +33,28 @@ final class UTTolerantDispatchGroup: XCTestCase {
// THEN
XCTAssertNotNil(dispatchGroup)
}

func testPriorityDefault() {
// WHEN
let dispatchGroup = TolerantDispatchGroup()

// THEN
XCTAssertNotNil(dispatchGroup)
XCTAssertEqual(dispatchGroup.syncQueue.qos, DispatchQoS.default, "default constructor should have default priority")
}

func testPriorityAnyIsSet() {
// GIVEN
guard let expectedQoS = DispatchQoS.allCases.randomElement() else {
XCTFail("unexpected")
return
}

// WHEN
let dispatchGroup = TolerantDispatchGroup(qos: expectedQoS)

// THEN
XCTAssertNotNil(dispatchGroup)
XCTAssertEqual(dispatchGroup.syncQueue.qos, expectedQoS, "QoS should match")
}
}

0 comments on commit 32b0630

Please sign in to comment.