Fix get summary does not include the latest bucket
In internalGetSummaryForNetwork, the history object will not include the latest bucket because of the logic in NetworkStatsHistory#recordHistory would only include buckets that atomically occur in the inclusive time range. This makes querySummaryForDevice callers who are also sensitive to the latest bucket fails. This CL revert the behavior back to S, in T this is temporarily changed after aosp/1937268. Also, for test purpose, this CL change System.currentTimeMillis to use mClock when querying. Test: NetworkStatsServiceTest#testGetLatestSummary Bug: 233733267 Change-Id: I55e2a5d953622b68da36a9b8d3e57c88d57543b0 Merged-In: I55e2a5d953622b68da36a9b8d3e57c88d57543b0 (pure cherry-picked from ag/18670248)
This commit is contained in:
@@ -1455,9 +1455,9 @@ public class NetworkStatsService extends INetworkStatsService.Stub {
|
||||
// We've been using pure XT stats long enough that we no longer need to
|
||||
// splice DEV and XT together.
|
||||
final NetworkStatsHistory history = internalGetHistoryForNetwork(template, flags, FIELD_ALL,
|
||||
accessLevel, callingUid, start, end);
|
||||
accessLevel, callingUid, Long.MIN_VALUE, Long.MAX_VALUE);
|
||||
|
||||
final long now = System.currentTimeMillis();
|
||||
final long now = mClock.millis();
|
||||
final NetworkStatsHistory.Entry entry = history.getValues(start, end, now, null);
|
||||
|
||||
final NetworkStats stats = new NetworkStats(end - start, 1);
|
||||
|
||||
@@ -144,7 +144,10 @@ import java.io.File;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.time.Clock;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZoneOffset;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.Executor;
|
||||
@@ -1104,6 +1107,40 @@ public class NetworkStatsServiceTest extends NetworkStatsBaseTest {
|
||||
DEFAULT_NETWORK_YES, 1024L, 8L, 512L, 4L, 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetLatestSummary() throws Exception {
|
||||
// Pretend that network comes online.
|
||||
expectDefaultSettings();
|
||||
NetworkStateSnapshot[] states = new NetworkStateSnapshot[]{buildWifiState()};
|
||||
expectNetworkStatsSummary(buildEmptyStats());
|
||||
expectNetworkStatsUidDetail(buildEmptyStats());
|
||||
|
||||
mService.notifyNetworkStatus(NETWORKS_WIFI, states, getActiveIface(states),
|
||||
new UnderlyingNetworkInfo[0]);
|
||||
|
||||
// Increase arbitrary time which does not align to the bucket edge, create some traffic.
|
||||
incrementCurrentTime(1751000L);
|
||||
NetworkStats.Entry entry = new NetworkStats.Entry(
|
||||
TEST_IFACE, UID_ALL, SET_DEFAULT, TAG_NONE, 50L, 5L, 51L, 1L, 3L);
|
||||
expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1).insertEntry(entry));
|
||||
expectNetworkStatsUidDetail(buildEmptyStats());
|
||||
forcePollAndWaitForIdle();
|
||||
|
||||
// Verify the mocked stats is returned by querying with the range of the latest bucket.
|
||||
final ZonedDateTime end =
|
||||
ZonedDateTime.ofInstant(mClock.instant(), ZoneId.systemDefault());
|
||||
final ZonedDateTime start = end.truncatedTo(ChronoUnit.HOURS);
|
||||
NetworkStats stats = mSession.getSummaryForNetwork(buildTemplateWifi(TEST_WIFI_NETWORK_KEY),
|
||||
start.toInstant().toEpochMilli(), end.toInstant().toEpochMilli());
|
||||
assertEquals(1, stats.size());
|
||||
assertValues(stats, IFACE_ALL, UID_ALL, SET_ALL, TAG_NONE, METERED_ALL, ROAMING_ALL,
|
||||
DEFAULT_NETWORK_ALL, 50L, 5L, 51L, 1L, 3L);
|
||||
|
||||
// For getHistoryIntervalForNetwork, only includes buckets that atomically occur in
|
||||
// the inclusive time range, instead of including the latest bucket. This behavior is
|
||||
// already documented publicly, refer to {@link NetworkStatsManager#queryDetails}.
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUidStatsForTransport() throws Exception {
|
||||
// pretend that network comes online
|
||||
|
||||
Reference in New Issue
Block a user