Skip to content

Commit

Permalink
Make sure @OnClose is called when ProtocolHandler.close throws IO…
Browse files Browse the repository at this point in the history
…Exception

Signed-off-by: jansupol <[email protected]>
  • Loading branch information
jansupol committed Oct 18, 2023
1 parent fbd9bbf commit d386cf8
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 3 deletions.
11 changes: 9 additions & 2 deletions core/src/main/java/org/glassfish/tyrus/core/ProtocolHandler.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2023 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -476,7 +476,14 @@ public synchronized Future<Frame> close(final int code, final String reason) {
outgoingCloseFrame = new CloseFrame(closeReason);
}

final Future<Frame> send = send(outgoingCloseFrame, null, CLOSE, false);
Future<Frame> send;
try {
send = send(outgoingCloseFrame, null, CLOSE, false);
} catch (Exception e) {
send = new TyrusFuture<>();
((TyrusFuture) send).setFailure(e);
LOGGER.warning(LocalizationMessages.EXCEPTION_CLOSE(e.getMessage()));
}

webSocket.onClose(new CloseFrame(closeReason));

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright (c) 2012, 2017 Oracle and/or its affiliates. All rights reserved.
# Copyright (c) 2012, 2023 Oracle and/or its affiliates. All rights reserved.
#
# This program and the accompanying materials are made available under the
# terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -93,6 +93,7 @@ message.handler.illegal.argument=Illegal MessageHandler argument value: {0}.
connection.null=Connection is null.
send.message.infragment=Attempting to send a message while sending fragments of another.
ioexception.close=IOException thrown when closing connection.
exception.close=Exception thrown when closing connection with message: {0}
extension.exception=Extension ''{0}'' threw an exception during processOutgoing method invocation: "{1}".
control.frame.fragmented=Fragmented control frame.
control.frame.length=Control frame payloads must be no greater than 125 bytes.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright (c) 2023 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/

package org.glassfish.tyrus.core;

import org.glassfish.tyrus.spi.CompletionHandler;
import org.glassfish.tyrus.spi.Writer;
import org.glassfish.tyrus.spi.WriterInfo;
import org.junit.Assert;
import org.junit.Test;

import javax.websocket.CloseReason;
import javax.websocket.DeploymentException;
import javax.websocket.Endpoint;
import javax.websocket.EndpointConfig;
import javax.websocket.Session;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.SocketException;
import java.nio.ByteBuffer;
import java.util.Collections;
import java.util.concurrent.CountDownLatch;

public class ProtocolHandlerTest {

public static class ProtocolHandlerOnCloseEndpoint extends Endpoint {
CountDownLatch onCloseLatch = new CountDownLatch(1);

public void onClose(Session session, CloseReason closeReason) {
onCloseLatch.countDown();
}

@Override
public void onOpen(Session session, EndpointConfig config) {

}
}

@Test
public void testOnCloseIsCalledWhenCloseThrowsError() throws DeploymentException {
ProtocolHandler handler = new ProtocolHandler(false, null);
handler.setWriter(new Writer() {
@Override
public void write(ByteBuffer buffer, CompletionHandler<ByteBuffer> completionHandler) {
throw new IllegalStateException("Not Expected");
}

@Override
public void write(ByteBuffer buffer, CompletionHandler<ByteBuffer> completionHandler, WriterInfo writerInfo) {
if (writerInfo.getMessageType() == WriterInfo.MessageType.CLOSE) {
throw new UncheckedIOException(new SocketException("Connection reset"));
}
}

@Override
public void close() throws IOException {
}
});

ProtocolHandlerOnCloseEndpoint endpoint = new ProtocolHandlerOnCloseEndpoint();
TyrusEndpointWrapper endpointWrapper = new TyrusEndpointWrapper(
endpoint, null, ComponentProviderService.create(), null, "path",
null, new TyrusEndpointWrapper.SessionListener() {}, null, null, null);

TyrusWebSocket tyrusWebSocket = new TyrusWebSocket(handler, endpointWrapper);
handler.setWebSocket(tyrusWebSocket);
endpointWrapper.createSessionForRemoteEndpoint(tyrusWebSocket, null, Collections.emptyList(), new DebugContext());
handler.close(1000, "TEST");
Assert.assertEquals(0, endpoint.onCloseLatch.getCount());
}
}

0 comments on commit d386cf8

Please sign in to comment.