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] Add async flush method #13851

Merged
merged 1 commit into from
Oct 8, 2024
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
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(
ncooke3 marked this conversation as resolved.
Show resolved Hide resolved
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
Loading