Skip to content

update heartbeat to provide support for c++ sdks #5011

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

Merged
merged 2 commits into from
May 17, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ public static FirebaseApp getInstance() {
+ ". Make sure to call "
+ "FirebaseApp.initializeApp(Context) first.");
}
defaultApp.defaultHeartBeatController.get().registerHeartBeat();
return defaultApp;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,22 +75,43 @@ int getHeartBeatCount() {

synchronized void deleteAllHeartBeats() {
SharedPreferences.Editor editor = firebaseSharedPreferences.edit();
int counter = 0;
for (Map.Entry<String, ?> entry : this.firebaseSharedPreferences.getAll().entrySet()) {
if (entry.getValue() instanceof Set) {
editor.remove(entry.getKey());
// All other heartbeats other than the heartbeats stored today will be deleted.
Set<String> dates = (Set<String>) entry.getValue();
String today = getFormattedDate(System.currentTimeMillis());
String key = entry.getKey();
if (dates.contains(today)) {
Set<String> userAgentDateSet = new HashSet<>();
userAgentDateSet.add(today);
counter += 1;
editor.putStringSet(key, userAgentDateSet);
} else {
editor.remove(key);
}
}
}
editor.remove(HEART_BEAT_COUNT_TAG);
if (counter == 0) {
editor.remove(HEART_BEAT_COUNT_TAG);
} else {
editor.putLong(HEART_BEAT_COUNT_TAG, counter);
}

editor.commit();
}

synchronized List<HeartBeatResult> getAllHeartBeats() {
ArrayList<HeartBeatResult> heartBeatResults = new ArrayList<>();
for (Map.Entry<String, ?> entry : this.firebaseSharedPreferences.getAll().entrySet()) {
if (entry.getValue() instanceof Set) {
heartBeatResults.add(
HeartBeatResult.create(
entry.getKey(), new ArrayList<String>((Set<String>) entry.getValue())));
Set<String> dates = new HashSet<>((Set<String>) entry.getValue());
String today = getFormattedDate(System.currentTimeMillis());
dates.remove(today);
if (!dates.isEmpty()) {
heartBeatResults.add(
HeartBeatResult.create(entry.getKey(), new ArrayList<String>(dates)));
}
}
}
updateGlobalHeartBeat(System.currentTimeMillis());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import androidx.test.runner.AndroidJUnit4;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -179,14 +180,30 @@ public void shouldSendGlobalHeartBeat_answerIsNo() {
assertThat(heartBeatInfoStorage.shouldSendGlobalHeartBeat(1)).isFalse();
}

@Test
public void currentDayHeartbeatNotSent_updatesCorrectly() {
long millis = System.currentTimeMillis();
assertThat(heartBeatInfoStorage.getHeartBeatCount()).isEqualTo(0);
heartBeatInfoStorage.storeHeartBeat(millis, "test-agent");
assertThat(heartBeatInfoStorage.getHeartBeatCount()).isEqualTo(1);
assertThat(heartBeatInfoStorage.getAllHeartBeats().size()).isEqualTo(0);
heartBeatInfoStorage.deleteAllHeartBeats();
assertThat(heartBeatInfoStorage.getHeartBeatCount()).isEqualTo(1);
assertThat(heartBeatSharedPreferences.getStringSet("test-agent", new HashSet<>())).isNotEmpty();
heartBeatInfoStorage.storeHeartBeat(millis, "test-agent-1");
assertThat(heartBeatSharedPreferences.getStringSet("test-agent", new HashSet<>())).isEmpty();
assertThat(heartBeatSharedPreferences.getStringSet("test-agent-1", new HashSet<>()))
.isNotEmpty();
}

@Test
public void postHeartBeatCleanUp_worksCorrectly() {
long millis = System.currentTimeMillis();
// Store using new method
heartBeatInfoStorage.storeHeartBeat(millis, "test-agent");
// Get global heartbeat using old method
assertThat(heartBeatInfoStorage.shouldSendGlobalHeartBeat(millis)).isTrue();
assertThat(heartBeatInfoStorage.getAllHeartBeats().size()).isEqualTo(1);
assertThat(heartBeatInfoStorage.getAllHeartBeats().size()).isEqualTo(0);
heartBeatInfoStorage.postHeartBeatCleanUp();
assertThat(heartBeatInfoStorage.getAllHeartBeats().size()).isEqualTo(0);
// Try storing using new method again.
Expand Down