Skip to content

Commit

Permalink
Merge pull request #1343 from gtroitsk/feature/websockets/number-of-o…
Browse files Browse the repository at this point in the history
…pened-sessions

Add client session single opening assert to WebSockets tests
  • Loading branch information
michalvavrik authored Aug 2, 2023
2 parents e8d947f + bff13c2 commit 2bced9c
Showing 1 changed file with 26 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Set;
import java.util.concurrent.ConcurrentSkipListSet;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

import jakarta.websocket.ClientEndpoint;
import jakarta.websocket.ContainerProvider;
Expand Down Expand Up @@ -42,6 +41,8 @@ public void smoke() throws Exception {
session.getAsyncRemote().sendText("hello world");
assertMessage(">> stu: hello world", client);
}

assertSessionWasOpenedJustOnce(client);
}

@Test
Expand All @@ -59,6 +60,8 @@ public void chatting() throws Exception {
assertMessage(">> bob: hello alice", bobChat, aliceChat);
}
}

assertSessionWasOpenedJustOnce(aliceChat, bobChat);
}

@Test
Expand All @@ -76,6 +79,8 @@ public void chattingSync() throws Exception {
assertMessage(">> bob: hello alice", bobChat, aliceChat);
}
}

assertSessionWasOpenedJustOnce(aliceChat, bobChat);
}

@Test
Expand All @@ -97,6 +102,8 @@ public void trio() throws Exception {
assertMessage("User athos left", porthosChat, aramisChat);
}
}

assertSessionWasOpenedJustOnce(athosChat, porthosChat, aramisChat);
}

@Test
Expand All @@ -106,6 +113,8 @@ public void i18n() throws Exception {
session.getAsyncRemote().sendText("今日は přátelé, как дела? \uD83E\uDED6 ?");
assertMessage(">> traveler: 今日は přátelé, как дела? \uD83E\uDED6 ?", client);
}

assertSessionWasOpenedJustOnce(client);
}

@Test
Expand All @@ -117,6 +126,8 @@ public void push() throws Exception {
assertMessage("Three", client);
assertMessage("Four", client);
}

assertSessionWasOpenedJustOnce(client);
}

private static URI getUri(String with) throws URISyntaxException {
Expand All @@ -140,6 +151,12 @@ private static void assertMessage(String expectedMessage, Client... clients) {
}
}

private static void assertSessionWasOpenedJustOnce(Client... clients) {
for (Client client : clients) {
Assertions.assertEquals(1, client.getNumberOfOpenedSessions());
}
}

private void broadcastPlainMsgAsync(Session session, String msg) {
session.getAsyncRemote().sendText(msg, result -> {
if (result.getException() != null) {
Expand All @@ -161,7 +178,7 @@ private void broadcastPlainMsg(Session session, String msg) {
@ClientEndpoint
public static class Client {
private final LinkedBlockingDeque<String> messages = new LinkedBlockingDeque<>();
private final Set<String> joins = new ConcurrentSkipListSet<>();
private final AtomicInteger sessionOpenCounter = new AtomicInteger();

@OnOpen
public void open(Session session) {
Expand All @@ -173,6 +190,8 @@ public void open(Session session) {
Assertions.fail(result.getException());
}
});

sessionOpenCounter.incrementAndGet();
}

@OnMessage
Expand All @@ -185,5 +204,9 @@ void message(String msg) {
public String getMessage() throws InterruptedException {
return messages.poll(10, TimeUnit.SECONDS);
}

public int getNumberOfOpenedSessions() {
return this.sessionOpenCounter.get();
}
}
}

0 comments on commit 2bced9c

Please sign in to comment.