From fdada0263c57168c514f34a2a19fbc99af9830e1 Mon Sep 17 00:00:00 2001 From: Gavin Whelan Date: Fri, 17 Jul 2020 19:16:38 +0000 Subject: [PATCH] (5.0) Fix of crash when logging is enabled. (#115) Also removed unused test code. Improve documentation on LDClient.flush() --- LaunchDarkly/LaunchDarkly/LDClient.swift | 8 ++- .../ObjectiveC/ObjcLDClient.swift | 6 +- .../Service Objects/ErrorNotifier.swift | 9 --- .../Service Objects/EventReporter.swift | 2 +- .../Service Objects/FlagSynchronizer.swift | 4 +- .../Extensions/DictionarySpec.swift | 9 +-- .../LaunchDarklyTests/LDClientSpec.swift | 34 ------------ .../Models/ErrorObserverSpec.swift | 6 -- .../LaunchDarklyTests/Models/EventSpec.swift | 55 ------------------- .../Networking/DarklyServiceSpec.swift | 4 -- .../Cache/UserEnvironmentFlagCacheSpec.swift | 10 ---- .../Service Objects/ErrorNotifierSpec.swift | 24 ++++---- .../Service Objects/EventReporterSpec.swift | 1 - .../FlagChangeNotifierSpec.swift | 4 +- .../FlagSynchronizerSpec.swift | 3 - 15 files changed, 28 insertions(+), 151 deletions(-) diff --git a/LaunchDarkly/LaunchDarkly/LDClient.swift b/LaunchDarkly/LaunchDarkly/LDClient.swift index 8ca1a897..f1f4b990 100644 --- a/LaunchDarkly/LaunchDarkly/LDClient.swift +++ b/LaunchDarkly/LaunchDarkly/LDClient.swift @@ -797,8 +797,12 @@ public class LDClient { } /** - Report events to LaunchDarkly servers. While online, the LDClient automatically reports events on the `LDConfig.eventFlushInterval`, and whenever the client app moves to the background. There should normally not be a need to call reportEvents. - */ + Tells the SDK to immediately send any currently queued events to LaunchDarkly. + + There should not normally be a need to call this function. While online, the LDClient automatically reports events + on an interval defined by `LDConfig.eventFlushInterval`. Note that this function does not block until events are + sent, it only triggers a background task to send events immediately. + */ public func flush() { LDClient.instances?.forEach { $1.internalFlush() } } diff --git a/LaunchDarkly/LaunchDarkly/ObjectiveC/ObjcLDClient.swift b/LaunchDarkly/LaunchDarkly/ObjectiveC/ObjcLDClient.swift index d430aa8e..1b888a47 100644 --- a/LaunchDarkly/LaunchDarkly/ObjectiveC/ObjcLDClient.swift +++ b/LaunchDarkly/LaunchDarkly/ObjectiveC/ObjcLDClient.swift @@ -784,7 +784,11 @@ public final class ObjcLDClient: NSObject { } /** - Report events to LaunchDarkly servers. While online, the LDClient automatically reports events on the `LDConfig.eventFlushInterval`, and whenever the client app moves to the background. There should normally not be a need to call reportEvents. + Tells the SDK to immediately send any currently queued events to LaunchDarkly. + + There should not normally be a need to call this function. While online, the LDClient automatically reports events + on an interval defined by `LDConfig.eventFlushInterval`. Note that this function does not block until events are + sent, it only triggers a background task to send events immediately. */ @objc public func flush() { ldClient.flush() diff --git a/LaunchDarkly/LaunchDarkly/Service Objects/ErrorNotifier.swift b/LaunchDarkly/LaunchDarkly/Service Objects/ErrorNotifier.swift index 9c7c9c3f..2c6fdd59 100644 --- a/LaunchDarkly/LaunchDarkly/Service Objects/ErrorNotifier.swift +++ b/LaunchDarkly/LaunchDarkly/Service Objects/ErrorNotifier.swift @@ -37,15 +37,6 @@ final class ErrorNotifier: ErrorNotifying { #if DEBUG extension ErrorNotifier { - convenience init(observers: [ErrorObserver]? = nil) { - self.init() - guard let observers = observers, observers.isEmpty == false - else { - return - } - errorObservers.append(contentsOf: observers) - } - func erase(owner: LDObserverOwner) { for index in 0.. [String: Any] { diff --git a/LaunchDarkly/LaunchDarklyTests/LDClientSpec.swift b/LaunchDarkly/LaunchDarklyTests/LDClientSpec.swift index f2c70905..63d71bda 100644 --- a/LaunchDarkly/LaunchDarklyTests/LDClientSpec.swift +++ b/LaunchDarkly/LaunchDarklyTests/LDClientSpec.swift @@ -118,16 +118,6 @@ final class LDClientSpec: QuickSpec { var onSyncComplete: FlagSyncCompleteClosure? { return serviceFactoryMock.onFlagSyncComplete } - // flag maintaining mock accessors - var replaceStoreComplete: CompletionClosure? { - return flagStoreMock.replaceStoreReceivedArguments?.completion - } - var updateStoreComplete: CompletionClosure? { - return flagStoreMock.updateStoreReceivedArguments?.completion - } - var deleteFlagComplete: CompletionClosure? { - return flagStoreMock.deleteFlagReceivedArguments?.completion - } var recordedEvent: LaunchDarkly.Event? { eventReporterMock.recordReceivedEvent } @@ -2597,27 +2587,3 @@ extension CacheConvertingMock { convertCacheDataReceivedArguments = nil } } - -extension LDConfig { - func copyReplacingMobileKey(_ mobileKey: MobileKey) -> LDConfig { - var newConfig = LDConfig(mobileKey: mobileKey) - newConfig.baseUrl = baseUrl - newConfig.eventsUrl = eventsUrl - newConfig.streamUrl = streamUrl - newConfig.eventCapacity = eventCapacity - newConfig.connectionTimeout = connectionTimeout - newConfig.eventFlushInterval = eventFlushInterval - newConfig.flagPollingInterval = flagPollingInterval - newConfig.backgroundFlagPollingInterval = backgroundFlagPollingInterval - newConfig.streamingMode = streamingMode - newConfig.enableBackgroundUpdates = enableBackgroundUpdates - newConfig.startOnline = startOnline - newConfig.allUserAttributesPrivate = allUserAttributesPrivate - newConfig.privateUserAttributes = privateUserAttributes - newConfig.useReport = useReport - newConfig.inlineUserInEvents = inlineUserInEvents - newConfig.isDebugMode = isDebugMode - - return newConfig - } -} diff --git a/LaunchDarkly/LaunchDarklyTests/Models/ErrorObserverSpec.swift b/LaunchDarkly/LaunchDarklyTests/Models/ErrorObserverSpec.swift index 51358246..4e420770 100644 --- a/LaunchDarkly/LaunchDarklyTests/Models/ErrorObserverSpec.swift +++ b/LaunchDarkly/LaunchDarklyTests/Models/ErrorObserverSpec.swift @@ -17,12 +17,6 @@ final class ErrorOwnerMock { } } -extension ErrorObserver { - static func createObservers(count: Int, using owner: ErrorOwnerMock = ErrorOwnerMock()) -> [ErrorObserver] { - (0.. Event { switch eventKind { @@ -1328,10 +1321,6 @@ extension Event { } } - static func stubFeatureEvent(_ featureFlag: FeatureFlag, with user: LDUser) -> Event { - return Event.featureEvent(key: UUID().uuidString, value: true, defaultValue: false, featureFlag: featureFlag, user: user, includeReason: false) - } - static func stubEvents(eventCount: Int = Event.Kind.allKinds.count, for user: LDUser) -> [Event] { var eventStubs = [Event]() while eventStubs.count < eventCount { @@ -1350,48 +1339,4 @@ extension Event { event.dictionaryValue(config: config) } } - - func matches(eventDictionary: [String: Any]?) -> Bool { - guard let eventDictionary = eventDictionary - else { - return false - } - if kind == .summary { - return kind == eventDictionary.eventKind && endDate?.isWithin(0.001, of: eventDictionary.eventEndDate) ?? false - } - guard let eventDictionaryKey = eventDictionary.eventKey, - let eventDictionaryCreationDateMillis = eventDictionary.eventCreationDateMillis - else { - return false - } - return key == eventDictionaryKey && creationDate?.millisSince1970 == eventDictionaryCreationDateMillis - } -} - -extension Array where Element == Event { - func matches(eventDictionaries: [[String: Any]]) -> Bool { - guard self.count == eventDictionaries.count else { - return false - } - for index in self.indices { - if !self[index].matches(eventDictionary: eventDictionaries[index]) { - return false - } - } - return true - } -} - -extension Array where Element == [String: Any] { - func matches(events: [Event]) -> Bool { - guard self.count == events.count else { - return false - } - for index in self.indices { - if !events[index].matches(eventDictionary: self[index]) { - return false - } - } - return true - } } diff --git a/LaunchDarkly/LaunchDarklyTests/Networking/DarklyServiceSpec.swift b/LaunchDarkly/LaunchDarklyTests/Networking/DarklyServiceSpec.swift index 33418d5d..67aa404b 100644 --- a/LaunchDarkly/LaunchDarklyTests/Networking/DarklyServiceSpec.swift +++ b/LaunchDarkly/LaunchDarklyTests/Networking/DarklyServiceSpec.swift @@ -1073,10 +1073,6 @@ extension DarklyService.StreamRequestPath { } extension LDUser { - func base64encoded(using config: LDConfig) -> String? { - return dictionaryValue(includeFlagConfig: false, includePrivateAttributes: true, config: config).base64UrlEncodedString - } - init?(base64urlEncodedString: String) { let base64encodedString = base64urlEncodedString.replacingOccurrences(of: "-", with: "+").replacingOccurrences(of: "_", with: "/") self.init(data: Data(base64Encoded: base64encodedString)) diff --git a/LaunchDarkly/LaunchDarklyTests/Service Objects/Cache/UserEnvironmentFlagCacheSpec.swift b/LaunchDarkly/LaunchDarklyTests/Service Objects/Cache/UserEnvironmentFlagCacheSpec.swift index 4a52ff1f..77aa3653 100644 --- a/LaunchDarkly/LaunchDarklyTests/Service Objects/Cache/UserEnvironmentFlagCacheSpec.swift +++ b/LaunchDarkly/LaunchDarklyTests/Service Objects/Cache/UserEnvironmentFlagCacheSpec.swift @@ -26,19 +26,9 @@ final class UserEnvironmentFlagCacheSpec: QuickSpec { var selectedUser: LDUser { return users.selectedUser } - var unchangedUsers: [LDUser] { - var remainingUsers = users - remainingUsers.remove(at: users.firstIndex(of: selectedUser)!) - return remainingUsers - } var selectedMobileKey: String { return userEnvironmentsCollection[selectedUser.key]!.environmentFlags.keys.selectedMobileKey } - var unchangedEnvironments: [MobileKey: CacheableEnvironmentFlags] { - var remainingEnvironments = userEnvironmentsCollection[selectedUser.key]!.environmentFlags - remainingEnvironments.removeValue(forKey: selectedMobileKey) - return remainingEnvironments - } var oldestUser: LDUser { //sort pairs youngest to oldest let sortedLastUpdatedPairs = userEnvironmentsCollection.compactMapValues { (cacheableUserEnvironments) in diff --git a/LaunchDarkly/LaunchDarklyTests/Service Objects/ErrorNotifierSpec.swift b/LaunchDarkly/LaunchDarklyTests/Service Objects/ErrorNotifierSpec.swift index 50d7df75..5a9881f6 100644 --- a/LaunchDarkly/LaunchDarklyTests/Service Objects/ErrorNotifierSpec.swift +++ b/LaunchDarkly/LaunchDarklyTests/Service Objects/ErrorNotifierSpec.swift @@ -36,25 +36,25 @@ final class ErrorNotifierSpec: QuickSpec { } init(observerCount: Int) { - self.init() - errorObservers = ErrorObserver.createObservers(count: observerCount) - errorNotifier = ErrorNotifier(observers: errorObservers) - originalObserverCount = errorObservers.count - observersPerOwner = observerCount + self.init(ownerCount: observerCount, observersPerOwner: 1) } init(ownerCount: Int, observersPerOwner: Int) { - self.init() + errorMock = ErrorMock(with: Constants.errorIdentifier) + nextErrorObserver = ErrorObserver(owner: nextErrorObserverOwner, errorHandler: nextErrorObserverOwner.handle) + errorNotifier = ErrorNotifier() - // create ownerCount owners, then observersPerOwner observers for each owner - let owners = (0..