Refactor buildKeepaliveMetrics to return DailyKeepaliveInfoReported
This also updates the tests and prepares them to assert the other fields of DailyKeepaliveInfoReported. Currently, only DurationPerNumOfKeepalive is asserted. Bug: 273451360 Test: atest FrameworksNetTests Change-Id: I727849fa8a08d66fe276909b353a75861c23d775
This commit is contained in:
@@ -17,12 +17,15 @@
|
||||
package com.android.server.connectivity;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
|
||||
import android.os.Build;
|
||||
|
||||
import androidx.test.filters.SmallTest;
|
||||
|
||||
import com.android.metrics.DailykeepaliveInfoReported;
|
||||
import com.android.metrics.DurationForNumOfKeepalive;
|
||||
import com.android.metrics.DurationPerNumOfKeepalive;
|
||||
import com.android.testutils.DevSdkIgnoreRule;
|
||||
@@ -38,6 +41,7 @@ import org.mockito.MockitoAnnotations;
|
||||
@SmallTest
|
||||
@DevSdkIgnoreRule.IgnoreUpTo(Build.VERSION_CODES.TIRAMISU)
|
||||
public class KeepaliveStatsTrackerTest {
|
||||
private static final int TEST_UID = 1234;
|
||||
|
||||
private KeepaliveStatsTracker mKeepaliveStatsTracker;
|
||||
@Mock KeepaliveStatsTracker.Dependencies mDependencies;
|
||||
@@ -87,18 +91,38 @@ public class KeepaliveStatsTrackerTest {
|
||||
}
|
||||
}
|
||||
|
||||
private void assertDailyKeepaliveInfoReported(
|
||||
DailykeepaliveInfoReported dailyKeepaliveInfoReported,
|
||||
int[] expectRegisteredDurations,
|
||||
int[] expectActiveDurations) {
|
||||
// TODO(b/273451360) Assert these values when they are filled.
|
||||
assertFalse(dailyKeepaliveInfoReported.hasKeepaliveLifetimePerCarrier());
|
||||
assertFalse(dailyKeepaliveInfoReported.hasKeepaliveRequests());
|
||||
assertFalse(dailyKeepaliveInfoReported.hasAutomaticKeepaliveRequests());
|
||||
assertFalse(dailyKeepaliveInfoReported.hasDistinctUserCount());
|
||||
assertTrue(dailyKeepaliveInfoReported.getUidList().isEmpty());
|
||||
|
||||
final DurationPerNumOfKeepalive resultDurations =
|
||||
dailyKeepaliveInfoReported.getDurationPerNumOfKeepalive();
|
||||
assertDurationMetrics(expectRegisteredDurations, expectActiveDurations, resultDurations);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNoKeepalive() {
|
||||
final int writeTime = 5000;
|
||||
|
||||
setUptimeMillis(writeTime);
|
||||
final DurationPerNumOfKeepalive resultDurations =
|
||||
final DailykeepaliveInfoReported dailyKeepaliveInfoReported =
|
||||
mKeepaliveStatsTracker.buildKeepaliveMetrics();
|
||||
|
||||
// Expect that the durations are all in numOfKeepalive = 0.
|
||||
final int[] expectRegisteredDurations = new int[] {writeTime};
|
||||
final int[] expectActiveDurations = new int[] {writeTime};
|
||||
assertDurationMetrics(expectRegisteredDurations, expectActiveDurations, resultDurations);
|
||||
|
||||
assertDailyKeepaliveInfoReported(
|
||||
dailyKeepaliveInfoReported,
|
||||
expectRegisteredDurations,
|
||||
expectActiveDurations);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -117,14 +141,17 @@ public class KeepaliveStatsTrackerTest {
|
||||
mKeepaliveStatsTracker.onStartKeepalive();
|
||||
|
||||
setUptimeMillis(writeTime);
|
||||
final DurationPerNumOfKeepalive resultDurations =
|
||||
final DailykeepaliveInfoReported dailyKeepaliveInfoReported =
|
||||
mKeepaliveStatsTracker.buildKeepaliveMetrics();
|
||||
|
||||
// The keepalive is never stopped, expect the duration for numberOfKeepalive of 1 to range
|
||||
// from startTime to writeTime.
|
||||
final int[] expectRegisteredDurations = new int[] {startTime, writeTime - startTime};
|
||||
final int[] expectActiveDurations = new int[] {startTime, writeTime - startTime};
|
||||
assertDurationMetrics(expectRegisteredDurations, expectActiveDurations, resultDurations);
|
||||
assertDailyKeepaliveInfoReported(
|
||||
dailyKeepaliveInfoReported,
|
||||
expectRegisteredDurations,
|
||||
expectActiveDurations);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -147,7 +174,7 @@ public class KeepaliveStatsTrackerTest {
|
||||
mKeepaliveStatsTracker.onPauseKeepalive();
|
||||
|
||||
setUptimeMillis(writeTime);
|
||||
final DurationPerNumOfKeepalive resultDurations =
|
||||
final DailykeepaliveInfoReported dailyKeepaliveInfoReported =
|
||||
mKeepaliveStatsTracker.buildKeepaliveMetrics();
|
||||
|
||||
// The keepalive is paused but not stopped, expect the registered duration for
|
||||
@@ -156,7 +183,10 @@ public class KeepaliveStatsTrackerTest {
|
||||
final int[] expectRegisteredDurations = new int[] {startTime, writeTime - startTime};
|
||||
final int[] expectActiveDurations =
|
||||
new int[] {startTime + (writeTime - pauseTime), pauseTime - startTime};
|
||||
assertDurationMetrics(expectRegisteredDurations, expectActiveDurations, resultDurations);
|
||||
assertDailyKeepaliveInfoReported(
|
||||
dailyKeepaliveInfoReported,
|
||||
expectRegisteredDurations,
|
||||
expectActiveDurations);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -183,7 +213,7 @@ public class KeepaliveStatsTrackerTest {
|
||||
mKeepaliveStatsTracker.onResumeKeepalive();
|
||||
|
||||
setUptimeMillis(writeTime);
|
||||
final DurationPerNumOfKeepalive resultDurations =
|
||||
final DailykeepaliveInfoReported dailyKeepaliveInfoReported =
|
||||
mKeepaliveStatsTracker.buildKeepaliveMetrics();
|
||||
|
||||
// The keepalive is paused and resumed but not stopped, expect the registered duration for
|
||||
@@ -195,7 +225,10 @@ public class KeepaliveStatsTrackerTest {
|
||||
startTime + (resumeTime - pauseTime),
|
||||
(pauseTime - startTime) + (writeTime - resumeTime)
|
||||
};
|
||||
assertDurationMetrics(expectRegisteredDurations, expectActiveDurations, resultDurations);
|
||||
assertDailyKeepaliveInfoReported(
|
||||
dailyKeepaliveInfoReported,
|
||||
expectRegisteredDurations,
|
||||
expectActiveDurations);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -226,7 +259,7 @@ public class KeepaliveStatsTrackerTest {
|
||||
mKeepaliveStatsTracker.onStopKeepalive(/* wasActive= */ true);
|
||||
|
||||
setUptimeMillis(writeTime);
|
||||
final DurationPerNumOfKeepalive resultDurations =
|
||||
final DailykeepaliveInfoReported dailyKeepaliveInfoReported =
|
||||
mKeepaliveStatsTracker.buildKeepaliveMetrics();
|
||||
|
||||
// The keepalive is now stopped, expect the registered duration for numberOfKeepalive of 1
|
||||
@@ -239,7 +272,10 @@ public class KeepaliveStatsTrackerTest {
|
||||
startTime + (resumeTime - pauseTime) + (writeTime - stopTime),
|
||||
(pauseTime - startTime) + (stopTime - resumeTime)
|
||||
};
|
||||
assertDurationMetrics(expectRegisteredDurations, expectActiveDurations, resultDurations);
|
||||
assertDailyKeepaliveInfoReported(
|
||||
dailyKeepaliveInfoReported,
|
||||
expectRegisteredDurations,
|
||||
expectActiveDurations);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -266,7 +302,7 @@ public class KeepaliveStatsTrackerTest {
|
||||
mKeepaliveStatsTracker.onStopKeepalive(/* wasActive= */ false);
|
||||
|
||||
setUptimeMillis(writeTime);
|
||||
final DurationPerNumOfKeepalive resultDurations =
|
||||
final DailykeepaliveInfoReported dailyKeepaliveInfoReported =
|
||||
mKeepaliveStatsTracker.buildKeepaliveMetrics();
|
||||
|
||||
// The keepalive is stopped while paused, expect the registered duration for
|
||||
@@ -276,7 +312,10 @@ public class KeepaliveStatsTrackerTest {
|
||||
new int[] {startTime + (writeTime - stopTime), stopTime - startTime};
|
||||
final int[] expectActiveDurations =
|
||||
new int[] {startTime + (writeTime - pauseTime), (pauseTime - startTime)};
|
||||
assertDurationMetrics(expectRegisteredDurations, expectActiveDurations, resultDurations);
|
||||
assertDailyKeepaliveInfoReported(
|
||||
dailyKeepaliveInfoReported,
|
||||
expectRegisteredDurations,
|
||||
expectActiveDurations);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -310,7 +349,7 @@ public class KeepaliveStatsTrackerTest {
|
||||
mKeepaliveStatsTracker.onStopKeepalive(/* wasActive= */ true);
|
||||
|
||||
setUptimeMillis(writeTime);
|
||||
final DurationPerNumOfKeepalive resultDurations =
|
||||
final DailykeepaliveInfoReported dailyKeepaliveInfoReported =
|
||||
mKeepaliveStatsTracker.buildKeepaliveMetrics();
|
||||
|
||||
final int[] expectRegisteredDurations =
|
||||
@@ -322,7 +361,10 @@ public class KeepaliveStatsTrackerTest {
|
||||
+ /* sum of (Pause - Resume) */ (700)
|
||||
+ (stopTime - pauseResumeTimes[5])
|
||||
};
|
||||
assertDurationMetrics(expectRegisteredDurations, expectActiveDurations, resultDurations);
|
||||
assertDailyKeepaliveInfoReported(
|
||||
dailyKeepaliveInfoReported,
|
||||
expectRegisteredDurations,
|
||||
expectActiveDurations);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -367,7 +409,7 @@ public class KeepaliveStatsTrackerTest {
|
||||
mKeepaliveStatsTracker.onStopKeepalive(/* wasActive= */ true);
|
||||
|
||||
setUptimeMillis(writeTime);
|
||||
final DurationPerNumOfKeepalive resultDurations =
|
||||
final DailykeepaliveInfoReported dailyKeepaliveInfoReported =
|
||||
mKeepaliveStatsTracker.buildKeepaliveMetrics();
|
||||
|
||||
// With two keepalives, the number of concurrent keepalives can vary from 0-2 depending on
|
||||
@@ -380,6 +422,7 @@ public class KeepaliveStatsTrackerTest {
|
||||
// 2 registered keepalives between keepalive2 start and keepalive1 stop.
|
||||
stopTime1 - startTime2
|
||||
};
|
||||
|
||||
final int[] expectActiveDurations =
|
||||
new int[] {
|
||||
// 0 active keepalives when keepalive1 is paused before keepalive2 starts.
|
||||
@@ -395,7 +438,10 @@ public class KeepaliveStatsTrackerTest {
|
||||
// 2 active keepalives before keepalive2 is paused and before keepalive1 stops.
|
||||
(pauseTime2 - resumeTime1) + (stopTime1 - resumeTime2)
|
||||
};
|
||||
assertDurationMetrics(expectRegisteredDurations, expectActiveDurations, resultDurations);
|
||||
assertDailyKeepaliveInfoReported(
|
||||
dailyKeepaliveInfoReported,
|
||||
expectRegisteredDurations,
|
||||
expectActiveDurations);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -416,34 +462,43 @@ public class KeepaliveStatsTrackerTest {
|
||||
mKeepaliveStatsTracker.onStartKeepalive();
|
||||
|
||||
setUptimeMillis(writeTime);
|
||||
final DurationPerNumOfKeepalive resultDurations =
|
||||
final DailykeepaliveInfoReported dailyKeepaliveInfoReported =
|
||||
mKeepaliveStatsTracker.buildKeepaliveMetrics();
|
||||
|
||||
// Same expect as testOneKeepalive_startOnly
|
||||
final int[] expectRegisteredDurations = new int[] {startTime, writeTime - startTime};
|
||||
final int[] expectActiveDurations = new int[] {startTime, writeTime - startTime};
|
||||
assertDurationMetrics(expectRegisteredDurations, expectActiveDurations, resultDurations);
|
||||
assertDailyKeepaliveInfoReported(
|
||||
dailyKeepaliveInfoReported,
|
||||
expectRegisteredDurations,
|
||||
expectActiveDurations);
|
||||
|
||||
// Reset metrics
|
||||
mKeepaliveStatsTracker.resetMetrics();
|
||||
|
||||
final DurationPerNumOfKeepalive resultDurations2 =
|
||||
final DailykeepaliveInfoReported dailyKeepaliveInfoReported2 =
|
||||
mKeepaliveStatsTracker.buildKeepaliveMetrics();
|
||||
// Expect the stored durations to be 0 but still contain the number of keepalive = 1.
|
||||
assertDurationMetrics(new int[] {0, 0}, new int[] {0, 0}, resultDurations2);
|
||||
assertDailyKeepaliveInfoReported(
|
||||
dailyKeepaliveInfoReported2,
|
||||
/* expectRegisteredDurations= */ new int[] {0, 0},
|
||||
/* expectActiveDurations= */ new int[] {0, 0});
|
||||
|
||||
// Expect that the keepalive is still registered after resetting so it can be stopped.
|
||||
setUptimeMillis(stopTime);
|
||||
mKeepaliveStatsTracker.onStopKeepalive(/* wasActive= */ true);
|
||||
|
||||
setUptimeMillis(writeTime2);
|
||||
final DurationPerNumOfKeepalive resultDurations3 =
|
||||
final DailykeepaliveInfoReported dailyKeepaliveInfoReported3 =
|
||||
mKeepaliveStatsTracker.buildKeepaliveMetrics();
|
||||
|
||||
final int[] expectRegisteredDurations2 =
|
||||
new int[] {writeTime2 - stopTime, stopTime - writeTime};
|
||||
final int[] expectActiveDurations2 =
|
||||
new int[] {writeTime2 - stopTime, stopTime - writeTime};
|
||||
assertDurationMetrics(expectRegisteredDurations2, expectActiveDurations2, resultDurations3);
|
||||
assertDailyKeepaliveInfoReported(
|
||||
dailyKeepaliveInfoReported3,
|
||||
expectRegisteredDurations2,
|
||||
expectActiveDurations2);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user