Skip to content

Fix heartbeat tests #3413

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 3 commits into from
Feb 7, 2022
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 @@ -46,6 +46,7 @@
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.annotation.Config;

@RunWith(AndroidJUnit4.class)
public class DefaultHeartBeatControllerTest {
Expand Down Expand Up @@ -97,6 +98,7 @@ public void getHeartBeatCode_noHeartBeat() {
assertThat(heartBeatController.getHeartBeatCode("fire-iid").getCode()).isEqualTo(0);
}

@Config(sdk = 29)
@Test
public void generateHeartBeat_oneHeartBeat()
throws ExecutionException, InterruptedException, JSONException, IOException {
Expand All @@ -123,6 +125,7 @@ public void generateHeartBeat_oneHeartBeat()
assertThat(getOnCompleteListener.await().replace("\n", "")).isEqualTo(expected);
}

@Config(sdk = 29)
@Test
public void firstNewThenOld_synchronizedCorrectly()
throws ExecutionException, InterruptedException {
Expand All @@ -147,6 +150,7 @@ public void firstNewThenOld_synchronizedCorrectly()
assertThat(heartBeatCode).isEqualTo(0);
}

@Config(sdk = 29)
@Test
public void firstOldThenNew_synchronizedCorrectly()
throws ExecutionException, InterruptedException, IOException {
Expand Down Expand Up @@ -176,6 +180,7 @@ public void firstOldThenNew_synchronizedCorrectly()
assertThat(output.replace("\n", "")).isEqualTo(emptyString);
}

@Config(sdk = 29)
@Test
public void generateHeartBeat_twoHeartBeatsSameUserAgent()
throws ExecutionException, InterruptedException, JSONException, IOException {
Expand Down Expand Up @@ -220,6 +225,7 @@ private String compress(String str) throws IOException {
return out.toString("UTF-8");
}

@Config(sdk = 29)
@Test
public void generateHeartBeat_twoHeartBeatstwoUserAgents()
throws ExecutionException, InterruptedException, JSONException, IOException {
Expand All @@ -239,6 +245,7 @@ public void generateHeartBeat_twoHeartBeatstwoUserAgents()
.registerHeartBeat()
.addOnCompleteListener(executor, storeOnCompleteListener);
storeOnCompleteListener.await();
Thread.sleep(1000);
verify(storage, times(2)).storeHeartBeat(anyLong(), anyString());
heartBeatController
.getHeartBeatsHeader()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@
import java.util.ArrayList;
import java.util.Collections;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.annotation.Config;

@RunWith(AndroidJUnit4.class)
public class HeartBeatInfoStorageTest {
Expand All @@ -37,17 +39,17 @@ public class HeartBeatInfoStorageTest {
private HeartBeatInfoStorage heartBeatInfoStorage =
new HeartBeatInfoStorage(heartBeatSharedPreferences);

@After
public void tearDown() {
@Before
public void setUp() {
heartBeatSharedPreferences.edit().clear().apply();
}

@Test
public void shouldSendSdkHeartBeat_answerIsNo() {
heartBeatSharedPreferences.edit().putLong(testSdk, 1).apply();
assertThat(heartBeatInfoStorage.shouldSendSdkHeartBeat(testSdk, 1)).isFalse();
@After
public void tearDown() {
heartBeatSharedPreferences.edit().clear().apply();
}

@Config(sdk = 29)
@Test
public void storeOneHeartbeat_storesProperly() {
assertThat(heartBeatInfoStorage.getHeartBeatCount()).isEqualTo(0);
Expand All @@ -73,6 +75,7 @@ public void storeOneHeartbeat_storesProperly() {
assertThat(results.size()).isEqualTo(0);
}

@Config(sdk = 29)
@Test
public void storeTwoHeartbeat_storesProperly() {
assertThat(heartBeatInfoStorage.getHeartBeatCount()).isEqualTo(0);
Expand All @@ -82,18 +85,24 @@ public void storeTwoHeartbeat_storesProperly() {
ArrayList<HeartBeatResult> results =
(ArrayList<HeartBeatResult>) heartBeatInfoStorage.getAllHeartBeats();
assertThat(results.size()).isEqualTo(2);
assertThat(results.get(0).getUserAgent()).isEqualTo("test-agent");
assertThat(results.get(0).getUsedDates())
int userAgentTest = 0, userAgentTest1 = 1;
if (results.get(0).getUserAgent().contains("-1")) {
userAgentTest = 1;
userAgentTest1 = 0;
}
assertThat(results.get(userAgentTest).getUserAgent()).isEqualTo("test-agent");
assertThat(results.get(userAgentTest).getUsedDates())
.isEqualTo(new ArrayList<String>(Collections.singleton("1970-01-01")));
assertThat(results.get(1).getUserAgent()).isEqualTo("test-agent-1");
assertThat(results.get(1).getUsedDates())
assertThat(results.get(userAgentTest1).getUserAgent()).isEqualTo("test-agent-1");
assertThat(results.get(userAgentTest1).getUsedDates())
.isEqualTo(new ArrayList<String>(Collections.singleton("1970-01-02")));
heartBeatInfoStorage.deleteAllHeartBeats();
assertThat(heartBeatInfoStorage.getHeartBeatCount()).isEqualTo(0);
results = (ArrayList<HeartBeatResult>) heartBeatInfoStorage.getAllHeartBeats();
assertThat(results.size()).isEqualTo(0);
}

@Config(sdk = 29)
@Test
public void storeExcessHeartBeats_cleanUpProperly() {
for (int i = 0; i < HEART_BEAT_COUNT_LIMIT - 1; i++) {
Expand All @@ -118,9 +127,14 @@ public void storeExcessHeartBeats_cleanUpProperly() {
heartBeatInfoStorage.storeHeartBeat((HEART_BEAT_COUNT_LIMIT) * (86400001L), "test-agent-1");
results = (ArrayList<HeartBeatResult>) heartBeatInfoStorage.getAllHeartBeats();
assertThat(results.size()).isEqualTo(2);
assertThat(results.get(0).getUsedDates().size()).isEqualTo(HEART_BEAT_COUNT_LIMIT - 2);
assertThat(results.get(0).getUsedDates()).doesNotContain("1970-01-01");
assertThat(results.get(0).getUsedDates()).doesNotContain("1970-01-02");
int testAgentIndex = 0;
if (results.get(1).getUserAgent().equals("test-agent")) {
testAgentIndex = 1;
}
assertThat(results.get(testAgentIndex).getUsedDates().size())
.isEqualTo(HEART_BEAT_COUNT_LIMIT - 2);
assertThat(results.get(testAgentIndex).getUsedDates()).doesNotContain("1970-01-01");
assertThat(results.get(testAgentIndex).getUsedDates()).doesNotContain("1970-01-02");

heartBeatInfoStorage.deleteAllHeartBeats();
assertThat(heartBeatInfoStorage.getHeartBeatCount()).isEqualTo(0);
Expand Down