Skip to content

Commit

Permalink
more robust testing
Browse files Browse the repository at this point in the history
  • Loading branch information
ceki committed May 10, 2013
1 parent b6fe312 commit c91fd5d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 39 deletions.
Expand Up @@ -33,6 +33,7 @@

import ch.qos.logback.core.status.InfoStatus;
import ch.qos.logback.core.status.StatusChecker;
import ch.qos.logback.core.testUtil.Wait;
import ch.qos.logback.core.util.Duration;
import ch.qos.logback.core.util.StatusPrinter;
import org.junit.After;
Expand Down Expand Up @@ -118,7 +119,7 @@ public void testStartUnknownHost() throws Exception {
receiver.start();
// invalid host name does not impact start status
assertTrue(receiver.isStarted());
waitForStatusMessage(statusChecker, "unknown host");
Wait.forStatusMessage(statusChecker, "unknown host");
statusChecker.assertContainsMatch(Status.ERROR, "unknown host");
}

Expand Down Expand Up @@ -170,7 +171,7 @@ public void testDispatchEventForEnabledLevel() throws Exception {
receiver.setPort(port);
receiver.start();
// TODO find a way to wait for connector creation
waitForActiveCountToEqual((ThreadPoolExecutor) receiversLoggerContext.getExecutorService(), 1);
Wait.forActiveCountToEqual((ThreadPoolExecutor) receiversLoggerContext.getExecutorService(), 1);
Thread.sleep(100);
//assertTrue(receiver.awaitConnectorCreated(DELAY));
Socket socket = serverSocket.accept();
Expand Down Expand Up @@ -215,25 +216,8 @@ public void testNoDispatchEventForDisabledLevel() throws Exception {
assertNull(mockAppender.awaitAppend(DELAY));
}

private void waitForActiveCountToEqual(ThreadPoolExecutor executorService, int i) {
while (executorService.getActiveCount() != i) {
try {
Thread.sleep(10);
System.out.print(".");
} catch (InterruptedException e) {
}
}
}

private void waitForStatusMessage(StatusChecker statusChecker, String regex) {
while (!statusChecker.containsMatch(regex)) {
try {
Thread.sleep(10);
System.out.print(".");
} catch (InterruptedException e) {
}
}
}


/**
* A {@link SocketReceiver} with instrumentation for unit testing.
Expand Down
Expand Up @@ -31,6 +31,7 @@
import ch.qos.logback.core.status.StatusChecker;
import ch.qos.logback.core.status.StatusListener;
import ch.qos.logback.core.status.StatusManager;
import ch.qos.logback.core.testUtil.Wait;
import ch.qos.logback.core.util.Duration;
import ch.qos.logback.core.util.StatusPrinter;
import org.junit.After;
Expand Down Expand Up @@ -80,14 +81,16 @@ public void appenderShouldFailToStartWithoutValidPort() throws Exception {
statusChecker.assertContainsMatch(Status.ERROR, "No port was configured");
}

@Test
public void appenderShouldFailToStartWithoutValidRemoteHost() throws Exception {
@Test(timeout = DELAY)
public void appenderShouldNotStartWithMissingRemoteHostProperty() throws Exception {
instrumentedAppender.setPort(1);
instrumentedAppender.setRemoteHost(null);
instrumentedAppender.setQueueSize(0);
instrumentedAppender.start();
assertFalse(instrumentedAppender.isStarted());
statusChecker.assertContainsMatch(Status.ERROR, "No remote host");
String errMessage = "No remote host was configured";
Wait.forStatusMessage(statusChecker, errMessage);
statusChecker.assertContainsMatch(Status.ERROR, errMessage);
}

@Test
Expand All @@ -100,7 +103,7 @@ public void appenderShouldFailToStartWithNegativeQueueSize() throws Exception {
statusChecker.assertContainsMatch(Status.ERROR, "Queue");
}

@Test(timeout=10000)
@Test(timeout = 10000)
public void appenderShouldFailToStartWithUnresolvableRemoteHost() throws Exception {
instrumentedAppender.setPort(1);
instrumentedAppender.setRemoteHost("NOT.A.VALID.REMOTE.HOST.NAME");
Expand All @@ -109,7 +112,7 @@ public void appenderShouldFailToStartWithUnresolvableRemoteHost() throws Excepti
instrumentedAppender.append("invalid host");

instrumentedAppender.stop();
waitForActiveCountToEqual(executorService, 0);
Wait.forActiveCountToEqual(executorService, 0);

StatusPrinter.print(mockContext);
assertFalse(instrumentedAppender.isStarted());
Expand Down Expand Up @@ -147,25 +150,14 @@ public void appenderShouldCleanupTasksWhenStopped() throws Exception {
instrumentedAppender.start();
assertTrue(instrumentedAppender.isStarted());

waitForActiveCountToEqual(executorService, 2);
Wait.forActiveCountToEqual(executorService, 2);
instrumentedAppender.stop();
waitForActiveCountToEqual(executorService, 0);
Wait.forActiveCountToEqual(executorService, 0);
StatusPrinter.print(mockContext);
assertEquals(0, executorService.getActiveCount());

}

private void waitForActiveCountToEqual(ThreadPoolExecutor executorService, int i) {
while (executorService.getActiveCount() != i) {
try {
Thread.yield();
Thread.sleep(1);
System.out.print(".");
} catch (InterruptedException e) {
}
}
}


@Test
public void testAppendWhenNotStarted() throws Exception {
Expand Down Expand Up @@ -219,7 +211,6 @@ public void testDispatchEvent() throws Exception {
serverSocket.close();



final int shortDelay = 100;
for (int i = 0, retries = DELAY / shortDelay;
!instrumentedAppender.lastQueue.isEmpty() && i < retries;
Expand Down
28 changes: 28 additions & 0 deletions logback-core/src/test/java/ch/qos/logback/core/testUtil/Wait.java
@@ -0,0 +1,28 @@
package ch.qos.logback.core.testUtil;

import ch.qos.logback.core.status.StatusChecker;

import java.util.concurrent.ThreadPoolExecutor;

public class Wait {

static public void forActiveCountToEqual(ThreadPoolExecutor executorService, int i) {
while (executorService.getActiveCount() != i) {
try {
Thread.sleep(10);
System.out.print(".");
} catch (InterruptedException e) {
}
}
}
static public void forStatusMessage(StatusChecker statusChecker, String regex) {
while (!statusChecker.containsMatch(regex)) {
try {
Thread.sleep(10);
System.out.print(".");
} catch (InterruptedException e) {
}
}
}

}

0 comments on commit c91fd5d

Please sign in to comment.