434077 - AnnotatedServerEndpointTest emits strange exception

Adding lock around use of remote, eliminating artificial EofException.
This commit is contained in:
Joakim Erdfelt 2014-05-06 12:12:42 -07:00
parent f5648a2096
commit 7d662360f1
1 changed files with 49 additions and 18 deletions

View File

@ -18,11 +18,12 @@
package org.eclipse.jetty.websocket.jsr356.server; package org.eclipse.jetty.websocket.jsr356.server;
import java.io.EOFException;
import java.io.IOException; import java.io.IOException;
import java.util.Queue; import java.util.Queue;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException; import java.util.concurrent.TimeoutException;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import org.eclipse.jetty.toolchain.test.EventQueue; import org.eclipse.jetty.toolchain.test.EventQueue;
import org.eclipse.jetty.util.log.Log; import org.eclipse.jetty.util.log.Log;
@ -47,8 +48,8 @@ public class JettyEchoSocket
private static final Logger LOG = Log.getLogger(JettyEchoSocket.class); private static final Logger LOG = Log.getLogger(JettyEchoSocket.class);
@SuppressWarnings("unused") @SuppressWarnings("unused")
private Session session; private Session session;
private volatile RemoteEndpoint remote; private Lock remoteLock = new ReentrantLock();
private volatile Boolean closed = null; private RemoteEndpoint remote;
private EventQueue<String> incomingMessages = new EventQueue<>(); private EventQueue<String> incomingMessages = new EventQueue<>();
public Queue<String> awaitMessages(int expected) throws TimeoutException, InterruptedException public Queue<String> awaitMessages(int expected) throws TimeoutException, InterruptedException
@ -57,17 +58,32 @@ public class JettyEchoSocket
return incomingMessages; return incomingMessages;
} }
public Boolean getClosed() public boolean getClosed()
{ {
return closed; remoteLock.lock();
try
{
return (remote == null);
}
finally
{
remoteLock.unlock();
}
} }
@OnWebSocketClose @OnWebSocketClose
public void onClose(int code, String reason) public void onClose(int code, String reason)
{ {
closed = true;
session = null; session = null;
remote = null; remoteLock.lock();
try
{
remote = null;
}
finally
{
remoteLock.unlock();
}
} }
@OnWebSocketError @OnWebSocketError
@ -86,21 +102,36 @@ public class JettyEchoSocket
@OnWebSocketConnect @OnWebSocketConnect
public void onOpen(Session session) public void onOpen(Session session)
{ {
this.closed = false;
this.session = session; this.session = session;
this.remote = session.getRemote(); remoteLock.lock();
try
{
this.remote = session.getRemote();
}
finally
{
remoteLock.unlock();
}
} }
public void sendMessage(String msg) throws IOException public void sendMessage(String msg) throws IOException
{ {
RemoteEndpoint r = remote; remoteLock.lock();
// TODO there is a race with onClose here and no sufficient memory barrier. Taking a local copy try
// stops the worst of the errors, but is probably not the best solution. {
if (r==null) RemoteEndpoint r = remote;
throw new EOFException("Closed="+closed); if (r == null)
{
r.sendStringByFuture(msg); return;
if (r.getBatchMode() == BatchMode.ON) }
r.flush();
r.sendStringByFuture(msg);
if (r.getBatchMode() == BatchMode.ON)
r.flush();
}
finally
{
remoteLock.unlock();
}
} }
} }