TestRetryProxy.java
上传用户:quxuerui
上传日期:2018-01-08
资源大小:41811k
文件大小:7k
源码类别:

网格计算

开发平台:

Java

  1. /**
  2.  * Licensed to the Apache Software Foundation (ASF) under one
  3.  * or more contributor license agreements.  See the NOTICE file
  4.  * distributed with this work for additional information
  5.  * regarding copyright ownership.  The ASF licenses this file
  6.  * to you under the Apache License, Version 2.0 (the
  7.  * "License"); you may not use this file except in compliance
  8.  * with the License.  You may obtain a copy of the License at
  9.  *
  10.  *     http://www.apache.org/licenses/LICENSE-2.0
  11.  *
  12.  * Unless required by applicable law or agreed to in writing, software
  13.  * distributed under the License is distributed on an "AS IS" BASIS,
  14.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15.  * See the License for the specific language governing permissions and
  16.  * limitations under the License.
  17.  */
  18. package org.apache.hadoop.io.retry;
  19. import static org.apache.hadoop.io.retry.RetryPolicies.RETRY_FOREVER;
  20. import static org.apache.hadoop.io.retry.RetryPolicies.TRY_ONCE_DONT_FAIL;
  21. import static org.apache.hadoop.io.retry.RetryPolicies.TRY_ONCE_THEN_FAIL;
  22. import static org.apache.hadoop.io.retry.RetryPolicies.retryByException;
  23. import static org.apache.hadoop.io.retry.RetryPolicies.retryByRemoteException;
  24. import static org.apache.hadoop.io.retry.RetryPolicies.retryUpToMaximumCountWithFixedSleep;
  25. import static org.apache.hadoop.io.retry.RetryPolicies.retryUpToMaximumCountWithProportionalSleep;
  26. import static org.apache.hadoop.io.retry.RetryPolicies.retryUpToMaximumTimeWithFixedSleep;
  27. import static org.apache.hadoop.io.retry.RetryPolicies.exponentialBackoffRetry;
  28. import java.util.Collections;
  29. import java.util.Map;
  30. import java.util.concurrent.TimeUnit;
  31. import junit.framework.TestCase;
  32. import org.apache.hadoop.io.retry.UnreliableInterface.FatalException;
  33. import org.apache.hadoop.io.retry.UnreliableInterface.UnreliableException;
  34. import org.apache.hadoop.ipc.RemoteException;
  35. public class TestRetryProxy extends TestCase {
  36.   
  37.   private UnreliableImplementation unreliableImpl;
  38.   
  39.   @Override
  40.   protected void setUp() throws Exception {
  41.     unreliableImpl = new UnreliableImplementation();
  42.   }
  43.   public void testTryOnceThenFail() throws UnreliableException {
  44.     UnreliableInterface unreliable = (UnreliableInterface)
  45.       RetryProxy.create(UnreliableInterface.class, unreliableImpl, TRY_ONCE_THEN_FAIL);
  46.     unreliable.alwaysSucceeds();
  47.     try {
  48.       unreliable.failsOnceThenSucceeds();
  49.       fail("Should fail");
  50.     } catch (UnreliableException e) {
  51.       // expected
  52.     }
  53.   }
  54.   
  55.   public void testTryOnceDontFail() throws UnreliableException {
  56.     UnreliableInterface unreliable = (UnreliableInterface)
  57.       RetryProxy.create(UnreliableInterface.class, unreliableImpl, TRY_ONCE_DONT_FAIL);
  58.     unreliable.alwaysSucceeds();
  59.     unreliable.failsOnceThenSucceeds();
  60.     try {
  61.       unreliable.failsOnceThenSucceedsWithReturnValue();
  62.       fail("Should fail");
  63.     } catch (UnreliableException e) {
  64.       // expected
  65.     }
  66.   }
  67.   
  68.   public void testRetryForever() throws UnreliableException {
  69.     UnreliableInterface unreliable = (UnreliableInterface)
  70.       RetryProxy.create(UnreliableInterface.class, unreliableImpl, RETRY_FOREVER);
  71.     unreliable.alwaysSucceeds();
  72.     unreliable.failsOnceThenSucceeds();
  73.     unreliable.failsTenTimesThenSucceeds();
  74.   }
  75.   
  76.   public void testRetryUpToMaximumCountWithFixedSleep() throws UnreliableException {
  77.     UnreliableInterface unreliable = (UnreliableInterface)
  78.       RetryProxy.create(UnreliableInterface.class, unreliableImpl,
  79.                         retryUpToMaximumCountWithFixedSleep(8, 1, TimeUnit.NANOSECONDS));
  80.     unreliable.alwaysSucceeds();
  81.     unreliable.failsOnceThenSucceeds();
  82.     try {
  83.       unreliable.failsTenTimesThenSucceeds();
  84.       fail("Should fail");
  85.     } catch (UnreliableException e) {
  86.       // expected
  87.     }
  88.   }
  89.   
  90.   public void testRetryUpToMaximumTimeWithFixedSleep() throws UnreliableException {
  91.     UnreliableInterface unreliable = (UnreliableInterface)
  92.       RetryProxy.create(UnreliableInterface.class, unreliableImpl,
  93.                         retryUpToMaximumTimeWithFixedSleep(80, 10, TimeUnit.NANOSECONDS));
  94.     unreliable.alwaysSucceeds();
  95.     unreliable.failsOnceThenSucceeds();
  96.     try {
  97.       unreliable.failsTenTimesThenSucceeds();
  98.       fail("Should fail");
  99.     } catch (UnreliableException e) {
  100.       // expected
  101.     }
  102.   }
  103.   
  104.   public void testRetryUpToMaximumCountWithProportionalSleep() throws UnreliableException {
  105.     UnreliableInterface unreliable = (UnreliableInterface)
  106.       RetryProxy.create(UnreliableInterface.class, unreliableImpl,
  107.                         retryUpToMaximumCountWithProportionalSleep(8, 1, TimeUnit.NANOSECONDS));
  108.     unreliable.alwaysSucceeds();
  109.     unreliable.failsOnceThenSucceeds();
  110.     try {
  111.       unreliable.failsTenTimesThenSucceeds();
  112.       fail("Should fail");
  113.     } catch (UnreliableException e) {
  114.       // expected
  115.     }
  116.   }
  117.   
  118.   public void testExponentialRetry() throws UnreliableException {
  119.     UnreliableInterface unreliable = (UnreliableInterface)
  120.       RetryProxy.create(UnreliableInterface.class, unreliableImpl,
  121.                         exponentialBackoffRetry(5, 1L, TimeUnit.NANOSECONDS));
  122.     unreliable.alwaysSucceeds();
  123.     unreliable.failsOnceThenSucceeds();
  124.     try {
  125.       unreliable.failsTenTimesThenSucceeds();
  126.       fail("Should fail");
  127.     } catch (UnreliableException e) {
  128.       // expected
  129.     }
  130.   }
  131.   
  132.   public void testRetryByException() throws UnreliableException {
  133.     Map<Class<? extends Exception>, RetryPolicy> exceptionToPolicyMap =
  134.       Collections.<Class<? extends Exception>, RetryPolicy>singletonMap(FatalException.class, TRY_ONCE_THEN_FAIL);
  135.     
  136.     UnreliableInterface unreliable = (UnreliableInterface)
  137.       RetryProxy.create(UnreliableInterface.class, unreliableImpl,
  138.                         retryByException(RETRY_FOREVER, exceptionToPolicyMap));
  139.     unreliable.failsOnceThenSucceeds();
  140.     try {
  141.       unreliable.alwaysFailsWithFatalException();
  142.       fail("Should fail");
  143.     } catch (FatalException e) {
  144.       // expected
  145.     }
  146.   }
  147.   
  148.   public void testRetryByRemoteException() throws UnreliableException {
  149.     Map<Class<? extends Exception>, RetryPolicy> exceptionToPolicyMap =
  150.       Collections.<Class<? extends Exception>, RetryPolicy>singletonMap(FatalException.class, TRY_ONCE_THEN_FAIL);
  151.     
  152.     UnreliableInterface unreliable = (UnreliableInterface)
  153.       RetryProxy.create(UnreliableInterface.class, unreliableImpl,
  154.                         retryByRemoteException(RETRY_FOREVER, exceptionToPolicyMap));
  155.     try {
  156.       unreliable.alwaysFailsWithRemoteFatalException();
  157.       fail("Should fail");
  158.     } catch (RemoteException e) {
  159.       // expected
  160.     }
  161.   }  
  162.   
  163. }