Ordering Issues with Logging Tests

The ordering of the test execution can cause the ThrottlingReporter test
to fail due to the class being loaded by an earlier test.
This commit is contained in:
Pfifer, Justin 2017-02-17 09:38:11 -08:00
parent 2b2ae45b0f
commit eb01d8759a
2 changed files with 25 additions and 52 deletions

View file

@ -3,6 +3,7 @@ package com.amazonaws.services.kinesis.clientlibrary.lib.worker;
import lombok.Getter; import lombok.Getter;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.apachecommons.CommonsLog; import lombok.extern.apachecommons.CommonsLog;
import org.apache.commons.logging.Log;
@RequiredArgsConstructor @RequiredArgsConstructor
@CommonsLog @CommonsLog
@ -19,9 +20,9 @@ class ThrottlingReporter {
+ consecutiveThrottles + " consecutively"; + consecutiveThrottles + " consecutively";
if (consecutiveThrottles > maxConsecutiveWarnThrottles) { if (consecutiveThrottles > maxConsecutiveWarnThrottles) {
log.error(message); getLog().error(message);
} else { } else {
log.warn(message); getLog().warn(message);
} }
} }
@ -30,4 +31,8 @@ class ThrottlingReporter {
consecutiveThrottles = 0; consecutiveThrottles = 0;
} }
protected Log getLog() {
return log;
}
} }

View file

@ -1,71 +1,27 @@
package com.amazonaws.services.kinesis.clientlibrary.lib.worker; package com.amazonaws.services.kinesis.clientlibrary.lib.worker;
import static org.mockito.Matchers.any; import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never; import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times; import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify; 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.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.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.Mockito; import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner; import org.mockito.runners.MockitoJUnitRunner;
import lombok.RequiredArgsConstructor;
@RunWith(MockitoJUnitRunner.class) @RunWith(MockitoJUnitRunner.class)
public class ThrottlingReporterTest { public class ThrottlingReporterTest {
private static final String SHARD_ID = "Shard-001"; private static final String SHARD_ID = "Shard-001";
private static final Log throttleLog = mock(Log.class); @Mock
private Log throttleLog;
@RequiredArgsConstructor
private static class DirectRegisterLogFactory extends LogFactoryImpl {
private final Map<Class<?>, Log> logMapping;
public static void mockLogFactory(Map<Class<?>, 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<Class<?>, 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);
}
@Test @Test
public void testLessThanMaxThrottles() { public void testLessThanMaxThrottles() {
ThrottlingReporter reporter = new ThrottlingReporter(5, SHARD_ID); ThrottlingReporter reporter = new LogTestingThrottingReporter(5, SHARD_ID);
reporter.throttled(); reporter.throttled();
verify(throttleLog).warn(any(Object.class)); verify(throttleLog).warn(any(Object.class));
verify(throttleLog, never()).error(any(Object.class)); verify(throttleLog, never()).error(any(Object.class));
@ -74,7 +30,7 @@ public class ThrottlingReporterTest {
@Test @Test
public void testMoreThanMaxThrottles() { public void testMoreThanMaxThrottles() {
ThrottlingReporter reporter = new ThrottlingReporter(1, SHARD_ID); ThrottlingReporter reporter = new LogTestingThrottingReporter(1, SHARD_ID);
reporter.throttled(); reporter.throttled();
reporter.throttled(); reporter.throttled();
verify(throttleLog).warn(any(Object.class)); verify(throttleLog).warn(any(Object.class));
@ -83,7 +39,7 @@ public class ThrottlingReporterTest {
@Test @Test
public void testSuccessResetsErrors() { public void testSuccessResetsErrors() {
ThrottlingReporter reporter = new ThrottlingReporter(1, SHARD_ID); ThrottlingReporter reporter = new LogTestingThrottingReporter(1, SHARD_ID);
reporter.throttled(); reporter.throttled();
reporter.throttled(); 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;
}
}
} }