Skip to content
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

[Core] Make storage class conform to Sendable and subsequent changes #13939

Merged
merged 8 commits into from
Oct 30, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add date wrapper for adjusting date
  • Loading branch information
ncooke3 committed Oct 28, 2024
commit 70039df64fc8a7c900d1a083bb7aebd252a39ea5
40 changes: 24 additions & 16 deletions FirebaseCore/Internal/Tests/Unit/HeartbeatControllerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@
@testable import FirebaseCoreInternal
import XCTest

/// Used to manipulate a date across multiple concurrent contexts for simulation purposes.
final class AdjustableDate: @unchecked Sendable {
var date: Date
init(date: Date) {
self.date = date
}
}

class HeartbeatControllerTests: XCTestCase {
// 2021-11-01 @ 00:00:00 (EST)
let date = Date(timeIntervalSince1970: 1_635_739_200)
Expand Down Expand Up @@ -99,11 +107,11 @@ class HeartbeatControllerTests: XCTestCase {

func testLogAtEndOfTimePeriodAndAcceptAtStartOfNextOne() throws {
// Given
var testDate = date
let testDate = AdjustableDate(date: date)

let controller = HeartbeatController(
storage: HeartbeatStorageFake(),
dateProvider: { testDate }
dateProvider: { testDate.date }
)

assertHeartbeatControllerFlushesEmptyPayload(controller)
Expand All @@ -113,12 +121,12 @@ class HeartbeatControllerTests: XCTestCase {
controller.log("dummy_agent")

// - Advance to 2021-11-01 @ 23:59:59 (EST)
testDate.addTimeInterval(60 * 60 * 24 - 1)
testDate.date.addTimeInterval(60 * 60 * 24 - 1)

controller.log("dummy_agent")

// - Advance to 2021-11-02 @ 00:00:00 (EST)
testDate.addTimeInterval(1)
testDate.date.addTimeInterval(1)

controller.log("dummy_agent")

Expand Down Expand Up @@ -271,18 +279,18 @@ class HeartbeatControllerTests: XCTestCase {
).date // 2021-11-02 @ 11 PM (Tokyo time zone)
)

var testDate = newYorkDate
let testDate = AdjustableDate(date: newYorkDate)

let heartbeatController = HeartbeatController(
storage: HeartbeatStorageFake(),
dateProvider: { testDate }
dateProvider: { testDate.date }
)

// When
heartbeatController.log("dummy_agent")

// Device travels from NYC to Tokyo.
testDate = tokyoDate
testDate.date = tokyoDate

heartbeatController.log("dummy_agent")

Expand All @@ -308,22 +316,22 @@ class HeartbeatControllerTests: XCTestCase {

func testLoggingDependsOnDateNotUserAgent() throws {
// Given
var testDate = date
let testDate = AdjustableDate(date: date)
let heartbeatController = HeartbeatController(
storage: HeartbeatStorageFake(),
dateProvider: { testDate }
dateProvider: { testDate.date }
)

// When
// - Day 1
heartbeatController.log("dummy_agent")

// - Day 2
testDate.addTimeInterval(60 * 60 * 24)
testDate.date.addTimeInterval(60 * 60 * 24)
heartbeatController.log("some_other_agent")

// - Day 3
testDate.addTimeInterval(60 * 60 * 24)
testDate.date.addTimeInterval(60 * 60 * 24)
heartbeatController.log("dummy_agent")

// Then
Expand Down Expand Up @@ -359,20 +367,20 @@ class HeartbeatControllerTests: XCTestCase {
let todaysDate = date
let tomorrowsDate = date.addingTimeInterval(60 * 60 * 24)

var testDate = yesterdaysDate
let testDate = AdjustableDate(date: yesterdaysDate)

let heartbeatController = HeartbeatController(
storage: HeartbeatStorageFake(),
dateProvider: { testDate }
dateProvider: { testDate.date }
)

// When
heartbeatController.log("yesterdays_dummy_agent")
testDate = todaysDate
testDate.date = todaysDate
heartbeatController.log("todays_dummy_agent")
testDate = tomorrowsDate
testDate.date = tomorrowsDate
heartbeatController.log("tomorrows_dummy_agent")
testDate = todaysDate
testDate.date = todaysDate

// Then
let payload = heartbeatController.flushHeartbeatFromToday()
Expand Down
Loading