From f3e2b9897e4ef68541d585b165f46aa4beb52bdd Mon Sep 17 00:00:00 2001 From: smodi Date: Thu, 5 Dec 2024 15:13:21 -0800 Subject: [PATCH] Updating unit tests --- .../ContentBlockingRulesCompilationTask.swift | 13 +++------- ...rRulesManagerInitialCompilationTests.swift | 14 +++++----- .../ContentBlockerRulesManagerTests.swift | 26 ++++++++----------- 3 files changed, 20 insertions(+), 33 deletions(-) diff --git a/Sources/BrowserServicesKit/ContentBlocking/ContentBlockingRulesCompilationTask.swift b/Sources/BrowserServicesKit/ContentBlocking/ContentBlockingRulesCompilationTask.swift index b1c7e4763..3755b623c 100644 --- a/Sources/BrowserServicesKit/ContentBlocking/ContentBlockingRulesCompilationTask.swift +++ b/Sources/BrowserServicesKit/ContentBlocking/ContentBlockingRulesCompilationTask.swift @@ -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 - } } enum ResultType { @@ -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 = [ @@ -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) } } diff --git a/Tests/BrowserServicesKitTests/ContentBlocker/ContentBlockerRulesManagerInitialCompilationTests.swift b/Tests/BrowserServicesKitTests/ContentBlocker/ContentBlockerRulesManagerInitialCompilationTests.swift index 455734d9b..59e6df6cf 100644 --- a/Tests/BrowserServicesKitTests/ContentBlocker/ContentBlockerRulesManagerInitialCompilationTests.swift +++ b/Tests/BrowserServicesKitTests/ContentBlocker/ContentBlockerRulesManagerInitialCompilationTests.swift @@ -62,8 +62,8 @@ final class ContentBlockerRulesManagerInitialCompilationTests: XCTestCase { let errorHandler = EventMapping { 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)") } @@ -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)") } @@ -211,10 +209,10 @@ final class ContentBlockerRulesManagerInitialCompilationTests: XCTestCase { let errorHandler = EventMapping { 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)") } @@ -271,8 +269,8 @@ final class ContentBlockerRulesManagerInitialCompilationTests: XCTestCase { let errorHandler = EventMapping { 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)") } diff --git a/Tests/BrowserServicesKitTests/ContentBlocker/ContentBlockerRulesManagerTests.swift b/Tests/BrowserServicesKitTests/ContentBlocker/ContentBlockerRulesManagerTests.swift index 15321e1ba..ec662566c 100644 --- a/Tests/BrowserServicesKitTests/ContentBlocker/ContentBlockerRulesManagerTests.swift +++ b/Tests/BrowserServicesKitTests/ContentBlocker/ContentBlockerRulesManagerTests.swift @@ -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 { event, _, params, _ in if case .contentBlockingCompilationFailed(let listName, let component) = event { XCTAssertEqual(listName, DefaultContentBlockerRulesListsSource.Constants.trackerDataSetRulesListName) @@ -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 { + XCTAssertEqual(retryCount, 0) } else { XCTFail("Unexpected event: \(event)") } @@ -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) @@ -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 { @@ -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") @@ -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)") } @@ -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") @@ -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 { + XCTAssertEqual(retryCount, 3) } else if case .contentBlockingLRCMissing = event { lookupAndFetchExp.fulfill() } else