Issue #207 - websocket-client test cleanup

This commit is contained in:
Joakim Erdfelt 2017-04-28 13:17:04 -07:00
parent 2bab817c6b
commit 5ab7bf37b2
4 changed files with 47 additions and 308 deletions

View File

@ -1,184 +0,0 @@
//
// ========================================================================
// Copyright (c) 1995-2017 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package org.eclipse.jetty.websocket.client;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertThat;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Exchanger;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.eclipse.jetty.toolchain.test.EventQueue;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.api.UpgradeRequest;
import org.eclipse.jetty.websocket.api.UpgradeResponse;
import org.eclipse.jetty.websocket.api.WebSocketAdapter;
import org.hamcrest.Matcher;
import org.junit.Assert;
/**
* Testing Socket used on client side WebSocket testing.
*/
public class JettyTrackingSocket extends WebSocketAdapter
{
private static final Logger LOG = Log.getLogger(JettyTrackingSocket.class);
public int closeCode = -1;
public Exchanger<String> messageExchanger;
public UpgradeRequest connectUpgradeRequest;
public UpgradeResponse connectUpgradeResponse;
public StringBuilder closeMessage = new StringBuilder();
public CountDownLatch openLatch = new CountDownLatch(1);
public CountDownLatch closeLatch = new CountDownLatch(1);
public CountDownLatch dataLatch = new CountDownLatch(1);
public EventQueue<String> messageQueue = new EventQueue<>();
public EventQueue<Throwable> errorQueue = new EventQueue<>();
public void assertClose(int expectedStatusCode, Matcher<String> reasonMatcher) throws InterruptedException
{
assertThat("Close Code / Received [" + closeCode + "]", closeCode, is(expectedStatusCode));
assertThat("Close Reason", closeMessage.toString(), reasonMatcher);
}
public void assertCloseCode(int expectedCode) throws InterruptedException
{
Assert.assertThat("Close Code / Received [" + closeMessage + "]",closeCode,is(expectedCode));
}
public void assertIsOpen() throws InterruptedException
{
assertWasOpened();
assertNotClosed();
}
public void assertMessage(String expected)
{
String actual = messageQueue.poll();
Assert.assertEquals("Message",expected,actual);
}
public void assertNotClosed()
{
LOG.debug("assertNotClosed() - {}", closeLatch.getCount());
Assert.assertThat("Closed Latch",closeLatch.getCount(),greaterThanOrEqualTo(1L));
}
public void assertNotOpened()
{
LOG.debug("assertNotOpened() - {}", openLatch.getCount());
Assert.assertThat("Open Latch",openLatch.getCount(),greaterThanOrEqualTo(1L));
}
public void assertWasOpened() throws InterruptedException
{
LOG.debug("assertWasOpened() - {}", openLatch.getCount());
Assert.assertThat("Was Opened",openLatch.await(30,TimeUnit.SECONDS),is(true));
}
public void awaitMessage(int expectedMessageCount, TimeUnit timeoutUnit, int timeoutDuration) throws TimeoutException, InterruptedException
{
messageQueue.awaitEventCount(expectedMessageCount,timeoutDuration,timeoutUnit);
}
public void clear()
{
messageQueue.clear();
}
@Override
public void onWebSocketBinary(byte[] payload, int offset, int len)
{
LOG.debug("onWebSocketBinary()");
dataLatch.countDown();
}
@Override
public void onWebSocketClose(int statusCode, String reason)
{
LOG.warn("onWebSocketClose({},{})",statusCode,reason);
super.onWebSocketClose(statusCode,reason);
closeCode = statusCode;
closeMessage.append(reason);
closeLatch.countDown();
}
@Override
public void onWebSocketConnect(Session session)
{
super.onWebSocketConnect(session);
assertThat("Session", session, notNullValue());
connectUpgradeRequest = session.getUpgradeRequest();
connectUpgradeResponse = session.getUpgradeResponse();
openLatch.countDown();
}
@Override
public void onWebSocketError(Throwable cause)
{
LOG.debug("onWebSocketError",cause);
Assert.assertThat("Error capture",errorQueue.offer(cause),is(true));
}
@Override
public void onWebSocketText(String message)
{
LOG.debug("onWebSocketText({})",message);
messageQueue.offer(message);
dataLatch.countDown();
if (messageExchanger != null)
{
try
{
messageExchanger.exchange(message);
}
catch (InterruptedException e)
{
LOG.debug(e);
}
}
}
public void waitForClose(int timeoutDuration, TimeUnit timeoutUnit) throws InterruptedException
{
Assert.assertThat("Client Socket Closed",closeLatch.await(timeoutDuration,timeoutUnit),is(true));
}
public void waitForConnected(int timeoutDuration, TimeUnit timeoutUnit) throws InterruptedException
{
Assert.assertThat("Client Socket Connected",openLatch.await(timeoutDuration,timeoutUnit),is(true));
}
public void waitForMessage(int timeoutDuration, TimeUnit timeoutUnit) throws InterruptedException
{
LOG.debug("Waiting for message");
Assert.assertThat("Message Received",dataLatch.await(timeoutDuration,timeoutUnit),is(true));
}
public void close()
{
getSession().close();
}
}

View File

@ -1,102 +0,0 @@
//
// ========================================================================
// Copyright (c) 1995-2017 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package org.eclipse.jetty.websocket.client;
import static org.hamcrest.Matchers.is;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import org.eclipse.jetty.toolchain.test.EventQueue;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.websocket.api.Session;
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.junit.Assert;
@WebSocket(maxTextMessageSize = 100*1024)
public class MaxMessageSocket
{
private static final Logger LOG = Log.getLogger(MaxMessageSocket.class);
private Session session;
public CountDownLatch openLatch = new CountDownLatch(1);
public CountDownLatch closeLatch = new CountDownLatch(1);
public CountDownLatch dataLatch = new CountDownLatch(1);
public EventQueue<String> messageQueue = new EventQueue<>();
public EventQueue<Throwable> errorQueue = new EventQueue<>();
public int closeCode = -1;
public StringBuilder closeMessage = new StringBuilder();
@OnWebSocketConnect
public void onConnect(Session session)
{
this.session = session;
openLatch.countDown();
}
@OnWebSocketClose
public void onClose(int statusCode, String reason)
{
LOG.debug("onWebSocketClose({},{})",statusCode,reason);
closeCode = statusCode;
closeMessage.append(reason);
closeLatch.countDown();
}
@OnWebSocketMessage
public void onMessage(String message)
{
LOG.debug("onWebSocketText({})",message);
messageQueue.offer(message);
dataLatch.countDown();
}
@OnWebSocketError
public void onError(Throwable cause)
{
LOG.debug("onWebSocketError",cause);
Assert.assertThat("Error capture",errorQueue.offer(cause),is(true));
}
public Session getSession()
{
return this.session;
}
public void awaitConnect(int duration, TimeUnit unit) throws InterruptedException
{
Assert.assertThat("Client Socket connected",openLatch.await(duration,unit),is(true));
}
public void waitForMessage(int timeoutDuration, TimeUnit timeoutUnit) throws InterruptedException
{
LOG.debug("Waiting for message");
Assert.assertThat("Message Received",dataLatch.await(timeoutDuration,timeoutUnit),is(true));
}
public void assertMessage(String expected)
{
String actual = messageQueue.poll();
Assert.assertEquals("Message",expected,actual);
}
}

View File

@ -16,14 +16,21 @@
// ========================================================================
//
package org.eclipse.jetty.websocket.client;
package org.eclipse.jetty.websocket.tests.client;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import java.net.URI;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import org.eclipse.jetty.toolchain.test.TestTracker;
import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketConnect;
import org.eclipse.jetty.websocket.api.annotations.WebSocket;
import org.eclipse.jetty.websocket.client.WebSocketClient;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
@ -36,67 +43,84 @@ import org.junit.runners.Parameterized.Parameters;
@RunWith(Parameterized.class)
public class WebSocketClientBadUriTest
{
@WebSocket
public static class OpenTrackingSocket
{
public CountDownLatch openLatch = new CountDownLatch(1);
@OnWebSocketConnect
public void onOpen(Session session)
{
openLatch.countDown();
}
public void assertNotOpened()
{
Assert.assertThat("Open Latch", openLatch.getCount(), greaterThanOrEqualTo(1L));
}
}
@Parameters
public static Collection<String[]> data()
{
List<String[]> data = new ArrayList<>();
// @formatter:off
// - not using right scheme
data.add(new String[] { "http://localhost" });
data.add(new String[] { "https://localhost" });
data.add(new String[] { "file://localhost" });
data.add(new String[] { "content://localhost" });
data.add(new String[] { "jar://localhost" });
data.add(new String[]{"http://localhost"});
data.add(new String[]{"https://localhost"});
data.add(new String[]{"file://localhost"});
data.add(new String[]{"content://localhost"});
data.add(new String[]{"jar://localhost"});
// - non-absolute uri
data.add(new String[] { "/mysocket" });
data.add(new String[] { "/sockets/echo" });
data.add(new String[] { "#echo" });
data.add(new String[] { "localhost:8080/echo" });
data.add(new String[]{"/mysocket"});
data.add(new String[]{"/sockets/echo"});
data.add(new String[]{"#echo"});
data.add(new String[]{"localhost:8080/echo"});
// @formatter:on
return data;
}
@Rule
public TestTracker tt = new TestTracker();
private WebSocketClient client;
private final String uriStr;
private final URI uri;
public WebSocketClientBadUriTest(String rawUri)
{
this.uriStr = rawUri;
this.uri = URI.create(uriStr);
}
@Before
public void startClient() throws Exception
{
client = new WebSocketClient();
client.start();
}
@After
public void stopClient() throws Exception
{
client.stop();
}
@Test
public void testBadURI() throws Exception
{
JettyTrackingSocket wsocket = new JettyTrackingSocket();
OpenTrackingSocket clientSocket = new OpenTrackingSocket();
try
{
client.connect(wsocket,uri); // should toss exception
client.connect(clientSocket, uri); // should toss exception
Assert.fail("Expected IllegalArgumentException");
}
catch (IllegalArgumentException e)
{
// expected path
wsocket.assertNotOpened();
clientSocket.assertNotOpened();
}
}
}

View File

@ -16,13 +16,14 @@
// ========================================================================
//
package org.eclipse.jetty.websocket.client;
package org.eclipse.jetty.websocket.tests.client;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertThat;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.websocket.client.WebSocketClient;
import org.junit.Test;
/**