|
| 1 | +/* |
| 2 | + * Copyright (c) 2023 Oracle and/or its affiliates. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.helidon.microprofile.tyrus; |
| 18 | + |
| 19 | +import java.net.URI; |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.List; |
| 22 | +import java.util.Map; |
| 23 | +import java.util.concurrent.ConcurrentHashMap; |
| 24 | +import java.util.concurrent.Semaphore; |
| 25 | +import java.util.concurrent.TimeUnit; |
| 26 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 27 | + |
| 28 | +import io.helidon.microprofile.tests.junit5.AddBean; |
| 29 | +import io.helidon.microprofile.tests.junit5.HelidonTest; |
| 30 | +import jakarta.annotation.PostConstruct; |
| 31 | +import jakarta.annotation.PreDestroy; |
| 32 | +import jakarta.enterprise.context.ApplicationScoped; |
| 33 | +import jakarta.inject.Inject; |
| 34 | +import jakarta.websocket.Endpoint; |
| 35 | +import jakarta.websocket.EndpointConfig; |
| 36 | +import jakarta.websocket.OnMessage; |
| 37 | +import jakarta.websocket.OnOpen; |
| 38 | +import jakarta.websocket.Session; |
| 39 | +import jakarta.websocket.server.PathParam; |
| 40 | +import jakarta.websocket.server.ServerEndpoint; |
| 41 | +import jakarta.ws.rs.client.WebTarget; |
| 42 | +import org.glassfish.tyrus.client.ClientManager; |
| 43 | +import org.glassfish.tyrus.container.jdk.client.JdkClientContainer; |
| 44 | +import org.junit.jupiter.api.Test; |
| 45 | + |
| 46 | +import static org.hamcrest.CoreMatchers.is; |
| 47 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 48 | + |
| 49 | +@HelidonTest |
| 50 | +@AddBean(ApplicationScopeTest.WebsocketEndpoint.class) |
| 51 | +class ApplicationScopeTest { |
| 52 | + |
| 53 | + static Semaphore semaphore = new Semaphore(0); |
| 54 | + |
| 55 | + @Inject |
| 56 | + protected WebsocketEndpoint endpoint; |
| 57 | + |
| 58 | + @Inject |
| 59 | + protected WebTarget webTarget; |
| 60 | + |
| 61 | + @Test |
| 62 | + void test() throws Exception { |
| 63 | + // two message sent over different sessions |
| 64 | + Session session_1 = connectToWebsocket("websocket/id-1"); |
| 65 | + session_1.getBasicRemote().sendText("A message 1"); |
| 66 | + Session session_2 = connectToWebsocket("websocket/id-2"); |
| 67 | + session_2.getBasicRemote().sendText("A message 2"); |
| 68 | + |
| 69 | + // wait until first two messages received |
| 70 | + assertThat(semaphore.tryAcquire(5, TimeUnit.SECONDS), is(true)); |
| 71 | + assertThat(semaphore.tryAcquire(5, TimeUnit.SECONDS), is(true)); |
| 72 | + |
| 73 | + // verify application scoped bean persists session closing |
| 74 | + assertThat(endpoint.messageMap().size(), is(2)); |
| 75 | + session_2.close(); |
| 76 | + assertThat(endpoint.messageMap().size(), is(2)); |
| 77 | + |
| 78 | + // and that we can push more messages to map if needed |
| 79 | + Session session_3 = connectToWebsocket("websocket/id-3"); |
| 80 | + session_3.getBasicRemote().sendText("A message 3"); |
| 81 | + |
| 82 | + // wait until last message received |
| 83 | + assertThat(semaphore.tryAcquire(5, TimeUnit.SECONDS), is(true)); |
| 84 | + |
| 85 | + // verify application scoped bean |
| 86 | + assertThat(endpoint.messageMap().size(), is(3)); |
| 87 | + |
| 88 | + // close other sessions |
| 89 | + session_1.close(); |
| 90 | + session_3.close(); |
| 91 | + } |
| 92 | + |
| 93 | + public Session connectToWebsocket(String path) { |
| 94 | + Endpoint endpoint = new Endpoint() { |
| 95 | + @Override |
| 96 | + public void onOpen(Session session, EndpointConfig config) { |
| 97 | + } |
| 98 | + }; |
| 99 | + |
| 100 | + try { |
| 101 | + ClientManager clientManager = ClientManager.createClient(JdkClientContainer.class.getName()); |
| 102 | + return clientManager.connectToServer(endpoint, |
| 103 | + new URI("ws://localhost:" + webTarget.getUri().getPort() + "/" + path)); |
| 104 | + } catch (Exception ex) { |
| 105 | + throw new RuntimeException(ex); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + @ApplicationScoped |
| 110 | + @ServerEndpoint("/websocket/{id}") |
| 111 | + public static class WebsocketEndpoint { |
| 112 | + |
| 113 | + private final Map<String, List<String>> messageMap = new ConcurrentHashMap<>(); |
| 114 | + |
| 115 | + @OnOpen |
| 116 | + public void onOpen(@PathParam("id") String id, Session session) { |
| 117 | + messageMap.put(id, new ArrayList<>()); |
| 118 | + } |
| 119 | + |
| 120 | + @OnMessage |
| 121 | + public void onMessage(@PathParam("id") String id, Session session, String message) { |
| 122 | + messageMap.get(id).add(message); |
| 123 | + semaphore.release(); |
| 124 | + } |
| 125 | + |
| 126 | + public Map<String, List<String>> messageMap() { |
| 127 | + return messageMap; |
| 128 | + } |
| 129 | + |
| 130 | + // Verify single instance is created/destroyed by CDI |
| 131 | + |
| 132 | + private static final AtomicBoolean live = new AtomicBoolean(); |
| 133 | + |
| 134 | + @PostConstruct |
| 135 | + void construct() { |
| 136 | + assertThat(live.compareAndSet(false, true), is(true)); |
| 137 | + } |
| 138 | + |
| 139 | + @PreDestroy |
| 140 | + void destroy() { |
| 141 | + assertThat(live.compareAndSet(true, false), is(true)); |
| 142 | + } |
| 143 | + } |
| 144 | +} |
0 commit comments