1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package android.cts.statsd.metadata; 17 18 import static com.google.common.truth.Truth.assertWithMessage; 19 20 import android.cts.statsd.atom.AtomTestCase; 21 22 import com.android.internal.os.StatsdConfigProto; 23 import com.android.internal.os.StatsdConfigProto.StatsdConfig; 24 import com.android.internal.os.StatsdConfigProto.Subscription; 25 import com.android.internal.os.StatsdConfigProto.TimeUnit; 26 import com.android.internal.os.StatsdConfigProto.ValueMetric; 27 import com.android.os.AtomsProto.AnomalyDetected; 28 import com.android.os.AtomsProto.AppBreadcrumbReported; 29 import com.android.os.AtomsProto.Atom; 30 import com.android.os.StatsLog.EventMetricData; 31 import com.android.os.StatsLog.StatsdStatsReport; 32 import com.android.os.StatsLog.StatsdStatsReport.ConfigStats; 33 import com.android.tradefed.log.LogUtil; 34 35 36 import java.util.List; 37 38 /** 39 * Statsd Metadata tests. 40 */ 41 public class MetadataTests extends MetadataTestCase { 42 43 private static final String TAG = "Statsd.MetadataTests"; 44 45 // Tests that the statsd config is reset after the specified ttl. testConfigTtl()46 public void testConfigTtl() throws Exception { 47 final int TTL_TIME_SEC = 8; 48 StatsdConfig.Builder config = getBaseConfig(); 49 config.setTtlInSeconds(TTL_TIME_SEC); // should reset in this many seconds. 50 51 uploadConfig(config); 52 long startTime = System.currentTimeMillis(); 53 Thread.sleep(WAIT_TIME_SHORT); 54 doAppBreadcrumbReportedStart(/* irrelevant val */ 6); // Event, within < TTL_TIME_SEC secs. 55 Thread.sleep(WAIT_TIME_SHORT); 56 StatsdStatsReport report = getStatsdStatsReport(); // Has only been 1 second 57 LogUtil.CLog.d("got following statsdstats report: " + report.toString()); 58 boolean foundActiveConfig = false; 59 int creationTime = 0; 60 for (ConfigStats stats: report.getConfigStatsList()) { 61 if (stats.getId() == CONFIG_ID && stats.getUid() == getHostUid()) { 62 if(!stats.hasDeletionTimeSec()) { 63 assertWithMessage("Found multiple active CTS configs!") 64 .that(foundActiveConfig).isFalse(); 65 foundActiveConfig = true; 66 creationTime = stats.getCreationTimeSec(); 67 } 68 } 69 } 70 assertWithMessage("Did not find an active CTS config").that(foundActiveConfig).isTrue(); 71 72 while(System.currentTimeMillis() - startTime < 8_000) { 73 Thread.sleep(10); 74 } 75 doAppBreadcrumbReportedStart(/* irrelevant val */ 6); // Event, after TTL_TIME_SEC secs. 76 Thread.sleep(WAIT_TIME_LONG); 77 report = getStatsdStatsReport(); 78 LogUtil.CLog.d("got following statsdstats report: " + report.toString()); 79 foundActiveConfig = false; 80 int expectedTime = creationTime + TTL_TIME_SEC; 81 for (ConfigStats stats: report.getConfigStatsList()) { 82 if (stats.getId() == CONFIG_ID && stats.getUid() == getHostUid()) { 83 // Original config should be TTL'd 84 if (stats.getCreationTimeSec() == creationTime) { 85 assertWithMessage("Config should have TTL'd but is still active") 86 .that(stats.hasDeletionTimeSec()).isTrue(); 87 assertWithMessage( 88 "Config deletion time should be about %s after creation", TTL_TIME_SEC 89 ).that(Math.abs(stats.getDeletionTimeSec() - expectedTime)).isAtMost(2); 90 } 91 // There should still be one active config, that is marked as reset. 92 if(!stats.hasDeletionTimeSec()) { 93 assertWithMessage("Found multiple active CTS configs!") 94 .that(foundActiveConfig).isFalse(); 95 foundActiveConfig = true; 96 creationTime = stats.getCreationTimeSec(); 97 assertWithMessage("Active config after TTL should be marked as reset") 98 .that(stats.hasResetTimeSec()).isTrue(); 99 assertWithMessage("Reset and creation time should be equal for TTl'd configs") 100 .that(stats.getResetTimeSec()).isEqualTo(stats.getCreationTimeSec()); 101 assertWithMessage( 102 "Reset config should be created when the original config TTL'd" 103 ).that(Math.abs(stats.getCreationTimeSec() - expectedTime)).isAtMost(2); 104 } 105 } 106 } 107 assertWithMessage("Did not find an active CTS config after the TTL") 108 .that(foundActiveConfig).isTrue(); 109 } 110 } 111