Fixed unit test race

Test was assuming that receiving completed response on client
always happened before server processing had completed.
Added a spin test.
This commit is contained in:
Greg Wilkins 2016-02-17 11:10:47 +01:00
parent 11d3448e28
commit 8068aeead9
2 changed files with 35 additions and 6 deletions

View File

@ -22,7 +22,9 @@ import static org.junit.Assert.assertEquals;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Supplier;
import javax.servlet.AsyncContext;
import javax.servlet.AsyncEvent;
@ -84,8 +86,8 @@ public class LocalAsyncContextTest
_handler.setCompleteAfter(-1);
response=process(null);
check(response,"TIMEOUT");
assertEquals(1,__completed.get());
assertEquals(1,__completed1.get());
spinAssertEquals(1,__completed::get);
spinAssertEquals(1,__completed1::get);
}
@Test
@ -218,8 +220,8 @@ public class LocalAsyncContextTest
_handler.setCompleteAfter2(-1);
response=process(null);
check(response,"STARTASYNC","DISPATCHED","startasync","STARTASYNC","DISPATCHED");
assertEquals(1,__completed.get());
assertEquals(0,__completed1.get());
spinAssertEquals(1,__completed::get);
spinAssertEquals(0,__completed1::get);
}
@ -596,4 +598,33 @@ public class LocalAsyncContextTest
}
};
static <T> void spinAssertEquals(T expected, Supplier<T> actualSupplier)
{
spinAssertEquals(expected,actualSupplier,10,TimeUnit.SECONDS);
}
static <T> void spinAssertEquals(T expected, Supplier<T> actualSupplier, long waitFor, TimeUnit units)
{
long now = System.nanoTime();
long end = now+units.toNanos(waitFor);
T actual=null;
while(now < end)
{
actual=actualSupplier.get();
if (actual==null && expected==null ||
actual!=null && actual.equals(expected))
break;
try
{
Thread.sleep(10);
}
catch (InterruptedException e)
{
}
now = System.nanoTime();
}
assertEquals(expected,actual);
}
}

View File

@ -49,10 +49,8 @@ import org.eclipse.jetty.server.handler.AbstractHandler;
import org.eclipse.jetty.util.Promise;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
@Ignore
public class HttpChannelAssociationTest extends AbstractTest
{
public HttpChannelAssociationTest(Transport transport)