Skip to content

Commit

Permalink
no enum in heartbeat api
Browse files Browse the repository at this point in the history
  • Loading branch information
paulb777 committed Oct 29, 2024
1 parent db30d8e commit d7527bd
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -562,11 +562,11 @@ class AuthBackendRPCImplementationTests: RPCBaseTests {
return handler()
}

func heartbeatCodeForToday() -> FIRDailyHeartbeatCode {
func heartbeatCodeForToday() -> NSInteger {
// This API should not be used by the below tests because the Auth
// SDK uses only the V2 heartbeat API (`flushHeartbeatsIntoPayload`) for
// getting heartbeats.
return FIRDailyHeartbeatCode.none
return 0
}
}

Expand Down
3 changes: 2 additions & 1 deletion FirebaseCore.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ Firebase Core includes FIRApp and FIROptions which provide central configuration

s.source_files = [
'FirebaseCore/Sources/**/*.[mh]',
'FirebaseCore/Extension/*.h'
'FirebaseCore/Extension/*.h',
'FirebaseCore/InternalObjC/*.h',
]

s.resource_bundles = {
Expand Down
14 changes: 2 additions & 12 deletions FirebaseCore/Extension/FIRHeartbeatLogger.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,13 @@ NS_ASSUME_NONNULL_BEGIN
@class FIRHeartbeatsPayload;
#endif // FIREBASE_BUILD_CMAKE

/// Enum representing different daily heartbeat codes.
/// This enum is only used by clients using platform logging V1. This is because
/// the V1 payload only supports a single daily heartbeat.
typedef NS_ENUM(NSInteger, FIRDailyHeartbeatCode) {
/// Represents the absence of a daily heartbeat.
FIRDailyHeartbeatCodeNone = 0,
/// Represents the presence of a daily heartbeat.
FIRDailyHeartbeatCodeSome = 2,
};

@protocol FIRHeartbeatLoggerProtocol <NSObject>

/// Asynchronously logs a heartbeat.
- (void)log;

/// Gets the heartbeat code for today.
- (FIRDailyHeartbeatCode)heartbeatCodeForToday;
- (NSInteger)heartbeatCodeForToday;

#ifndef FIREBASE_BUILD_CMAKE
/// Returns the header value for the heartbeat logger via the given completion handler..
Expand Down Expand Up @@ -97,7 +87,7 @@ NSString *_Nullable FIRHeaderValueFromHeartbeatsPayload(FIRHeartbeatsPayload *he
///
/// @note This API is thread-safe.
/// @return Heartbeat code indicating whether or not there is an unsent global heartbeat.
- (FIRDailyHeartbeatCode)heartbeatCodeForToday;
- (NSInteger)heartbeatCodeForToday;

@end

Expand Down
3 changes: 1 addition & 2 deletions FirebaseCore/Sources/FIRComponentContainerInternal.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@
*/
#import <Foundation/Foundation.h>

@import FirebaseCoreInternal;
#ifdef SWIFT_PACKAGE
@import FirebaseCoreInternalObjC;
#else
@import FirebaseCoreInternal;
#import "FirebaseCore/InternalObjC/FirebaseCoreInternal.h"
#endif

@class FIRApp;
Expand Down
8 changes: 4 additions & 4 deletions FirebaseCore/Sources/FIRHeartbeatLogger.m
Original file line number Diff line number Diff line change
Expand Up @@ -95,17 +95,17 @@ - (void)flushHeartbeatsIntoPayloadWithCompletionHandler:
}
#endif // FIREBASE_BUILD_CMAKE

- (FIRDailyHeartbeatCode)heartbeatCodeForToday {
- (NSInteger)heartbeatCodeForToday {
#ifndef FIREBASE_BUILD_CMAKE
FIRHeartbeatsPayload *todaysHeartbeatPayload = [_heartbeatController flushHeartbeatFromToday];

if ([todaysHeartbeatPayload isEmpty]) {
return FIRDailyHeartbeatCodeNone;
return 0;
} else {
return FIRDailyHeartbeatCodeSome;
return 2;
}
#else
return FIRDailyHeartbeatCodeNone;
return 0;
#endif // FIREBASE_BUILD_CMAKE
}

Expand Down
16 changes: 8 additions & 8 deletions FirebaseCore/Tests/Unit/FIRHeartbeatLoggerTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -96,18 +96,18 @@ - (void)testFlushing_UsingV1API_WhenHeartbeatsAreStored_ReturnsFIRDailyHeartbeat
FIRHeartbeatLogger *heartbeatLogger = self.heartbeatLogger;
// When
[heartbeatLogger log];
FIRDailyHeartbeatCode heartbeatInfoCode = [heartbeatLogger heartbeatCodeForToday];
NSInteger heartbeatInfoCode = [heartbeatLogger heartbeatCodeForToday];
// Then
XCTAssertEqual(heartbeatInfoCode, FIRDailyHeartbeatCodeSome);
XCTAssertEqual(heartbeatInfoCode, 2);
}

- (void)testFlushing_UsingV1API_WhenNoHeartbeatsAreStored_ReturnsFIRDailyHeartbeatCodeNone {
// Given
FIRHeartbeatLogger *heartbeatLogger = self.heartbeatLogger;
// When
FIRDailyHeartbeatCode heartbeatInfoCode = [heartbeatLogger heartbeatCodeForToday];
NSInteger heartbeatInfoCode = [heartbeatLogger heartbeatCodeForToday];
// Then
XCTAssertEqual(heartbeatInfoCode, FIRDailyHeartbeatCodeNone);
XCTAssertEqual(heartbeatInfoCode, 0);
}

- (void)testFlushing_UsingV2API_WhenHeartbeatsAreStored_ReturnsNonEmptyPayload {
Expand Down Expand Up @@ -178,10 +178,10 @@ - (void)testLogAndFlushUsingV1API_AndThenFlushAgainUsingV2API_FlushesHeartbeatIn
FIRHeartbeatLogger *heartbeatLogger = self.heartbeatLogger;
[heartbeatLogger log];
// When
FIRDailyHeartbeatCode heartbeatInfoCode = [heartbeatLogger heartbeatCodeForToday];
NSInteger heartbeatInfoCode = [heartbeatLogger heartbeatCodeForToday];
FIRHeartbeatsPayload *heartbeatsPayload = [heartbeatLogger flushHeartbeatsIntoPayload];
// Then
XCTAssertEqual(heartbeatInfoCode, FIRDailyHeartbeatCodeSome);
XCTAssertEqual(heartbeatInfoCode, 2);
[self assertHeartbeatsPayloadIsEmpty:heartbeatsPayload];
}

Expand All @@ -192,14 +192,14 @@ - (void)testLogAndFlushUsingV2API_AndThenFlushAgainUsingV1API_FlushesHeartbeatIn
[heartbeatLogger log];
// When
FIRHeartbeatsPayload *heartbeatsPayload = [heartbeatLogger flushHeartbeatsIntoPayload];
FIRDailyHeartbeatCode heartbeatInfoCode = [heartbeatLogger heartbeatCodeForToday];
NSInteger heartbeatInfoCode = [heartbeatLogger heartbeatCodeForToday];
// Then
[self assertEncodedPayloadHeader:FIRHeaderValueFromHeartbeatsPayload(heartbeatsPayload)
isEqualToPayloadJSON:@{
@"version" : @2,
@"heartbeats" : @[ @{@"agent" : @"dummy_agent", @"dates" : @[ expectedDate ]} ]
}];
XCTAssertEqual(heartbeatInfoCode, FIRDailyHeartbeatCodeNone);
XCTAssertEqual(heartbeatInfoCode, 0);
}

- (void)testHeartbeatLoggersWithSameIDShareTheSameStorage {
Expand Down
1 change: 1 addition & 0 deletions FirebaseFunctions/Tests/Unit/FunctionsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import Foundation

import FirebaseCore
import FirebaseCoreExtension
@testable import FirebaseFunctions
#if COCOAPODS
import GTMSessionFetcher
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ - (nonnull FIRHeartbeatsPayload *)flushHeartbeatsIntoPayload {
}
}

- (FIRDailyHeartbeatCode)heartbeatCodeForToday {
- (NSInteger)heartbeatCodeForToday {
// This API should not be used by the below tests because the Installations
// SDK uses only the V2 heartbeat API (`flushHeartbeatsIntoPayload`) for
// getting heartbeats.
[self doesNotRecognizeSelector:_cmd];
return FIRDailyHeartbeatCodeNone;
return 0;
}

- (void)log {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ - (void)performTokenOperation {

/// A fake heartbeat logger used for dependency injection during testing.
@interface FIRHeartbeatLoggerFake : NSObject <FIRHeartbeatLoggerProtocol>
@property(nonatomic, copy, nullable) FIRDailyHeartbeatCode (^onHeartbeatCodeForTodayHandler)(void);
@property(nonatomic, copy, nullable) NSInteger (^onHeartbeatCodeForTodayHandler)(void);
@end

@implementation FIRHeartbeatLoggerFake
Expand All @@ -84,11 +84,11 @@ - (nonnull FIRHeartbeatsPayload *)flushHeartbeatsIntoPayload {
return nil;
}

- (FIRDailyHeartbeatCode)heartbeatCodeForToday {
- (NSInteger)heartbeatCodeForToday {
if (self.onHeartbeatCodeForTodayHandler) {
return self.onHeartbeatCodeForTodayHandler();
} else {
return FIRDailyHeartbeatCodeNone;
return 0;
}
}

Expand Down Expand Up @@ -404,26 +404,24 @@ - (void)testHTTPAuthHeaderGenerationFromCheckin {
}

- (void)testTokenFetchOperationFirebaseUserAgentAndHeartbeatHeader_WhenHeartbeatNeedsSending {
[self assertTokenFetchOperationRequestContainsFirebaseUserAgentAndHeartbeatInfoCode:
FIRDailyHeartbeatCodeSome];
[self assertTokenFetchOperationRequestContainsFirebaseUserAgentAndHeartbeatInfoCode:2];
}

- (void)testTokenFetchOperationFirebaseUserAgentAndHeartbeatHeader_WhenNoHeartbeatNeedsSending {
[self assertTokenFetchOperationRequestContainsFirebaseUserAgentAndHeartbeatInfoCode:
FIRDailyHeartbeatCodeNone];
[self assertTokenFetchOperationRequestContainsFirebaseUserAgentAndHeartbeatInfoCode:0];
}

#pragma mark - Internal Helpers

- (void)assertTokenFetchOperationRequestContainsFirebaseUserAgentAndHeartbeatInfoCode:
(FIRDailyHeartbeatCode)heartbeatInfoCode {
(NSInteger)heartbeatInfoCode {
XCTestExpectation *completionExpectation =
[self expectationWithDescription:@"completionExpectation"];

FIRHeartbeatLoggerFake *heartbeatLoggerFake = [[FIRHeartbeatLoggerFake alloc] init];
XCTestExpectation *heartbeatExpectation =
[self expectationWithDescription:@"heartbeatExpectation"];
heartbeatLoggerFake.onHeartbeatCodeForTodayHandler = ^FIRDailyHeartbeatCode {
heartbeatLoggerFake.onHeartbeatCodeForTodayHandler = ^NSInteger {
[heartbeatExpectation fulfill];
return heartbeatInfoCode;
};
Expand Down
6 changes: 3 additions & 3 deletions Firestore/core/src/remote/firebase_metadata_provider_apple.mm
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
return MakeString([FIRApp firebaseUserAgent]);
}

FIRDailyHeartbeatCode GetHeartbeat(FIRApp* app) {
NSInteger GetHeartbeat(FIRApp* app) {
return [app.heartbeatLogger heartbeatCodeForToday];
}

Expand All @@ -51,11 +51,11 @@ FIRDailyHeartbeatCode GetHeartbeat(FIRApp* app) {

void FirebaseMetadataProviderApple::UpdateMetadata(
grpc::ClientContext& context) {
FIRDailyHeartbeatCode heartbeat = GetHeartbeat(app_);
NSInteger heartbeat = GetHeartbeat(app_);
// TODO(ncooke3): If support for notifying a heartbeat logger when a
// request fails is implemented, we will need to change the below
// code to place the heartbeat data back into heartbeat storage.
if (heartbeat != FIRDailyHeartbeatCodeNone) {
if (heartbeat != 0) {
context.AddMetadata(kXFirebaseClientLogTypeHeader,
std::to_string(heartbeat));
}
Expand Down

0 comments on commit d7527bd

Please sign in to comment.