Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(#824) Test fixes in mina.netty: avoided blocking wait in NioSocketIT.testBindAsync… #825

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,6 @@ public void exceptionCaught(IoSession session, Throwable cause) throws Exception
public void disposeShouldStopAll_IO_Threads() throws Exception {
shouldEchoBytes();
disposeResources();
Thread.sleep(1000); // experience shows Timer.cancel() does not immediately stop the timer thread
assertNoWorkerThreads("after disposeResources");
}

Expand Down Expand Up @@ -302,22 +301,36 @@ public void sessionCreated(IoSession session) throws Exception {
assertEquals("filter.sessionCreated", actions.get(1));
}

private void assertNoWorkerThreads(String when) {
Thread[] threads = new Thread[Thread.activeCount()];
Thread.enumerate(threads);
private void assertNoWorkerThreads(String when) throws Exception {
int workersFound = 0;
int bossesFound = 0;
List<String> badThreads = new LinkedList<>();
for (Thread thread : threads) {
System.out.println(thread.getName());
if (thread.getName().matches(".*I/O worker.*")) {
badThreads.add(thread.getName());
workersFound++;
int tries = 10;
for (;;) {
Thread[] threads = new Thread[Thread.activeCount()];
Thread.enumerate(threads);
for (Thread thread : threads) {
if (thread == null) {
// a thread died after the call to Thread.activeCount
continue;
}
System.out.println(thread.getName());
if (thread.getName().matches(".*I/O worker.*")) {
badThreads.add(thread.getName());
workersFound++;
}
if (thread.getName().matches(".*boss")) {
badThreads.add(thread.getName());
bossesFound++;
}
}
if (thread.getName().matches(".*boss")) {
badThreads.add(thread.getName());
bossesFound++;
if ((workersFound == 0 && bossesFound == 0) || --tries == 0) {
break;
}
workersFound = 0;
bossesFound = 0;
badThreads.clear();
Thread.sleep(500); // experience shows Timer.cancel() does not immediately stop the timer thread
}
assertTrue(String.format("No worker or boss threads should be running %s, found %d workers, %d bosses: %s",
when, workersFound, bossesFound, badThreads), workersFound == 0 && bossesFound == 0);
Expand Down Expand Up @@ -401,6 +414,7 @@ public Object invoke(Invocation invocation) throws Throwable {
int eos = socket.getInputStream().read();
assertEquals(-1, eos);

socket.close();
acceptor.unbind();
}

Expand Down Expand Up @@ -445,6 +459,7 @@ private void shouldRepeatedlyIdleTimeoutWhenIdle(final IdleStatus statusUnderTes
int eos = socket.getInputStream().read();
assertEquals(-1, eos);

socket.close();
acceptor.unbind();
}

Expand Down
26 changes: 16 additions & 10 deletions mina.netty/src/test/java/org/kaazing/mina/netty/NioSocketIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static java.lang.String.format;
import static java.nio.ByteBuffer.wrap;
import static java.util.concurrent.Executors.newCachedThreadPool;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.jboss.netty.channel.Channels.pipeline;
import static org.jboss.netty.channel.Channels.pipelineFactory;
import static org.junit.Assert.assertEquals;
Expand Down Expand Up @@ -369,19 +370,24 @@ public void initializeSession(IoSession session, ConnectFuture future) {

WriteFuture written = session.write(allocator.wrap(wrap(new byte[] { 0x00, 0x01, 0x02 })));

await(written, "session.write");
try {
await(written, "session.write");

await(echoedMessageReceived, "echoedMessageReceived");
await(echoedMessageReceived, "echoedMessageReceived");

assertEquals("Exceptions caught by connect handler", 0, connectExceptionsCaught.get());
assertEquals("Exceptions caught by accept handler", 0, acceptExceptionsCaught.get());
assertNull("acceptor.bind in acceptor IO thread threw exception " + bindException[0], bindException[0]);
await(boundInIoThread[0], "asynchronous bind from I/O thread");
assertTrue("Bind in IO thread failed with exception " + boundInIoThread[0].getException(), boundInIoThread[0]
.isBound());
}
finally {
session.close(true).await(10, SECONDS);
acceptor.unbind(bindTo);
}

assertEquals("Exceptions caught by connect handler", 0, connectExceptionsCaught.get());
assertEquals("Exceptions caught by accept handler", 0, acceptExceptionsCaught.get());
assertNull("acceptor.bind in acceptor IO thread threw exception " + bindException[0], bindException[0]);
boundInIoThread[0].await();
assertTrue("Bind in IO thread failed with exception " + boundInIoThread[0].getException(), boundInIoThread[0]
.isBound());

await(session.close(true), "session close(true) future");
acceptor.unbind(bindTo);
}

@Test
Expand Down