diff --git a/src/main/java/com/amazonaws/services/kinesis/clientlibrary/lib/worker/ThrottlingReporter.java b/src/main/java/com/amazonaws/services/kinesis/clientlibrary/lib/worker/ThrottlingReporter.java index cd78487f..f88f131f 100644 --- a/src/main/java/com/amazonaws/services/kinesis/clientlibrary/lib/worker/ThrottlingReporter.java +++ b/src/main/java/com/amazonaws/services/kinesis/clientlibrary/lib/worker/ThrottlingReporter.java @@ -3,6 +3,7 @@ package com.amazonaws.services.kinesis.clientlibrary.lib.worker; import lombok.Getter; import lombok.RequiredArgsConstructor; import lombok.extern.apachecommons.CommonsLog; +import org.apache.commons.logging.Log; @RequiredArgsConstructor @CommonsLog @@ -19,9 +20,9 @@ class ThrottlingReporter { + consecutiveThrottles + " consecutively"; if (consecutiveThrottles > maxConsecutiveWarnThrottles) { - log.error(message); + getLog().error(message); } else { - log.warn(message); + getLog().warn(message); } } @@ -30,4 +31,8 @@ class ThrottlingReporter { consecutiveThrottles = 0; } + protected Log getLog() { + return log; + } + } diff --git a/src/test/java/com/amazonaws/services/kinesis/clientlibrary/lib/worker/ThrottlingReporterTest.java b/src/test/java/com/amazonaws/services/kinesis/clientlibrary/lib/worker/ThrottlingReporterTest.java index 90560a9e..d0645229 100644 --- a/src/test/java/com/amazonaws/services/kinesis/clientlibrary/lib/worker/ThrottlingReporterTest.java +++ b/src/test/java/com/amazonaws/services/kinesis/clientlibrary/lib/worker/ThrottlingReporterTest.java @@ -1,71 +1,27 @@ package com.amazonaws.services.kinesis.clientlibrary.lib.worker; import static org.mockito.Matchers.any; -import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; -import java.util.HashMap; -import java.util.Map; - import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogConfigurationException; -import org.apache.commons.logging.impl.LogFactoryImpl; -import org.junit.Before; -import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; -import org.mockito.Mockito; +import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; -import lombok.RequiredArgsConstructor; - @RunWith(MockitoJUnitRunner.class) public class ThrottlingReporterTest { private static final String SHARD_ID = "Shard-001"; - private static final Log throttleLog = mock(Log.class); - - @RequiredArgsConstructor - private static class DirectRegisterLogFactory extends LogFactoryImpl { - - private final Map, Log> logMapping; - - public static void mockLogFactory(Map, Log> logMapping) { - factories.put(Thread.currentThread().getContextClassLoader(), new DirectRegisterLogFactory(logMapping)); - } - - @Override - public Log getInstance(Class clazz) throws LogConfigurationException { - if (logMapping.containsKey(clazz)) { - return logMapping.get(clazz); - } - return super.getInstance(clazz); - } - } - - @BeforeClass - public static void beforeClass() { - Map, Log> logMapping = new HashMap<>(); - logMapping.put(ThrottlingReporter.class, throttleLog); - - DirectRegisterLogFactory.mockLogFactory(logMapping); - } - - @Before - public void before() { - // - // Have to to do this since the only time that the logFactory will be able to inject a mock is on - // class load. - // - Mockito.reset(throttleLog); - } + @Mock + private Log throttleLog; @Test public void testLessThanMaxThrottles() { - ThrottlingReporter reporter = new ThrottlingReporter(5, SHARD_ID); + ThrottlingReporter reporter = new LogTestingThrottingReporter(5, SHARD_ID); reporter.throttled(); verify(throttleLog).warn(any(Object.class)); verify(throttleLog, never()).error(any(Object.class)); @@ -74,7 +30,7 @@ public class ThrottlingReporterTest { @Test public void testMoreThanMaxThrottles() { - ThrottlingReporter reporter = new ThrottlingReporter(1, SHARD_ID); + ThrottlingReporter reporter = new LogTestingThrottingReporter(1, SHARD_ID); reporter.throttled(); reporter.throttled(); verify(throttleLog).warn(any(Object.class)); @@ -83,7 +39,7 @@ public class ThrottlingReporterTest { @Test public void testSuccessResetsErrors() { - ThrottlingReporter reporter = new ThrottlingReporter(1, SHARD_ID); + ThrottlingReporter reporter = new LogTestingThrottingReporter(1, SHARD_ID); reporter.throttled(); reporter.throttled(); reporter.throttled(); @@ -95,4 +51,16 @@ public class ThrottlingReporterTest { } + private class LogTestingThrottingReporter extends ThrottlingReporter { + + public LogTestingThrottingReporter(int maxConsecutiveWarnThrottles, String shardId) { + super(maxConsecutiveWarnThrottles, shardId); + } + + @Override + protected Log getLog() { + return throttleLog; + } + } + } \ No newline at end of file