Skip to content

Commit

Permalink
Empty session problem & Rust version (#330)
Browse files Browse the repository at this point in the history
  • Loading branch information
zelytra authored Jul 27, 2024
1 parent 7e101aa commit a7ec69c
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 4 deletions.
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

0 comments on commit a7ec69c

Please sign in to comment.