Home | History | Annotate | Download | only in notification
      1 /*
      2  * Copyright (C) 2016 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 com.android.server.notification;
     17 
     18 import android.support.test.runner.AndroidJUnit4;
     19 import android.test.suitebuilder.annotation.SmallTest;
     20 import org.junit.Before;
     21 import org.junit.Test;
     22 import org.junit.runner.RunWith;
     23 
     24 import static org.junit.Assert.assertTrue;
     25 import static org.junit.Assert.assertFalse;
     26 
     27 @SmallTest
     28 @RunWith(AndroidJUnit4.class)
     29 public class RateEstimatorTest extends NotificationTestCase {
     30     private long mTestStartTime;
     31     private RateEstimator mEstimator;
     32 
     33     @Before
     34     public void setUp() {
     35         mTestStartTime = 1225731600000L;
     36         mEstimator = new RateEstimator();
     37     }
     38 
     39     @Test
     40     public void testRunningTimeBackwardDoesntExplodeUpdate() throws Exception {
     41         assertUpdateTime(mTestStartTime);
     42         assertUpdateTime(mTestStartTime - 1000L);
     43     }
     44 
     45     @Test
     46     public void testRunningTimeBackwardDoesntExplodeGet() throws Exception {
     47         assertUpdateTime(mTestStartTime);
     48         final float rate = mEstimator.getRate(mTestStartTime - 1000L);
     49         assertFalse(Float.isInfinite(rate));
     50         assertFalse(Float.isNaN(rate));
     51     }
     52 
     53     @Test
     54     public void testInstantaneousEventsDontExplodeUpdate() throws Exception {
     55         assertUpdateTime(mTestStartTime);
     56         assertUpdateTime(mTestStartTime);
     57     }
     58 
     59     @Test
     60     public void testInstantaneousEventsDontExplodeGet() throws Exception {
     61         assertUpdateTime(mTestStartTime);
     62         assertUpdateTime(mTestStartTime);
     63         final float rate = mEstimator.getRate(mTestStartTime);
     64         assertFalse(Float.isInfinite(rate));
     65         assertFalse(Float.isNaN(rate));
     66     }
     67 
     68     @Test
     69     public void testInstantaneousBurstIsEstimatedUnderTwoPercent() throws Exception {
     70         assertUpdateTime(mTestStartTime);
     71         long eventStart = mTestStartTime + 1000; // start event a long time after initialization
     72         long nextEventTime = postEvents(eventStart, 0, 5); // five events at \inf
     73         final float rate = mEstimator.getRate(nextEventTime);
     74         assertLessThan("Rate", rate, 20f);
     75     }
     76 
     77     @Test
     78     public void testCompactBurstIsEstimatedUnderTwoPercent() throws Exception {
     79         assertUpdateTime(mTestStartTime);
     80         long eventStart = mTestStartTime + 1000; // start event a long time after initialization
     81         long nextEventTime = postEvents(eventStart, 1, 5); // five events at 1000Hz
     82         final float rate = mEstimator.getRate(nextEventTime);
     83         assertLessThan("Rate", rate, 20f);
     84     }
     85 
     86     @Test
     87     public void testSustained1000HzBurstIsEstimatedOverNinetyPercent() throws Exception {
     88         assertUpdateTime(mTestStartTime);
     89         long eventStart = mTestStartTime + 1000; // start event a long time after initialization
     90         long nextEventTime = postEvents(eventStart, 1, 100); // one hundred events at 1000Hz
     91         final float rate = mEstimator.getRate(nextEventTime);
     92         assertGreaterThan("Rate", rate, 900f);
     93     }
     94 
     95     @Test
     96     public void testSustained100HzBurstIsEstimatedOverNinetyPercent() throws Exception {
     97         assertUpdateTime(mTestStartTime);
     98         long eventStart = mTestStartTime + 1000; // start event a long time after initialization
     99         long nextEventTime = postEvents(eventStart, 10, 100); // one hundred events at 100Hz
    100         final float rate = mEstimator.getRate(nextEventTime);
    101 
    102         assertGreaterThan("Rate", rate, 90f);
    103     }
    104 
    105     @Test
    106     public void testRecoverQuicklyAfterSustainedBurst() throws Exception {
    107         assertUpdateTime(mTestStartTime);
    108         long eventStart = mTestStartTime + 1000; // start event a long time after initialization
    109         long nextEventTime = postEvents(eventStart, 10, 1000); // one hundred events at 100Hz
    110         final float rate = mEstimator.getRate(nextEventTime + 5000L); // two seconds later
    111         assertLessThan("Rate", rate, 2f);
    112     }
    113 
    114     @Test
    115     public void testEstimateShouldNotOvershoot() throws Exception {
    116         assertUpdateTime(mTestStartTime);
    117         long eventStart = mTestStartTime + 1000; // start event a long time after initialization
    118         long nextEventTime = postEvents(eventStart, 1, 1000); // one thousand events at 1000Hz
    119         final float rate = mEstimator.getRate(nextEventTime);
    120         assertLessThan("Rate", rate, 1000f);
    121     }
    122 
    123     @Test
    124     public void testGetRateWithoutUpdate() throws Exception {
    125         final float rate = mEstimator.getRate(mTestStartTime);
    126         assertLessThan("Rate", rate, 0.1f);
    127     }
    128 
    129     @Test
    130     public void testGetRateWithOneUpdate() throws Exception {
    131         assertUpdateTime(mTestStartTime);
    132         final float rate = mEstimator.getRate(mTestStartTime+1);
    133         assertLessThan("Rate", rate, 1f);
    134     }
    135 
    136     private void assertLessThan(String label, float a, float b)  {
    137         assertTrue(String.format("%s was %f, but should be less than %f", label, a, b), a <= b);
    138     }
    139 
    140     private void assertGreaterThan(String label, float a, float b)  {
    141         assertTrue(String.format("%s was %f, but should be more than %f", label, a, b), a >= b);
    142     }
    143 
    144     /** @returns the next event time. */
    145     private long postEvents(long start, long dt, int num) {
    146         long time = start;
    147         for (int i = 0; i < num; i++) {
    148             mEstimator.update(time);
    149             time += dt;
    150         }
    151         return time;
    152     }
    153 
    154     private void assertUpdateTime(long time) {
    155         final float rate = mEstimator.update(time);
    156         assertFalse(Float.isInfinite(rate));
    157         assertFalse(Float.isNaN(rate));
    158     }
    159 }
    160