Issue #2768 - JSR356 Session.getUserProperties() is null during @OnOpen

Signed-off-by: Joakim Erdfelt <joakim.erdfelt@gmail.com>
This commit is contained in:
Joakim Erdfelt 2018-08-02 09:16:34 -05:00
parent d37b506cb1
commit 586484850f
6 changed files with 39 additions and 13 deletions

View File

@ -18,7 +18,9 @@
package org.eclipse.jetty.websocket.jsr356;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.is;
import java.net.URI;
import java.util.List;
@ -125,7 +127,8 @@ public class ConfiguratorTest
session.getBasicRemote().sendText("Echo");
// Wait for echo
echoer.textCapture.messageQueue.awaitMessages(1,1000,TimeUnit.MILLISECONDS);
String echoed = echoer.textCapture.messages.poll(1,TimeUnit.SECONDS);
assertThat("Echoed", echoed, is("Echo"));
// Validate client side configurator use
Assert.assertThat("configurator.request",configurator.request,notNullValue());

View File

@ -20,13 +20,15 @@ package org.eclipse.jetty.websocket.jsr356;
import javax.websocket.MessageHandler;
import org.eclipse.jetty.util.BlockingArrayQueue;
public class EchoCaptureHandler implements MessageHandler.Whole<String>
{
public MessageQueue messageQueue = new MessageQueue();
public BlockingArrayQueue<String> messages = new BlockingArrayQueue<>();
@Override
public void onMessage(String message)
{
messageQueue.offer(message);
messages.offer(message);
}
}

View File

@ -52,6 +52,7 @@ public class EndpointEchoClient extends Endpoint
if (LOG.isDebugEnabled())
LOG.debug("onOpen({}, {})",session,config);
this.session = session;
this.session.getUserProperties().put("endpoint", this);
Assert.assertThat("Session is required",session,notNullValue());
Assert.assertThat("EndpointConfig is required",config,notNullValue());
this.session.addMessageHandler(textCapture);

View File

@ -18,7 +18,9 @@
package org.eclipse.jetty.websocket.jsr356;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import java.net.URI;
import java.util.concurrent.TimeUnit;
@ -98,7 +100,8 @@ public class EndpointEchoTest
session.getBasicRemote().sendText("Echo");
if (LOG.isDebugEnabled())
LOG.debug("Client Message Sent");
echoer.textCapture.messageQueue.awaitMessages(1,1000,TimeUnit.MILLISECONDS);
String echoed = echoer.textCapture.messages.poll(1, TimeUnit.SECONDS);
assertThat("Echoed message", echoed, is("Echo"));
}
@Test
@ -113,8 +116,9 @@ public class EndpointEchoTest
session.getBasicRemote().sendText("Echo");
if (LOG.isDebugEnabled())
LOG.debug("Client Message Sent");
// TODO: figure out echo verification.
// echoer.textCapture.messageQueue.awaitMessages(1,1000,TimeUnit.MILLISECONDS);
EndpointEchoClient client = (EndpointEchoClient) session.getUserProperties().get("endpoint");
String echoed = client.textCapture.messages.poll(1, TimeUnit.SECONDS);
assertThat("Echoed message", echoed, is("Echo"));
}
@Test
@ -131,7 +135,8 @@ public class EndpointEchoTest
session.getBasicRemote().sendText("Echo");
if (LOG.isDebugEnabled())
LOG.debug("Client Message Sent");
echoer.messageQueue.awaitMessages(1,1000,TimeUnit.MILLISECONDS);
String echoed = echoer.messages.poll(1, TimeUnit.SECONDS);
assertThat("Echoed message", echoed, is("Echo"));
}
@Test
@ -146,7 +151,8 @@ public class EndpointEchoTest
session.getBasicRemote().sendText("Echo");
if (LOG.isDebugEnabled())
LOG.debug("Client Message Sent");
// TODO: figure out echo verification.
// echoer.messageQueue.awaitMessages(1,1000,TimeUnit.MILLISECONDS);
EchoStringEndpoint client = (EchoStringEndpoint) session.getUserProperties().get("endpoint");
String echoed = client.messages.poll(1, TimeUnit.SECONDS);
assertThat("Echoed message", echoed, is("Echo"));
}
}

View File

@ -25,6 +25,10 @@ import org.eclipse.jetty.util.BlockingArrayQueue;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
/**
* @deprecated use {@code BlockingArrayQueue<String>} instead
*/
@Deprecated
public class MessageQueue extends BlockingArrayQueue<String>
{
private static final Logger LOG = Log.getLogger(MessageQueue.class);

View File

@ -18,19 +18,29 @@
package org.eclipse.jetty.websocket.jsr356.samples;
import org.eclipse.jetty.websocket.jsr356.MessageQueue;
import javax.websocket.EndpointConfig;
import javax.websocket.Session;
import org.eclipse.jetty.util.BlockingArrayQueue;
/**
* Legitimate structure for an Endpoint
*/
public class EchoStringEndpoint extends AbstractStringEndpoint
{
public MessageQueue messageQueue = new MessageQueue();
public BlockingArrayQueue<String> messages = new BlockingArrayQueue<>();
@Override
public void onOpen(Session session, EndpointConfig config)
{
super.onOpen(session, config);
this.session.getUserProperties().put("endpoint", this);
}
@Override
public void onMessage(String message)
{
messageQueue.offer(message);
session.getAsyncRemote().sendText(message);
this.messages.offer(message);
this.session.getAsyncRemote().sendText(message);
}
}