Skip to content

Commit

Permalink
Updating unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
studiosutara committed Dec 5, 2024
1 parent 02702a6 commit f3e2b98
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,6 @@ extension ContentBlockerRulesManager {
struct PerformanceInfo {
let compilationTime: TimeInterval
let iterationCount: Int

// if none of the sources are broken, we do a minimum of one iteration which should be successful
init(compilationTime: TimeInterval, iterationCount: Int = 1) {
self.compilationTime = compilationTime
self.iterationCount = iterationCount
}
}

Check failure on line 37 in Sources/BrowserServicesKit/ContentBlocking/ContentBlockingRulesCompilationTask.swift

View workflow job for this annotation

GitHub Actions / Run SwiftLint

Lines should not have trailing whitespace (trailing_whitespace)
enum ResultType {
Expand Down Expand Up @@ -200,8 +194,8 @@ extension ContentBlockerRulesManager {

func getCompilationRetryCount() -> Int {
guard let brokenSources = sourceManager.brokenSources else {
// if none of the sources are broken, we do a minimum of one iteration which should be successful
return 1
// if none of the sources are broken, we do not do any retries
return 0
}

let identifiers = [
Expand All @@ -211,8 +205,7 @@ extension ContentBlockerRulesManager {
brokenSources.tdsIdentifier
]

// add 1 to account for the first iteration before we retry with any broken sources
return (identifiers.compactMap { $0 }.count) + 1
return (identifiers.compactMap { $0 }.count)
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ final class ContentBlockerRulesManagerInitialCompilationTests: XCTestCase {
let errorHandler = EventMapping<ContentBlockerDebugEvents> { event, _, params, _ in
if case .contentBlockingLRCMissing = event {
lookupAndFetchExp.fulfill()
} else if case .contentBlockingCompilationTime = event {
XCTAssertNotNil(params?["compilationTime"])
} else if case .contentBlockingCompilationTaskPerformance(let retryCount, _) = event {
XCTAssertEqual(retryCount, 0)
} else {
XCTFail("Unexpected event: \(event)")
}
Expand Down Expand Up @@ -123,8 +123,6 @@ final class ContentBlockerRulesManagerInitialCompilationTests: XCTestCase {
XCTFail("Should not fetch LRC")
} else if case .contentBlockingLookupRulesSucceeded = event {
lookupAndFetchExp.fulfill()
} else if case .contentBlockingCompilationTime = event {
XCTAssertNotNil(params?["compilationTime"])
} else {
XCTFail("Unexpected event: \(event)")
}
Expand Down Expand Up @@ -211,10 +209,10 @@ final class ContentBlockerRulesManagerInitialCompilationTests: XCTestCase {
let errorHandler = EventMapping<ContentBlockerDebugEvents> { event, _, params, _ in
if case .contentBlockingFetchLRCSucceeded = event {
XCTFail("Should not fetch LRC")
} else if case .contentBlockingCompilationTaskPerformance(let retryCount, _) = event {
XCTAssertEqual(retryCount, 0)
} else if case .contentBlockingNoMatchInLRC = event {
lookupAndFetchExp.fulfill()
} else if case .contentBlockingCompilationTime = event {
XCTAssertNotNil(params?["compilationTime"])
} else {
XCTFail("Unexpected event: \(event)")
}
Expand Down Expand Up @@ -271,8 +269,8 @@ final class ContentBlockerRulesManagerInitialCompilationTests: XCTestCase {
let errorHandler = EventMapping<ContentBlockerDebugEvents> { event, _, params, _ in
if case .contentBlockingFetchLRCSucceeded = event {
lookupAndFetchExp.fulfill()
} else if case .contentBlockingCompilationTime = event {
XCTAssertNotNil(params?["compilationTime"])
} else if case .contentBlockingCompilationTaskPerformance(let retryCount, _) = event {
XCTAssertEqual(retryCount, 0)
} else {
XCTFail("Unexpected event: \(event)")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,6 @@ class ContentBlockerRulesManagerLoadingTests: ContentBlockerRulesManagerTests {
errorExp.isInverted = true

let lookupAndFetchExp = expectation(description: "Look and Fetch rules failed")
let compilationTimeExp = expectation(description: "Compilation Time reported")
let errorHandler = EventMapping<ContentBlockerDebugEvents> { event, _, params, _ in
if case .contentBlockingCompilationFailed(let listName, let component) = event {
XCTAssertEqual(listName, DefaultContentBlockerRulesListsSource.Constants.trackerDataSetRulesListName)
Expand All @@ -216,11 +215,10 @@ class ContentBlockerRulesManagerLoadingTests: ContentBlockerRulesManagerTests {
XCTFail("Unexpected component: \(component)")
}

} else if case .contentBlockingCompilationTime = event {
XCTAssertNotNil(params?["compilationTime"])
compilationTimeExp.fulfill()
} else if case .contentBlockingLRCMissing = event {
lookupAndFetchExp.fulfill()
} else if case .contentBlockingCompilationTaskPerformance(let retryCount, _) = event {

Check failure on line 220 in Tests/BrowserServicesKitTests/ContentBlocker/ContentBlockerRulesManagerTests.swift

View workflow job for this annotation

GitHub Actions / Run SwiftLint

Else and catch should be on the same line, one space after the previous declaration (statement_position)
XCTAssertEqual(retryCount, 0)
} else {
XCTFail("Unexpected event: \(event)")
}
Expand All @@ -231,7 +229,7 @@ class ContentBlockerRulesManagerLoadingTests: ContentBlockerRulesManagerTests {
updateListener: rulesUpdateListener,
errorReporting: errorHandler)

wait(for: [exp, errorExp, compilationTimeExp, lookupAndFetchExp], timeout: 15.0)
wait(for: [exp, errorExp, lookupAndFetchExp], timeout: 15.0)

XCTAssertNotNil(cbrm.currentRules)
XCTAssertEqual(cbrm.currentRules.first?.etag, mockRulesSource.trackerData?.etag)
Expand Down Expand Up @@ -270,8 +268,8 @@ class ContentBlockerRulesManagerLoadingTests: ContentBlockerRulesManagerTests {
XCTFail("Unexpected component: \(component)")
}

} else if case .contentBlockingCompilationTime = event {
XCTAssertNotNil(params?["compilationTime"])
} else if case .contentBlockingCompilationTaskPerformance(let retryCount, _) = event {
XCTAssertEqual(retryCount, 1)
} else if case .contentBlockingLRCMissing = event {
lookupAndFetchExp.fulfill()
} else {
Expand Down Expand Up @@ -546,7 +544,7 @@ class ContentBlockerRulesManagerLoadingTests: ContentBlockerRulesManagerTests {
}

let errorExp = expectation(description: "Error reported")
errorExp.expectedFulfillmentCount = 5
errorExp.expectedFulfillmentCount = 4

let lookupAndFetchExp = expectation(description: "Look and Fetch rules failed")

Expand All @@ -562,11 +560,10 @@ class ContentBlockerRulesManagerLoadingTests: ContentBlockerRulesManagerTests {
XCTFail("Unexpected component: \(component)")
}

} else if case .contentBlockingCompilationTime = event {
XCTAssertNotNil(params?["compilationTime"])
errorExp.fulfill()
} else if case .contentBlockingLRCMissing = event {
lookupAndFetchExp.fulfill()
} else if case .contentBlockingCompilationTaskPerformance(let retryCount, _) = event {
XCTAssertEqual(retryCount, 4)
} else {
XCTFail("Unexpected event: \(event)")
}
Expand Down Expand Up @@ -631,7 +628,7 @@ class ContentBlockerRulesManagerLoadingTests: ContentBlockerRulesManagerTests {
}

let errorExp = expectation(description: "Error reported")
errorExp.expectedFulfillmentCount = 4
errorExp.expectedFulfillmentCount = 3

let lookupAndFetchExp = expectation(description: "Look and Fetch rules failed")

Expand All @@ -647,9 +644,8 @@ class ContentBlockerRulesManagerLoadingTests: ContentBlockerRulesManagerTests {
XCTFail("Unexpected component: \(component)")
}

} else if case .contentBlockingCompilationTime = event {
XCTAssertNotNil(params?["compilationTime"])
errorExp.fulfill()
}else if case .contentBlockingCompilationTaskPerformance(let retryCount, _) = event {

Check failure on line 647 in Tests/BrowserServicesKitTests/ContentBlocker/ContentBlockerRulesManagerTests.swift

View workflow job for this annotation

GitHub Actions / Run SwiftLint

Else and catch should be on the same line, one space after the previous declaration (statement_position)
XCTAssertEqual(retryCount, 3)
} else if case .contentBlockingLRCMissing = event {
lookupAndFetchExp.fulfill()
} else
Expand Down

0 comments on commit f3e2b98

Please sign in to comment.