1 /* 2 * Copyright (C) 2017 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.metrics; 17 18 import android.metrics.MetricsReader.Event; 19 20 import com.android.internal.logging.nano.MetricsProto.MetricsEvent; 21 22 import junit.framework.TestCase; 23 24 import java.util.Collection; 25 26 public class MetricsReaderTest extends TestCase { 27 private static final int FULL_N = 10; 28 private static final int CHECKPOINTED_N = 4; 29 private static final int PID = 1; 30 private static final int UID = 2; 31 32 class FakeLogReader extends MetricsReader.LogReader { 33 MetricsReader.Event[] mEvents; 34 private long mHorizonMs; 35 36 public FakeLogReader() { 37 mEvents = new MetricsReader.Event[FULL_N]; 38 for (int i = 0; i < FULL_N; i++) { 39 mEvents[i] = new MetricsReader.Event( 40 1000L + i, 41 PID, 42 UID, 43 new LogMaker(i).serialize()); 44 } 45 } 46 47 @Override 48 public void readEvents(int[] tags, long horizonMs, Collection<Event> events) { 49 mHorizonMs = horizonMs; 50 for (int i = 0; i < mEvents.length; i++) { 51 events.add(mEvents[i]); 52 } 53 } 54 55 @Override 56 public void writeCheckpoint(int tag) { 57 int i = FULL_N - CHECKPOINTED_N - 1; 58 mEvents[i].setData(new LogMaker(MetricsEvent.METRICS_CHECKPOINT) 59 .setSubtype(tag) 60 .serialize()); 61 } 62 } 63 FakeLogReader mLogReader; 64 MetricsReader mReader = new MetricsReader(); 65 66 public void setUp() { 67 mLogReader = new FakeLogReader(); 68 mReader.setLogReader(mLogReader); 69 } 70 71 public void testNonBlockingRead() { 72 mReader.read(0); 73 assertEquals(0, mLogReader.mHorizonMs); 74 for (int i = 0; i < FULL_N; i++) { 75 assertTrue(mReader.hasNext()); 76 LogMaker log = mReader.next(); 77 assertEquals(i, log.getCategory()); 78 } 79 } 80 81 public void testReset() { 82 mReader.read(0); 83 while (mReader.hasNext()) { 84 mReader.next(); 85 } 86 mReader.reset(); 87 for (int i = 0; i < FULL_N; i++) { 88 assertTrue(mReader.hasNext()); 89 LogMaker log = mReader.next(); 90 assertEquals(i, log.getCategory()); 91 } 92 } 93 94 public void testPidUid() { 95 mReader.read(0); 96 LogMaker log = mReader.next(); 97 assertEquals(PID, log.getProcessId()); 98 assertEquals(UID, log.getUid()); 99 } 100 101 public void testBlockingRead_readResetsHorizon() { 102 mReader.read(1000); 103 assertEquals(1000, mLogReader.mHorizonMs); 104 } 105 106 public void testWriteCheckpoint() { 107 mReader.checkpoint(); 108 mReader.read(0); 109 int m = FULL_N - CHECKPOINTED_N; 110 for (int i = m; i < FULL_N; i++) { 111 assertTrue(mReader.hasNext()); 112 LogMaker log = mReader.next(); 113 assertEquals(i, log.getCategory()); 114 } 115 } 116 } 117