Made tests more reliable by waiting for the websocket connection to be established

and for onOpen() to be called.
This commit is contained in:
Simone Bordet 2012-01-03 12:15:51 +01:00
parent e7b787996f
commit 06f0498a48
4 changed files with 29 additions and 29 deletions

View File

@ -15,8 +15,6 @@
*******************************************************************************/
package org.eclipse.jetty.websocket;
import static org.hamcrest.Matchers.*;
import java.net.URI;
import java.util.concurrent.TimeUnit;
@ -24,7 +22,6 @@ import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.util.log.StdErrLog;
import org.eclipse.jetty.websocket.helper.CaptureSocket;
import org.eclipse.jetty.websocket.helper.SafariD00;
import org.eclipse.jetty.websocket.helper.WebSocketCaptureServlet;
@ -32,9 +29,11 @@ import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
public class SafariWebsocketDraft0Test
{
private Server server;
@ -76,7 +75,7 @@ public class SafariWebsocketDraft0Test
serverUri = new URI(String.format("ws://%s:%d/",host,port));
// System.out.printf("Server URI: %s%n",serverUri);
}
@Test
public void testSendTextMessages() throws Exception
{
@ -96,10 +95,11 @@ public class SafariWebsocketDraft0Test
CaptureSocket socket = servlet.captures.get(0);
Assert.assertThat("CaptureSocket",socket,notNullValue());
Assert.assertThat("CaptureSocket.isConnected", socket.isConnected(), is(true));
Assert.assertThat("CaptureSocket.isConnected", socket.awaitConnected(1000), is(true));
// Give servlet 500 millisecond to process messages
// Give servlet time to process messages
threadSleep(1,TimeUnit.SECONDS);
// Should have captured 5 messages.
Assert.assertThat("CaptureSocket.messages.size",socket.messages.size(),is(5));
}
@ -109,13 +109,13 @@ public class SafariWebsocketDraft0Test
safari.disconnect();
}
}
public static void threadSleep(int dur, TimeUnit unit) throws InterruptedException
{
long ms = TimeUnit.MILLISECONDS.convert(dur,unit);
Thread.sleep(ms);
}
@After
public void stopServer() throws Exception
{

View File

@ -15,8 +15,6 @@
*******************************************************************************/
package org.eclipse.jetty.websocket;
import static org.hamcrest.Matchers.*;
import java.net.URI;
import java.util.concurrent.TimeUnit;
@ -32,6 +30,9 @@ import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
/**
* WebSocketCommTest - to test reported undelivered messages in bug <a
* href="https://jira.codehaus.org/browse/JETTY-1463">JETTY-1463</a>
@ -109,11 +110,11 @@ public class WebSocketCommTest
CaptureSocket socket = servlet.captures.get(0);
Assert.assertThat("CaptureSocket",socket,notNullValue());
Assert.assertThat("CaptureSocket.isConnected",socket.isConnected(),is(true));
Assert.assertThat("CaptureSocket.isConnected",socket.awaitConnected(1000),is(true));
// Give servlet 500 millisecond to process messages
// Give servlet time to process messages
TimeUnit.MILLISECONDS.sleep(500);
// Should have captured 5 messages.
Assert.assertThat("CaptureSocket.messages.size",socket.messages.size(),is(5));
}

View File

@ -17,12 +17,14 @@ package org.eclipse.jetty.websocket.helper;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import org.eclipse.jetty.websocket.WebSocket;
public class CaptureSocket implements WebSocket, WebSocket.OnTextMessage
public class CaptureSocket implements WebSocket.OnTextMessage
{
private Connection conn;
private final CountDownLatch latch = new CountDownLatch(1);
public List<String> messages;
public CaptureSocket()
@ -30,13 +32,9 @@ public class CaptureSocket implements WebSocket, WebSocket.OnTextMessage
messages = new ArrayList<String>();
}
public boolean isConnected()
public boolean awaitConnected(long timeout) throws InterruptedException
{
if (conn == null)
{
return false;
}
return conn.isOpen();
return latch.await(timeout, TimeUnit.MILLISECONDS);
}
public void onMessage(String data)
@ -47,11 +45,10 @@ public class CaptureSocket implements WebSocket, WebSocket.OnTextMessage
public void onOpen(Connection connection)
{
this.conn = connection;
latch.countDown();
}
public void onClose(int closeCode, String message)
{
this.conn = null;
}
}
}

View File

@ -15,8 +15,6 @@
*******************************************************************************/
package org.eclipse.jetty.websocket.helper;
import static org.hamcrest.Matchers.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
@ -32,6 +30,8 @@ import org.eclipse.jetty.io.ByteArrayBuffer;
import org.eclipse.jetty.util.TypeUtil;
import org.junit.Assert;
import static org.hamcrest.Matchers.is;
public class SafariD00
{
private URI uri;
@ -48,7 +48,7 @@ public class SafariD00
/**
* Open the Socket to the destination endpoint and
*
*
* @return the open java Socket.
* @throws IOException
*/
@ -65,7 +65,7 @@ public class SafariD00
/**
* Issue an Http websocket (Draft-0) upgrade request using the Safari particulars.
*
*
* @throws UnsupportedEncodingException
*/
public void issueHandshake() throws IOException
@ -96,6 +96,8 @@ public class SafariD00
InputStreamReader reader = new InputStreamReader(in);
BufferedReader br = new BufferedReader(reader);
socket.setSoTimeout(5000);
boolean foundEnd = false;
String line;
while (!foundEnd)