Fill the other values for DailyKeepaliveInfoReported

Store the number of requests and app uids on start keepalives. The
values are also stored in KeepaliveStats and can be recalculated from
KeepaliveStats when reset. Fill the DailyKeepaliveInfoReported proto
with these values.

Bug: 273451360
Test: atest FrameworksNetTests
Change-Id: Ifb9a0048896d31c8ea897cf14768e94dcfd3b010
This commit is contained in:
Hansen Kurli
2023-04-25 06:30:35 +00:00
parent c48d856976
commit 91adc204d8
4 changed files with 172 additions and 17 deletions

View File

@@ -459,7 +459,9 @@ public class AutomaticOnOffKeepaliveTracker {
autoKi.getNetwork(), autoKi.getNetwork(),
autoKi.mKi.getSlot(), autoKi.mKi.getSlot(),
autoKi.mKi.getNai().networkCapabilities, autoKi.mKi.getNai().networkCapabilities,
autoKi.mKi.getKeepaliveIntervalSec()); autoKi.mKi.getKeepaliveIntervalSec(),
autoKi.mKi.getUid(),
STATE_ALWAYS_ON != autoKi.mAutomaticOnOffState);
// Add automatic on/off request into list to track its life cycle. // Add automatic on/off request into list to track its life cycle.
try { try {

View File

@@ -45,11 +45,12 @@ import com.android.modules.utils.BackgroundThread;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.Set;
// TODO(b/273451360): Also track DailykeepaliveInfoReported
/** /**
* Tracks carrier and duration metrics of automatic on/off keepalives. * Tracks carrier and duration metrics of automatic on/off keepalives.
* *
@@ -81,6 +82,10 @@ public class KeepaliveStatsTracker {
public final int transportTypes; public final int transportTypes;
// The keepalive interval in millis. // The keepalive interval in millis.
public final int intervalMs; public final int intervalMs;
// The uid of the app that requested the keepalive.
public final int appUid;
// Indicates if the keepalive is an automatic keepalive.
public final boolean isAutoKeepalive;
// Snapshot of the lifetime stats // Snapshot of the lifetime stats
public static class LifetimeStats { public static class LifetimeStats {
@@ -123,10 +128,18 @@ public class KeepaliveStatsTracker {
return mKeepaliveActive; return mKeepaliveActive;
} }
KeepaliveStats(int carrierId, int transportTypes, int intervalSeconds, long timeNow) { KeepaliveStats(
int carrierId,
int transportTypes,
int intervalSeconds,
int appUid,
boolean isAutoKeepalive,
long timeNow) {
this.carrierId = carrierId; this.carrierId = carrierId;
this.transportTypes = transportTypes; this.transportTypes = transportTypes;
this.intervalMs = intervalSeconds * 1000; this.intervalMs = intervalSeconds * 1000;
this.appUid = appUid;
this.isAutoKeepalive = isAutoKeepalive;
mLastUpdateLifetimeTimestamp = timeNow; mLastUpdateLifetimeTimestamp = timeNow;
} }
@@ -217,6 +230,10 @@ public class KeepaliveStatsTracker {
final Map<LifetimeKey, KeepaliveLifetimeForCarrier.Builder> mAggregateKeepaliveLifetime = final Map<LifetimeKey, KeepaliveLifetimeForCarrier.Builder> mAggregateKeepaliveLifetime =
new HashMap<>(); new HashMap<>();
private final Set<Integer> mAppUids = new HashSet<Integer>();
private int mNumKeepaliveRequests = 0;
private int mNumAutomaticKeepaliveRequests = 0;
private int mNumRegisteredKeepalive = 0; private int mNumRegisteredKeepalive = 0;
private int mNumActiveKeepalive = 0; private int mNumActiveKeepalive = 0;
@@ -375,14 +392,20 @@ public class KeepaliveStatsTracker {
@NonNull Network network, @NonNull Network network,
int slot, int slot,
@NonNull NetworkCapabilities nc, @NonNull NetworkCapabilities nc,
int intervalSeconds) { int intervalSeconds,
int appUid,
boolean isAutoKeepalive) {
ensureRunningOnHandlerThread(); ensureRunningOnHandlerThread();
final int keepaliveId = getKeepaliveId(network, slot); final int keepaliveId = getKeepaliveId(network, slot);
if (mKeepaliveStatsPerId.contains(keepaliveId)) { if (mKeepaliveStatsPerId.contains(keepaliveId)) {
throw new IllegalArgumentException( throw new IllegalArgumentException(
"Attempt to start keepalive stats on a known network, slot pair"); "Attempt to start keepalive stats on a known network, slot pair");
} }
mNumKeepaliveRequests++;
if (isAutoKeepalive) mNumAutomaticKeepaliveRequests++;
mAppUids.add(appUid);
final long timeNow = mDependencies.getUptimeMillis(); final long timeNow = mDependencies.getUptimeMillis();
updateDurationsPerNumOfKeepalive(timeNow); updateDurationsPerNumOfKeepalive(timeNow);
@@ -391,7 +414,12 @@ public class KeepaliveStatsTracker {
final KeepaliveStats newKeepaliveStats = final KeepaliveStats newKeepaliveStats =
new KeepaliveStats( new KeepaliveStats(
getCarrierId(nc), getTransportTypes(nc), intervalSeconds, timeNow); getCarrierId(nc),
getTransportTypes(nc),
intervalSeconds,
appUid,
isAutoKeepalive,
timeNow);
mKeepaliveStatsPerId.put(keepaliveId, newKeepaliveStats); mKeepaliveStatsPerId.put(keepaliveId, newKeepaliveStats);
} }
@@ -548,9 +576,12 @@ public class KeepaliveStatsTracker {
final DailykeepaliveInfoReported.Builder dailyKeepaliveInfoReported = final DailykeepaliveInfoReported.Builder dailyKeepaliveInfoReported =
DailykeepaliveInfoReported.newBuilder(); DailykeepaliveInfoReported.newBuilder();
// TODO(b/273451360): fill all the other values and write to ConnectivityStatsLog.
dailyKeepaliveInfoReported.setDurationPerNumOfKeepalive(durationPerNumOfKeepalive); dailyKeepaliveInfoReported.setDurationPerNumOfKeepalive(durationPerNumOfKeepalive);
dailyKeepaliveInfoReported.setKeepaliveLifetimePerCarrier(keepaliveLifetimePerCarrier); dailyKeepaliveInfoReported.setKeepaliveLifetimePerCarrier(keepaliveLifetimePerCarrier);
dailyKeepaliveInfoReported.setKeepaliveRequests(mNumKeepaliveRequests);
dailyKeepaliveInfoReported.setAutomaticKeepaliveRequests(mNumAutomaticKeepaliveRequests);
dailyKeepaliveInfoReported.setDistinctUserCount(mAppUids.size());
dailyKeepaliveInfoReported.addAllUid(mAppUids);
return dailyKeepaliveInfoReported.build(); return dailyKeepaliveInfoReported.build();
} }
@@ -568,12 +599,22 @@ public class KeepaliveStatsTracker {
final DailykeepaliveInfoReported metrics = buildKeepaliveMetrics(timeNow); final DailykeepaliveInfoReported metrics = buildKeepaliveMetrics(timeNow);
mDurationPerNumOfKeepalive.clear(); mDurationPerNumOfKeepalive.clear();
mAggregateKeepaliveLifetime.clear();
mAppUids.clear();
mNumKeepaliveRequests = 0;
mNumAutomaticKeepaliveRequests = 0;
// Update the metrics with the existing keepalives.
ensureDurationPerNumOfKeepaliveSize(); ensureDurationPerNumOfKeepaliveSize();
mAggregateKeepaliveLifetime.clear(); mAggregateKeepaliveLifetime.clear();
// Reset the stats for existing keepalives // Reset the stats for existing keepalives
for (int i = 0; i < mKeepaliveStatsPerId.size(); i++) { for (int i = 0; i < mKeepaliveStatsPerId.size(); i++) {
mKeepaliveStatsPerId.valueAt(i).resetLifetimeStats(timeNow); final KeepaliveStats keepaliveStats = mKeepaliveStatsPerId.valueAt(i);
keepaliveStats.resetLifetimeStats(timeNow);
mAppUids.add(keepaliveStats.appUid);
mNumKeepaliveRequests++;
if (keepaliveStats.isAutoKeepalive) mNumAutomaticKeepaliveRequests++;
} }
return metrics; return metrics;

View File

@@ -271,6 +271,10 @@ public class KeepaliveTracker {
return mInterval; return mInterval;
} }
public int getUid() {
return mUid;
}
private int checkNetworkConnected() { private int checkNetworkConnected() {
if (!mNai.networkInfo.isConnectedOrConnecting()) { if (!mNai.networkInfo.isConnectedOrConnecting()) {
return ERROR_INVALID_NETWORK; return ERROR_INVALID_NETWORK;

View File

@@ -21,10 +21,9 @@ import static android.net.NetworkCapabilities.TRANSPORT_WIFI;
import static com.android.testutils.HandlerUtils.visibleOnHandlerThread; import static com.android.testutils.HandlerUtils.visibleOnHandlerThread;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.ArgumentMatchers.anyLong;
@@ -54,6 +53,7 @@ import com.android.metrics.DurationPerNumOfKeepalive;
import com.android.metrics.KeepaliveLifetimeForCarrier; import com.android.metrics.KeepaliveLifetimeForCarrier;
import com.android.metrics.KeepaliveLifetimePerCarrier; import com.android.metrics.KeepaliveLifetimePerCarrier;
import com.android.modules.utils.BackgroundThread; import com.android.modules.utils.BackgroundThread;
import com.android.net.module.util.CollectionUtils;
import com.android.testutils.DevSdkIgnoreRule; import com.android.testutils.DevSdkIgnoreRule;
import com.android.testutils.DevSdkIgnoreRunner; import com.android.testutils.DevSdkIgnoreRunner;
import com.android.testutils.HandlerUtils; import com.android.testutils.HandlerUtils;
@@ -89,6 +89,7 @@ public class KeepaliveStatsTrackerTest {
buildCellNetworkCapabilitiesWithSubId(TEST_SUB_ID_1); buildCellNetworkCapabilitiesWithSubId(TEST_SUB_ID_1);
private static final NetworkCapabilities TEST_NETWORK_CAPABILITIES_2 = private static final NetworkCapabilities TEST_NETWORK_CAPABILITIES_2 =
buildCellNetworkCapabilitiesWithSubId(TEST_SUB_ID_2); buildCellNetworkCapabilitiesWithSubId(TEST_SUB_ID_2);
private static final int TEST_UID = 1234;
private static NetworkCapabilities buildCellNetworkCapabilitiesWithSubId(int subId) { private static NetworkCapabilities buildCellNetworkCapabilitiesWithSubId(int subId) {
final TelephonyNetworkSpecifier telephonyNetworkSpecifier = final TelephonyNetworkSpecifier telephonyNetworkSpecifier =
@@ -238,9 +239,15 @@ public class KeepaliveStatsTrackerTest {
private void onStartKeepalive( private void onStartKeepalive(
long time, int slot, NetworkCapabilities nc, int intervalSeconds) { long time, int slot, NetworkCapabilities nc, int intervalSeconds) {
onStartKeepalive(time, slot, nc, intervalSeconds, TEST_UID, /* isAutoKeepalive= */ true);
}
private void onStartKeepalive(long time, int slot, NetworkCapabilities nc, int intervalSeconds,
int uid, boolean isAutoKeepalive) {
setUptimeMillis(time); setUptimeMillis(time);
visibleOnHandlerThread(mTestHandler, () -> visibleOnHandlerThread(mTestHandler, () ->
mKeepaliveStatsTracker.onStartKeepalive(TEST_NETWORK, slot, nc, intervalSeconds)); mKeepaliveStatsTracker.onStartKeepalive(TEST_NETWORK, slot, nc, intervalSeconds,
uid, isAutoKeepalive));
} }
private void onPauseKeepalive(long time, int slot) { private void onPauseKeepalive(long time, int slot) {
@@ -270,7 +277,9 @@ public class KeepaliveStatsTrackerTest {
TEST_NETWORK, TEST_NETWORK,
TEST_SLOT, TEST_SLOT,
TEST_NETWORK_CAPABILITIES, TEST_NETWORK_CAPABILITIES,
TEST_KEEPALIVE_INTERVAL_SEC)); TEST_KEEPALIVE_INTERVAL_SEC,
TEST_UID,
/* isAutoKeepalive */ true));
assertThrows( assertThrows(
IllegalStateException.class, IllegalStateException.class,
() -> mKeepaliveStatsTracker.onPauseKeepalive(TEST_NETWORK, TEST_SLOT)); () -> mKeepaliveStatsTracker.onPauseKeepalive(TEST_NETWORK, TEST_SLOT));
@@ -378,14 +387,20 @@ public class KeepaliveStatsTrackerTest {
private void assertDailyKeepaliveInfoReported( private void assertDailyKeepaliveInfoReported(
DailykeepaliveInfoReported dailyKeepaliveInfoReported, DailykeepaliveInfoReported dailyKeepaliveInfoReported,
int expectRequestsCount,
int expectAutoRequestsCount,
int[] expectAppUids,
int[] expectRegisteredDurations, int[] expectRegisteredDurations,
int[] expectActiveDurations, int[] expectActiveDurations,
KeepaliveCarrierStats[] expectKeepaliveCarrierStatsArray) { KeepaliveCarrierStats[] expectKeepaliveCarrierStatsArray) {
// TODO(b/273451360) Assert these values when they are filled. assertEquals(expectRequestsCount, dailyKeepaliveInfoReported.getKeepaliveRequests());
assertFalse(dailyKeepaliveInfoReported.hasKeepaliveRequests()); assertEquals(
assertFalse(dailyKeepaliveInfoReported.hasAutomaticKeepaliveRequests()); expectAutoRequestsCount,
assertFalse(dailyKeepaliveInfoReported.hasDistinctUserCount()); dailyKeepaliveInfoReported.getAutomaticKeepaliveRequests());
assertTrue(dailyKeepaliveInfoReported.getUidList().isEmpty()); assertEquals(expectAppUids.length, dailyKeepaliveInfoReported.getDistinctUserCount());
final int[] uidArray = CollectionUtils.toIntArray(dailyKeepaliveInfoReported.getUidList());
assertArrayEquals(expectAppUids, uidArray);
final DurationPerNumOfKeepalive actualDurations = final DurationPerNumOfKeepalive actualDurations =
dailyKeepaliveInfoReported.getDurationPerNumOfKeepalive(); dailyKeepaliveInfoReported.getDurationPerNumOfKeepalive();
@@ -410,6 +425,9 @@ public class KeepaliveStatsTrackerTest {
assertDailyKeepaliveInfoReported( assertDailyKeepaliveInfoReported(
dailyKeepaliveInfoReported, dailyKeepaliveInfoReported,
/* expectRequestsCount= */ 0,
/* expectAutoRequestsCount= */ 0,
/* expectAppUids= */ new int[0],
expectRegisteredDurations, expectRegisteredDurations,
expectActiveDurations, expectActiveDurations,
new KeepaliveCarrierStats[0]); new KeepaliveCarrierStats[0]);
@@ -438,6 +456,9 @@ public class KeepaliveStatsTrackerTest {
final int[] expectActiveDurations = new int[] {startTime, writeTime - startTime}; final int[] expectActiveDurations = new int[] {startTime, writeTime - startTime};
assertDailyKeepaliveInfoReported( assertDailyKeepaliveInfoReported(
dailyKeepaliveInfoReported, dailyKeepaliveInfoReported,
/* expectRequestsCount= */ 1,
/* expectAutoRequestsCount= */ 1,
/* expectAppUids= */ new int[] {TEST_UID},
expectRegisteredDurations, expectRegisteredDurations,
expectActiveDurations, expectActiveDurations,
new KeepaliveCarrierStats[] { new KeepaliveCarrierStats[] {
@@ -473,6 +494,9 @@ public class KeepaliveStatsTrackerTest {
new int[] {startTime + (writeTime - pauseTime), pauseTime - startTime}; new int[] {startTime + (writeTime - pauseTime), pauseTime - startTime};
assertDailyKeepaliveInfoReported( assertDailyKeepaliveInfoReported(
dailyKeepaliveInfoReported, dailyKeepaliveInfoReported,
/* expectRequestsCount= */ 1,
/* expectAutoRequestsCount= */ 1,
/* expectAppUids= */ new int[] {TEST_UID},
expectRegisteredDurations, expectRegisteredDurations,
expectActiveDurations, expectActiveDurations,
new KeepaliveCarrierStats[] { new KeepaliveCarrierStats[] {
@@ -514,6 +538,9 @@ public class KeepaliveStatsTrackerTest {
}; };
assertDailyKeepaliveInfoReported( assertDailyKeepaliveInfoReported(
dailyKeepaliveInfoReported, dailyKeepaliveInfoReported,
/* expectRequestsCount= */ 1,
/* expectAutoRequestsCount= */ 1,
/* expectAppUids= */ new int[] {TEST_UID},
expectRegisteredDurations, expectRegisteredDurations,
expectActiveDurations, expectActiveDurations,
new KeepaliveCarrierStats[] { new KeepaliveCarrierStats[] {
@@ -559,6 +586,9 @@ public class KeepaliveStatsTrackerTest {
}; };
assertDailyKeepaliveInfoReported( assertDailyKeepaliveInfoReported(
dailyKeepaliveInfoReported, dailyKeepaliveInfoReported,
/* expectRequestsCount= */ 1,
/* expectAutoRequestsCount= */ 1,
/* expectAppUids= */ new int[] {TEST_UID},
expectRegisteredDurations, expectRegisteredDurations,
expectActiveDurations, expectActiveDurations,
new KeepaliveCarrierStats[] { new KeepaliveCarrierStats[] {
@@ -598,6 +628,9 @@ public class KeepaliveStatsTrackerTest {
new int[] {startTime + (writeTime - pauseTime), (pauseTime - startTime)}; new int[] {startTime + (writeTime - pauseTime), (pauseTime - startTime)};
assertDailyKeepaliveInfoReported( assertDailyKeepaliveInfoReported(
dailyKeepaliveInfoReported, dailyKeepaliveInfoReported,
/* expectRequestsCount= */ 1,
/* expectAutoRequestsCount= */ 1,
/* expectAppUids= */ new int[] {TEST_UID},
expectRegisteredDurations, expectRegisteredDurations,
expectActiveDurations, expectActiveDurations,
new KeepaliveCarrierStats[] { new KeepaliveCarrierStats[] {
@@ -646,6 +679,9 @@ public class KeepaliveStatsTrackerTest {
}; };
assertDailyKeepaliveInfoReported( assertDailyKeepaliveInfoReported(
dailyKeepaliveInfoReported, dailyKeepaliveInfoReported,
/* expectRequestsCount= */ 1,
/* expectAutoRequestsCount= */ 1,
/* expectAppUids= */ new int[] {TEST_UID},
expectRegisteredDurations, expectRegisteredDurations,
expectActiveDurations, expectActiveDurations,
new KeepaliveCarrierStats[] { new KeepaliveCarrierStats[] {
@@ -719,6 +755,9 @@ public class KeepaliveStatsTrackerTest {
assertDailyKeepaliveInfoReported( assertDailyKeepaliveInfoReported(
dailyKeepaliveInfoReported, dailyKeepaliveInfoReported,
/* expectRequestsCount= */ 2,
/* expectAutoRequestsCount= */ 2,
/* expectAppUids= */ new int[] {TEST_UID},
expectRegisteredDurations, expectRegisteredDurations,
expectActiveDurations, expectActiveDurations,
// The carrier stats are aggregated here since the keepalives have the same // The carrier stats are aggregated here since the keepalives have the same
@@ -754,6 +793,9 @@ public class KeepaliveStatsTrackerTest {
final int[] expectActiveDurations = new int[] {startTime, writeTime - startTime}; final int[] expectActiveDurations = new int[] {startTime, writeTime - startTime};
assertDailyKeepaliveInfoReported( assertDailyKeepaliveInfoReported(
dailyKeepaliveInfoReported, dailyKeepaliveInfoReported,
/* expectRequestsCount= */ 1,
/* expectAutoRequestsCount= */ 1,
/* expectAppUids= */ new int[] {TEST_UID},
expectRegisteredDurations, expectRegisteredDurations,
expectActiveDurations, expectActiveDurations,
new KeepaliveCarrierStats[] { new KeepaliveCarrierStats[] {
@@ -767,6 +809,9 @@ public class KeepaliveStatsTrackerTest {
// Expect the stored durations to be 0 but still contain the number of keepalive = 1. // Expect the stored durations to be 0 but still contain the number of keepalive = 1.
assertDailyKeepaliveInfoReported( assertDailyKeepaliveInfoReported(
dailyKeepaliveInfoReported2, dailyKeepaliveInfoReported2,
/* expectRequestsCount= */ 1,
/* expectAutoRequestsCount= */ 1,
/* expectAppUids= */ new int[] {TEST_UID},
/* expectRegisteredDurations= */ new int[] {0, 0}, /* expectRegisteredDurations= */ new int[] {0, 0},
/* expectActiveDurations= */ new int[] {0, 0}, /* expectActiveDurations= */ new int[] {0, 0},
new KeepaliveCarrierStats[] {getDefaultCarrierStats(0, 0)}); new KeepaliveCarrierStats[] {getDefaultCarrierStats(0, 0)});
@@ -783,6 +828,9 @@ public class KeepaliveStatsTrackerTest {
new int[] {writeTime2 - stopTime, stopTime - writeTime}; new int[] {writeTime2 - stopTime, stopTime - writeTime};
assertDailyKeepaliveInfoReported( assertDailyKeepaliveInfoReported(
dailyKeepaliveInfoReported3, dailyKeepaliveInfoReported3,
/* expectRequestsCount= */ 1,
/* expectAutoRequestsCount= */ 1,
/* expectAppUids= */ new int[] {TEST_UID},
expectRegisteredDurations2, expectRegisteredDurations2,
expectActiveDurations2, expectActiveDurations2,
new KeepaliveCarrierStats[] { new KeepaliveCarrierStats[] {
@@ -844,6 +892,9 @@ public class KeepaliveStatsTrackerTest {
assertDailyKeepaliveInfoReported( assertDailyKeepaliveInfoReported(
dailyKeepaliveInfoReported, dailyKeepaliveInfoReported,
/* expectRequestsCount= */ 2,
/* expectAutoRequestsCount= */ 2,
/* expectAppUids= */ new int[] {TEST_UID},
expectRegisteredDurations, expectRegisteredDurations,
expectActiveDurations, expectActiveDurations,
new KeepaliveCarrierStats[] { new KeepaliveCarrierStats[] {
@@ -868,6 +919,9 @@ public class KeepaliveStatsTrackerTest {
assertDailyKeepaliveInfoReported( assertDailyKeepaliveInfoReported(
dailyKeepaliveInfoReported2, dailyKeepaliveInfoReported2,
/* expectRequestsCount= */ 1,
/* expectAutoRequestsCount= */ 1,
/* expectAppUids= */ new int[] {TEST_UID},
expectRegisteredDurations2, expectRegisteredDurations2,
expectActiveDurations2, expectActiveDurations2,
new KeepaliveCarrierStats[] {expectKeepaliveCarrierStats3}); new KeepaliveCarrierStats[] {expectKeepaliveCarrierStats3});
@@ -893,6 +947,9 @@ public class KeepaliveStatsTrackerTest {
assertDailyKeepaliveInfoReported( assertDailyKeepaliveInfoReported(
dailyKeepaliveInfoReported, dailyKeepaliveInfoReported,
/* expectRequestsCount= */ 1,
/* expectAutoRequestsCount= */ 1,
/* expectAppUids= */ new int[] {TEST_UID},
expectRegisteredDurations, expectRegisteredDurations,
expectActiveDurations, expectActiveDurations,
new KeepaliveCarrierStats[] { new KeepaliveCarrierStats[] {
@@ -932,6 +989,9 @@ public class KeepaliveStatsTrackerTest {
assertDailyKeepaliveInfoReported( assertDailyKeepaliveInfoReported(
dailyKeepaliveInfoReported, dailyKeepaliveInfoReported,
/* expectRequestsCount= */ 2,
/* expectAutoRequestsCount= */ 2,
/* expectAppUids= */ new int[] {TEST_UID},
expectRegisteredDurations, expectRegisteredDurations,
expectActiveDurations, expectActiveDurations,
new KeepaliveCarrierStats[] { new KeepaliveCarrierStats[] {
@@ -976,6 +1036,9 @@ public class KeepaliveStatsTrackerTest {
writeTime - startTime); writeTime - startTime);
assertDailyKeepaliveInfoReported( assertDailyKeepaliveInfoReported(
dailyKeepaliveInfoReported, dailyKeepaliveInfoReported,
/* expectRequestsCount= */ 2,
/* expectAutoRequestsCount= */ 2,
/* expectAppUids= */ new int[] {TEST_UID},
/* expectRegisteredDurations= */ new int[] {startTime, 0, writeTime - startTime}, /* expectRegisteredDurations= */ new int[] {startTime, 0, writeTime - startTime},
/* expectActiveDurations= */ new int[] {startTime, 0, writeTime - startTime}, /* expectActiveDurations= */ new int[] {startTime, 0, writeTime - startTime},
new KeepaliveCarrierStats[] { new KeepaliveCarrierStats[] {
@@ -1016,8 +1079,53 @@ public class KeepaliveStatsTrackerTest {
assertDailyKeepaliveInfoReported( assertDailyKeepaliveInfoReported(
dailyKeepaliveInfoReported, dailyKeepaliveInfoReported,
/* expectRequestsCount= */ 1,
/* expectAutoRequestsCount= */ 1,
/* expectAppUids= */ new int[] {TEST_UID},
/* expectRegisteredDurations= */ new int[] {startTime, writeTime - startTime}, /* expectRegisteredDurations= */ new int[] {startTime, writeTime - startTime},
/* expectActiveDurations= */ new int[] {startTime, writeTime - startTime}, /* expectActiveDurations= */ new int[] {startTime, writeTime - startTime},
new KeepaliveCarrierStats[] {expectKeepaliveCarrierStats}); new KeepaliveCarrierStats[] {expectKeepaliveCarrierStats});
} }
@Test
public void testKeepaliveCountsAndUids() {
final int startTime1 = 1000, startTime2 = 2000, startTime3 = 3000;
final int writeTime = 5000;
final int[] uids = new int[] {TEST_UID, TEST_UID + 1, TEST_UID + 2};
onStartKeepalive(startTime1, TEST_SLOT, TEST_NETWORK_CAPABILITIES,
TEST_KEEPALIVE_INTERVAL_SEC, uids[0], /* isAutoKeepalive= */ true);
onStartKeepalive(startTime2, TEST_SLOT + 1, TEST_NETWORK_CAPABILITIES,
TEST_KEEPALIVE_INTERVAL_SEC, uids[1], /* isAutoKeepalive= */ false);
onStartKeepalive(startTime3, TEST_SLOT + 2, TEST_NETWORK_CAPABILITIES,
TEST_KEEPALIVE_INTERVAL_SEC, uids[2], /* isAutoKeepalive= */ true);
final DailykeepaliveInfoReported dailyKeepaliveInfoReported =
buildKeepaliveMetrics(writeTime);
final int[] expectRegisteredDurations =
new int[] {
startTime1,
(startTime2 - startTime1),
(startTime3 - startTime2),
(writeTime - startTime3)
};
final int[] expectActiveDurations =
new int[] {
startTime1,
(startTime2 - startTime1),
(startTime3 - startTime2),
(writeTime - startTime3)
};
assertDailyKeepaliveInfoReported(
dailyKeepaliveInfoReported,
/* expectRequestsCount= */ 3,
/* expectAutoRequestsCount= */ 2,
/* expectAppUids= */ uids,
expectRegisteredDurations,
expectActiveDurations,
new KeepaliveCarrierStats[] {
getDefaultCarrierStats(
writeTime * 3 - startTime1 - startTime2 - startTime3,
writeTime * 3 - startTime1 - startTime2 - startTime3)
});
}
} }