Skip to content

[Swift 6] Sessions Swift 6 changes #14575

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions FirebaseSessions/Sources/FirebaseSessions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,14 @@ private enum GoogleDataTransportConfig {

super.init()

for subscriberName in SessionsDependencies.dependencies {
let dependencies = SessionsDependencies.dependencies
for subscriberName in dependencies {
subscriberPromises[subscriberName] = Promise<Void>.pending()
}

Logger
.logDebug(
"Version \(FirebaseVersion()). Expecting subscriptions from: \(SessionsDependencies.dependencies)"
"Version \(FirebaseVersion()). Expecting subscriptions from: \(dependencies)"
)

self.initiator.beginListening {
Expand Down
62 changes: 53 additions & 9 deletions FirebaseSessions/Sources/Public/SessionsDependencies.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,62 @@

import Foundation

// Sessions Dependencies determines when a dependent SDK is
// installed in the app. The Sessions SDK uses this to figure
// out which dependencies to wait for to getting the data
// collection state.
//
// This is important because the Sessions SDK starts up before
// dependent SDKs
private final class AtomicBox<T> {
private var _value: T
private let lock = NSLock()

init(_ value: T) {
_value = value
}

func value() -> T {
lock.withLock {
_value
}
}

@discardableResult func withLock(_ body: (_ value: inout T) -> Void) -> T {
lock.withLock {
body(&_value)
return _value
}
}
}

/// Sessions Dependencies determines when a dependent SDK is
/// installed in the app. The Sessions SDK uses this to figure
/// out which dependencies to wait for to getting the data
/// collection state.
///
/// This is important because the Sessions SDK starts up before
/// dependent SDKs
@objc(FIRSessionsDependencies)
public class SessionsDependencies: NSObject {
static var dependencies: Set<SessionsSubscriberName> = .init()
#if compiler(>=6)
private nonisolated(unsafe) static let _dependencies: AtomicBox<Set<SessionsSubscriberName>> =
AtomicBox(
Set()
)
#else
private static let _dependencies: AtomicBox<Set<SessionsSubscriberName>> = AtomicBox(
Set()
)
#endif

static var dependencies: Set<SessionsSubscriberName> {
_dependencies.value()
}

@objc public static func addDependency(name: SessionsSubscriberName) {
SessionsDependencies.dependencies.insert(name)
_dependencies.withLock { dependencies in
dependencies.insert(name)
}
}

/// For testing only.
static func removeAll() {
_dependencies.withLock { dependencies in
dependencies.removeAll()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
postLogEvent: @escaping (Result<Void, FirebaseSessionsError>,
[SessionsSubscriber]) -> Void) {
// This class is static, so we need to clear global state
SessionsDependencies.dependencies.removeAll()
SessionsDependencies.removeAll()

for subscriberSDK in subscriberSDKs {
SessionsDependencies.addDependency(name: subscriberSDK.sessionsSubscriberName)
Expand Down Expand Up @@ -123,7 +123,7 @@

// Wait for the Sessions SDK to log the session before finishing
// the test.
wait(for: [loggedEventExpectation], timeout: 3)

Check failure on line 126 in FirebaseSessions/Tests/Unit/Library/FirebaseSessionsTestsBase.swift

View workflow job for this annotation

GitHub Actions / spm (macos-14, Xcode_15.4, iOS)

test_multipleInitiations_logsSessionEventEachInitiation, Asynchronous wait failed: Exceeded timeout of 3 seconds, with unfulfilled expectations: "Called loggedEventCallback".

Check failure on line 126 in FirebaseSessions/Tests/Unit/Library/FirebaseSessionsTestsBase.swift

View workflow job for this annotation

GitHub Actions / spm (macos-14, Xcode_15.4, iOS)

test_sessionSampled_doesNotLogSessionEventButDoesFetchSettings, Asynchronous wait failed: Exceeded timeout of 3 seconds, with unfulfilled expectations: "Called loggedEventCallback".

Check failure on line 126 in FirebaseSessions/Tests/Unit/Library/FirebaseSessionsTestsBase.swift

View workflow job for this annotation

GitHub Actions / spm (macos-14, Xcode_15.4, iOS)

test_settingsDisabled_doesNotLogSessionEventButDoesFetchSettings, Asynchronous wait failed: Exceeded timeout of 3 seconds, with unfulfilled expectations: "Called loggedEventCallback".

Check failure on line 126 in FirebaseSessions/Tests/Unit/Library/FirebaseSessionsTestsBase.swift

View workflow job for this annotation

GitHub Actions / spm (macos-14, Xcode_15.4, iOS)

test_defaultSamplingRate_isSetInProto, Asynchronous wait failed: Exceeded timeout of 3 seconds, with unfulfilled expectations: "Called loggedEventCallback".

Check failure on line 126 in FirebaseSessions/Tests/Unit/Library/FirebaseSessionsTestsBase.swift

View workflow job for this annotation

GitHub Actions / spm (macos-14, Xcode_15.4, iOS)

test_subscribersAllDataCollectionDisabled_doesNotLogSessionEvent, Asynchronous wait failed: Exceeded timeout of 3 seconds, with unfulfilled expectations: "Called loggedEventCallback".

Check failure on line 126 in FirebaseSessions/Tests/Unit/Library/FirebaseSessionsTestsBase.swift

View workflow job for this annotation

GitHub Actions / spm (macos-14, Xcode_15.4, iOS)

test_subscribersSomeDataCollectionDisabled_logsSessionEvent, Asynchronous wait failed: Exceeded timeout of 3 seconds, with unfulfilled expectations: "Called loggedEventCallback".

Check failure on line 126 in FirebaseSessions/Tests/Unit/Library/FirebaseSessionsTestsBase.swift

View workflow job for this annotation

GitHub Actions / spm (macos-14, Xcode_15.4, iOS)

test_subscriberWithDataCollectionEnabled_logsSessionEvent, Asynchronous wait failed: Exceeded timeout of 3 seconds, with unfulfilled expectations: "Called loggedEventCallback".

Check failure on line 126 in FirebaseSessions/Tests/Unit/Library/FirebaseSessionsTestsBase.swift

View workflow job for this annotation

GitHub Actions / spm (macos-14, Xcode_15.4, iOS)

test_registerSubscriber_callsOnSessionChanged, Asynchronous wait failed: Exceeded timeout of 3 seconds, with unfulfilled expectations: "Called loggedEventCallback".

Check failure on line 126 in FirebaseSessions/Tests/Unit/Library/FirebaseSessionsTestsBase.swift

View workflow job for this annotation

GitHub Actions / spm (macos-14, Xcode_15.4, iOS)

test_subscribersDataCollectionDisabled_callsOnSessionChanged, Asynchronous wait failed: Exceeded timeout of 3 seconds, with unfulfilled expectations: "Called loggedEventCallback".

Check failure on line 126 in FirebaseSessions/Tests/Unit/Library/FirebaseSessionsTestsBase.swift

View workflow job for this annotation

GitHub Actions / spm (macos-15, Xcode_16.2, watchOS)

test_sessionSampled_doesNotLogSessionEventButDoesFetchSettings, Asynchronous wait failed: Exceeded timeout of 3 seconds, with unfulfilled expectations: "Called loggedEventCallback".

Check failure on line 126 in FirebaseSessions/Tests/Unit/Library/FirebaseSessionsTestsBase.swift

View workflow job for this annotation

GitHub Actions / spm (macos-15, Xcode_16.2, watchOS)

test_settingsDisabled_doesNotLogSessionEventButDoesFetchSettings, Asynchronous wait failed: Exceeded timeout of 3 seconds, with unfulfilled expectations: "Called loggedEventCallback".

Check failure on line 126 in FirebaseSessions/Tests/Unit/Library/FirebaseSessionsTestsBase.swift

View workflow job for this annotation

GitHub Actions / spm (macos-15, Xcode_16.2, watchOS)

test_defaultSamplingRate_isSetInProto, Asynchronous wait failed: Exceeded timeout of 3 seconds, with unfulfilled expectations: "Called loggedEventCallback".

Check failure on line 126 in FirebaseSessions/Tests/Unit/Library/FirebaseSessionsTestsBase.swift

View workflow job for this annotation

GitHub Actions / spm (macos-15, Xcode_16.2, watchOS)

test_subscribersAllDataCollectionDisabled_doesNotLogSessionEvent, Asynchronous wait failed: Exceeded timeout of 3 seconds, with unfulfilled expectations: "Called loggedEventCallback".

Check failure on line 126 in FirebaseSessions/Tests/Unit/Library/FirebaseSessionsTestsBase.swift

View workflow job for this annotation

GitHub Actions / spm (macos-15, Xcode_16.2, watchOS)

test_sessionSampled_doesNotLogSessionEventButDoesFetchSettings, Asynchronous wait failed: Exceeded timeout of 3 seconds, with unfulfilled expectations: "Called loggedEventCallback".

Check failure on line 126 in FirebaseSessions/Tests/Unit/Library/FirebaseSessionsTestsBase.swift

View workflow job for this annotation

GitHub Actions / spm (macos-15, Xcode_16.2, watchOS)

test_settingsDisabled_doesNotLogSessionEventButDoesFetchSettings, Asynchronous wait failed: Exceeded timeout of 3 seconds, with unfulfilled expectations: "Called loggedEventCallback".

Check failure on line 126 in FirebaseSessions/Tests/Unit/Library/FirebaseSessionsTestsBase.swift

View workflow job for this annotation

GitHub Actions / spm (macos-15, Xcode_16.2, watchOS)

test_defaultSamplingRate_isSetInProto, Asynchronous wait failed: Exceeded timeout of 3 seconds, with unfulfilled expectations: "Called loggedEventCallback".

Check failure on line 126 in FirebaseSessions/Tests/Unit/Library/FirebaseSessionsTestsBase.swift

View workflow job for this annotation

GitHub Actions / spm (macos-15, Xcode_16.2, watchOS)

test_subscribersAllDataCollectionDisabled_doesNotLogSessionEvent, Asynchronous wait failed: Exceeded timeout of 3 seconds, with unfulfilled expectations: "Called loggedEventCallback".
}

func assertSuccess(result: Result<Void, FirebaseSessionsError>) {
Expand Down
Loading