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.
WebSockets Next: error handlers part 4
- use error handlers to process Mutiny Multi failures
- Loading branch information
Showing
10 changed files
with
513 additions
and
39 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
63 changes: 63 additions & 0 deletions
63
...ment/src/test/java/io/quarkus/websockets/next/test/errors/MultiBinaryDecodeErrorTest.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,63 @@ | ||
package io.quarkus.websockets.next.test.errors; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.net.URI; | ||
|
||
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.BinaryDecodeException; | ||
import io.quarkus.websockets.next.OnBinaryMessage; | ||
import io.quarkus.websockets.next.OnError; | ||
import io.quarkus.websockets.next.WebSocket; | ||
import io.quarkus.websockets.next.test.utils.WSClient; | ||
import io.smallrye.mutiny.Multi; | ||
import io.vertx.core.Vertx; | ||
import io.vertx.core.buffer.Buffer; | ||
import io.vertx.mutiny.core.Context; | ||
|
||
public class MultiBinaryDecodeErrorTest { | ||
|
||
@RegisterExtension | ||
public static final QuarkusUnitTest test = new QuarkusUnitTest() | ||
.withApplicationRoot(root -> { | ||
root.addClasses(Echo.class, WSClient.class); | ||
}); | ||
|
||
@Inject | ||
Vertx vertx; | ||
|
||
@TestHTTPResource("echo") | ||
URI testUri; | ||
|
||
@Test | ||
void testError() { | ||
WSClient client = WSClient.create(vertx).connect(testUri); | ||
client.send(Buffer.buffer("1")); | ||
client.waitForMessages(1); | ||
assertEquals("Problem decoding: 1", client.getLastMessage().toString()); | ||
} | ||
|
||
@WebSocket(path = "/echo") | ||
public static class Echo { | ||
|
||
@OnBinaryMessage | ||
Multi<Integer> process(Multi<Integer> messages) { | ||
return messages; | ||
} | ||
|
||
@OnError | ||
String decodingError(BinaryDecodeException e) { | ||
assertTrue(Context.isOnWorkerThread()); | ||
return "Problem decoding: " + e.getBytes().toString(); | ||
} | ||
|
||
} | ||
|
||
} |
63 changes: 63 additions & 0 deletions
63
...ment/src/test/java/io/quarkus/websockets/next/test/errors/MultiBinaryEncodeErrorTest.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,63 @@ | ||
package io.quarkus.websockets.next.test.errors; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.net.URI; | ||
|
||
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.BinaryEncodeException; | ||
import io.quarkus.websockets.next.OnBinaryMessage; | ||
import io.quarkus.websockets.next.OnError; | ||
import io.quarkus.websockets.next.WebSocket; | ||
import io.quarkus.websockets.next.test.utils.WSClient; | ||
import io.smallrye.mutiny.Multi; | ||
import io.vertx.core.Vertx; | ||
import io.vertx.core.buffer.Buffer; | ||
import io.vertx.mutiny.core.Context; | ||
|
||
public class MultiBinaryEncodeErrorTest { | ||
|
||
@RegisterExtension | ||
public static final QuarkusUnitTest test = new QuarkusUnitTest() | ||
.withApplicationRoot(root -> { | ||
root.addClasses(Echo.class, WSClient.class); | ||
}); | ||
|
||
@Inject | ||
Vertx vertx; | ||
|
||
@TestHTTPResource("echo") | ||
URI testUri; | ||
|
||
@Test | ||
void testError() { | ||
WSClient client = WSClient.create(vertx).connect(testUri); | ||
client.send(Buffer.buffer("1")); | ||
client.waitForMessages(1); | ||
assertEquals("Problem encoding: 1", client.getLastMessage().toString()); | ||
} | ||
|
||
@WebSocket(path = "/echo") | ||
public static class Echo { | ||
|
||
@OnBinaryMessage | ||
Multi<Integer> process(Buffer message) { | ||
return Multi.createFrom().item(Integer.parseInt(message.toString())); | ||
} | ||
|
||
@OnError | ||
String encodingError(BinaryEncodeException e) { | ||
assertTrue(Context.isOnWorkerThread()); | ||
return "Problem encoding: " + e.getEncodedObject().toString(); | ||
} | ||
|
||
} | ||
|
||
} |
70 changes: 70 additions & 0 deletions
70
...src/test/java/io/quarkus/websockets/next/test/errors/MultiFailureCloseConnectionTest.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,70 @@ | ||
package io.quarkus.websockets.next.test.errors; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import java.net.URI; | ||
import java.time.Duration; | ||
|
||
import jakarta.inject.Inject; | ||
|
||
import org.awaitility.Awaitility; | ||
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.OnError; | ||
import io.quarkus.websockets.next.OnTextMessage; | ||
import io.quarkus.websockets.next.WebSocket; | ||
import io.quarkus.websockets.next.WebSocketConnection; | ||
import io.quarkus.websockets.next.test.utils.WSClient; | ||
import io.smallrye.mutiny.Multi; | ||
import io.smallrye.mutiny.Uni; | ||
import io.vertx.core.Vertx; | ||
|
||
public class MultiFailureCloseConnectionTest { | ||
|
||
@RegisterExtension | ||
public static final QuarkusUnitTest test = new QuarkusUnitTest() | ||
.withApplicationRoot(root -> { | ||
root.addClasses(Echo.class, WSClient.class); | ||
}); | ||
|
||
@Inject | ||
Vertx vertx; | ||
|
||
@TestHTTPResource("echo") | ||
URI testUri; | ||
|
||
@Test | ||
void testError() { | ||
WSClient client = WSClient.create(vertx).connect(testUri); | ||
client.sendAndAwait("bar,foo,baz"); | ||
// "bar" should be sent back | ||
client.waitForMessages(1); | ||
// "foo" results in a failure -> connection closed | ||
Awaitility.await().atMost(Duration.ofSeconds(5)).until(() -> client.isClosed()); | ||
// "foo" and "baz" should never be sent back | ||
assertEquals(1, client.getMessages().size()); | ||
} | ||
|
||
@WebSocket(path = "/echo") | ||
public static class Echo { | ||
|
||
@OnTextMessage | ||
Multi<String> process(String message) { | ||
return Multi.createFrom().items(message.split(",")).invoke(s -> { | ||
if (s.equals("foo")) { | ||
throw new IllegalArgumentException(); | ||
} | ||
}); | ||
} | ||
|
||
@OnError | ||
Uni<Void> runtimeProblem(IllegalArgumentException e, WebSocketConnection connection) { | ||
return connection.close(); | ||
} | ||
|
||
} | ||
|
||
} |
63 changes: 63 additions & 0 deletions
63
...ver/deployment/src/test/java/io/quarkus/websockets/next/test/errors/MultiFailureTest.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,63 @@ | ||
package io.quarkus.websockets.next.test.errors; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import java.net.URI; | ||
|
||
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.OnError; | ||
import io.quarkus.websockets.next.OnTextMessage; | ||
import io.quarkus.websockets.next.WebSocket; | ||
import io.quarkus.websockets.next.test.utils.WSClient; | ||
import io.smallrye.mutiny.Multi; | ||
import io.vertx.core.Vertx; | ||
|
||
public class MultiFailureTest { | ||
|
||
@RegisterExtension | ||
public static final QuarkusUnitTest test = new QuarkusUnitTest() | ||
.withApplicationRoot(root -> { | ||
root.addClasses(Echo.class, WSClient.class); | ||
}); | ||
|
||
@Inject | ||
Vertx vertx; | ||
|
||
@TestHTTPResource("echo") | ||
URI testUri; | ||
|
||
@Test | ||
void testError() { | ||
WSClient client = WSClient.create(vertx).connect(testUri); | ||
client.sendAndAwait("bar,foo,baz"); | ||
client.waitForMessages(2); | ||
assertEquals("bar", client.getMessages().get(0).toString()); | ||
assertEquals("foo detected", client.getMessages().get(1).toString()); | ||
} | ||
|
||
@WebSocket(path = "/echo") | ||
public static class Echo { | ||
|
||
@OnTextMessage | ||
Multi<String> process(String message) { | ||
return Multi.createFrom().items(message.split(",")).invoke(s -> { | ||
if (s.equals("foo")) { | ||
throw new IllegalArgumentException(); | ||
} | ||
}); | ||
} | ||
|
||
@OnError | ||
String runtimeProblem(IllegalArgumentException e) { | ||
return "foo detected"; | ||
} | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.