Fix incorrect use of TestLogHandler that causes tests to be flaky

The following test failed repeatedly and consistently during testing of
[] - part of the work to upgrade mockito.
//third_party/java_src/gtld/javatests/google/registry/flows:EppControllerTest

The test failure was not caused by the mockito change, although it's possible
that the mockito change did change something that made it more likely to occur.
The failure was caused because the test added a TestLogHandler to a Logger in
order to intercept a LogRecord but did not maintain a strong reference to the
Logger. So sometimes it was GCed before the EppController class obtained its
own reference to it. That meant that the Logger that produced the LogRecord
was not the same as the Logger to which the TestLogHandler was attached and so
the LogRecord was never intercepted and the test failed.

This change keeps a strong reference and also removes the handler after the
test has finished.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=171824566
This commit is contained in:
jianglai 2017-10-11 08:22:28 -07:00
parent 9a3877fdb2
commit 5790f9743f

View file

@ -53,6 +53,7 @@ import java.util.logging.LogRecord;
import java.util.logging.Logger;
import org.joda.time.DateTime;
import org.json.simple.JSONValue;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
@ -85,6 +86,12 @@ public class EppControllerTest extends ShardableTestCase {
private final Clock clock = new FakeClock(START_TIME);
private final TestLogHandler logHandler = new TestLogHandler();
/**
* Hold a strong reference to the logger whose output is being intercepted to prevent it being
* GCed.
*/
private final Logger loggerToIntercept = Logger.getLogger(EppController.class.getCanonicalName());
private final String domainCreateXml =
loadFileWithSubstitutions(
getClass(), "domain_create_prettyprinted.xml", ImmutableMap.<String, String>of());
@ -93,7 +100,7 @@ public class EppControllerTest extends ShardableTestCase {
@Before
public void setUp() throws Exception {
Logger.getLogger(EppController.class.getCanonicalName()).addHandler(logHandler);
loggerToIntercept.addHandler(logHandler);
when(sessionMetadata.getClientId()).thenReturn("some-client");
when(flowComponentBuilder.flowModule(Matchers.<FlowModule>any()))
@ -114,6 +121,11 @@ public class EppControllerTest extends ShardableTestCase {
eppController.serverTridProvider = new FakeServerTridProvider();
}
@After
public void tearDown() {
loggerToIntercept.removeHandler(logHandler);
}
@Test
public void testMarshallingUnknownError() throws Exception {
marshal(