Home | History | Annotate | Download | only in telephony
      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 
     17 package com.android.internal.telephony;
     18 
     19 import static org.junit.Assert.assertTrue;
     20 import static org.mockito.ArgumentMatchers.any;
     21 import static org.mockito.ArgumentMatchers.anyLong;
     22 import static org.mockito.Mockito.doAnswer;
     23 import static org.mockito.Mockito.times;
     24 import static org.mockito.Mockito.verify;
     25 
     26 import android.os.Handler;
     27 import android.os.Looper;
     28 import android.support.test.runner.AndroidJUnit4;
     29 
     30 import com.android.internal.telephony.ims.ImsTestBase;
     31 
     32 import org.junit.After;
     33 import org.junit.Before;
     34 import org.junit.Test;
     35 import org.junit.runner.RunWith;
     36 import org.mockito.Mock;
     37 @RunWith(AndroidJUnit4.class)
     38 public class ExponentialBackoffTest extends ImsTestBase {
     39 
     40     private static final int START_DELAY_MS = 10;
     41     private static final int MAXIMUM_DELAY_MS = 1000;
     42     private static final int MULTIPLIER = 2;
     43 
     44     private ExponentialBackoff mBackoffUnderTest;
     45     private Handler mHandler = new Handler(Looper.getMainLooper());
     46     @Mock private Runnable mRunnable;
     47     @Mock private ExponentialBackoff.HandlerAdapter mHandlerAdapter;
     48 
     49     @Before
     50     public void setUp() throws Exception {
     51         super.setUp();
     52         mBackoffUnderTest = new ExponentialBackoff(
     53                 START_DELAY_MS, MAXIMUM_DELAY_MS, MULTIPLIER, mHandler, mRunnable);
     54         mBackoffUnderTest.setHandlerAdapter(mHandlerAdapter);
     55         doAnswer(invocation -> mHandler.postDelayed((Runnable) invocation.getArguments()[0],
     56                         (long) invocation.getArguments()[1])
     57         ).when(mHandlerAdapter).postDelayed(any(Runnable.class), anyLong());
     58         doAnswer(invocation -> {
     59             mHandler.removeCallbacks((Runnable) invocation.getArguments()[0]);
     60             return null;
     61         }).when(mHandlerAdapter).removeCallbacks(any(Runnable.class));
     62     }
     63 
     64     @After
     65     public void tearDown() {
     66         mBackoffUnderTest.stop();
     67     }
     68 
     69     @Test
     70     public void testStartBackoff() {
     71         mBackoffUnderTest.start();
     72         long delay = mBackoffUnderTest.getCurrentDelay();
     73         waitForHandlerActionDelayed(mHandler, delay, 2 * delay);
     74 
     75         // The runnable is executed after timeout event occurred.
     76         verify(mRunnable).run();
     77     }
     78 
     79     @Test
     80     public void testStopBackoff() {
     81         mBackoffUnderTest.start();
     82 
     83         mBackoffUnderTest.stop();
     84         // removeCallbacks is called during start() and stop()
     85         verify(mHandlerAdapter, times(2)).removeCallbacks(mRunnable);
     86     }
     87 
     88     @Test
     89     public void testDelayIncreasedExponentially() {
     90         mBackoffUnderTest.start();
     91         // guarantee START_DELAY_MS * 2 ^ i <= MAXIMUM_DELAY_MS
     92         for (int i = 1; i < 5; i++) {
     93             mBackoffUnderTest.notifyFailed();
     94             long delay = mBackoffUnderTest.getCurrentDelay();
     95             long minDelay = (long) (START_DELAY_MS * Math.pow(MULTIPLIER, i - 1));
     96             long maxDelay = (long) (START_DELAY_MS * Math.pow(MULTIPLIER, i));
     97             assertTrue("delay = " + delay + " minDelay = " + minDelay, delay >= minDelay);
     98             assertTrue("delay = " + delay + " maxDelay = " + maxDelay, delay <= maxDelay);
     99         }
    100     }
    101 
    102     @Test
    103     public void testDelayShouldNotExceededTheMaximumLimit() {
    104         mBackoffUnderTest.start();
    105         // guarantee START_DELAY_MS * 2 ^ 30 > MAXIMUM_DELAY_MS
    106         for (int i = 1; i < 30; i++) {
    107             mBackoffUnderTest.notifyFailed();
    108         }
    109         long delay = mBackoffUnderTest.getCurrentDelay();
    110         assertTrue(
    111                 "delay = " + delay + " maximumDelay = " + MAXIMUM_DELAY_MS,
    112                 delay <= MAXIMUM_DELAY_MS);
    113     }
    114 }
    115