PageRenderTime 46ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/atlassian/fugue
Java | 101 lines | 71 code | 15 blank | 15 comment | 1 complexity | 05a6f35477e9a99bfe46cc3d48cf4c68 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.assertSame;
  15. import static junit.framework.Assert.fail;
  16. import static org.hamcrest.MatcherAssert.assertThat;
  17. import static org.hamcrest.Matchers.is;
  18. import static org.mockito.Mockito.doThrow;
  19. import static org.mockito.Mockito.times;
  20. import static org.mockito.Mockito.verify;
  21. import static org.mockito.Mockito.verifyZeroInteractions;
  22. import static org.mockito.MockitoAnnotations.initMocks;
  23. import java.util.concurrent.atomic.AtomicReference;
  24. import org.junit.Before;
  25. import org.junit.Test;
  26. import org.mockito.Mock;
  27. public class RetryTaskTest {
  28. private static final int ATTEMPTS = 4;
  29. @Mock private Runnable task;
  30. @Mock private ExceptionHandler exceptionHandler;
  31. @Mock private RuntimeException runtimeException;
  32. @Before public void setUp() {
  33. initMocks(this);
  34. }
  35. @Test public void basicTask() {
  36. new RetryTask(task, ATTEMPTS).run();
  37. verify(task).run();
  38. }
  39. @Test public void basicTaskRetry() {
  40. doThrow(runtimeException).when(task).run();
  41. try {
  42. new RetryTask(task, ATTEMPTS).run();
  43. fail("Expected a exception.");
  44. } catch (final RuntimeException e) {
  45. assertSame(runtimeException, e);
  46. }
  47. verify(task, times(ATTEMPTS)).run();
  48. }
  49. @Test public void taskWithExceptionHandler() {
  50. new RetryTask(task, ATTEMPTS, exceptionHandler).run();
  51. verify(task).run();
  52. verifyZeroInteractions(exceptionHandler);
  53. }
  54. @Test public void taskRetryWithExceptions() {
  55. doThrow(runtimeException).when(task).run();
  56. try {
  57. new RetryTask(task, ATTEMPTS, exceptionHandler).run();
  58. fail("Expected a exception.");
  59. } catch (final RuntimeException e) {
  60. assertSame(runtimeException, e);
  61. }
  62. verify(task, times(ATTEMPTS)).run();
  63. verify(exceptionHandler, times(ATTEMPTS)).handle(runtimeException);
  64. }
  65. @Test public void taskEarlyExit() {
  66. final AtomicReference<Integer> failcount = new AtomicReference<Integer>(0);
  67. Runnable localTask = new Runnable() {
  68. public void run() {
  69. failcount.set(failcount.get() + 1);
  70. switch (failcount.get()) {
  71. case 1:
  72. throw new RuntimeException("First attempt");
  73. case 2:
  74. return;
  75. default:
  76. throw new RuntimeException("Third runthrough (fail)");
  77. }
  78. }
  79. };
  80. new RetryTask(localTask, ATTEMPTS).run();
  81. assertThat(failcount.get(), is(2));
  82. }
  83. }