source: proiecte/HadoopJUnit/hadoop-0.20.1/src/test/org/apache/hadoop/io/retry/TestRetryProxy.java @ 120

Last change on this file since 120 was 120, checked in by (none), 14 years ago

Added the mail files for the Hadoop JUNit Project

  • Property svn:executable set to *
File size: 6.5 KB
Line 
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
19package org.apache.hadoop.io.retry;
20
21import static org.apache.hadoop.io.retry.RetryPolicies.RETRY_FOREVER;
22import static org.apache.hadoop.io.retry.RetryPolicies.TRY_ONCE_DONT_FAIL;
23import static org.apache.hadoop.io.retry.RetryPolicies.TRY_ONCE_THEN_FAIL;
24import static org.apache.hadoop.io.retry.RetryPolicies.retryByException;
25import static org.apache.hadoop.io.retry.RetryPolicies.retryByRemoteException;
26import static org.apache.hadoop.io.retry.RetryPolicies.retryUpToMaximumCountWithFixedSleep;
27import static org.apache.hadoop.io.retry.RetryPolicies.retryUpToMaximumCountWithProportionalSleep;
28import static org.apache.hadoop.io.retry.RetryPolicies.retryUpToMaximumTimeWithFixedSleep;
29import static org.apache.hadoop.io.retry.RetryPolicies.exponentialBackoffRetry;
30
31import java.util.Collections;
32import java.util.Map;
33import java.util.concurrent.TimeUnit;
34
35import junit.framework.TestCase;
36
37import org.apache.hadoop.io.retry.UnreliableInterface.FatalException;
38import org.apache.hadoop.io.retry.UnreliableInterface.UnreliableException;
39import org.apache.hadoop.ipc.RemoteException;
40
41public class TestRetryProxy extends TestCase {
42 
43  private UnreliableImplementation unreliableImpl;
44 
45  @Override
46  protected void setUp() throws Exception {
47    unreliableImpl = new UnreliableImplementation();
48  }
49
50  public void testTryOnceThenFail() throws UnreliableException {
51    UnreliableInterface unreliable = (UnreliableInterface)
52      RetryProxy.create(UnreliableInterface.class, unreliableImpl, TRY_ONCE_THEN_FAIL);
53    unreliable.alwaysSucceeds();
54    try {
55      unreliable.failsOnceThenSucceeds();
56      fail("Should fail");
57    } catch (UnreliableException e) {
58      // expected
59    }
60  }
61 
62  public void testTryOnceDontFail() throws UnreliableException {
63    UnreliableInterface unreliable = (UnreliableInterface)
64      RetryProxy.create(UnreliableInterface.class, unreliableImpl, TRY_ONCE_DONT_FAIL);
65    unreliable.alwaysSucceeds();
66    unreliable.failsOnceThenSucceeds();
67    try {
68      unreliable.failsOnceThenSucceedsWithReturnValue();
69      fail("Should fail");
70    } catch (UnreliableException e) {
71      // expected
72    }
73  }
74 
75  public void testRetryForever() throws UnreliableException {
76    UnreliableInterface unreliable = (UnreliableInterface)
77      RetryProxy.create(UnreliableInterface.class, unreliableImpl, RETRY_FOREVER);
78    unreliable.alwaysSucceeds();
79    unreliable.failsOnceThenSucceeds();
80    unreliable.failsTenTimesThenSucceeds();
81  }
82 
83  public void testRetryUpToMaximumCountWithFixedSleep() throws UnreliableException {
84    UnreliableInterface unreliable = (UnreliableInterface)
85      RetryProxy.create(UnreliableInterface.class, unreliableImpl,
86                        retryUpToMaximumCountWithFixedSleep(8, 1, TimeUnit.NANOSECONDS));
87    unreliable.alwaysSucceeds();
88    unreliable.failsOnceThenSucceeds();
89    try {
90      unreliable.failsTenTimesThenSucceeds();
91      fail("Should fail");
92    } catch (UnreliableException e) {
93      // expected
94    }
95  }
96 
97  public void testRetryUpToMaximumTimeWithFixedSleep() throws UnreliableException {
98    UnreliableInterface unreliable = (UnreliableInterface)
99      RetryProxy.create(UnreliableInterface.class, unreliableImpl,
100                        retryUpToMaximumTimeWithFixedSleep(80, 10, TimeUnit.NANOSECONDS));
101    unreliable.alwaysSucceeds();
102    unreliable.failsOnceThenSucceeds();
103    try {
104      unreliable.failsTenTimesThenSucceeds();
105      fail("Should fail");
106    } catch (UnreliableException e) {
107      // expected
108    }
109  }
110 
111  public void testRetryUpToMaximumCountWithProportionalSleep() throws UnreliableException {
112    UnreliableInterface unreliable = (UnreliableInterface)
113      RetryProxy.create(UnreliableInterface.class, unreliableImpl,
114                        retryUpToMaximumCountWithProportionalSleep(8, 1, TimeUnit.NANOSECONDS));
115    unreliable.alwaysSucceeds();
116    unreliable.failsOnceThenSucceeds();
117    try {
118      unreliable.failsTenTimesThenSucceeds();
119      fail("Should fail");
120    } catch (UnreliableException e) {
121      // expected
122    }
123  }
124 
125  public void testExponentialRetry() throws UnreliableException {
126    UnreliableInterface unreliable = (UnreliableInterface)
127      RetryProxy.create(UnreliableInterface.class, unreliableImpl,
128                        exponentialBackoffRetry(5, 1L, TimeUnit.NANOSECONDS));
129    unreliable.alwaysSucceeds();
130    unreliable.failsOnceThenSucceeds();
131    try {
132      unreliable.failsTenTimesThenSucceeds();
133      fail("Should fail");
134    } catch (UnreliableException e) {
135      // expected
136    }
137  }
138 
139  public void testRetryByException() throws UnreliableException {
140    Map<Class<? extends Exception>, RetryPolicy> exceptionToPolicyMap =
141      Collections.<Class<? extends Exception>, RetryPolicy>singletonMap(FatalException.class, TRY_ONCE_THEN_FAIL);
142   
143    UnreliableInterface unreliable = (UnreliableInterface)
144      RetryProxy.create(UnreliableInterface.class, unreliableImpl,
145                        retryByException(RETRY_FOREVER, exceptionToPolicyMap));
146    unreliable.failsOnceThenSucceeds();
147    try {
148      unreliable.alwaysFailsWithFatalException();
149      fail("Should fail");
150    } catch (FatalException e) {
151      // expected
152    }
153  }
154 
155  public void testRetryByRemoteException() throws UnreliableException {
156    Map<Class<? extends Exception>, RetryPolicy> exceptionToPolicyMap =
157      Collections.<Class<? extends Exception>, RetryPolicy>singletonMap(FatalException.class, TRY_ONCE_THEN_FAIL);
158   
159    UnreliableInterface unreliable = (UnreliableInterface)
160      RetryProxy.create(UnreliableInterface.class, unreliableImpl,
161                        retryByRemoteException(RETRY_FOREVER, exceptionToPolicyMap));
162    try {
163      unreliable.alwaysFailsWithRemoteFatalException();
164      fail("Should fail");
165    } catch (RemoteException e) {
166      // expected
167    }
168  } 
169 
170}
Note: See TracBrowser for help on using the repository browser.