1515@testable import FirebaseCoreInternal
1616import XCTest
1717
18+ extension HeartbeatsBundle {
19+ static let testHeartbeatBundle : Self = {
20+ var heartbeatBundle = HeartbeatsBundle ( capacity: 1 )
21+ heartbeatBundle. append ( Heartbeat ( agent: " dummy_agent " , date: Date ( ) ) )
22+ return heartbeatBundle
23+ } ( )
24+ }
25+
1826class HeartbeatStorageTests : XCTestCase {
1927 // MARK: - Instance Management
2028
@@ -95,23 +103,20 @@ class HeartbeatStorageTests: XCTestCase {
95103 let expectation = expectation ( description: #function)
96104 let heartbeatStorage = HeartbeatStorage ( id: #function, storage: StorageFake ( ) )
97105
98- var dummyHeartbeatsBundle = HeartbeatsBundle ( capacity: 1 )
99- dummyHeartbeatsBundle. append ( Heartbeat ( agent: " dummy_agent " , date: Date ( ) ) )
100-
101106 // When
102107 heartbeatStorage. readAndWriteAsync { heartbeatsBundle in
103108 // Assert that heartbeat storage is empty.
104109 XCTAssertNil ( heartbeatsBundle)
105110 // Write new value.
106- return dummyHeartbeatsBundle
111+ return HeartbeatsBundle . testHeartbeatBundle
107112 }
108113
109114 heartbeatStorage. readAndWriteAsync { heartbeatsBundle in
110115 expectation. fulfill ( )
111116 // Assert old value is read.
112117 XCTAssertEqual (
113118 heartbeatsBundle? . makeHeartbeatsPayload ( ) ,
114- dummyHeartbeatsBundle . makeHeartbeatsPayload ( )
119+ HeartbeatsBundle . testHeartbeatBundle . makeHeartbeatsPayload ( )
115120 )
116121 // Write some new value.
117122 return heartbeatsBundle
@@ -145,9 +150,6 @@ class HeartbeatStorageTests: XCTestCase {
145150 let storageFake = StorageFake ( )
146151 let heartbeatStorage = HeartbeatStorage ( id: #function, storage: storageFake)
147152
148- var dummyHeartbeatsBundle = HeartbeatsBundle ( capacity: 1 )
149- dummyHeartbeatsBundle. append ( Heartbeat ( agent: " dummy_agent " , date: Date ( ) ) )
150-
151153 // When
152154 storageFake. onWrite = { _ in
153155 expectation. fulfill ( ) // Fulfilled 2 times.
@@ -156,18 +158,18 @@ class HeartbeatStorageTests: XCTestCase {
156158
157159 heartbeatStorage. readAndWriteAsync { heartbeatsBundle in
158160 expectation. fulfill ( )
159- return dummyHeartbeatsBundle
161+ return HeartbeatsBundle . testHeartbeatBundle
160162 }
161163
162164 // Then
163165 heartbeatStorage. readAndWriteAsync { heartbeatsBundle in
164166 expectation. fulfill ( )
165167 XCTAssertNotEqual (
166168 heartbeatsBundle? . makeHeartbeatsPayload ( ) ,
167- dummyHeartbeatsBundle . makeHeartbeatsPayload ( ) ,
169+ HeartbeatsBundle . testHeartbeatBundle . makeHeartbeatsPayload ( ) ,
168170 " They should not be equal because the previous save failed. "
169171 )
170- return dummyHeartbeatsBundle
172+ return HeartbeatsBundle . testHeartbeatBundle
171173 }
172174
173175 wait ( for: [ expectation] , timeout: 0.5 )
@@ -212,16 +214,13 @@ class HeartbeatStorageTests: XCTestCase {
212214 // Given
213215 let heartbeatStorage = HeartbeatStorage ( id: #function, storage: StorageFake ( ) )
214216
215- var dummyHeartbeatsBundle = HeartbeatsBundle ( capacity: 1 )
216- dummyHeartbeatsBundle. append ( Heartbeat ( agent: " dummy_agent " , date: Date ( ) ) )
217-
218217 // When
219218 let expectation1 = expectation ( description: #function + " _1 " )
220219 heartbeatStorage. getAndSetAsync { heartbeatsBundle in
221220 // Assert that heartbeat storage is empty.
222221 XCTAssertNil ( heartbeatsBundle)
223222 // Write new value.
224- return dummyHeartbeatsBundle
223+ return HeartbeatsBundle . testHeartbeatBundle
225224 } completion: { result in
226225 switch result {
227226 case . success: break
@@ -237,7 +236,7 @@ class HeartbeatStorageTests: XCTestCase {
237236 // Assert old value is read.
238237 XCTAssertEqual (
239238 heartbeatsBundle? . makeHeartbeatsPayload ( ) ,
240- dummyHeartbeatsBundle . makeHeartbeatsPayload ( )
239+ HeartbeatsBundle . testHeartbeatBundle . makeHeartbeatsPayload ( )
241240 )
242241 // Write some new value.
243242 expectation2. fulfill ( )
@@ -368,7 +367,7 @@ class HeartbeatStorageTests: XCTestCase {
368367 let expectations : [ XCTestExpectation ] = try ( 0 ... 1000 ) . map { i in
369368 let expectation = expectation ( description: " \( #function) _ \( i) " )
370369
371- let transform : ( HeartbeatsBundle ? ) -> HeartbeatsBundle ? = { heartbeatsBundle in
370+ let transform : @ Sendable ( HeartbeatsBundle ? ) -> HeartbeatsBundle ? = { heartbeatsBundle in
372371 expectation. fulfill ( )
373372 return heartbeatsBundle
374373 }
@@ -431,7 +430,10 @@ class HeartbeatStorageTests: XCTestCase {
431430 }
432431}
433432
434- private class StorageFake : Storage {
433+ private final class StorageFake : Storage , @unchecked Sendable {
434+ // The unchecked Sendable conformance is used to prevent warnings for the below var, which
435+ // violates the class's Sendable conformance. Ignoring this violation should be okay for
436+ // testing purposes.
435437 var fakeFile : Data ?
436438 var onRead : ( ( ) throws -> Data ) ?
437439 var onWrite : ( ( Data ? ) throws -> Void ) ?
0 commit comments