Skip to content

Commit

Permalink
WebSockets Next: allow Uni<Void> as a return type of Kotlin @onpong and
Browse files Browse the repository at this point in the history
@onclose methods
  • Loading branch information
Ladicek committed Jan 7, 2025
1 parent 7a54ece commit bfa7828
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -766,12 +766,13 @@ static String getPathPrefix(IndexView index, DotName enclosingClassName) {

private void validateOnPongMessage(Callback callback) {
if (KotlinUtils.isKotlinMethod(callback.method)) {
if (!callback.isReturnTypeVoid() && !callback.isKotlinSuspendFunctionReturningUnit()) {
if (!callback.isReturnTypeVoid() && !isUniVoid(callback.returnType())
&& !callback.isKotlinSuspendFunctionReturningUnit()) {
throw new WebSocketServerException(
"@OnPongMessage callback must return Unit: " + callback.asString());
"@OnPongMessage callback must return Unit or Uni<Void>: " + callback.asString());
}
} else {
if (callback.returnType().kind() != Kind.VOID && !WebSocketProcessor.isUniVoid(callback.returnType())) {
if (!callback.isReturnTypeVoid() && !isUniVoid(callback.returnType())) {
throw new WebSocketServerException(
"@OnPongMessage callback must return void or Uni<Void>: " + callback.asString());
}
Expand All @@ -786,12 +787,13 @@ private void validateOnPongMessage(Callback callback) {

private void validateOnClose(Callback callback) {
if (KotlinUtils.isKotlinMethod(callback.method)) {
if (!callback.isReturnTypeVoid() && !callback.isKotlinSuspendFunctionReturningUnit()) {
if (!callback.isReturnTypeVoid() && !isUniVoid(callback.returnType())
&& !callback.isKotlinSuspendFunctionReturningUnit()) {
throw new WebSocketServerException(
"@OnClose callback must return Unit: " + callback.asString());
"@OnClose callback must return Unit or Uni<Void>: " + callback.asString());
}
} else {
if (callback.returnType().kind() != Kind.VOID && !WebSocketProcessor.isUniVoid(callback.returnType())) {
if (!callback.isReturnTypeVoid() && !isUniVoid(callback.returnType())) {
throw new WebSocketServerException(
"@OnClose callback must return void or Uni<Void>: " + callback.asString());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.quarkus.websockets.next.test.kotlin

import io.quarkus.websockets.next.OnBinaryMessage
import io.quarkus.websockets.next.OnClose
import io.quarkus.websockets.next.WebSocket
import io.vertx.core.buffer.Buffer
import kotlinx.coroutines.delay
Expand All @@ -11,4 +12,8 @@ class BinaryEcho {
fun process(msg: Buffer): Buffer {
return msg
}

@OnClose
fun close() {
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.quarkus.websockets.next.test.kotlin

import io.quarkus.websockets.next.OnBinaryMessage
import io.quarkus.websockets.next.OnClose
import io.quarkus.websockets.next.WebSocket
import io.vertx.core.buffer.Buffer
import kotlinx.coroutines.delay
Expand All @@ -12,4 +13,9 @@ class BinaryEchoSuspend {
delay(100)
return msg
}

@OnClose
suspend fun close() {
delay(100)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.quarkus.websockets.next.test.kotlin

import io.quarkus.websockets.next.OnBinaryMessage
import io.quarkus.websockets.next.OnClose
import io.quarkus.websockets.next.WebSocket
import io.smallrye.mutiny.Uni
import io.vertx.core.buffer.Buffer
import kotlinx.coroutines.delay

@WebSocket(path = "/binary-echo-uni")
class BinaryEchoUni {
@OnBinaryMessage
fun process(msg: Buffer): Uni<Buffer> {
return Uni.createFrom().item(msg)
}


@OnClose
fun close(): Uni<Void> {
return Uni.createFrom().voidItem()
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.quarkus.websockets.next.test.kotlin

import io.quarkus.websockets.next.OnClose
import io.quarkus.websockets.next.OnTextMessage
import io.quarkus.websockets.next.WebSocket

Expand All @@ -9,4 +10,8 @@ class Echo {
fun process(msg: Message): Message {
return msg
}

@OnClose
fun close() {
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.quarkus.websockets.next.test.kotlin

import io.quarkus.websockets.next.OnClose
import io.quarkus.websockets.next.OnTextMessage
import io.quarkus.websockets.next.WebSocket
import kotlinx.coroutines.delay
Expand All @@ -11,4 +12,9 @@ class EchoSuspend {
delay(100)
return msg
}

@OnClose
suspend fun close() {
delay(100)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.quarkus.websockets.next.test.kotlin

import io.quarkus.websockets.next.OnClose
import io.quarkus.websockets.next.OnTextMessage
import io.quarkus.websockets.next.WebSocket
import io.smallrye.mutiny.Uni

@WebSocket(path = "/echo-uni")
class EchoUni {
@OnTextMessage
fun process(msg: Message): Uni<Message> {
return Uni.createFrom().item(msg)
}

@OnClose
fun close(): Uni<Void> {
return Uni.createFrom().voidItem()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ class KotlinWebSocketTest {
@RegisterExtension
val test = QuarkusUnitTest()
.withApplicationRoot { jar ->
jar.addClasses(Echo::class.java, EchoSuspend::class.java, BinaryEcho::class.java,
BinaryEchoSuspend::class.java, Message::class.java, WSClient::class.java)
jar.addClasses(Echo::class.java, EchoSuspend::class.java, EchoUni::class.java,
BinaryEcho::class.java, BinaryEchoSuspend::class.java, BinaryEchoUni::class.java,
Message::class.java, WSClient::class.java)
}
}

Expand All @@ -31,12 +32,18 @@ class KotlinWebSocketTest {
@TestHTTPResource("echo-suspend")
lateinit var echoSuspend: URI

@TestHTTPResource("echo-uni")
lateinit var echoUni: URI

@TestHTTPResource("binary-echo")
lateinit var binaryEcho: URI

@TestHTTPResource("binary-echo-suspend")
lateinit var binaryEchoSuspend: URI

@TestHTTPResource("binary-echo-uni")
lateinit var binaryEchoUni: URI

@Test
fun testEcho() {
doTest(echo)
Expand All @@ -47,6 +54,11 @@ class KotlinWebSocketTest {
doTest(echoSuspend)
}

@Test
fun testEchoUni() {
doTest(echoUni)
}

private fun doTest(uri: URI) {
WSClient.create(vertx).connect(uri).use { client ->
val req = JsonObject().put("msg", "hello")
Expand All @@ -65,6 +77,11 @@ class KotlinWebSocketTest {
doTestBinary(binaryEchoSuspend)
}

@Test
fun testBinaryEchoUni() {
doTestBinary(binaryEchoUni)
}

private fun doTestBinary(uri: URI) {
WSClient.create(vertx).connect(uri).use { client ->
val req = Buffer.buffer("hello there!")
Expand Down

0 comments on commit bfa7828

Please sign in to comment.