forked from quarkusio/quarkus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request quarkusio#40545 from mkouba/issue-40535
WebSockets Next: support close status code/reason
- Loading branch information
Showing
15 changed files
with
303 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
...ment/src/main/java/io/quarkus/websockets/next/deployment/CloseReasonCallbackArgument.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package io.quarkus.websockets.next.deployment; | ||
|
||
import io.quarkus.gizmo.MethodDescriptor; | ||
import io.quarkus.gizmo.ResultHandle; | ||
import io.quarkus.websockets.next.CloseReason; | ||
import io.quarkus.websockets.next.runtime.WebSocketConnectionBase; | ||
|
||
class CloseReasonCallbackArgument implements CallbackArgument { | ||
|
||
@Override | ||
public boolean matches(ParameterContext context) { | ||
return context.callbackAnnotation().name().equals(WebSocketDotNames.ON_CLOSE) | ||
&& context.parameter().type().name().equals(WebSocketDotNames.CLOSE_REASON); | ||
} | ||
|
||
@Override | ||
public ResultHandle get(InvocationBytecodeContext context) { | ||
return context.bytecode().invokeVirtualMethod( | ||
MethodDescriptor.ofMethod(WebSocketConnectionBase.class, "closeReason", CloseReason.class), | ||
context.getConnection()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
...ment/src/test/java/io/quarkus/websockets/next/test/closereason/ClientCloseReasonTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package io.quarkus.websockets.next.test.closereason; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.net.URI; | ||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
|
||
import jakarta.inject.Inject; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.test.QuarkusUnitTest; | ||
import io.quarkus.test.common.http.TestHTTPResource; | ||
import io.quarkus.websockets.next.BasicWebSocketConnector; | ||
import io.quarkus.websockets.next.CloseReason; | ||
import io.quarkus.websockets.next.OnClose; | ||
import io.quarkus.websockets.next.OnOpen; | ||
import io.quarkus.websockets.next.WebSocket; | ||
import io.quarkus.websockets.next.WebSocketClientConnection; | ||
import io.vertx.core.Vertx; | ||
|
||
public class ClientCloseReasonTest { | ||
|
||
@RegisterExtension | ||
public static final QuarkusUnitTest test = new QuarkusUnitTest() | ||
.withApplicationRoot(root -> { | ||
root.addClasses(Closing.class); | ||
}); | ||
|
||
@Inject | ||
Vertx vertx; | ||
|
||
@TestHTTPResource("closing") | ||
URI closingUri; | ||
|
||
@Test | ||
public void testClosed() throws InterruptedException { | ||
CountDownLatch closedClientLatch = new CountDownLatch(1); | ||
AtomicReference<Integer> closeStatusCode = new AtomicReference<>(); | ||
AtomicReference<String> closeMessage = new AtomicReference<>(); | ||
WebSocketClientConnection connection = BasicWebSocketConnector | ||
.create() | ||
.baseUri(closingUri) | ||
.onClose((c, cr) -> { | ||
closeStatusCode.set((int) cr.getCode()); | ||
closeMessage.set(cr.getMessage()); | ||
closedClientLatch.countDown(); | ||
}) | ||
.connectAndAwait(); | ||
connection.closeAndAwait(new CloseReason(4001, "foo")); | ||
assertTrue(Closing.CLOSED.await(5, TimeUnit.SECONDS)); | ||
assertTrue(closedClientLatch.await(5, TimeUnit.SECONDS)); | ||
assertEquals(4001, closeStatusCode.get()); | ||
assertEquals("foo", closeMessage.get()); | ||
} | ||
|
||
@WebSocket(path = "/closing") | ||
public static class Closing { | ||
|
||
static final CountDownLatch CLOSED = new CountDownLatch(1); | ||
|
||
@OnOpen | ||
public String open() { | ||
return "ready"; | ||
} | ||
|
||
@OnClose | ||
void onClose(CloseReason reason) { | ||
assertEquals(4001, reason.getCode()); | ||
assertEquals("foo", reason.getMessage()); | ||
CLOSED.countDown(); | ||
} | ||
|
||
} | ||
|
||
} |
82 changes: 82 additions & 0 deletions
82
...ment/src/test/java/io/quarkus/websockets/next/test/closereason/ServerCloseReasonTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package io.quarkus.websockets.next.test.closereason; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.net.URI; | ||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
|
||
import jakarta.inject.Inject; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.test.QuarkusUnitTest; | ||
import io.quarkus.test.common.http.TestHTTPResource; | ||
import io.quarkus.websockets.next.BasicWebSocketConnector; | ||
import io.quarkus.websockets.next.CloseReason; | ||
import io.quarkus.websockets.next.OnClose; | ||
import io.quarkus.websockets.next.OnTextMessage; | ||
import io.quarkus.websockets.next.WebSocket; | ||
import io.quarkus.websockets.next.WebSocketClientConnection; | ||
import io.quarkus.websockets.next.WebSocketConnection; | ||
import io.smallrye.mutiny.Uni; | ||
import io.vertx.core.Vertx; | ||
|
||
public class ServerCloseReasonTest { | ||
|
||
@RegisterExtension | ||
public static final QuarkusUnitTest test = new QuarkusUnitTest() | ||
.withApplicationRoot(root -> { | ||
root.addClasses(Closing.class); | ||
}); | ||
|
||
@Inject | ||
Vertx vertx; | ||
|
||
@TestHTTPResource("closing") | ||
URI closingUri; | ||
|
||
@Test | ||
public void testClosed() throws InterruptedException { | ||
CountDownLatch closedClientLatch = new CountDownLatch(1); | ||
AtomicReference<Integer> closeStatusCode = new AtomicReference<>(); | ||
AtomicReference<String> closeMessage = new AtomicReference<>(); | ||
WebSocketClientConnection connection = BasicWebSocketConnector | ||
.create() | ||
.baseUri(closingUri) | ||
.onClose((c, cr) -> { | ||
closeStatusCode.set((int) cr.getCode()); | ||
closeMessage.set(cr.getMessage()); | ||
closedClientLatch.countDown(); | ||
}) | ||
.connectAndAwait(); | ||
connection.sendTextAndAwait("foo"); | ||
assertTrue(Closing.CLOSED.await(5, TimeUnit.SECONDS)); | ||
assertTrue(closedClientLatch.await(5, TimeUnit.SECONDS)); | ||
assertEquals(4001, closeStatusCode.get()); | ||
assertEquals("foo", closeMessage.get()); | ||
} | ||
|
||
@WebSocket(path = "/closing") | ||
public static class Closing { | ||
|
||
static final CountDownLatch CLOSED = new CountDownLatch(1); | ||
|
||
@OnTextMessage | ||
public Uni<Void> onMessage(String message, WebSocketConnection connection) { | ||
return connection.close(new CloseReason(4001, message)); | ||
} | ||
|
||
@OnClose | ||
void onClose(CloseReason reason) { | ||
assertEquals(4001, reason.getCode()); | ||
assertEquals("foo", reason.getMessage()); | ||
CLOSED.countDown(); | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
extensions/websockets-next/runtime/src/main/java/io/quarkus/websockets/next/CloseReason.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package io.quarkus.websockets.next; | ||
|
||
import io.netty.handler.codec.http.websocketx.WebSocketCloseStatus; | ||
|
||
/** | ||
* Indicates a reason for closing a connection. See also RFC-6455 | ||
* <a href="https://datatracker.ietf.org/doc/html/rfc6455#section-5.5.1">section 5.5.1</a>. The pre-defined status codes are | ||
* listed in <a href="https://datatracker.ietf.org/doc/html/rfc6455#section-7.4.1">section 7.4.1</a>. | ||
* | ||
* @see WebSocketCloseStatus | ||
* @see WebSocketConnection#close(CloseReason) | ||
* @see WebSocketClientConnection#close(CloseReason) | ||
*/ | ||
public class CloseReason { | ||
|
||
public static final CloseReason NORMAL = new CloseReason(WebSocketCloseStatus.NORMAL_CLOSURE.code()); | ||
|
||
private final int code; | ||
|
||
private final String message; | ||
|
||
/** | ||
* | ||
* @param code The status code must comply with RFC-6455 | ||
*/ | ||
public CloseReason(int code) { | ||
this(code, null); | ||
} | ||
|
||
/** | ||
* | ||
* @param code The status code must comply with RFC-6455 | ||
* @param message | ||
*/ | ||
public CloseReason(int code, String message) { | ||
if (!WebSocketCloseStatus.isValidStatusCode(code)) { | ||
throw new IllegalArgumentException("Invalid status code: " + code); | ||
} | ||
this.code = code; | ||
this.message = message; | ||
} | ||
|
||
public int getCode() { | ||
return code; | ||
} | ||
|
||
public String getMessage() { | ||
return message; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.