Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions FirebaseSessions/Sources/EventGDTLogger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import Foundation
internal import GoogleDataTransport

protocol EventGDTLoggerProtocol: Sendable {
func logEvent(event: SessionStartEvent, completion: @escaping (Result<Void, Error>) -> Void)
func logEvent(event: SessionStartEvent,
completion: @escaping @Sendable (Result<Void, Error>) -> Void)
}

///
Expand All @@ -38,7 +39,8 @@ final class EventGDTLogger: EventGDTLoggerProtocol {

/// Logs the event to FireLog, taking into account debugging cases such as running
/// in simulator.
func logEvent(event: SessionStartEvent, completion: @escaping (Result<Void, Error>) -> Void) {
func logEvent(event: SessionStartEvent,
completion: @escaping @Sendable (Result<Void, Error>) -> Void) {
let gdtEvent = googleDataTransport.eventForTransport()
gdtEvent.dataObject = event
gdtEvent.qosTier = GDTCOREventQoS.qosDefault
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ enum GoogleDataTransportProtocolErrors: Error {
}

protocol GoogleDataTransportProtocol: Sendable {
func logGDTEvent(event: GDTCOREvent, completion: @escaping (Result<Void, Error>) -> Void)
func logGDTEvent(event: GDTCOREvent,
completion: @escaping @Sendable (Result<Void, Error>) -> Void)
func eventForTransport() -> GDTCOREvent
}

Expand All @@ -38,7 +39,8 @@ final class GoogleDataTransporter: GoogleDataTransportProtocol {
transporter = GDTCORTransport(mappingID: mappingID, transformers: transformers, target: target)!
}

func logGDTEvent(event: GDTCOREvent, completion: @escaping (Result<Void, any Error>) -> Void) {
func logGDTEvent(event: GDTCOREvent,
completion: @escaping @Sendable (Result<Void, any Error>) -> Void) {
transporter.sendDataEvent(event) { wasWritten, error in
if let error {
completion(.failure(error))
Expand Down
8 changes: 6 additions & 2 deletions FirebaseSessions/Sources/SessionCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import Foundation

protocol SessionCoordinatorProtocol: Sendable {
func attemptLoggingSessionStart(event: SessionStartEvent,
callback: @escaping (Result<Void, FirebaseSessionsError>) -> Void)
callback: @escaping @Sendable (Result<Void,
FirebaseSessionsError>) -> Void)
}

///
Expand All @@ -37,7 +38,10 @@ final class SessionCoordinator: SessionCoordinatorProtocol {
/// Begins the process of logging a SessionStartEvent to FireLog after
/// it has been approved for sending
func attemptLoggingSessionStart(event: SessionStartEvent,
callback: @escaping (Result<Void, FirebaseSessionsError>)
callback: @escaping @Sendable (Result<
Void,
FirebaseSessionsError
>)
-> Void) {
/// Order of execution
/// 1. Fetch the installations Id. Regardless of success, move to step 2
Expand Down
39 changes: 23 additions & 16 deletions FirebaseSessions/Tests/Unit/SessionCoordinatorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ import XCTest

@testable import FirebaseSessions

private class SendableBox<Value>: @unchecked Sendable {
var value: Value
init(value: Value) {
self.value = value
}
}

class SessionCoordinatorTests: XCTestCase {
var installations = MockInstallationsProtocol()
var time = MockTimeProvider()
Expand Down Expand Up @@ -50,13 +57,13 @@ class SessionCoordinatorTests: XCTestCase {

func test_attemptLoggingSessionStart_logsToGDT() throws {
let event = SessionStartEvent(sessionInfo: defaultSessionInfo, appInfo: appInfo, time: time)
var resultSuccess = false
let resultSuccess = SendableBox(value: false)
coordinator.attemptLoggingSessionStart(event: event) { result in
switch result {
case .success(()):
resultSuccess = true
resultSuccess.value = true
case .failure:
resultSuccess = false
resultSuccess.value = false
}
}
// Make sure we've set the Installation ID
Expand All @@ -71,7 +78,7 @@ class SessionCoordinatorTests: XCTestCase {

// We should have logged successfully
XCTAssertEqual(fireLogger.loggedEvent, event)
XCTAssert(resultSuccess)
XCTAssert(resultSuccess.value)
}

func test_attemptLoggingSessionStart_handlesGDTError() throws {
Expand All @@ -80,13 +87,13 @@ class SessionCoordinatorTests: XCTestCase {
let event = SessionStartEvent(sessionInfo: defaultSessionInfo, appInfo: appInfo, time: time)

// Start success so it must be set to false
var resultSuccess = true
let resultSuccess = SendableBox(value: true)
coordinator.attemptLoggingSessionStart(event: event) { result in
switch result {
case .success(()):
resultSuccess = true
resultSuccess.value = true
case .failure:
resultSuccess = false
resultSuccess.value = false
}
}

Expand All @@ -102,7 +109,7 @@ class SessionCoordinatorTests: XCTestCase {

// We should have logged the event, but with a failed result
XCTAssertEqual(fireLogger.loggedEvent, event)
XCTAssertFalse(resultSuccess)
XCTAssertFalse(resultSuccess.value)
}

func test_attemptLoggingSessionStart_handlesInstallationsError() throws {
Expand All @@ -111,21 +118,21 @@ class SessionCoordinatorTests: XCTestCase {
let event = SessionStartEvent(sessionInfo: defaultSessionInfo, appInfo: appInfo, time: time)

// Start success so it must be set to false
var resultSuccess = true
let resultSuccess = SendableBox(value: true)
coordinator.attemptLoggingSessionStart(event: event) { result in
switch result {
case .success(()):
resultSuccess = true
resultSuccess.value = true
case .failure:
resultSuccess = false
resultSuccess.value = false
}
}

XCTAssertTrue(installations.authTokenFinished)
XCTAssertTrue(installations.installationIdFinished)
// We should have logged the event, but with a failed result
XCTAssertNotNil(fireLogger.loggedEvent)
XCTAssertFalse(resultSuccess)
XCTAssertFalse(resultSuccess.value)
}

func test_attemptLoggingSessionStart_handlesGDTAndInstallationsError() throws {
Expand All @@ -138,13 +145,13 @@ class SessionCoordinatorTests: XCTestCase {
let event = SessionStartEvent(sessionInfo: defaultSessionInfo, appInfo: appInfo, time: time)

// Start success so it must be set to false
var resultSuccess = true
let resultSuccess = SendableBox(value: true)
coordinator.attemptLoggingSessionStart(event: event) { result in
switch result {
case .success(()):
resultSuccess = true
resultSuccess.value = true
case let .failure(err):
resultSuccess = false
resultSuccess.value = false
// Result should use the FireLog error if there's an error in both
// Installations and FireLog
XCTAssertEqual(err, FirebaseSessionsError.DataTransportError(fireLogError))
Expand All @@ -164,6 +171,6 @@ class SessionCoordinatorTests: XCTestCase {

// We should have logged the event, but with a failed result
XCTAssertEqual(fireLogger.loggedEvent, event)
XCTAssertFalse(resultSuccess)
XCTAssertFalse(resultSuccess.value)
}
}
Loading