Skip to content

Commit

Permalink
fixed flaky mocking in AbstractSocketAppenderTest due to multi-thread…
Browse files Browse the repository at this point in the history
…ing issues
  • Loading branch information
SierraGolf committed Dec 29, 2014
1 parent 929b984 commit 1f4bdb3
Showing 1 changed file with 21 additions and 6 deletions.
Expand Up @@ -76,7 +76,7 @@ public class AbstractSocketAppenderTest {

@Before
public void setupValidAppenderWithMockDependencies() throws Exception {
doReturn(socket).when(socketConnector).call();

doReturn(objectWriter).when(objectWriterFactory).newAutoFlushingObjectWriter(any(OutputStream.class));
doReturn(deque).when(queueFactory).<String>newLinkedBlockingDeque(anyInt());

Expand All @@ -89,8 +89,6 @@ public void tearDown() throws Exception {
appender.stop();
assertFalse(appender.isStarted());

breakOutSocketReConnectionLoop();

executorService.shutdownNow();
assertTrue(executorService.awaitTermination(TIMEOUT, TimeUnit.MILLISECONDS));
}
Expand Down Expand Up @@ -191,9 +189,10 @@ public void createsSocketConnectorWithConfiguredParameters() throws Exception {
}

@Test
public void addsInfoMessageWhenSocketConnectionWasEstablished() {
public void addsInfoMessageWhenSocketConnectionWasEstablished() throws Exception {

// when
mockOneSuccessfulSocketConnection();
appender.start();

// then
Expand All @@ -204,6 +203,7 @@ public void addsInfoMessageWhenSocketConnectionWasEstablished() {
public void addsInfoMessageWhenSocketConnectionFailed() throws Exception {

// given
mockOneSuccessfulSocketConnection();
doThrow(new IOException()).when(objectWriterFactory).newAutoFlushingObjectWriter(any(OutputStream.class));
appender.start();

Expand All @@ -218,6 +218,7 @@ public void addsInfoMessageWhenSocketConnectionFailed() throws Exception {
public void closesSocketOnException() throws Exception {

// given
mockOneSuccessfulSocketConnection();
doThrow(new IOException()).when(objectWriterFactory).newAutoFlushingObjectWriter(any(OutputStream.class));
appender.start();

Expand All @@ -232,6 +233,7 @@ public void closesSocketOnException() throws Exception {
public void addsInfoMessageWhenSocketConnectionClosed() throws Exception {

// given
mockOneSuccessfulSocketConnection();
doThrow(new IOException()).when(objectWriterFactory).newAutoFlushingObjectWriter(any(OutputStream.class));
appender.start();

Expand All @@ -246,6 +248,7 @@ public void addsInfoMessageWhenSocketConnectionClosed() throws Exception {
public void shutsDownOnInterruptWhileWaitingForEvent() throws Exception {

// given
mockOneSuccessfulSocketConnection();
doThrow(new InterruptedException()).when(deque).takeFirst();

// when
Expand Down Expand Up @@ -328,6 +331,7 @@ public void addsInfoMessageWhenEventCouldNotBeQueuedInConfiguredTimeoutDueToQueu
public void takesEventsFromTheFrontOfTheDeque() throws Exception {

// given
mockOneSuccessfulSocketConnection();
appender.start();
awaitStartOfEventDispatching();

Expand All @@ -342,6 +346,7 @@ public void takesEventsFromTheFrontOfTheDeque() throws Exception {
public void reAddsEventAtTheFrontOfTheDequeWhenTransmissionFails() throws Exception {

// given
mockOneSuccessfulSocketConnection();
doThrow(new IOException()).when(objectWriter).write(anyObject());
appender.start();
awaitStartOfEventDispatching();
Expand Down Expand Up @@ -372,6 +377,7 @@ public void addsErrorMessageWhenAppendingIsInterruptedWhileWaitingForTheQueueToA
public void postProcessesEventsBeforeTransformingItToASerializable() throws Exception {

// given
mockOneSuccessfulSocketConnection();
appender.start();
awaitStartOfEventDispatching();

Expand All @@ -389,6 +395,7 @@ public void postProcessesEventsBeforeTransformingItToASerializable() throws Exce
public void writesSerializedEventToStream() throws Exception {

// given
mockOneSuccessfulSocketConnection();
when(preSerializationTransformer.transform("some event")).thenReturn("some serialized event");
appender.start();
awaitStartOfEventDispatching();
Expand All @@ -404,6 +411,7 @@ public void writesSerializedEventToStream() throws Exception {
public void addsInfoMessageWhenEventIsBeingDroppedBecauseOfConnectionProblemAndDequeCapacityLimitReached() throws Exception {

// given
mockOneSuccessfulSocketConnection();
doThrow(new IOException()).when(objectWriter).write(anyObject());
doReturn(false).when(deque).offerFirst("some event");
appender.start();
Expand All @@ -421,6 +429,7 @@ public void addsInfoMessageWhenEventIsBeingDroppedBecauseOfConnectionProblemAndD
public void reEstablishesSocketConnectionOnConnectionDropWhenWritingEvent() throws Exception {

// given
mockTwoSuccessfulSocketConnections();
doThrow(new IOException()).when(objectWriter).write(anyObject());
appender.start();
awaitStartOfEventDispatching();
Expand All @@ -436,6 +445,7 @@ public void reEstablishesSocketConnectionOnConnectionDropWhenWritingEvent() thro
public void triesToReEstablishSocketConnectionIfItFailed() throws Exception {

// given
mockOneSuccessfulSocketConnection();
doThrow(new IOException()).when(socket).getOutputStream();
appender.start();

Expand All @@ -450,6 +460,7 @@ public void triesToReEstablishSocketConnectionIfItFailed() throws Exception {
public void usesConfiguredAcceptConnectionTimeoutAndResetsSocketTimeoutAfterSuccessfulConnection() throws Exception {

// when
mockOneSuccessfulSocketConnection();
appender.setAcceptConnectionTimeout(42);
appender.start();
awaitStartOfEventDispatching();
Expand All @@ -468,8 +479,12 @@ private void awaitStartOfEventDispatching() throws InterruptedException {
verify(deque, timeout(TIMEOUT)).takeFirst();
}

private void breakOutSocketReConnectionLoop() throws InterruptedException {
doReturn(null).when(socketConnector).call();
private void mockOneSuccessfulSocketConnection() throws InterruptedException {
doReturn(socket).doReturn(null).when(socketConnector).call();
}

private void mockTwoSuccessfulSocketConnections() throws InterruptedException {
doReturn(socket).doReturn(socket).doReturn(null).when(socketConnector).call();
}

private static class InstrumentedSocketAppender extends AbstractSocketAppender<String> {
Expand Down

0 comments on commit 1f4bdb3

Please sign in to comment.