AdminConnectionRetryInterceptor implements MethodInterceptor { @Override public Object invoke(MethodInvocation invocation) throws Throwable { long retryCount = 0L; Throwable cause = null; Object returnObject = null; long lastExceptionTime = System.currentTimeMillis(); while (true) { try { cause = null; returnObject = invocation.proceed(); break; } catch (DataAccessException | TransactionException e) { if (System.currentTimeMillis() - lastExceptionTime > retryReset) { retryCount = 0L; } lastExceptionTime = System.currentTimeMillis(); cause = e; if (retryCount >= maxRetryCount) { LOGGER.error(LogId.EAL025063, cause, maxRetryCount); break; } TimeUnit.MILLISECONDS.sleep(retryInterval); retryCount++; LOGGER.info(LogId.IAL025017, retryCount, maxRetryCount, retryReset, retryInterval); } } if (cause != null) { throw cause; } return returnObject; }  @Override Object invoke(MethodInvocation invocation);  }
@Test public void testInvoke01() throws Throwable { MethodInvocation mockMethodInvocation = mock(MethodInvocation.class); when(mockMethodInvocation.proceed()).thenReturn(null); assertNull(adminConnectionRetryInterceptor.invoke( mockMethodInvocation)); }
@Test public void testInvoke02() throws Throwable { MethodInvocation mockMethodInvocation = mock(MethodInvocation.class); RecoverableDataAccessException recoverableDataAccessException = new RecoverableDataAccessException(null); try { when(mockMethodInvocation.proceed()).thenThrow( recoverableDataAccessException); adminConnectionRetryInterceptor.invoke(mockMethodInvocation); fail(); } catch (DataAccessException e) { assertSame(recoverableDataAccessException, e); } assertEquals(Level.ERROR, logger.getLoggingEvents().get(0).getLevel()); assertEquals( "[EAL025063] Connection retry count exceeded limit. maxRetryCount:0", logger.getLoggingEvents().get(0).getMessage()); }
@Test public void testInvoke03() throws Throwable { MethodInvocation mockMethodInvocation = mock(MethodInvocation.class); TransactionException transactionSystemException = new TransactionSystemException("test exception"); try { when(mockMethodInvocation.proceed()).thenThrow( transactionSystemException); adminConnectionRetryInterceptor.invoke(mockMethodInvocation); fail(); } catch (TransactionException e) { assertSame(transactionSystemException, e); } assertEquals(Level.ERROR, logger.getLoggingEvents().get(0).getLevel()); assertEquals( "[EAL025063] Connection retry count exceeded limit. maxRetryCount:0", logger.getLoggingEvents().get(0).getMessage()); }
@Test public void testInvoke05() throws Throwable { MethodInvocation mockMethodInvocation = mock(MethodInvocation.class); ReflectionTestUtils.setField(adminConnectionRetryInterceptor, "retryInterval", 500); ReflectionTestUtils.setField(adminConnectionRetryInterceptor, "maxRetryCount", 3); when(mockMethodInvocation.proceed()).thenThrow( new RecoverableDataAccessException(null)).thenThrow( new RecoverableDataAccessException(null)).thenReturn( null); assertNull(adminConnectionRetryInterceptor.invoke( mockMethodInvocation)); verify(mockMethodInvocation, times(3)).proceed(); assertEquals(Level.INFO, logger.getLoggingEvents().get(0).getLevel()); assertEquals( "[IAL025017] RetryDetails. currentRetryCount:1, retryMaxCount:3, retryReset:600,000 ms, retryInterval:500 ms", logger.getLoggingEvents().get(0).getMessage()); assertEquals(Level.INFO, logger.getLoggingEvents().get(1).getLevel()); assertEquals( "[IAL025017] RetryDetails. currentRetryCount:2, retryMaxCount:3, retryReset:600,000 ms, retryInterval:500 ms", logger.getLoggingEvents().get(1).getMessage()); }
@Test public void testInvoke06() throws Throwable { MethodInvocation mockMethodInvocation = mock(MethodInvocation.class); ReflectionTestUtils.setField(adminConnectionRetryInterceptor, "retryInterval", 500); ReflectionTestUtils.setField(adminConnectionRetryInterceptor, "maxRetryCount", 3); when(mockMethodInvocation.proceed()).thenThrow( new TransactionSystemException("test exception")).thenThrow( new TransactionSystemException("test exception")) .thenReturn(null); assertNull(adminConnectionRetryInterceptor.invoke( mockMethodInvocation)); verify(mockMethodInvocation, times(3)).proceed(); assertEquals(Level.INFO, logger.getLoggingEvents().get(0).getLevel()); assertEquals( "[IAL025017] RetryDetails. currentRetryCount:1, retryMaxCount:3, retryReset:600,000 ms, retryInterval:500 ms", logger.getLoggingEvents().get(0).getMessage()); assertEquals(Level.INFO, logger.getLoggingEvents().get(1).getLevel()); assertEquals( "[IAL025017] RetryDetails. currentRetryCount:2, retryMaxCount:3, retryReset:600,000 ms, retryInterval:500 ms", logger.getLoggingEvents().get(1).getMessage()); }
@Test public void testInvoke08() throws Throwable { MethodInvocation mockMethodInvocation = mock(MethodInvocation.class); RecoverableDataAccessException recoverableDataAccessException = new RecoverableDataAccessException(null); ReflectionTestUtils.setField(adminConnectionRetryInterceptor, "retryInterval", 500); ReflectionTestUtils.setField(adminConnectionRetryInterceptor, "maxRetryCount", 3); try { when(mockMethodInvocation.proceed()).thenThrow( new RecoverableDataAccessException(null)).thenThrow( new RecoverableDataAccessException(null)).thenThrow( new RecoverableDataAccessException(null)) .thenThrow(recoverableDataAccessException).thenReturn(null); adminConnectionRetryInterceptor.invoke(mockMethodInvocation); fail(); } catch (DataAccessException e) { assertSame(recoverableDataAccessException, e); } verify(mockMethodInvocation, times(4)).proceed(); assertEquals(Level.INFO, logger.getLoggingEvents().get(0).getLevel()); assertEquals( "[IAL025017] RetryDetails. currentRetryCount:1, retryMaxCount:3, retryReset:600,000 ms, retryInterval:500 ms", logger.getLoggingEvents().get(0).getMessage()); assertEquals(Level.INFO, logger.getLoggingEvents().get(1).getLevel()); assertEquals( "[IAL025017] RetryDetails. currentRetryCount:2, retryMaxCount:3, retryReset:600,000 ms, retryInterval:500 ms", logger.getLoggingEvents().get(1).getMessage()); assertEquals(Level.INFO, logger.getLoggingEvents().get(2).getLevel()); assertEquals( "[IAL025017] RetryDetails. currentRetryCount:3, retryMaxCount:3, retryReset:600,000 ms, retryInterval:500 ms", logger.getLoggingEvents().get(2).getMessage()); assertEquals(Level.ERROR, logger.getLoggingEvents().get(3).getLevel()); assertEquals( "[EAL025063] Connection retry count exceeded limit. maxRetryCount:3", logger.getLoggingEvents().get(3).getMessage()); }
@Test public void testInvoke09() throws Throwable { MethodInvocation mockMethodInvocation = mock(MethodInvocation.class); TransactionException transactionException = new TransactionSystemException("test exception"); ReflectionTestUtils.setField(adminConnectionRetryInterceptor, "retryInterval", 500); ReflectionTestUtils.setField(adminConnectionRetryInterceptor, "maxRetryCount", 3); try { when(mockMethodInvocation.proceed()).thenThrow( new TransactionSystemException("test exception 1")) .thenThrow( new TransactionSystemException("test exception 2")) .thenThrow( new TransactionSystemException("test exception 3")) .thenThrow(transactionException).thenReturn(null); adminConnectionRetryInterceptor.invoke(mockMethodInvocation); fail(); } catch (TransactionException e) { assertSame(transactionException, e); } verify(mockMethodInvocation, times(4)).proceed(); assertEquals(Level.INFO, logger.getLoggingEvents().get(0).getLevel()); assertEquals( "[IAL025017] RetryDetails. currentRetryCount:1, retryMaxCount:3, retryReset:600,000 ms, retryInterval:500 ms", logger.getLoggingEvents().get(0).getMessage()); assertEquals(Level.INFO, logger.getLoggingEvents().get(1).getLevel()); assertEquals( "[IAL025017] RetryDetails. currentRetryCount:2, retryMaxCount:3, retryReset:600,000 ms, retryInterval:500 ms", logger.getLoggingEvents().get(1).getMessage()); assertEquals(Level.INFO, logger.getLoggingEvents().get(2).getLevel()); assertEquals( "[IAL025017] RetryDetails. currentRetryCount:3, retryMaxCount:3, retryReset:600,000 ms, retryInterval:500 ms", logger.getLoggingEvents().get(2).getMessage()); assertEquals(Level.ERROR, logger.getLoggingEvents().get(3).getLevel()); assertEquals( "[EAL025063] Connection retry count exceeded limit. maxRetryCount:3", logger.getLoggingEvents().get(3).getMessage()); }
@Test public void testInvoke11() throws Throwable { MethodInvocation mockMethodInvocation = mock(MethodInvocation.class); ReflectionTestUtils.setField(adminConnectionRetryInterceptor, "retryInterval", 500); ReflectionTestUtils.setField(adminConnectionRetryInterceptor, "maxRetryCount", 3); ReflectionTestUtils.setField(adminConnectionRetryInterceptor, "retryReset", 300); when(mockMethodInvocation.proceed()).thenThrow( new RecoverableDataAccessException(null)).thenThrow( new RecoverableDataAccessException(null)).thenThrow( new RecoverableDataAccessException(null)) .thenThrow(new RecoverableDataAccessException(null)).thenReturn( null); assertNull(adminConnectionRetryInterceptor.invoke( mockMethodInvocation)); verify(mockMethodInvocation, times(5)).proceed(); assertEquals(Level.INFO, logger.getLoggingEvents().get(0).getLevel()); assertEquals( "[IAL025017] RetryDetails. currentRetryCount:1, retryMaxCount:3, retryReset:300 ms, retryInterval:500 ms", logger.getLoggingEvents().get(0).getMessage()); assertEquals(Level.INFO, logger.getLoggingEvents().get(1).getLevel()); assertEquals( "[IAL025017] RetryDetails. currentRetryCount:1, retryMaxCount:3, retryReset:300 ms, retryInterval:500 ms", logger.getLoggingEvents().get(1).getMessage()); assertEquals(Level.INFO, logger.getLoggingEvents().get(2).getLevel()); assertEquals( "[IAL025017] RetryDetails. currentRetryCount:1, retryMaxCount:3, retryReset:300 ms, retryInterval:500 ms", logger.getLoggingEvents().get(2).getMessage()); assertEquals(Level.INFO, logger.getLoggingEvents().get(3).getLevel()); assertEquals( "[IAL025017] RetryDetails. currentRetryCount:1, retryMaxCount:3, retryReset:300 ms, retryInterval:500 ms", logger.getLoggingEvents().get(3).getMessage()); }