-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Rollouts] Crashlytics Rollouts interop Integration (#12200)
- Loading branch information
1 parent
e92874c
commit 7c2ac50
Showing
5 changed files
with
181 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
Crashlytics/Crashlytics/Rollouts/CrashlyticsRemoteConfigManager.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Copyright 2023 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import FirebaseRemoteConfigInterop | ||
import Foundation | ||
|
||
protocol CrashlyticsPersistentLog: NSObject { | ||
func updateRolloutsStateToPersistence(rolloutAssignments: [RolloutAssignment]) | ||
} | ||
|
||
@objc(FIRCLSRemoteConfigManager) | ||
public class CrashlyticsRemoteConfigManager: NSObject { | ||
public static let maxRolloutAssignments = 128 | ||
public static let maxParameterValueLength = 256 | ||
|
||
var remoteConfig: RemoteConfigInterop | ||
public private(set) var rolloutAssignment: [RolloutAssignment] = [] | ||
weak var persistenceDelegate: CrashlyticsPersistentLog? | ||
|
||
@objc public init(remoteConfig: RemoteConfigInterop) { | ||
self.remoteConfig = remoteConfig | ||
} | ||
|
||
@objc public func updateRolloutsState(rolloutsState: RolloutsState) { | ||
rolloutAssignment = normalizeRolloutAssignment(assignments: Array(rolloutsState.assignments)) | ||
} | ||
} | ||
|
||
private extension CrashlyticsRemoteConfigManager { | ||
func normalizeRolloutAssignment(assignments: [RolloutAssignment]) -> [RolloutAssignment] { | ||
var validatedAssignments = assignments | ||
if assignments.count > CrashlyticsRemoteConfigManager.maxRolloutAssignments { | ||
debugPrint("Rollouts excess the maximum number of assignments can pass to Crashlytics") | ||
validatedAssignments = | ||
Array(assignments[..<CrashlyticsRemoteConfigManager.maxRolloutAssignments]) | ||
} | ||
|
||
_ = validatedAssignments.map { assignment in | ||
if assignment.parameterValue.count > CrashlyticsRemoteConfigManager.maxParameterValueLength { | ||
debugPrint( | ||
"Rollouts excess the maximum length of parameter value can pass to Crashlytics", | ||
assignment.parameterValue | ||
) | ||
let upperBound = String.Index( | ||
utf16Offset: CrashlyticsRemoteConfigManager.maxParameterValueLength, | ||
in: assignment.parameterValue | ||
) | ||
let slicedParameterValue = assignment.parameterValue[..<upperBound] | ||
assignment.parameterValue = String(slicedParameterValue) | ||
} | ||
} | ||
|
||
return validatedAssignments | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
Crashlytics/UnitTestsSwift/CrashlyticsRemoteConfigManagerTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// Copyright 2023 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
#if SWIFT_PACKAGE | ||
@testable import FirebaseCrashlyticsSwift | ||
#else | ||
@testable import FirebaseCrashlytics | ||
#endif | ||
import FirebaseRemoteConfigInterop | ||
import XCTest | ||
|
||
class RemoteConfigConfigMock: RemoteConfigInterop { | ||
func registerRolloutsStateSubscriber(_ subscriber: FirebaseRemoteConfigInterop | ||
.RolloutsStateSubscriber, | ||
for namespace: String) {} | ||
} | ||
|
||
final class CrashlyticsRemoteConfigManagerTests: XCTestCase { | ||
let rollouts: RolloutsState = { | ||
let assignment1 = RolloutAssignment( | ||
rolloutId: "rollout_1", | ||
variantId: "control", | ||
templateVersion: 1, | ||
parameterKey: "my_feature", | ||
parameterValue: "false" | ||
) | ||
let assignment2 = RolloutAssignment( | ||
rolloutId: "rollout_2", | ||
variantId: "enabled", | ||
templateVersion: 1, | ||
parameterKey: "themis_big_feature", | ||
parameterValue: "1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111" | ||
) | ||
let rollouts = RolloutsState(assignmentList: [assignment1, assignment2]) | ||
return rollouts | ||
}() | ||
|
||
let rcInterop = RemoteConfigConfigMock() | ||
|
||
func testRemoteConfigManagerProperlyProcessRolloutsState() throws { | ||
let rcManager = CrashlyticsRemoteConfigManager(remoteConfig: rcInterop) | ||
rcManager.updateRolloutsState(rolloutsState: rollouts) | ||
XCTAssertEqual(rcManager.rolloutAssignment.count, 2) | ||
|
||
for assignment in rollouts.assignments { | ||
if assignment.parameterKey == "themis_big_feature" { | ||
XCTAssertEqual( | ||
assignment.parameterValue.count, | ||
CrashlyticsRemoteConfigManager.maxParameterValueLength | ||
) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters