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: introduce OpenConnections
- Loading branch information
Showing
9 changed files
with
205 additions
and
18 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
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
110 changes: 110 additions & 0 deletions
110
...nt/src/test/java/io/quarkus/websockets/next/test/openconnections/OpenConnectionsTest.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,110 @@ | ||
package io.quarkus.websockets.next.test.openconnections; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.junit.jupiter.api.Assertions.assertNull; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.net.URI; | ||
import java.util.Collection; | ||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
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.OnClose; | ||
import io.quarkus.websockets.next.OnOpen; | ||
import io.quarkus.websockets.next.OpenConnections; | ||
import io.quarkus.websockets.next.WebSocket; | ||
import io.quarkus.websockets.next.WebSocketConnection; | ||
import io.quarkus.websockets.next.test.utils.WSClient; | ||
import io.vertx.core.Vertx; | ||
import io.vertx.core.http.WebSocketConnectOptions; | ||
|
||
public class OpenConnectionsTest { | ||
|
||
@RegisterExtension | ||
public static final QuarkusUnitTest test = new QuarkusUnitTest() | ||
.withApplicationRoot(root -> { | ||
root.addClasses(Endpoint.class, WSClient.class); | ||
}); | ||
|
||
@Inject | ||
Vertx vertx; | ||
|
||
@TestHTTPResource("endpoint") | ||
URI endUri; | ||
|
||
@Inject | ||
OpenConnections connections; | ||
|
||
@Test | ||
void testOpenConnections() throws Exception { | ||
String headerName = "X-Test"; | ||
String header2 = "foo"; | ||
String header3 = "bar"; | ||
|
||
try (WSClient client1 = WSClient.create(vertx).connect(endUri); | ||
WSClient client2 = WSClient.create(vertx).connect(new WebSocketConnectOptions().addHeader(headerName, header2), | ||
endUri); | ||
WSClient client3 = WSClient.create(vertx).connect(new WebSocketConnectOptions().addHeader(headerName, header3), | ||
endUri)) { | ||
|
||
client1.waitForMessages(1); | ||
String client1Id = client1.getMessages().get(0).toString(); | ||
|
||
client2.waitForMessages(1); | ||
String client2Id = client2.getMessages().get(0).toString(); | ||
|
||
client3.waitForMessages(1); | ||
String client3Id = client3.getMessages().get(0).toString(); | ||
|
||
assertNotNull(connections.get(client1Id)); | ||
Collection<WebSocketConnection> found = connections.stream() | ||
.filter(c -> header3.equals(c.handshakeRequest().header(headerName))) | ||
.toList(); | ||
assertEquals(1, found.size()); | ||
assertEquals(client3Id, found.iterator().next().id()); | ||
|
||
found = connections.get(); | ||
assertEquals(3, found.size()); | ||
for (WebSocketConnection c : found) { | ||
assertTrue(c.id().equals(client1Id) || c.id().equals(client2Id) || c.id().equals(client3Id)); | ||
} | ||
|
||
client2.disconnect(); | ||
assertTrue(Endpoint.CLOSED_LATCH.await(5, TimeUnit.SECONDS)); | ||
|
||
assertEquals(2, connections.get().size()); | ||
assertNull(connections.stream().filter(c -> c.id().equals(client2Id)).findFirst().orElse(null)); | ||
|
||
found = connections.stream().filter( | ||
c -> c.endpoint().equals("io.quarkus.websockets.next.test.openconnections.OpenConnectionsTest$Endpoint")) | ||
.toList(); | ||
assertEquals(2, found.size()); | ||
} | ||
} | ||
|
||
@WebSocket(path = "/endpoint") | ||
public static class Endpoint { | ||
|
||
static final CountDownLatch CLOSED_LATCH = new CountDownLatch(1); | ||
|
||
@OnOpen | ||
String open(WebSocketConnection connection) { | ||
return connection.id(); | ||
} | ||
|
||
@OnClose | ||
void close() { | ||
CLOSED_LATCH.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
36 changes: 36 additions & 0 deletions
36
...sockets-next/server/runtime/src/main/java/io/quarkus/websockets/next/OpenConnections.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,36 @@ | ||
package io.quarkus.websockets.next; | ||
|
||
import java.util.Collection; | ||
import java.util.stream.Stream; | ||
|
||
import io.smallrye.common.annotation.Experimental; | ||
|
||
/** | ||
* Provides convenient access to all open connections from clients to {@link WebSocket} endpoints on the server. | ||
* | ||
* @see WebSocketConnection | ||
*/ | ||
@Experimental("This API is experimental and may change in the future") | ||
public interface OpenConnections { | ||
|
||
/** | ||
* | ||
* @return an immutable collection of all open connections | ||
*/ | ||
Collection<WebSocketConnection> get(); | ||
|
||
/** | ||
* | ||
* @param id | ||
* @return the open connection or {@code null} if no open connection with the given id exists | ||
* @see WebSocketConnection#id() | ||
*/ | ||
WebSocketConnection get(String id); | ||
|
||
/** | ||
* | ||
* @return the stream of open connections | ||
*/ | ||
Stream<WebSocketConnection> stream(); | ||
|
||
} |
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
21 changes: 20 additions & 1 deletion
21
...xt/server/runtime/src/main/java/io/quarkus/websockets/next/runtime/ConnectionManager.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
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