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
- also add WebSocket#endpointId() and WebSocketConnection#endpointId()
- Loading branch information
Showing
13 changed files
with
308 additions
and
26 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
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
45 changes: 45 additions & 0 deletions
45
...ment/src/test/java/io/quarkus/websockets/next/test/endpoints/AmbiguousEndpointIdTest.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,45 @@ | ||
package io.quarkus.websockets.next.test.endpoints; | ||
|
||
import static org.junit.jupiter.api.Assertions.fail; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.test.QuarkusUnitTest; | ||
import io.quarkus.websockets.next.OnOpen; | ||
import io.quarkus.websockets.next.WebSocket; | ||
import io.quarkus.websockets.next.WebSocketServerException; | ||
|
||
public class AmbiguousEndpointIdTest { | ||
|
||
@RegisterExtension | ||
public static final QuarkusUnitTest test = new QuarkusUnitTest() | ||
.withApplicationRoot(root -> { | ||
root.addClasses(Endpoint1.class, Endpoint2.class); | ||
}) | ||
.setExpectedException(WebSocketServerException.class); | ||
|
||
@Test | ||
public void testEndpointIds() { | ||
fail(); | ||
} | ||
|
||
@WebSocket(path = "/ws1", endpointId = "foo") | ||
public static class Endpoint1 { | ||
|
||
@OnOpen | ||
void open() { | ||
} | ||
|
||
} | ||
|
||
@WebSocket(path = "/ws2", endpointId = "foo") | ||
public static class Endpoint2 { | ||
|
||
@OnOpen | ||
void open() { | ||
} | ||
|
||
} | ||
|
||
} |
113 changes: 113 additions & 0 deletions
113
...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,113 @@ | ||
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 static org.junit.jupiter.api.Assertions.fail; | ||
|
||
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"; | ||
|
||
for (WebSocketConnection c : connections) { | ||
fail("No connection should be found: " + c); | ||
} | ||
|
||
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.findByConnectionId(client1Id).orElse(null)); | ||
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.listAll(); | ||
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.listAll().size()); | ||
assertNull(connections.stream().filter(c -> c.id().equals(client2Id)).findFirst().orElse(null)); | ||
|
||
found = connections.findByEndpointId("end"); | ||
assertEquals(2, found.size()); | ||
} | ||
} | ||
|
||
@WebSocket(endpointId = "end", 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
55 changes: 55 additions & 0 deletions
55
...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,55 @@ | ||
package io.quarkus.websockets.next; | ||
|
||
import java.util.Collection; | ||
import java.util.Optional; | ||
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. | ||
* <p> | ||
* Quarkus provides a built-in CDI bean with the {@link jakarta.inject.Singleton} scope that implements this interface. | ||
*/ | ||
@Experimental("This API is experimental and may change in the future") | ||
public interface OpenConnections extends Iterable<WebSocketConnection> { | ||
|
||
/** | ||
* Returns an immutable snapshot of all open connections at the given time. | ||
* | ||
* @return an immutable collection of all open connections | ||
*/ | ||
default Collection<WebSocketConnection> listAll() { | ||
return stream().toList(); | ||
} | ||
|
||
/** | ||
* Returns an immutable snapshot of all open connections for the given endpoint id. | ||
* | ||
* @param endpointId | ||
* @return an immutable collection of all open connections for the given endpoint id | ||
* @see WebSocket#endpointId() | ||
*/ | ||
default Collection<WebSocketConnection> findByEndpointId(String endpointId) { | ||
return stream().filter(c -> c.endpointId().equals(endpointId)).toList(); | ||
} | ||
|
||
/** | ||
* Returns the open connection with the given id. | ||
* | ||
* @param connectionId | ||
* @return the open connection or empty {@link Optional} if no open connection with the given id exists | ||
* @see WebSocketConnection#id() | ||
*/ | ||
default Optional<WebSocketConnection> findByConnectionId(String connectionId) { | ||
return stream().filter(c -> c.id().equals(connectionId)).findFirst(); | ||
} | ||
|
||
/** | ||
* Returns the stream of all open connections at the given time. | ||
* | ||
* @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
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.