Skip to content

Commit

Permalink
#9412 Fixed missing callback failure when ws endpoint cannot be insta…
Browse files Browse the repository at this point in the history
…ntiated

Signed-off-by: Ludovic Orban <[email protected]>
  • Loading branch information
lorban committed Mar 2, 2023
1 parent 474136d commit 0fa3ae5
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ public Map<String, Object> getUserProperties()
catch (InstantiationException e)
{
LOG.warn("Unable to create websocket: {}", config.getEndpointClass().getName(), e);
callback.failed(e);
return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,21 @@
import org.eclipse.jetty.ee10.websocket.jakarta.client.JakartaWebSocketClientContainer;
import org.eclipse.jetty.ee10.websocket.jakarta.server.JakartaWebSocketServerContainer;
import org.eclipse.jetty.ee10.websocket.jakarta.server.config.JakartaWebSocketServletContainerInitializer;
import org.eclipse.jetty.ee10.websocket.jakarta.server.internal.JakartaWebSocketCreator;
import org.eclipse.jetty.logging.StacklessLogging;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.util.ajax.JSON;
import org.eclipse.jetty.websocket.core.exception.UpgradeException;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

public class ProgrammaticWebSocketUpgradeTest
{
Expand Down Expand Up @@ -97,6 +102,20 @@ public void onOpen(Session session, EndpointConfig config)
}
}

public static class BrokenEndpoint extends Endpoint
{
public BrokenEndpoint()
{
throw new RuntimeException("Broken");
}

@Override
public void onOpen(Session session, EndpointConfig config)
{
throw new UnsupportedOperationException();
}
}

public static class CustomUpgradeServlet extends HttpServlet
{
private JakartaWebSocketServerContainer container;
Expand Down Expand Up @@ -128,6 +147,12 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) t
container.upgradeHttpToWebSocket(request, response, sec, PATH_PARAMS);
break;
}
case "/brokenEndpoint":
{
ServerEndpointConfig sec = ServerEndpointConfig.Builder.create(BrokenEndpoint.class, "/").build();
container.upgradeHttpToWebSocket(request, response, sec, PATH_PARAMS);
break;
}
default:
throw new IllegalStateException();
}
Expand Down Expand Up @@ -155,6 +180,25 @@ public void testWebSocketUpgrade() throws Exception
assertThat(socket.closeReason.getCloseCode(), is(CloseReason.CloseCodes.NORMAL_CLOSURE));
}

@Test
public void testWebSocketUpgradeFailure() throws Exception
{
try (StacklessLogging ignore = new StacklessLogging(JakartaWebSocketCreator.class);)
{
URI uri = URI.create("ws://localhost:" + connector.getLocalPort() + "/brokenEndpoint");
EventSocket socket = new EventSocket();
try
{
client.connectToServer(socket, uri);
fail("expected IOException");
}
catch (IOException ioe)
{
assertInstanceOf(UpgradeException.class, ioe.getCause());
}
}
}

@Test
public void testPathParameters() throws Exception
{
Expand Down

0 comments on commit 0fa3ae5

Please sign in to comment.