Issue #207 - more test failure fixes

This commit is contained in:
Joakim Erdfelt 2017-04-27 16:26:32 -07:00
parent d216206fcd
commit 56fac5fdb1
11 changed files with 158 additions and 176 deletions

View File

@ -20,10 +20,10 @@ package org.eclipse.jetty.websocket.jsr356.server;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import java.net.URI;
import java.util.List;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
@ -33,7 +33,6 @@ import org.eclipse.jetty.webapp.WebAppContext;
import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.client.WebSocketClient;
import org.eclipse.jetty.websocket.jsr356.server.samples.echo.BasicEchoSocket;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
@ -72,14 +71,16 @@ public class AltFilterTest
try
{
client.start();
JettyEchoSocket clientEcho = new JettyEchoSocket();
Future<List<String>> clientMessagesFuture = clientEcho.expectedMessages(1);
Future<Session> future = client.connect(clientEcho,uri.resolve("echo"));
JettyEchoSocket clientSocket = new JettyEchoSocket();
Future<Session> clientConnectFuture = client.connect(clientSocket,uri.resolve("echo"));
// wait for connect
future.get(1,TimeUnit.SECONDS);
clientEcho.sendMessage("Hello Echo");
List<String> msgs = clientMessagesFuture.get(1, TimeUnit.SECONDS);
Assert.assertEquals("Expected message","Hello Echo",msgs.get(0));
Session clientSession = clientConnectFuture.get(5,TimeUnit.SECONDS);
clientSocket.sendMessage("Hello Echo");
String incomingMessage = clientSocket.messageQueue.poll(1, TimeUnit.SECONDS);
assertEquals("Expected message","Hello Echo",incomingMessage);
clientSession.close();
clientSocket.awaitCloseEvent("Client");
}
finally
{

View File

@ -22,7 +22,6 @@ import static org.hamcrest.Matchers.containsString;
import java.net.URI;
import java.nio.file.Path;
import java.util.List;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
@ -75,29 +74,28 @@ public class AnnotatedServerEndpointTest
server.stop();
}
private void assertResponse(String message, String... expectedTexts) throws Exception
private void assertResponse(String message, String expectedText) throws Exception
{
WebSocketClient client = new WebSocketClient(bufferPool);
try
{
client.start();
JettyEchoSocket clientEcho = new JettyEchoSocket();
Future<List<String>> clientMessagesFuture = clientEcho.expectedMessages(1);
JettyEchoSocket clientSocket = new JettyEchoSocket();
URI uri = server.getServerBaseURI().resolve("echo");
ClientUpgradeRequest req = new ClientUpgradeRequest();
req.setSubProtocols("echo");
Future<Session> foo = client.connect(clientEcho,uri,req);
Future<Session> clientConnectFuture = client.connect(clientSocket,uri,req);
// wait for connect
foo.get(1,TimeUnit.SECONDS);
Session clientSession = clientConnectFuture.get(5,TimeUnit.SECONDS);
clientEcho.sendMessage(message);
List<String> msgs = clientMessagesFuture.get(1, TimeUnit.SECONDS);
clientSocket.sendMessage(message);
String incomingMessage = clientSocket.messageQueue.poll(1, TimeUnit.SECONDS);
Assert.assertThat("Expected message",incomingMessage,containsString(expectedText));
String response = msgs.get(0);
for (String expected : expectedTexts)
{
Assert.assertThat("Expected message",response,containsString(expected));
}
clientSession.close();
clientSocket.awaitCloseEvent("Client");
}
finally
{

View File

@ -19,7 +19,6 @@
package org.eclipse.jetty.websocket.jsr356.server;
import java.net.URI;
import java.util.List;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
@ -68,14 +67,16 @@ public class BasicEndpointTest
try
{
client.start();
JettyEchoSocket clientEcho = new JettyEchoSocket();
Future<List<String>> clientMessagesFuture = clientEcho.expectedMessages(1);
Future<Session> future = client.connect(clientEcho,uri.resolve("echo"));
JettyEchoSocket clientSocket = new JettyEchoSocket();
Future<Session> clientConnectFuture = client.connect(clientSocket,uri.resolve("echo"));
// wait for connect
future.get(1,TimeUnit.SECONDS);
clientEcho.sendMessage("Hello World");
List<String> msgs = clientMessagesFuture.get(1, TimeUnit.SECONDS);
Assert.assertEquals("Expected message","Hello World",msgs.get(0));
Session clientSession = clientConnectFuture.get(5,TimeUnit.SECONDS);
clientSocket.sendMessage("Hello World");
String incomingMessage = clientSocket.messageQueue.poll(1, TimeUnit.SECONDS);
Assert.assertEquals("Expected message","Hello World",incomingMessage);
clientSession.close();
}
finally
{

View File

@ -19,24 +19,18 @@
package org.eclipse.jetty.websocket.jsr356.server;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertThat;
import java.io.IOException;
import java.net.URI;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
import org.eclipse.jetty.toolchain.test.TestingDir;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.webapp.WebAppContext;
import org.eclipse.jetty.websocket.api.CloseException;
import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.api.StatusCode;
import org.eclipse.jetty.websocket.client.WebSocketClient;
@ -86,50 +80,30 @@ public class IdleTimeoutTest
server.stop();
}
private void assertConnectionTimeout(URI uri) throws Exception, IOException, InterruptedException, ExecutionException, TimeoutException
private void assertConnectionTimeout(URI uri) throws Exception
{
WebSocketClient client = new WebSocketClient(bufferPool);
try
{
client.start();
JettyEchoSocket clientEcho = new JettyEchoSocket();
Future<List<String>> clientMessagesFuture = clientEcho.expectedMessages(1);
JettyEchoSocket clientSocket = new JettyEchoSocket();
if (LOG.isDebugEnabled())
LOG.debug("Client Attempting to connnect");
Future<Session> future = client.connect(clientEcho,uri);
Future<Session> clientConnectFuture = client.connect(clientSocket,uri);
// wait for connect
future.get(1,TimeUnit.SECONDS);
if (LOG.isDebugEnabled())
LOG.debug("Client Connected");
clientConnectFuture.get(1,TimeUnit.SECONDS);
// wait 1 second
if (LOG.isDebugEnabled())
LOG.debug("Waiting 1 second");
TimeUnit.SECONDS.sleep(1);
if (LOG.isDebugEnabled())
LOG.debug("Waited 1 second");
// Try to write
clientEcho.sendMessage("You shouldn't be there");
try
{
List<String> msgs = clientMessagesFuture.get(1, TimeUnit.SECONDS);
assertThat("Should not have received messages echoed back",msgs,is(empty()));
}
catch (ExecutionException e)
{
Throwable cause = e.getCause();
if(cause instanceof CloseException)
{
CloseException ce = (CloseException) cause;
assertThat("CloseException.statusCode", ce.getStatusCode(), is(StatusCode.SHUTDOWN));
assertThat("CloseException.reason", ce.getMessage(), containsString("Idle Timeout"));
}
else
{
throw e;
}
}
clientSocket.sendMessage("You shouldn't be there");
// See if remote sent anything (it shouldn't have)
String incomingMessage = clientSocket.messageQueue.poll(1, TimeUnit.SECONDS);
assertThat("Should not have received messages echoed back",incomingMessage,nullValue());
// wait for local close
clientSocket.awaitCloseEvent("Client");
clientSocket.assertCloseInfo("Client", StatusCode.SHUTDOWN, containsString("Idle Timeout"));
}
finally
{

View File

@ -18,25 +18,29 @@
package org.eclipse.jetty.websocket.jsr356.server;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.websocket.api.BatchMode;
import org.eclipse.jetty.websocket.api.CloseException;
import org.eclipse.jetty.websocket.api.RemoteEndpoint;
import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.api.StatusCode;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketClose;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketConnect;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketError;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage;
import org.eclipse.jetty.websocket.api.annotations.WebSocket;
import org.eclipse.jetty.websocket.common.CloseInfo;
import org.hamcrest.Matcher;
import org.hamcrest.Matchers;
/**
* This is a Jetty API version of a websocket.
@ -48,53 +52,30 @@ public class JettyEchoSocket
{
private static final Logger LOG = Log.getLogger(JettyEchoSocket.class);
private RemoteEndpoint remote;
private CompletableFuture<List<String>> expectedMessagesFuture;
private AtomicInteger expectedMessageCount;
private List<String> messages = new ArrayList<>();
public Future<List<String>> expectedMessages(int expected)
{
expectedMessagesFuture = new CompletableFuture<>();
expectedMessageCount = new AtomicInteger(expected);
return expectedMessagesFuture;
}
public CountDownLatch closeLatch = new CountDownLatch(1);
public BlockingQueue<String> messageQueue = new LinkedBlockingDeque<>();
public AtomicReference<CloseInfo> closeInfo = new AtomicReference<>();
@OnWebSocketClose
public void onClose(int code, String reason)
public void onClose(int statusCode, String reason)
{
remote = null;
synchronized (expectedMessagesFuture)
{
if ((code != StatusCode.NORMAL) ||
(code != StatusCode.NO_CODE))
{
expectedMessagesFuture.completeExceptionally(new CloseException(code, reason));
}
}
CloseInfo close = new CloseInfo(statusCode, reason);
boolean closeTracked = closeInfo.compareAndSet(null, close);
this.closeLatch.countDown();
assertTrue("Close only happened once", closeTracked);
}
@OnWebSocketError
public void onError(Throwable t)
{
LOG.warn(t);
synchronized (expectedMessagesFuture)
{
expectedMessagesFuture.completeExceptionally(t);
}
}
@OnWebSocketMessage
public void onMessage(String msg) throws IOException
{
messages.add(msg);
synchronized (expectedMessagesFuture)
{
int countLeft = expectedMessageCount.decrementAndGet();
if (countLeft <= 0)
{
expectedMessagesFuture.complete(messages);
}
}
messageQueue.offer(msg);
sendMessage(msg);
}
@ -104,6 +85,19 @@ public class JettyEchoSocket
this.remote = session.getRemote();
}
public void awaitCloseEvent(String prefix) throws InterruptedException
{
assertTrue(prefix + " onClose event", closeLatch.await(5, TimeUnit.SECONDS));
}
public void assertCloseInfo(String prefix, int expectedCloseStatusCode, Matcher<? super String> reasonMatcher) throws InterruptedException
{
CloseInfo close = closeInfo.get();
assertThat(prefix + " close info", close, Matchers.notNullValue());
assertThat(prefix + " received close code", close.getStatusCode(), Matchers.is(expectedCloseStatusCode));
assertThat(prefix + " received close reason", close.getReason(), reasonMatcher);
}
public void sendMessage(String msg) throws IOException
{
RemoteEndpoint r = remote;

View File

@ -18,10 +18,12 @@
package org.eclipse.jetty.websocket.jsr356.server;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
@ -31,7 +33,6 @@ import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.client.WebSocketClient;
import org.eclipse.jetty.websocket.common.test.LeakTrackingBufferPoolRule;
import org.eclipse.jetty.websocket.jsr356.server.samples.echo.LargeEchoConfiguredSocket;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
@ -68,18 +69,24 @@ public class LargeAnnotatedTest
{
client.getPolicy().setMaxTextMessageSize(128*1024);
client.start();
JettyEchoSocket clientEcho = new JettyEchoSocket();
Future<List<String>> clientMessagesFuture = clientEcho.expectedMessages(1);
Future<Session> foo = client.connect(clientEcho,uri.resolve("echo/large"));
JettyEchoSocket clientSocket = new JettyEchoSocket();
Future<Session> clientConnectFuture = client.connect(clientSocket,uri.resolve("echo/large"));
// wait for connect
foo.get(1,TimeUnit.SECONDS);
Session clientSession = clientConnectFuture.get(1,TimeUnit.SECONDS);
// The message size should be bigger than default, but smaller than the limit that LargeEchoSocket specifies
byte txt[] = new byte[100 * 1024];
Arrays.fill(txt,(byte)'o');
String msg = new String(txt,StandardCharsets.UTF_8);
clientEcho.sendMessage(msg);
List<String> msgs = clientMessagesFuture.get(1, TimeUnit.SECONDS);
Assert.assertEquals("Expected message",msg,msgs.get(0));
clientSocket.sendMessage(msg);
// Receive echo
String incomingMessage = clientSocket.messageQueue.poll(5, TimeUnit.SECONDS);
assertThat("Expected message",incomingMessage,is(msg));
clientSession.close();
}
finally
{

View File

@ -18,10 +18,12 @@
package org.eclipse.jetty.websocket.jsr356.server;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
@ -31,7 +33,6 @@ import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.client.WebSocketClient;
import org.eclipse.jetty.websocket.common.test.LeakTrackingBufferPoolRule;
import org.eclipse.jetty.websocket.jsr356.server.samples.echo.LargeEchoDefaultSocket;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
@ -68,18 +69,24 @@ public class LargeContainerTest
{
client.getPolicy().setMaxTextMessageSize(128*1024);
client.start();
JettyEchoSocket clientEcho = new JettyEchoSocket();
Future<List<String>> clientMessagesFuture = clientEcho.expectedMessages(1);
Future<Session> foo = client.connect(clientEcho,uri.resolve("echo/large"));
JettyEchoSocket clientSocket = new JettyEchoSocket();
Future<Session> clientConnectFuture = client.connect(clientSocket,uri.resolve("echo/large"));
// wait for connect
foo.get(1,TimeUnit.SECONDS);
Session clientSession = clientConnectFuture.get(5,TimeUnit.SECONDS);
// The message size should be bigger than default, but smaller than the limit that LargeEchoSocket specifies
byte txt[] = new byte[100 * 1024];
Arrays.fill(txt,(byte)'o');
String msg = new String(txt,StandardCharsets.UTF_8);
clientEcho.sendMessage(msg);
List<String> msgs = clientMessagesFuture.get(1, TimeUnit.SECONDS);
Assert.assertEquals("Expected message",msg,msgs.get(0));
clientSocket.sendMessage(msg);
// Confirm echo
String incomingMessage = clientSocket.messageQueue.poll(5, TimeUnit.SECONDS);
assertThat("Expected message",incomingMessage,is(msg));
clientSession.close();
}
finally
{

View File

@ -18,8 +18,10 @@
package org.eclipse.jetty.websocket.jsr356.server;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import java.net.URI;
import java.util.List;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
@ -29,7 +31,6 @@ import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.client.WebSocketClient;
import org.eclipse.jetty.websocket.common.test.LeakTrackingBufferPoolRule;
import org.eclipse.jetty.websocket.jsr356.server.samples.echo.EchoReturnEndpoint;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
@ -60,14 +61,21 @@ public class OnMessageReturnTest
try
{
client.start();
JettyEchoSocket clientEcho = new JettyEchoSocket();
Future<List<String>> clientMessagesFuture = clientEcho.expectedMessages(1);
Future<Session> future = client.connect(clientEcho,uri.resolve("echoreturn"));
JettyEchoSocket clientSocket = new JettyEchoSocket();
Future<Session> clientConnectFuture = client.connect(clientSocket,uri.resolve("echoreturn"));
// wait for connect
future.get(1,TimeUnit.SECONDS);
clientEcho.sendMessage("Hello World");
List<String> msgs = clientMessagesFuture.get(1, TimeUnit.SECONDS);
Assert.assertEquals("Expected message","Hello World",msgs.get(0));
Session clientSession = clientConnectFuture.get(5,TimeUnit.SECONDS);
// Send message
clientSocket.sendMessage("Hello World");
// Confirm response
String incomingMessage = clientSocket.messageQueue.poll(5, TimeUnit.SECONDS);
assertThat("Expected message",incomingMessage,is("Hello World"));
clientSession.close();
}
finally
{

View File

@ -46,9 +46,9 @@ import org.junit.runners.Parameterized.Parameters;
@RunWith(Parameterized.class)
public class SessionTest
{
private static interface Case
private interface Case
{
public void customize(WebAppContext context);
void customize(WebAppContext context);
}
@Parameters
@ -56,44 +56,28 @@ public class SessionTest
{
List<Case[]> cases = new ArrayList<>();
cases.add(new Case[]
{ new Case()
{context ->
{
@Override
public void customize(WebAppContext context)
{
// no customization
}
} });
// no customization
}});
cases.add(new Case[]
{ new Case()
{context ->
{
@Override
public void customize(WebAppContext context)
{
// Test with DefaultServlet only
context.addServlet(DefaultServlet.class,"/");
}
} });
// Test with DefaultServlet only
context.addServlet(DefaultServlet.class,"/");
}});
cases.add(new Case[]
{ new Case()
{context ->
{
@Override
public void customize(WebAppContext context)
{
// Test with Servlet mapped to "/*"
context.addServlet(DefaultServlet.class,"/*");
}
} });
// Test with Servlet mapped to "/*"
context.addServlet(DefaultServlet.class,"/*");
}});
cases.add(new Case[]
{ new Case()
{context ->
{
@Override
public void customize(WebAppContext context)
{
// Test with Servlet mapped to "/info/*"
context.addServlet(DefaultServlet.class,"/info/*");
}
} });
// Test with Servlet mapped to "/info/*"
context.addServlet(DefaultServlet.class,"/info/*");
}});
return cases;
}
@ -137,14 +121,18 @@ public class SessionTest
try
{
client.start();
JettyEchoSocket clientEcho = new JettyEchoSocket();
Future<List<String>> clientMessagesFuture = clientEcho.expectedMessages(1);
Future<Session> future = client.connect(clientEcho,serverUri.resolve(requestPath));
JettyEchoSocket clientSocket = new JettyEchoSocket();
Future<Session> clientConnectFuture = client.connect(clientSocket,serverUri.resolve(requestPath));
// wait for connect
future.get(1,TimeUnit.SECONDS);
clientEcho.sendMessage(requestMessage);
List<String> msgs = clientMessagesFuture.get(1, TimeUnit.SECONDS);
Assert.assertThat("Expected message",msgs.get(0),is(expectedResponse));
Session clientSession = clientConnectFuture.get(5,TimeUnit.SECONDS);
clientSocket.sendMessage(requestMessage);
String incomingMessage = clientSocket.messageQueue.poll(5, TimeUnit.SECONDS);
Assert.assertThat("Expected message",incomingMessage,is(expectedResponse));
clientSession.close();
clientSocket.awaitCloseEvent("Client");
}
finally
{

View File

@ -133,6 +133,7 @@ public class WebSocketClient extends ContainerLifeCycle implements WebSocketCont
public WebSocketClient(ByteBufferPool bufferPool)
{
this(new HttpClient());
addBean(this.httpClient);
this.httpClient.setByteBufferPool(bufferPool);
}
@ -145,6 +146,7 @@ public class WebSocketClient extends ContainerLifeCycle implements WebSocketCont
public WebSocketClient(SslContextFactory sslContextFactory)
{
this(new HttpClient(sslContextFactory));
addBean(this.httpClient);
}
/**
@ -158,6 +160,7 @@ public class WebSocketClient extends ContainerLifeCycle implements WebSocketCont
public WebSocketClient(SslContextFactory sslContextFactory, Executor executor)
{
this(new HttpClient(sslContextFactory));
addBean(this.httpClient);
this.httpClient.setExecutor(executor);
}

View File

@ -162,6 +162,7 @@ public class WSServer
{
contexts.addHandler(webapp);
contexts.manage(webapp);
webapp.setThrowUnavailableOnStartupException(true);
webapp.start();
if (LOG.isDebugEnabled())
{