403380 - Introduce WebSocketTimeoutException to differentiate between EOF on write and Timeout
This commit is contained in:
parent
04d86bd49e
commit
54fd961354
|
@ -0,0 +1,42 @@
|
|||
//
|
||||
// ========================================================================
|
||||
// Copyright (c) 1995-2013 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.api;
|
||||
|
||||
/**
|
||||
* Exception thrown to indicate a connection I/O timeout.
|
||||
*/
|
||||
public class WebSocketTimeoutException extends WebSocketException
|
||||
{
|
||||
private static final long serialVersionUID = -6145098200250676673L;
|
||||
|
||||
public WebSocketTimeoutException(String message)
|
||||
{
|
||||
super(message);
|
||||
}
|
||||
|
||||
public WebSocketTimeoutException(String message, Throwable cause)
|
||||
{
|
||||
super(message,cause);
|
||||
}
|
||||
|
||||
public WebSocketTimeoutException(Throwable cause)
|
||||
{
|
||||
super(cause);
|
||||
}
|
||||
}
|
|
@ -25,6 +25,7 @@ import org.eclipse.jetty.websocket.api.WebSocketException;
|
|||
*/
|
||||
public interface IncomingFrames
|
||||
{
|
||||
// TODO: JSR-356 change to Throwable
|
||||
public void incomingError(WebSocketException e);
|
||||
|
||||
public void incomingFrame(Frame frame);
|
||||
|
|
|
@ -46,6 +46,7 @@ import org.eclipse.jetty.websocket.api.StatusCode;
|
|||
import org.eclipse.jetty.websocket.api.SuspendToken;
|
||||
import org.eclipse.jetty.websocket.api.WebSocketException;
|
||||
import org.eclipse.jetty.websocket.api.WebSocketPolicy;
|
||||
import org.eclipse.jetty.websocket.api.WebSocketTimeoutException;
|
||||
import org.eclipse.jetty.websocket.api.WriteCallback;
|
||||
import org.eclipse.jetty.websocket.api.extensions.ExtensionConfig;
|
||||
import org.eclipse.jetty.websocket.api.extensions.Frame;
|
||||
|
@ -94,6 +95,7 @@ public abstract class AbstractWebSocketConnection extends AbstractConnection imp
|
|||
|
||||
// Abnormal Close
|
||||
reason = CloseStatus.trimMaxReasonLength(reason);
|
||||
session.incomingError(new WebSocketException(x)); // TODO: JSR-356 change to Throwable
|
||||
session.notifyClose(StatusCode.NO_CLOSE,reason);
|
||||
|
||||
disconnect(); // disconnect endpoint & connection
|
||||
|
@ -508,6 +510,7 @@ public abstract class AbstractWebSocketConnection extends AbstractConnection imp
|
|||
|
||||
// Initiate close - politely send close frame.
|
||||
// Note: it is not possible in 100% of cases during read timeout to send this close frame.
|
||||
session.incomingError(new WebSocketTimeoutException("Timeout on Read"));
|
||||
session.close(StatusCode.NORMAL,"Idle Timeout");
|
||||
|
||||
// Force closure of writeBytes
|
||||
|
|
|
@ -0,0 +1,99 @@
|
|||
//
|
||||
// ========================================================================
|
||||
// Copyright (c) 1995-2013 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.server;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.eclipse.jetty.websocket.common.WebSocketFrame;
|
||||
import org.eclipse.jetty.websocket.server.blockhead.BlockheadClient;
|
||||
import org.eclipse.jetty.websocket.server.helper.RFCSocket;
|
||||
import org.eclipse.jetty.websocket.servlet.WebSocketServlet;
|
||||
import org.eclipse.jetty.websocket.servlet.WebSocketServletFactory;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Assert;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
public class IdleTimeoutTest
|
||||
{
|
||||
@SuppressWarnings("serial")
|
||||
public static class TimeoutServlet extends WebSocketServlet
|
||||
{
|
||||
@Override
|
||||
public void configure(WebSocketServletFactory factory)
|
||||
{
|
||||
factory.getPolicy().setIdleTimeout(500);
|
||||
factory.register(RFCSocket.class);
|
||||
}
|
||||
}
|
||||
|
||||
private static SimpleServletServer server;
|
||||
|
||||
@BeforeClass
|
||||
public static void startServer() throws Exception
|
||||
{
|
||||
server = new SimpleServletServer(new TimeoutServlet());
|
||||
server.start();
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void stopServer()
|
||||
{
|
||||
server.stop();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test IdleTimeout on server.
|
||||
*/
|
||||
@Test
|
||||
public void testIdleTimeout() throws Exception
|
||||
{
|
||||
BlockheadClient client = new BlockheadClient(server.getServerUri());
|
||||
client.setProtocols("onConnect");
|
||||
client.setTimeout(TimeUnit.MILLISECONDS,1500);
|
||||
try
|
||||
{
|
||||
client.connect();
|
||||
client.sendStandardRequest();
|
||||
client.expectUpgradeResponse();
|
||||
|
||||
// This wait should be shorter than client timeout above, but
|
||||
// longer than server timeout configured in TimeoutServlet
|
||||
client.sleep(TimeUnit.MILLISECONDS,1000);
|
||||
|
||||
// Write to server (the server should be timed out and disconnect now)
|
||||
client.write(WebSocketFrame.text("Hello"));
|
||||
|
||||
// now attempt to read 2 echoed frames from server (shouldn't work)
|
||||
client.readFrames(2,TimeUnit.MILLISECONDS,1500);
|
||||
Assert.fail("Should have resulted in IOException");
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
Assert.assertThat("IOException",e.getMessage(),anyOf(containsString("closed"),containsString("disconnected")));
|
||||
}
|
||||
finally
|
||||
{
|
||||
client.close();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -190,35 +190,6 @@ public class WebSocketServletRFCTest
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore("Need a better idle timeout test")
|
||||
public void testIdle() throws Exception
|
||||
{
|
||||
BlockheadClient client = new BlockheadClient(server.getServerUri());
|
||||
client.setProtocols("onConnect");
|
||||
client.setTimeout(TimeUnit.MILLISECONDS,800);
|
||||
try
|
||||
{
|
||||
client.connect();
|
||||
client.sendStandardRequest();
|
||||
client.expectUpgradeResponse();
|
||||
|
||||
client.sleep(TimeUnit.SECONDS,1);
|
||||
|
||||
client.write(WebSocketFrame.text("Hello"));
|
||||
|
||||
// now wait for the server to time out
|
||||
// should be 2 frames, the TextFrame echo, and then the Close on disconnect
|
||||
IncomingFramesCapture capture = client.readFrames(2,TimeUnit.SECONDS,2);
|
||||
Assert.assertThat("frames[0].opcode",capture.getFrames().get(0).getOpCode(),is(OpCode.TEXT));
|
||||
Assert.assertThat("frames[1].opcode",capture.getFrames().get(1).getOpCode(),is(OpCode.CLOSE));
|
||||
}
|
||||
finally
|
||||
{
|
||||
client.close();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the requirement of responding with server terminated close code 1011 when there is an unhandled (internal server error) being produced by the
|
||||
* WebSocket POJO.
|
||||
|
|
|
@ -29,7 +29,9 @@ import java.io.InputStreamReader;
|
|||
import java.io.OutputStream;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.InetAddress;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.Socket;
|
||||
import java.net.SocketAddress;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.nio.ByteBuffer;
|
||||
|
@ -202,10 +204,14 @@ public class BlockheadClient implements IncomingFrames, OutgoingFrames
|
|||
{
|
||||
InetAddress destAddr = InetAddress.getByName(destHttpURI.getHost());
|
||||
int port = destHttpURI.getPort();
|
||||
socket = new Socket(destAddr,port);
|
||||
|
||||
SocketAddress endpoint = new InetSocketAddress(destAddr,port);
|
||||
|
||||
socket = new Socket();
|
||||
socket.setSoTimeout(timeout);
|
||||
socket.connect(endpoint);
|
||||
|
||||
out = socket.getOutputStream();
|
||||
socket.setSoTimeout(timeout);
|
||||
in = socket.getInputStream();
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue