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 android.autofillservice.cts; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import android.platform.test.annotations.AppModeFull; 22 import android.support.test.runner.AndroidJUnit4; 23 24 import org.junit.Test; 25 import org.junit.runner.Description; 26 import org.junit.runner.RunWith; 27 import org.junit.runners.model.Statement; 28 29 @RunWith(AndroidJUnit4.class) 30 @AppModeFull // Unit test 31 public class RetryRuleTest { 32 33 private final Description mDescription = Description.createSuiteDescription("Whatever"); 34 35 private static final RetryableException sRetryableException = 36 new RetryableException("Y U NO RETRY?"); 37 38 private static class RetryableStatement<T extends Exception> extends Statement { 39 private final int mNumberFailures; 40 private int mNumberCalls; 41 private final T mException; 42 43 RetryableStatement(int numberFailures, T exception) { 44 mNumberFailures = numberFailures; 45 mException = exception; 46 } 47 48 @Override 49 public void evaluate() throws Throwable { 50 mNumberCalls ++; 51 if (mNumberCalls <= mNumberFailures) { 52 throw mException; 53 } 54 } 55 56 @Override 57 public String toString() { 58 return "RetryableStatement: failures=" + mNumberFailures + ", calls=" + mNumberCalls; 59 } 60 } 61 62 @Test 63 public void testPassOnRetryableException() throws Throwable { 64 final RetryRule rule = new RetryRule(2); 65 rule.apply(new RetryableStatement<RetryableException>(1, sRetryableException), mDescription) 66 .evaluate(); 67 } 68 69 @Test 70 public void testPassOnRetryableExceptionWithTimeout() throws Throwable { 71 final Timeout timeout = new Timeout("YOUR TIME IS GONE", 1, 2, 10); 72 final RetryableException exception = new RetryableException(timeout, "Y U NO?"); 73 final RetryRule rule = new RetryRule(2); 74 rule.apply(new RetryableStatement<RetryableException>(1, exception), mDescription) 75 .evaluate(); 76 // Assert timeout was increased 77 assertThat(timeout.ms()).isEqualTo(2); 78 } 79 80 @Test 81 public void testFailOnRetryableException() throws Throwable { 82 final RetryRule rule = new RetryRule(2); 83 try { 84 rule.apply(new RetryableStatement<RetryableException>(2, sRetryableException), 85 mDescription).evaluate(); 86 throw new AssertionError("2ND CALL, Y U NO FAIL?"); 87 } catch (RetryableException e) { 88 assertThat(e).isSameAs(sRetryableException); 89 } 90 } 91 } 92