Bohtvaroh

Blogger - FPOWJPWB - RetryTest

Jul 26th, 2012
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 4.42 KB | None | 0 0
  1. import org.junit.Rule;
  2. import org.junit.Test;
  3. import org.junit.rules.ExpectedException;
  4.  
  5. import static org.junit.Assert.assertEquals;
  6. import static org.junit.Assert.assertFalse;
  7. import static org.junit.Assert.assertTrue;
  8. import static org.junit.Assert.fail;
  9.  
  10. public class RetryTest {
  11.     private static final String RESULT = "operation result";
  12.  
  13.     class State {
  14.         public boolean operationCalled = false, preRetryCalled = false;
  15.         public int operationCalledTimes = 0, preRetryCalledTimes = 0;
  16.     }
  17.  
  18.     @Rule
  19.     public ExpectedException thrown = ExpectedException.none();
  20.  
  21.     @Test
  22.     public void noRetriesNeeded() throws Exception {
  23.         String result = Retry.retry(
  24.                 // 1 retry
  25.                 1,
  26.                 // operation
  27.                 new Function0<String>() {
  28.                     public String apply() {
  29.                         return RESULT;
  30.                     }
  31.                 },
  32.                 // checker
  33.                 new Function1<String, Boolean>() {
  34.                     public Boolean apply(String checkedResult) {
  35.                         return true;
  36.                     }
  37.                 },
  38.                 // pre-retry
  39.                 new Function0<Void>() {
  40.                     public Void apply() {
  41.                         fail("wrong pre-retry");
  42.                         return null;
  43.                     }
  44.                 },
  45.                 // use default error message
  46.                 null
  47.         );
  48.  
  49.         assertEquals(RESULT, result);
  50.     }
  51.  
  52.     @Test
  53.     public void succeededAfterRetry() throws Exception {
  54.         final State state = new State();
  55.         String result = Retry.retry(
  56.                 // 1 retry max
  57.                 1,
  58.                 // operation
  59.                 new Function0<String>() {
  60.                     public String apply() {
  61.                         return RESULT;
  62.                     }
  63.                 },
  64.                 // checker
  65.                 new Function1<String, Boolean>() {
  66.                     public Boolean apply(String checkedResult) {
  67.                         final boolean result;
  68.                         state.operationCalledTimes++;
  69.                         if (state.operationCalled) {
  70.                             result = true;
  71.                         } else {
  72.                             state.operationCalled = true;
  73.                             result = false;
  74.                         }
  75.                         return result;
  76.                     }
  77.                 },
  78.                 // pre-retry
  79.                 new Function0<Void>() {
  80.                     public Void apply() {
  81.                         state.preRetryCalledTimes++;
  82.                         if (state.preRetryCalled) {
  83.                             fail("pre-retry already called");
  84.                         } else {
  85.                             state.preRetryCalled = true;
  86.                         }
  87.                         return null;
  88.                     }
  89.                 },
  90.                 // use default error message
  91.                 null
  92.         );
  93.  
  94.         assertEquals(RESULT, result);
  95.         assertTrue(state.operationCalled);
  96.         assertTrue(state.preRetryCalled);
  97.         assertEquals(2, state.operationCalledTimes);
  98.         assertEquals(1, state.preRetryCalledTimes);
  99.     }
  100.  
  101.     @Test
  102.     public void failedAfterRetriesExhausted() throws Exception {
  103.         final String errorMessage = "oops";
  104.         thrown.expect(Exception.class);
  105.         thrown.expectMessage(errorMessage);
  106.  
  107.         final State state = new State();
  108.         Retry.retry(
  109.                 // 5 retries
  110.                 5,
  111.                 // operation
  112.                 new Function0<String>() {
  113.                     public String apply() {
  114.                         return RESULT;
  115.                     }
  116.                 },
  117.                 // checker
  118.                 new Function1<String, Boolean>() {
  119.                     public Boolean apply(String checkedResult) {
  120.                         return false;
  121.                     }
  122.                 },
  123.                 // pre-retry
  124.                 new Function0<Void>() {
  125.                     public Void apply() {
  126.                         state.preRetryCalledTimes++;
  127.                         assertFalse(state.preRetryCalledTimes > 5);
  128.                         return null;
  129.                     }
  130.                 },
  131.                 // custom error message
  132.                 errorMessage
  133.         );
  134.     }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment