From 959e72296f251595b7ece58460efccc795a4b282 Mon Sep 17 00:00:00 2001 From: Diogo Balseiro Date: Thu, 1 Oct 2020 11:29:19 +0100 Subject: [PATCH 1/4] Improved Export Flows Performance --- FNMNetworkMonitor.podspec | 2 +- .../Classes/Exporter/FNMRecordExporter.swift | 80 +++++++++++++------ ...FNMHTTPRequestRecordCodableContainer.swift | 43 ++-------- .../Classes/FNMNetworkMonitor.swift | 16 +--- README.md | 8 +- Tests/NetworkMonitorAppLaunchTests.swift | 3 +- Tests/NetworkMonitorDebugUITests.swift | 4 +- Tests/NetworkMonitorFlowTests.swift | 8 +- 8 files changed, 79 insertions(+), 85 deletions(-) diff --git a/FNMNetworkMonitor.podspec b/FNMNetworkMonitor.podspec index b749c92..6308be9 100755 --- a/FNMNetworkMonitor.podspec +++ b/FNMNetworkMonitor.podspec @@ -1,7 +1,7 @@ Pod::Spec.new do |spec| spec.name = 'FNMNetworkMonitor' spec.module_name = 'FNMNetworkMonitor' - spec.version = '9.2.0' + spec.version = '10.0.0' spec.summary = 'A network monitor' spec.homepage = 'https://github.com/Farfetch/network-monitor-ios' spec.license = 'MIT' diff --git a/NetworkMonitor/Classes/Exporter/FNMRecordExporter.swift b/NetworkMonitor/Classes/Exporter/FNMRecordExporter.swift index 1f388e9..18f82fe 100644 --- a/NetworkMonitor/Classes/Exporter/FNMRecordExporter.swift +++ b/NetworkMonitor/Classes/Exporter/FNMRecordExporter.swift @@ -8,42 +8,75 @@ import Foundation -public enum FNMRecordExporterSortOption: String { +public enum FNMRecordExporterPreference { - case sortedStartTimestamp = "sorted-start-timestamp" - case sortedAlphabetically = "sorted-alphabetically" - case sortedSlowest = "sorted-slowest" + case off + case on(setting: FNMRecordExporterPreferenceSetting) + + public enum FNMRecordExporterPreferenceSetting { + + case unlimited + case first(numberOfRecords: Int) + case last(numberOfRecords: Int) + } } struct FNMRecordExporter { + static var requestRecordExportQueued: Bool = false + static func export(_ requestRecords: [FNMHTTPRequestRecord], - option: FNMRecordExporterSortOption) { + preference: FNMRecordExporterPreference) { - DispatchQueue.global().async { + if case let FNMRecordExporterPreference.on(setting) = preference { - do { + NSLog("🌀 Export requested, \(requestRecords.count)") + if self.requestRecordExportQueued == false { + NSLog("🌀🌀 Export queued") + self.requestRecordExportQueued = true - let recordsFilenameURL = try self.recordsFilenameURL(option: option) + DispatchQueue.global().asyncAfter(deadline: .now() + Constants.exportDebounceDelay, + execute: { - let serializableObject = FNMHTTPRequestRecordCodableContainer(records: requestRecords, - option: option) + do { + NSLog("🌀🌀🌀 Export executing") + let recordsFilenameURL = try self.recordsFilenameURL() + let requestRecordsToProcess: [FNMHTTPRequestRecord] - try self.encodeObject(serializableObject, - fileUrl: recordsFilenameURL) + switch setting { - FNMRecordExporter.log(message: "Exported Request Records Using Option '\(option)'") + case .unlimited: + requestRecordsToProcess = requestRecords - } catch { + case .first(let numberOfRecords): + requestRecordsToProcess = Array(requestRecords.prefix(numberOfRecords)) - assertionFailure("Failed to export, please advise") + case .last(let numberOfRecords): + requestRecordsToProcess = Array(requestRecords.suffix(numberOfRecords)) + } + + let serializableObject = FNMHTTPRequestRecordCodableContainer(records: requestRecordsToProcess) + + try self.encodeObject(serializableObject, + fileUrl: recordsFilenameURL) + + FNMRecordExporter.log(message: "Exported Request Records Using Setting '\(setting)'") + + self.requestRecordExportQueued = false + NSLog("🌀🌀🌀🌀 Export executed, \(requestRecordsToProcess.count)") + + } catch { + + assertionFailure("Failed to export, please advise") + } + }) } } } static func exportRecord(_ record: FNMRecord, - requestRecords: [FNMHTTPRequestRecord], - overallRecords: Bool = false) { + requestRecords: [FNMHTTPRequestRecord], + overallRecords: Bool = false) { DispatchQueue.global().async { @@ -86,14 +119,13 @@ extension FNMRecordExporter { return self.currentRunConfigurationFilenameURL(path: currentRunFolderPath) } - static func recordsFilenameURL(option: FNMRecordExporterSortOption) throws -> URL { + static func recordsFilenameURL() throws -> URL { let folderPath = try self.relativeRecordsFolder(title: self.folderBaseName()) _ = try self.createFolder(at: folderPath) - return self.recordsFilenameURL(path: folderPath, - option: option) + return self.recordsFilenameURL(path: folderPath) } private static func log(message: String) { @@ -152,14 +184,13 @@ extension FNMRecordExporter { } } - private static func recordsFilenameURL(path: URL, - option: FNMRecordExporterSortOption) -> URL { + private static func recordsFilenameURL(path: URL) -> URL { var filePath = path filePath.appendPathComponent(Constants.exportRecordsSimpleFilename + Constants.exportFileSeparator + - option.rawValue + + Constants.exportRecordsFilename + Constants.exportFileExtension) return filePath @@ -213,9 +244,12 @@ private extension FNMRecordExporter { static let exportGeneralFolderPath = "AppLaunch" static let exportCurrentRunFolderPath = "CurrentRun" static let exportRecordsGeneralFolderPath = "Records" + static let exportRecordsFilename = "sorted-start-timestamp" static let exportCurrentRunFilename = "configuration" static let exportRecordsSimpleFilename = "records" static let exportFileExtension = ".json" static let exportFileSeparator = "-" + + static let exportDebounceDelay = 1.5 } } diff --git a/NetworkMonitor/Classes/Exporter/RequestRecord/FNMHTTPRequestRecordCodableContainer.swift b/NetworkMonitor/Classes/Exporter/RequestRecord/FNMHTTPRequestRecordCodableContainer.swift index d2f09fc..0ec0d89 100644 --- a/NetworkMonitor/Classes/Exporter/RequestRecord/FNMHTTPRequestRecordCodableContainer.swift +++ b/NetworkMonitor/Classes/Exporter/RequestRecord/FNMHTTPRequestRecordCodableContainer.swift @@ -12,11 +12,9 @@ struct FNMHTTPRequestRecordCodableContainer { let records: [FNMHTTPRequestRecord] - init(records: [FNMHTTPRequestRecord], - option: FNMRecordExporterSortOption) { + init(records: [FNMHTTPRequestRecord]) { - self.records = type(of: self).sorted(records: records, - option: option) + self.records = type(of: self).sorted(records: records) } } @@ -41,42 +39,11 @@ private extension FNMHTTPRequestRecordCodableContainer { private extension FNMHTTPRequestRecordCodableContainer { - static func sorted(records: [FNMHTTPRequestRecord], - option: FNMRecordExporterSortOption) -> [FNMHTTPRequestRecord] { + static func sorted(records: [FNMHTTPRequestRecord]) -> [FNMHTTPRequestRecord] { - switch option { - case .sortedAlphabetically: + return records.sorted { - return records.sorted { - - guard let recordAAbsoluteURL = $0.request.url?.absoluteString, - let recordAScheme = $0.request.url?.scheme, - let recordBAbsoluteURL = $1.request.url?.absoluteString, - let recordBScheme = $1.request.url?.scheme else { return false } - - // The scheme is not relevant for this sort - let recordATrimmedURL = recordAAbsoluteURL.replacingOccurrences(of: recordAScheme, with: "") - let recordBTrimmedURL = recordBAbsoluteURL.replacingOccurrences(of: recordBScheme, with: "") - - return recordATrimmedURL.compare(recordBTrimmedURL) == .orderedAscending - } - - case .sortedSlowest: - - return records.sorted { - - guard let recordATimeSpent = $0.timeSpent, - let recordBTimeSpent = $1.timeSpent else { return false } - - return recordATimeSpent > recordBTimeSpent - } - - case .sortedStartTimestamp: - - return records.sorted { - - return $0.startTimestamp.timeIntervalSince1970 < $1.startTimestamp.timeIntervalSince1970 - } + return $0.startTimestamp.timeIntervalSince1970 < $1.startTimestamp.timeIntervalSince1970 } } diff --git a/NetworkMonitor/Classes/FNMNetworkMonitor.swift b/NetworkMonitor/Classes/FNMNetworkMonitor.swift index 012812b..c56b565 100644 --- a/NetworkMonitor/Classes/FNMNetworkMonitor.swift +++ b/NetworkMonitor/Classes/FNMNetworkMonitor.swift @@ -31,7 +31,7 @@ public final class FNMNetworkMonitor: NSObject { public var logScope: FNMLogScope? /// Whether the passive export is enabled - public var passiveExport = false + public var passiveExportPreference: FNMRecordExporterPreference = .off /// The current collection of records recorded so far @objc @@ -55,9 +55,7 @@ public final class FNMNetworkMonitor: NSObject { /// Start monitoring the network @objc - public func startMonitoring(passiveExport: Bool = false) { - - self.passiveExport = passiveExport + public func startMonitoring() { self.subscribe(observer: self) @@ -176,13 +174,7 @@ public final class FNMNetworkMonitor: NSObject { let localRecordsSequence = Array(localRecords.values) FNMRecordExporter.export(localRecordsSequence, - option: .sortedAlphabetically) - - FNMRecordExporter.export(localRecordsSequence, - option: .sortedStartTimestamp) - - FNMRecordExporter.export(localRecordsSequence, - option: .sortedSlowest) + preference: self.passiveExportPreference) } } @@ -324,7 +316,7 @@ extension FNMNetworkMonitor: FNMNetworkMonitorObserver { public func recordsUpdated(records: [FNMHTTPRequestRecord]) { - guard self.passiveExport, + guard case FNMRecordExporterPreference.on(_) = self.passiveExportPreference, records.filter({ $0.conclusion == nil }).count == 0 else { return } self.exportRecordData() diff --git a/README.md b/README.md index 83e2a5b..ba62c7a 100755 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ Preview: ```swift FNMNetworkMonitor.registerToLoadingSystem() -FNMNetworkMonitor.shared.startMonitoring(passiveExport: false) +FNMNetworkMonitor.shared.startMonitoring() ``` 2. Monitoring custom URLSessions by supplying the FNMMonitor URL Protocol: @@ -37,7 +37,7 @@ FNMNetworkMonitor.shared.startMonitoring(passiveExport: false) let sessionConfig = URLSessionConfiguration.ephemeral sessionConfig.protocolClasses = FNMNetworkMonitor.normalizedURLProtocols() self.customSession = URLSession(configuration: sessionConfig) -FNMNetworkMonitor.shared.startMonitoring(passiveExport: false) +FNMNetworkMonitor.shared.startMonitoring() ``` 3. You can also take advantage of sizzling the URLSessionConfiguration creation to configure the URL Protocol to all sessions, allowing to monitor 3rd party SDKs too. @@ -52,7 +52,7 @@ let profiles = [FNMProfile(request: request, responseHolder: .keyValue(value: [ "FieldA": 1 ]) delay: 0.25)])] FNMNetworkMonitor.shared.configure(profiles: profiles) -FNMNetworkMonitor.shared.startMonitoring(passiveExport: false) +FNMNetworkMonitor.shared.startMonitoring() ``` Make sure to follow steps 1, 2 or 3, depending on the URLSession that runs that particular request. @@ -73,7 +73,7 @@ FNMNetworkMonitor.shared.logScope = [.export, .profile, .urlProtocol] Finally, you can turn on the passive export and the requests will be exported to a json file inside a folder found the Documents application folder. ```swift -FNMNetworkMonitor.shared.startMonitoring(passiveExport: true) +FNMNetworkMonitor.shared.passiveExportPreference = FNMRecordExporterPreference.on(setting: .unlimited) ``` ### Sample app diff --git a/Tests/NetworkMonitorAppLaunchTests.swift b/Tests/NetworkMonitorAppLaunchTests.swift index e9c8caf..f68cc91 100755 --- a/Tests/NetworkMonitorAppLaunchTests.swift +++ b/Tests/NetworkMonitorAppLaunchTests.swift @@ -23,7 +23,8 @@ class NetworkMonitorTests: NetworkMonitorUnitTests { self.networkMonitor.configure(profiles: Constants.Sites.allCases.map { $0.profile }) self.networkMonitor.clear(completion: { } ) FNMNetworkMonitor.registerToLoadingSystem() - FNMNetworkMonitor.shared.startMonitoring(passiveExport: true) + FNMNetworkMonitor.shared.startMonitoring() + FNMNetworkMonitor.shared.passiveExportPreference = .on(setting: .unlimited) let debugListingViewController = FNMDebugListingViewController() debugListingViewController.view.layoutIfNeeded() diff --git a/Tests/NetworkMonitorDebugUITests.swift b/Tests/NetworkMonitorDebugUITests.swift index d6b2a3f..ad0e9db 100755 --- a/Tests/NetworkMonitorDebugUITests.swift +++ b/Tests/NetworkMonitorDebugUITests.swift @@ -19,7 +19,7 @@ class NetworkMonitorDebugUITests: NetworkMonitorUnitTests { XCTAssertEqual(self.networkMonitor.records.count, 0) FNMNetworkMonitor.registerToLoadingSystem() - FNMNetworkMonitor.shared.startMonitoring(passiveExport: false) + FNMNetworkMonitor.shared.startMonitoring() let debugListingViewController = FNMDebugListingViewController() debugListingViewController.view.layoutIfNeeded() @@ -43,7 +43,7 @@ class NetworkMonitorDebugUITests: NetworkMonitorUnitTests { self.networkMonitor.configure(profiles: Constants.Sites.allCases.map { $0.profile }) self.networkMonitor.clear(completion: { } ) FNMNetworkMonitor.registerToLoadingSystem() - FNMNetworkMonitor.shared.startMonitoring(passiveExport: false) + FNMNetworkMonitor.shared.startMonitoring() let robotsExpectation = expectation(description: "Some Robots") self.reachSitesSequencially(sites: [.alphabet], expectation: robotsExpectation) diff --git a/Tests/NetworkMonitorFlowTests.swift b/Tests/NetworkMonitorFlowTests.swift index dba8e75..e031730 100755 --- a/Tests/NetworkMonitorFlowTests.swift +++ b/Tests/NetworkMonitorFlowTests.swift @@ -144,7 +144,7 @@ class NetworkMonitorFlowTests: NetworkMonitorUnitTests { XCTAssertEqual(self.networkMonitor.records.count, 0) FNMNetworkMonitor.registerToLoadingSystem() - FNMNetworkMonitor.shared.startMonitoring(passiveExport: false) + FNMNetworkMonitor.shared.startMonitoring() let robotsExpectation = expectation(description: "Some Robots") self.reachVariousSitesConcurrently(expectation: robotsExpectation) @@ -190,7 +190,7 @@ class NetworkMonitorFlowTests: NetworkMonitorUnitTests { XCTAssertEqual(self.networkMonitor.records.count, 0) FNMNetworkMonitor.registerToLoadingSystem() - FNMNetworkMonitor.shared.startMonitoring(passiveExport: false) + FNMNetworkMonitor.shared.startMonitoring() let robotsExpectation = expectation(description: "Some Robots") self.reachSitesSequencially(expectation: robotsExpectation) @@ -226,7 +226,7 @@ class NetworkMonitorFlowTests: NetworkMonitorUnitTests { self.networkMonitor.configure(profiles: Constants.Sites.allCases.map { $0.profile }) self.networkMonitor.clear(completion: { } ) FNMNetworkMonitor.registerToLoadingSystem() - FNMNetworkMonitor.shared.startMonitoring(passiveExport: false) + FNMNetworkMonitor.shared.startMonitoring() XCTAssertNotNil(FNMNetworkMonitor.shared) XCTAssertEqual(self.networkMonitor.records.count, 0) @@ -289,7 +289,7 @@ class NetworkMonitorFlowTests: NetworkMonitorUnitTests { self.networkMonitor.configure(profiles: Constants.Sites.allCases.map { $0.profile }) self.networkMonitor.clear(completion: { } ) FNMNetworkMonitor.registerToLoadingSystem() - FNMNetworkMonitor.shared.startMonitoring(passiveExport: false) + FNMNetworkMonitor.shared.startMonitoring() XCTAssertNotNil(FNMNetworkMonitor.shared) XCTAssertEqual(self.networkMonitor.records.count, 0) From b176b70a935a458c2a13c1f1e45027cd6aeb0cf4 Mon Sep 17 00:00:00 2001 From: Diogo Balseiro Date: Fri, 2 Oct 2020 16:18:26 +0100 Subject: [PATCH 2/4] Cleaned the tests --- NetworkMonitor.xcodeproj/project.pbxproj | 4 ++- .../xcschemes/NetworkMonitor.xcscheme | 2 +- .../xcshareddata/xcschemes/Sample.xcscheme | 2 +- Sample/ViewController.swift | 3 ++- Tests/NetworkMonitorExportTests.swift | 26 +++++++------------ 5 files changed, 17 insertions(+), 20 deletions(-) diff --git a/NetworkMonitor.xcodeproj/project.pbxproj b/NetworkMonitor.xcodeproj/project.pbxproj index b18bba2..c3005c2 100644 --- a/NetworkMonitor.xcodeproj/project.pbxproj +++ b/NetworkMonitor.xcodeproj/project.pbxproj @@ -463,7 +463,7 @@ isa = PBXProject; attributes = { LastSwiftUpdateCheck = 1130; - LastUpgradeCheck = 1020; + LastUpgradeCheck = 1200; ORGANIZATIONNAME = Farfetch; TargetAttributes = { F793CA4C1F9E361900AA9C41 = { @@ -647,6 +647,7 @@ CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; CLANG_WARN_STRICT_PROTOTYPES = YES; CLANG_WARN_SUSPICIOUS_MOVE = YES; @@ -712,6 +713,7 @@ CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; CLANG_WARN_STRICT_PROTOTYPES = YES; CLANG_WARN_SUSPICIOUS_MOVE = YES; diff --git a/NetworkMonitor.xcodeproj/xcshareddata/xcschemes/NetworkMonitor.xcscheme b/NetworkMonitor.xcodeproj/xcshareddata/xcschemes/NetworkMonitor.xcscheme index 2ac0755..e557f30 100644 --- a/NetworkMonitor.xcodeproj/xcshareddata/xcschemes/NetworkMonitor.xcscheme +++ b/NetworkMonitor.xcodeproj/xcshareddata/xcschemes/NetworkMonitor.xcscheme @@ -1,6 +1,6 @@ Date: Fri, 2 Oct 2020 16:20:56 +0100 Subject: [PATCH 3/4] Last minute clean up --- NetworkMonitor/Classes/Exporter/FNMRecordExporter.swift | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/NetworkMonitor/Classes/Exporter/FNMRecordExporter.swift b/NetworkMonitor/Classes/Exporter/FNMRecordExporter.swift index 18f82fe..85663b5 100644 --- a/NetworkMonitor/Classes/Exporter/FNMRecordExporter.swift +++ b/NetworkMonitor/Classes/Exporter/FNMRecordExporter.swift @@ -30,16 +30,15 @@ struct FNMRecordExporter { if case let FNMRecordExporterPreference.on(setting) = preference { - NSLog("🌀 Export requested, \(requestRecords.count)") if self.requestRecordExportQueued == false { - NSLog("🌀🌀 Export queued") + self.requestRecordExportQueued = true DispatchQueue.global().asyncAfter(deadline: .now() + Constants.exportDebounceDelay, execute: { do { - NSLog("🌀🌀🌀 Export executing") + let recordsFilenameURL = try self.recordsFilenameURL() let requestRecordsToProcess: [FNMHTTPRequestRecord] @@ -63,7 +62,6 @@ struct FNMRecordExporter { FNMRecordExporter.log(message: "Exported Request Records Using Setting '\(setting)'") self.requestRecordExportQueued = false - NSLog("🌀🌀🌀🌀 Export executed, \(requestRecordsToProcess.count)") } catch { From b6bb85f44c6163f7cee24c2b5317b30a1d784906 Mon Sep 17 00:00:00 2001 From: Diogo Balseiro Date: Wed, 7 Oct 2020 16:51:57 +0100 Subject: [PATCH 4/4] Last minute clean up --- Tests/NetworkMonitorExportTests.swift | 62 +++++++++++++++++++++++--- Tests/NetworkMonitorFlowTests.swift | 19 ++++---- Tests/NetworkMonitorProfileTests.swift | 15 ++++--- 3 files changed, 75 insertions(+), 21 deletions(-) diff --git a/Tests/NetworkMonitorExportTests.swift b/Tests/NetworkMonitorExportTests.swift index c17a4a9..c90fa32 100755 --- a/Tests/NetworkMonitorExportTests.swift +++ b/Tests/NetworkMonitorExportTests.swift @@ -13,7 +13,7 @@ import XCTest class NetworkMonitorExportTests: NetworkMonitorUnitTests { - func testExportPassiveFiles() { + func testExportPassiveFilesUnlimited() { XCTAssertNotNil(FNMNetworkMonitor.shared) XCTAssertEqual(self.networkMonitor.records.count, 0) @@ -27,21 +27,60 @@ class NetworkMonitorExportTests: NetworkMonitorUnitTests { let robotsExpectation = expectation(description: "Some Robots") self.reachSitesSequencially(sites: [.alphabet, .intel]) { - DispatchQueue.global().asyncAfter(deadline: .now() + 1.0) { + self.forceExport { let exportFiles = self.exportPassiveFiles /// The files arent decodable, so lets just check for the string count. We just want to make sure something is being written out XCTAssertLessThan(exportFiles, 1000) - self.reachSitesSequencially(sites: [.amazon, .netflix]) { + self.reachSitesSequencially(sites: [.alphabet, .intel]) { - DispatchQueue.global().asyncAfter(deadline: .now() + 1.0) { + self.forceExport { let exportFiles = self.exportPassiveFiles /// The files arent decodable, so lets just check for the string count. We just want to make sure something is being written out - XCTAssertGreaterThan(exportFiles, 100) + XCTAssertGreaterThan(exportFiles, 1000) + + robotsExpectation.fulfill() + } + } + } + } + + waitForExpectations(timeout: 60) { _ in } + } + + func testExportPassiveFilesLimited() { + + XCTAssertNotNil(FNMNetworkMonitor.shared) + XCTAssertEqual(self.networkMonitor.records.count, 0) + + self.networkMonitor.configure(profiles: Constants.Sites.allCases.map { $0.profile }) + self.networkMonitor.clear(completion: { } ) + FNMNetworkMonitor.registerToLoadingSystem() + FNMNetworkMonitor.shared.startMonitoring() + FNMNetworkMonitor.shared.passiveExportPreference = .on(setting: .first(numberOfRecords: 2)) + + let robotsExpectation = expectation(description: "Some Robots") + self.reachSitesSequencially(sites: [.alphabet, .intel]) { + + self.forceExport { + + let exportFiles = self.exportPassiveFiles + + /// The files arent decodable, so lets just check for the string count. We just want to make sure something is being written out + XCTAssertLessThan(exportFiles, 1000) + + self.reachSitesSequencially(sites: [.alphabet, .intel]) { + + self.forceExport { + + let exportFiles = self.exportPassiveFiles + + /// The files arent decodable, so lets just check for the string count. We just want to make sure something is being written out + XCTAssertLessThan(exportFiles, 1000) robotsExpectation.fulfill() } @@ -92,6 +131,19 @@ class NetworkMonitorExportTests: NetworkMonitorUnitTests { } } + func forceExport(closure: @escaping () -> Void) { + + DispatchQueue.global().asyncAfter(deadline: .now() + 4.0) { + + FNMNetworkMonitor.shared.exportRecordData() + + DispatchQueue.global().asyncAfter(deadline: .now() + 4.0) { + + closure() + } + } + } + func exportActiveFiles(from records: [FNMHTTPRequestRecord]) -> (recordsCount: Int, detailInfosCount: Int) { do { diff --git a/Tests/NetworkMonitorFlowTests.swift b/Tests/NetworkMonitorFlowTests.swift index e031730..e17e354 100755 --- a/Tests/NetworkMonitorFlowTests.swift +++ b/Tests/NetworkMonitorFlowTests.swift @@ -138,14 +138,20 @@ extension XCTestCase { class NetworkMonitorFlowTests: NetworkMonitorUnitTests { + override func setUp() { + + FNMNetworkMonitor.registerToLoadingSystem() + FNMNetworkMonitor.shared.startMonitoring() + FNMNetworkMonitor.shared.passiveExportPreference = .off + + super.setUp() + } + func testLiveRequestRecordsConcurrently() { XCTAssertNotNil(FNMNetworkMonitor.shared) XCTAssertEqual(self.networkMonitor.records.count, 0) - FNMNetworkMonitor.registerToLoadingSystem() - FNMNetworkMonitor.shared.startMonitoring() - let robotsExpectation = expectation(description: "Some Robots") self.reachVariousSitesConcurrently(expectation: robotsExpectation) @@ -189,9 +195,6 @@ class NetworkMonitorFlowTests: NetworkMonitorUnitTests { XCTAssertNotNil(FNMNetworkMonitor.shared) XCTAssertEqual(self.networkMonitor.records.count, 0) - FNMNetworkMonitor.registerToLoadingSystem() - FNMNetworkMonitor.shared.startMonitoring() - let robotsExpectation = expectation(description: "Some Robots") self.reachSitesSequencially(expectation: robotsExpectation) @@ -225,8 +228,6 @@ class NetworkMonitorFlowTests: NetworkMonitorUnitTests { self.networkMonitor.configure(profiles: Constants.Sites.allCases.map { $0.profile }) self.networkMonitor.clear(completion: { } ) - FNMNetworkMonitor.registerToLoadingSystem() - FNMNetworkMonitor.shared.startMonitoring() XCTAssertNotNil(FNMNetworkMonitor.shared) XCTAssertEqual(self.networkMonitor.records.count, 0) @@ -288,8 +289,6 @@ class NetworkMonitorFlowTests: NetworkMonitorUnitTests { self.networkMonitor.configure(profiles: Constants.Sites.allCases.map { $0.profile }) self.networkMonitor.clear(completion: { } ) - FNMNetworkMonitor.registerToLoadingSystem() - FNMNetworkMonitor.shared.startMonitoring() XCTAssertNotNil(FNMNetworkMonitor.shared) XCTAssertEqual(self.networkMonitor.records.count, 0) diff --git a/Tests/NetworkMonitorProfileTests.swift b/Tests/NetworkMonitorProfileTests.swift index d27bd10..68a27d0 100755 --- a/Tests/NetworkMonitorProfileTests.swift +++ b/Tests/NetworkMonitorProfileTests.swift @@ -41,6 +41,15 @@ class NetworkMonitorProfileTests: NetworkMonitorUnitTests { static let bodySwimsuitBodyLong = " swimsuit body Swimsuit body " } + override func setUp() { + + FNMNetworkMonitor.registerToLoadingSystem() + FNMNetworkMonitor.shared.startMonitoring() + FNMNetworkMonitor.shared.passiveExportPreference = .off + + super.setUp() + } + func testProfilesCompletionStatic() { let profileRequest = FNMProfileRequest(urlPattern: .staticPattern(url: URLConstants.repeatTastySecretiveYarnMuddledGET)) @@ -51,8 +60,6 @@ class NetworkMonitorProfileTests: NetworkMonitorUnitTests { ]))] FNMNetworkMonitor.shared.configure(profiles: [FNMProfile(request: profileRequest, responses: responses)]) - FNMNetworkMonitor.shared.startMonitoring() - FNMNetworkMonitor.registerToLoadingSystem() let request = self.request(for: URLConstants.repeatTastySecretiveYarnMuddledGET, httpMethod: "GET") @@ -94,8 +101,6 @@ class NetworkMonitorProfileTests: NetworkMonitorUnitTests { ]))] FNMNetworkMonitor.shared.configure(profiles: [FNMProfile(request: profileRequest, responses: responses)]) - FNMNetworkMonitor.shared.startMonitoring() - FNMNetworkMonitor.registerToLoadingSystem() let request = self.request(for: URLConstants.repeatTastySecretiveYarnMuddledGET, httpMethod: "GET") @@ -141,8 +146,6 @@ class NetworkMonitorProfileTests: NetworkMonitorUnitTests { FNMNetworkMonitor.shared.configure(profiles: [FNMProfile(request: profileRequestA, responses: responsesA), FNMProfile(request: profileRequestB, responses: responsesB)]) - FNMNetworkMonitor.shared.startMonitoring() - FNMNetworkMonitor.registerToLoadingSystem() let requestA = self.request(for: URLConstants.repeatTastySecretiveYarnMuddledGET, httpMethod: "GET")