Skip to content

Commit

Permalink
#9412 ee10: Fixed missing callback failure when ws endpoint cannot b…
Browse files Browse the repository at this point in the history
…e instantiated

Signed-off-by: Ludovic Orban <[email protected]>
  • Loading branch information
lorban committed Mar 6, 2023
1 parent 474136d commit 5bac801
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,10 @@ public Map<String, Object> getUserProperties()
Object endpoint = config.getConfigurator().getEndpointInstance(endpointClass);
return new ConfiguredEndpoint(endpoint, config);
}
catch (InstantiationException e)
catch (Throwable x)
{
LOG.warn("Unable to create websocket: {}", config.getEndpointClass().getName(), e);
LOG.warn("Unable to create websocket: {}", config.getEndpointClass().getName(), x);
callback.failed(x);
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 5bac801

Please sign in to comment.