Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Empty session problem #330

Merged
merged 4 commits into from
Jul 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ jobs:
node-version: 20

- name: install Rust stable
uses: dtolnay/rust-toolchain@stable
uses: dtolnay/rust-toolchain@1.79

- uses: Swatinem/rust-cache@v2
with:
Expand Down
4 changes: 3 additions & 1 deletion backend/src/main/java/fr/zelytra/session/SessionManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ public Fleet joinSession(String sessionId, Player player) {
if (fleet == null) {
sendDataToPlayer(player.getSocket(), MessageType.SESSION_NOT_FOUND, null);
try {
player.getSocket().close();
if (player.getSocket() != null) {
player.getSocket().close();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand Down
4 changes: 2 additions & 2 deletions backend/src/main/java/fr/zelytra/session/SessionSocket.java
Original file line number Diff line number Diff line change
Expand Up @@ -229,14 +229,14 @@ public void handleConnectMessage(Player player, Session session, String sessionI
String newSessionId = manager.createSession();
Fleet fleet = manager.joinSession(newSessionId, player);
player.setMaster(true);
player.setSocket(session);
if (fleet != null) {
player.setSocket(session);
sessionManager.broadcastDataToSession(newSessionId, MessageType.UPDATE, fleet);
}
} else {
player.setSocket(session);
Fleet fleet = manager.joinSession(sessionId, player);
if (fleet != null) {
player.setSocket(session);
sessionManager.broadcastDataToSession(sessionId, MessageType.UPDATE, fleet);
}
}
Expand Down
18 changes: 18 additions & 0 deletions backend/src/test/java/fr/zelytra/session/SessionSocketTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,24 @@ void playerWithNoUsernameTryToConnect() throws Exception {
assertEquals(1, sessionManager.getSessions().size());
}

@Test
void playerConnectToNullSession() throws Exception {
Player player = new Player();
player.setUsername("Player 1");
player.setClientVersion(appVersion.get(0));
player.setReady(false);

BetterFleetClient playerClient = new BetterFleetClient();
SocketSecurityEntity socketSecurity = new SocketSecurityEntity();
URI uri = new URI("ws://" + websocketEndpoint.getHost() + ":" + websocketEndpoint.getPort() + "/sessions/" + socketSecurity.getKey() + "/ABCDEF");

ContainerProvider.getWebSocketContainer().connectToServer(playerClient, uri);
playerClient.sendMessage(MessageType.CONNECT, player);

assertTrue(playerClient.getLatch().await(1, TimeUnit.SECONDS));
assertEquals(0, sessionManager.getSessions().size());
}

private List<Player> generateFakePlayer(int amount) {
List<Player> fakePlayer = new ArrayList<>();
for (int x = 0; x < amount; x++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ public <T> T getMessageReceived(Class<T> tClass) {
return new ObjectMapper().convertValue(messageReceived.data(), tClass);
}

public Session getSession() {
return session;
}

// Getter for latch
public CountDownLatch getLatch() {
return latch;
Expand Down
Loading