PageRenderTime 57ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/src/test/java/com/atlassian/fugue/retry/RetryFunctionTest.java

https://bitbucket.org/atlassian/fugue
Java | 91 lines | 60 code | 16 blank | 15 comment | 0 complexity | 39763e0551ab4caf7f37b953dde4eb5c MD5 | raw file
  1. /*
  2. Copyright 2010 Atlassian
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package com.atlassian.fugue.retry;
  14. import static junit.framework.Assert.assertEquals;
  15. import static junit.framework.Assert.assertSame;
  16. import static junit.framework.Assert.fail;
  17. import static org.mockito.Matchers.anyString;
  18. import static org.mockito.Mockito.times;
  19. import static org.mockito.Mockito.verify;
  20. import static org.mockito.Mockito.verifyNoMoreInteractions;
  21. import static org.mockito.Mockito.when;
  22. import static org.mockito.MockitoAnnotations.initMocks;
  23. import org.junit.Before;
  24. import org.junit.Test;
  25. import org.mockito.Mock;
  26. import com.google.common.base.Function;
  27. public class RetryFunctionTest {
  28. private static final int ATTEMPTS = 4;
  29. @Mock private Function<String, Integer> function;
  30. @Mock private ExceptionHandler exceptionHandler;
  31. @Mock private RuntimeException runtimeException;
  32. public static final String INPUT = "1";
  33. public static final Integer EXPECTED = 1;
  34. @Before public void setUp() {
  35. initMocks(this);
  36. }
  37. @Test public void basicFunction() {
  38. when(function.apply(INPUT)).thenReturn(EXPECTED);
  39. final Integer result = new RetryFunction<String, Integer>(function, ATTEMPTS).apply(INPUT);
  40. verify(function).apply(INPUT);
  41. assertEquals(EXPECTED, result);
  42. }
  43. @Test public void basicFunctionRetry() {
  44. when(function.apply(anyString())).thenThrow(runtimeException);
  45. try {
  46. new RetryFunction<String, Integer>(function, ATTEMPTS).apply(INPUT);
  47. fail("Expected a exception.");
  48. } catch (final RuntimeException e) {
  49. assertSame(runtimeException, e);
  50. }
  51. verify(function, times(ATTEMPTS)).apply(INPUT);
  52. }
  53. @Test public void functionRetryWithExceptionHandler() {
  54. when(function.apply(INPUT)).thenThrow(runtimeException);
  55. try {
  56. new RetryFunction<String, Integer>(function, ATTEMPTS, exceptionHandler).apply(INPUT);
  57. fail("Expected a exception.");
  58. } catch (final RuntimeException e) {
  59. assertSame(runtimeException, e);
  60. }
  61. verify(function, times(ATTEMPTS)).apply(INPUT);
  62. verify(exceptionHandler, times(ATTEMPTS)).handle(runtimeException);
  63. }
  64. @Test public void functionEarlyExit() {
  65. when(function.apply(INPUT)).thenThrow(new RuntimeException("First attempt")).thenReturn(EXPECTED)
  66. .thenThrow(new RuntimeException("Third attempt")).thenThrow(new RuntimeException("Fourth attempt"));
  67. final Integer result = new RetryFunction<String, Integer>(function, ATTEMPTS).apply(INPUT);
  68. assertEquals(EXPECTED, result);
  69. verify(function, times(2)).apply(INPUT);
  70. verifyNoMoreInteractions(function);
  71. }
  72. }