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

Added test for multiple connections #64

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions src/main/java/org/fungover/thunder/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,8 @@ public void subscribeToTopic(String topic) {
public Set<String> getSubscribedTopics() {
return new HashSet<>(subscribedTopics);
}

public void connectToServer(String localhost, int i) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The connectToServer method doesn't actually connect to the server. It just sets the connected field to true. This could lead to false positives in tests, as the client might not be able to connect to the server in a real-world scenario.

this.connected = true;
}
}
1 change: 0 additions & 1 deletion src/main/java/org/fungover/thunder/ClientHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,4 @@ public void removeDisconnectedClients() {
public List<Socket> getClients() {
return clients;
}

}
8 changes: 8 additions & 0 deletions src/main/java/org/fungover/thunder/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,12 @@ public void start() throws IOException {
e.printStackTrace();
}
}
public void stop(){
try {
serverSocket.close();
System.out.println("Server stopped");
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The stop method catches an IOException and logs it, but it also prints a message to the console. This is redundant and can lead to confusion. It would be better to only log the exception and remove the console print statement.

Suggested change
System.out.println("Server stopped");
logger.log(Level.INFO, "Server stopped");

} catch (IOException e) {
e.printStackTrace();
}
}
}
6 changes: 6 additions & 0 deletions src/test/java/org/fungover/thunder/ClientHandlerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,19 @@
import org.junit.jupiter.api.Test;

import java.io.*;
import java.lang.reflect.Field;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.ExecutorService;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

Expand Down
63 changes: 63 additions & 0 deletions src/test/java/org/fungover/thunder/ServerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package org.fungover.thunder;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ServerTest {
private static Server server;
@BeforeAll
public static void setUp() {
try {
server = new Server();
ExecutorService executorService = Executors.newFixedThreadPool(5);
executorService.submit(() -> {
try {
server.start();
} catch (IOException e) {
e.printStackTrace();
}
});
Thread.sleep(1000);
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
@AfterAll
public static void tearDown() {
if (server != null) {
server.stop();
}
}
@Test
public void testMultipleConnections() {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The testMultipleConnections method doesn't actually test the server's ability to handle multiple connections. It just creates multiple clients and calls their connectToServer method, which doesn't actually connect to the server. This could lead to false positives in tests, as the server might not be able to handle multiple connections in a real-world scenario.

int numClients = 5;
ExecutorService executorService = Executors.newFixedThreadPool(numClients);

for (int i = 0; i < numClients; i++) {
final int clientId = i;
executorService.submit(() -> {
try {
Client client = new Client("Client" + clientId);
client.connectToServer("localhost", 1883);
assertTrue(client.isConnected());
} catch (Exception e) {
e.printStackTrace();
}
});
}
executorService.shutdown();
while (!executorService.isTerminated()) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}