Skip to content

Commit

Permalink
Fix SpotBugs warnings
Browse files Browse the repository at this point in the history
int -> AtomicInteger to address atomicity issues
Refactoring to address Serialization issues
  • Loading branch information
markt-asf committed Feb 28, 2019
1 parent 9e052de commit bc3c072
Showing 1 changed file with 27 additions and 22 deletions.
49 changes: 27 additions & 22 deletions test/org/apache/catalina/nonblocking/TestNonBlockingAPI.java
Expand Up @@ -32,6 +32,7 @@
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

import javax.net.SocketFactory;
import javax.servlet.AsyncContext;
Expand All @@ -57,11 +58,15 @@
import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.startup.TomcatBaseTest;
import org.apache.catalina.valves.TesterAccessLogValve;
import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
import org.apache.tomcat.util.buf.ByteChunk;
import org.apache.tomcat.util.net.ContainerThreadMarker;

public class TestNonBlockingAPI extends TomcatBaseTest {

private static final Log log = LogFactory.getLog(TestNonBlockingAPI.class);

private static final int CHUNK_SIZE = 1024 * 1024;
private static final int WRITE_SIZE = CHUNK_SIZE * 10;
private static final byte[] DATA = new byte[WRITE_SIZE];
Expand Down Expand Up @@ -125,8 +130,8 @@ private void doTestNonBlockingRead(boolean ignoreIsReady, boolean async) throws
if (async) {
Assert.assertEquals(2000000 * 8, servlet.listener.body.length());
TestAsyncReadListener listener = (TestAsyncReadListener) servlet.listener;
Assert.assertTrue(Math.abs(listener.containerThreadCount - listener.notReadyCount) <= 1);
Assert.assertEquals(listener.isReadyCount, listener.nonContainerThreadCount);
Assert.assertTrue(Math.abs(listener.containerThreadCount.get() - listener.notReadyCount.get()) <= 1);
Assert.assertEquals(listener.isReadyCount.get(), listener.nonContainerThreadCount.get());
} else {
Assert.assertEquals(5 * 8, servlet.listener.body.length());
}
Expand Down Expand Up @@ -470,11 +475,11 @@ public byte[] next() {
}

@WebServlet(asyncSupported = true)
public class NBReadServlet extends TesterServlet {
public static class NBReadServlet extends TesterServlet {
private static final long serialVersionUID = 1L;
private final boolean async;
private final boolean ignoreIsReady;
TestReadListener listener;
transient TestReadListener listener;

public NBReadServlet(boolean ignoreIsReady, boolean async) {
this.async = async;
Expand Down Expand Up @@ -524,10 +529,10 @@ public void onComplete(AsyncEvent event) throws IOException {
}

@WebServlet(asyncSupported = true)
public class NBWriteServlet extends TesterServlet {
public static class NBWriteServlet extends TesterServlet {
private static final long serialVersionUID = 1L;
public volatile TestWriteListener wlistener;
public volatile TestReadListener rlistener;
public transient volatile TestWriteListener wlistener;
public transient volatile TestReadListener rlistener;

@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Expand Down Expand Up @@ -570,9 +575,9 @@ public void onComplete(AsyncEvent event) throws IOException {
}

@WebServlet(asyncSupported = true)
public class NBReadWriteServlet extends TesterServlet {
public static class NBReadWriteServlet extends TesterServlet {
private static final long serialVersionUID = 1L;
public volatile TestReadWriteListener rwlistener;
public transient volatile TestReadWriteListener rwlistener;

@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Expand All @@ -587,7 +592,7 @@ protected void service(HttpServletRequest req, HttpServletResponse resp) throws
}
}

private class TestReadListener implements ReadListener {
private static class TestReadListener implements ReadListener {
protected final AsyncContext ctx;
protected final boolean usingNonBlockingWrite;
protected final boolean ignoreIsReady;
Expand Down Expand Up @@ -649,12 +654,12 @@ public void onError(Throwable throwable) {
}
}

private class TestAsyncReadListener extends TestReadListener {
private static class TestAsyncReadListener extends TestReadListener {

volatile int isReadyCount = 0;
volatile int notReadyCount = 0;
volatile int containerThreadCount = 0;
volatile int nonContainerThreadCount = 0;
AtomicInteger isReadyCount = new AtomicInteger(0);
AtomicInteger notReadyCount = new AtomicInteger(0);
AtomicInteger containerThreadCount = new AtomicInteger(0);
AtomicInteger nonContainerThreadCount = new AtomicInteger(0);

public TestAsyncReadListener(AsyncContext ctx,
boolean usingNonBlockingWrite, boolean ignoreIsReady) {
Expand All @@ -664,9 +669,9 @@ public TestAsyncReadListener(AsyncContext ctx,
@Override
public void onDataAvailable() throws IOException {
if (ContainerThreadMarker.isContainerThread()) {
containerThreadCount++;
containerThreadCount.incrementAndGet();
} else {
nonContainerThreadCount++;
nonContainerThreadCount.incrementAndGet();
}
new Thread() {
@Override
Expand All @@ -685,9 +690,9 @@ public void run() {
}
boolean isReady = ignoreIsReady || in.isReady();
if (isReady) {
isReadyCount++;
isReadyCount.incrementAndGet();
} else {
notReadyCount++;
notReadyCount.incrementAndGet();
}
if (isReady) {
onDataAvailable();
Expand Down Expand Up @@ -716,7 +721,7 @@ public void onError(Throwable throwable) {
}
}

private class TestWriteListener implements WriteListener {
private static class TestWriteListener implements WriteListener {
AsyncContext ctx;
int written = 0;
public volatile boolean onErrorInvoked = false;
Expand Down Expand Up @@ -760,7 +765,7 @@ public void onError(Throwable throwable) {

}

private class TestReadWriteListener implements ReadListener {
private static class TestReadWriteListener implements ReadListener {
AsyncContext ctx;
private final StringBuilder body = new StringBuilder();

Expand Down Expand Up @@ -1030,7 +1035,7 @@ public void testNonBlockingReadWithDispatch() throws Exception {


@WebServlet(asyncSupported = true)
private final class NBReadWithDispatchServlet extends TesterServlet {
private static final class NBReadWithDispatchServlet extends TesterServlet {

private static final long serialVersionUID = 1L;

Expand Down

0 comments on commit bc3c072

Please sign in to comment.