Skip to content

Commit

Permalink
[Core] Add async flush method (firebase#13851)
Browse files Browse the repository at this point in the history
  • Loading branch information
ncooke3 authored and MojtabaHs committed Oct 17, 2024
1 parent 4b1dd4b commit aec7b1c
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
12 changes: 11 additions & 1 deletion FirebaseCore/Extension/FIRHeartbeatLogger.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,23 @@ NSString *_Nullable FIRHeaderValueFromHeartbeatsPayload(FIRHeartbeatsPayload *he
- (void)log;

#ifndef FIREBASE_BUILD_CMAKE
/// Flushes heartbeats from storage into a structured payload of heartbeats.
/// Synchronously flushes heartbeats from storage into a structured payload of heartbeats.
///
/// This API is for clients using platform logging V2.
///
/// @note This API is thread-safe.
/// @return A payload of heartbeats.
- (FIRHeartbeatsPayload *)flushHeartbeatsIntoPayload;

/// Asynchronously flushes heartbeats from storage into a structured payload of heartbeats.
///
/// This API is for clients using platform logging V2.
///
/// @note This API is thread-safe.
/// @param completionHandler A completion handler to process the flushed payload of heartbeats.
- (void)flushHeartbeatsIntoPayloadWithCompletionHandler:
(void (^)(FIRHeartbeatsPayload *))completionHandler
API_AVAILABLE(ios(13.0), macosx(10.15), macCatalyst(13.0), tvos(13.0), watchos(6.0));
#endif // FIREBASE_BUILD_CMAKE

/// Gets today's corresponding heartbeat code.
Expand Down
7 changes: 7 additions & 0 deletions FirebaseCore/Sources/FIRHeartbeatLogger.m
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@ - (FIRHeartbeatsPayload *)flushHeartbeatsIntoPayload {
FIRHeartbeatsPayload *payload = [_heartbeatController flush];
return payload;
}

- (void)flushHeartbeatsIntoPayloadWithCompletionHandler:
(void (^)(FIRHeartbeatsPayload *))completionHandler {
[_heartbeatController flushAsyncWithCompletionHandler:^(FIRHeartbeatsPayload *payload) {
completionHandler(payload);
}];
}
#endif // FIREBASE_BUILD_CMAKE

- (FIRDailyHeartbeatCode)heartbeatCodeForToday {
Expand Down
39 changes: 39 additions & 0 deletions FirebaseCore/Tests/Unit/FIRHeartbeatLoggerTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,30 @@ - (void)testFlushing_UsingV2API_WhenHeartbeatsAreStored_ReturnsNonEmptyPayload {
}];
}

- (void)testFlushingAsync_UsingV2API_WhenHeartbeatsAreStored_ReturnsNonEmptyPayload API_AVAILABLE(
ios(13.0), macosx(10.15), macCatalyst(13.0), tvos(13.0), watchos(6.0)) {
// Given
FIRHeartbeatLogger *heartbeatLogger = self.heartbeatLogger;
NSString *expectedDate = [[self class] formattedStringForDate:[NSDate date]];
// When
[heartbeatLogger log];
XCTestExpectation *expectation = [self expectationWithDescription:@"async flush"];
[heartbeatLogger
flushHeartbeatsIntoPayloadWithCompletionHandler:^(FIRHeartbeatsPayload *heartbeatsPayload) {
// Then
[self assertEncodedPayloadHeader:FIRHeaderValueFromHeartbeatsPayload(heartbeatsPayload)
isEqualToPayloadJSON:@{
@"version" : @2,
@"heartbeats" : @[
@{@"agent" : @"dummy_agent",
@"dates" : @[ expectedDate ]}
]
}];
[expectation fulfill];
}];
[self waitForExpectations:@[ expectation ] timeout:1.0];
}

- (void)testFlushing_UsingV2API_WhenNoHeartbeatsAreStored_ReturnsEmptyPayload {
// Given
FIRHeartbeatLogger *heartbeatLogger = self.heartbeatLogger;
Expand All @@ -134,6 +158,21 @@ - (void)testFlushing_UsingV2API_WhenNoHeartbeatsAreStored_ReturnsEmptyPayload {
[self assertHeartbeatsPayloadIsEmpty:heartbeatsPayload];
}

- (void)testFlushingAsync_UsingV2API_WhenNoHeartbeatsAreStored_ReturnsEmptyPayload API_AVAILABLE(
ios(13.0), macosx(10.15), macCatalyst(13.0), tvos(13.0), watchos(6.0)) {
// Given
FIRHeartbeatLogger *heartbeatLogger = self.heartbeatLogger;
// When
XCTestExpectation *expectation = [self expectationWithDescription:@"async flush"];
[heartbeatLogger
flushHeartbeatsIntoPayloadWithCompletionHandler:^(FIRHeartbeatsPayload *heartbeatsPayload) {
// Then
[self assertHeartbeatsPayloadIsEmpty:heartbeatsPayload];
[expectation fulfill];
}];
[self waitForExpectations:@[ expectation ] timeout:1.0];
}

- (void)testLogAndFlushUsingV1API_AndThenFlushAgainUsingV2API_FlushesHeartbeatInTheFirstFlush {
// Given
FIRHeartbeatLogger *heartbeatLogger = self.heartbeatLogger;
Expand Down

0 comments on commit aec7b1c

Please sign in to comment.